<commit_before>[[apim-kubernetes-operator-user-guide-sync-crds]]
= How to sync API CRDs with a Management API
:page-sidebar: apim_3_x_sidebar
:page-permalink: apim/3.x/apim_kubernetes_operator_user_guide_sync_crds.html
:page-folder: apim/kubernetes
:page-layout: apim3x


== How to synchronize your API CRDs with an existing Management API

The following examples for creating a Management Context and creating an API referecing that context are based on the assumption that a Management API has already been deployed in a namespace called `apim-blue`. The connection to that Management API uses the default in-memory credentials.

=== Example - creating a Management Context for an existing Management API

[,shell]
----
echo """
apiVersion: gravitee.io/v1alpha1
kind: ManagementContext
metadata:
  name: apim-example-context
  namespace: apim-blue
spec:
  baseUrl: http://acme-apim3-api.apim-example.svc:83
  environmentId: DEFAULT
  organizationId: DEFAULT
  auth:
    credentials:
      username: admin
      password: <PASSWORD>
""" | kubectl apply -f -
----

=== Example - creating an API referencing an existing Management Context

[,shell]
----
echo """
apiVersion: gravitee.io/v1alpha1
kind: ApiDefinition
metadata:
  name: basic-api-example
  namespace: apim-blue
spec:
  name: gko-example
  contextRef:
    name: apim-example-context
    namespace: apim-blue
  version: "1.0.0"
  description: "Basic api managed by Gravitee Kubernetes Operator"
  proxy:
    virtual_hosts:
      - path: "/k8s-basic"
    groups:
      - endpoints:
          - name: "Default"
            target: "https://api.gravitee.io/echo"
""" | kubectl apply -f -
----

=== Using an APIM export endpoint to create an API Definition from an existing API

The Management API feature provides an link:https://docs.gravitee.io/apim/3.x/management-api/3.18/index.html#tag/APIs/operation/getEvent[export endpoint^] that enables you to export an API as an API Definition resource.

This allows you to easily create an API Definition from a given environment by calling the endpoint and piping the result to a `kubectl` command. For example:

[,shell]
----
curl -s -H "Authorization: Bearer $TOKEN" "https://apim-blue-api.team-gko.gravitee.xyz/management/organizations/DEFAULT/environments/DEFAULT/apis/$API_ID/crd" | kubectl apply -f -
----

=== Starting and stopping your API

By default the API will start automatically. If you want to stop it later (or just create an API definition in "stop mode"), set the `state` property value to `STOPPED`, as shown in the example below:

[,shell]
----
echo """
apiVersion: gravitee.io/v1alpha1
kind: ApiDefinition
metadata:
  name: basic-api-example
  namespace: apim-blue
spec:
  name: gko-example
  contextRef:
    name: apim-example-context
    namespace: apim-blue
  version: "1.0.0"
  description: "Basic api managed by Gravitee Kubernetes Operator"
  state: "STOPPED"
  proxy:
    virtual_hosts:
      - path: "/k8s-basic"
    groups:
      - endpoints:
          - name: "Default"
            target: "https://api.gravitee.io/echo"
""" | kubectl apply -f -
----

To start the API again, change the `state` property value back to `STARTED`.

=== Updating your API

To update the API, follow the example below - it shows a simple update of the API name, path, and endpoint target.

NOTE: Before you proceed, ensure that the steps above have been completed.


[,shell]
----
echo """
apiVersion: gravitee.io/v1alpha1
kind: ApiDefinition
metadata:
  name: basic-api-example
  namespace: apim-blue
spec:
  name: gko-example-updated
  contextRef:
    name: apim-example-context
    namespace: apim-blue
  version: "1.0.0"
  description: "Basic api managed by Gravitee Kubernetes Operator"
  proxy:
    virtual_hosts:
      - path: "/k8s-basic-updated"
    groups:
      - endpoints:
          - name: "Default"
            target: "https://api.gravitee.io/whattimeisit"
""" | kubectl apply -f -
----

=== Deleting your API

NOTE: Before you proceed, ensure that the steps above have been completed.

The example below shows a simple delettion of the API definition:

[,shell]
----
kubectl -n apim-blue delete apidefinitions.gravitee.io basic-api-example
----<commit_msg>More apim-blue replacements<commit_after>[[apim-kubernetes-operator-user-guide-sync-crds]]
= How to sync API CRDs with a Management API
:page-sidebar: apim_3_x_sidebar
:page-permalink: apim/3.x/apim_kubernetes_operator_user_guide_sync_crds.html
:page-folder: apim/kubernetes
:page-layout: apim3x


== How to synchronize your API CRDs with an existing Management API

The following examples for creating a Management Context and creating an API referecing that context are based on the assumption that a Management API has already been deployed in a namespace called `apim-example`. The connection to that Management API uses the default in-memory credentials.

=== Example - creating a Management Context for an existing Management API

[,shell]
----
echo """
apiVersion: gravitee.io/v1alpha1
kind: ManagementContext
metadata:
  name: apim-example-context
  namespace: apim-example
spec:
  baseUrl: http://acme-apim3-api.apim-example.svc:83
  environmentId: DEFAULT
  organizationId: DEFAULT
  auth:
    credentials:
      username: admin
      password: <PASSWORD>
""" | kubectl apply -f -
----

=== Example - creating an API referencing an existing Management Context

[,shell]
----
echo """
apiVersion: gravitee.io/v1alpha1
kind: ApiDefinition
metadata:
  name: basic-api-example
  namespace: apim-example
spec:
  name: gko-example
  contextRef:
    name: apim-example-context
    namespace: apim-example
  version: "1.0.0"
  description: "Basic api managed by Gravitee Kubernetes Operator"
  proxy:
    virtual_hosts:
      - path: "/k8s-basic"
    groups:
      - endpoints:
          - name: "Default"
            target: "https://api.gravitee.io/echo"
""" | kubectl apply -f -
----

=== Using an APIM export endpoint to create an API Definition from an existing API

The Management API feature provides an link:https://docs.gravitee.io/apim/3.x/management-api/3.18/index.html#tag/APIs/operation/getEvent[export endpoint^] that enables you to export an API as an API Definition resource.

This allows you to easily create an API Definition from a given environment by calling the endpoint and piping the result to a `kubectl` command. For example:

[,shell]
----
curl -s -H "Authorization: Bearer $TOKEN" "https://apim-example-api.team-gko.gravitee.xyz/management/organizations/DEFAULT/environments/DEFAULT/apis/$API_ID/crd" | kubectl apply -f -
----

=== Starting and stopping your API

By default the API will start automatically. If you want to stop it later (or just create an API definition in "stop mode"), set the `state` property value to `STOPPED`, as shown in the example below:

[,shell]
----
echo """
apiVersion: gravitee.io/v1alpha1
kind: ApiDefinition
metadata:
  name: basic-api-example
  namespace: apim-example
spec:
  name: gko-example
  contextRef:
    name: apim-example-context
    namespace: apim-example
  version: "1.0.0"
  description: "Basic api managed by Gravitee Kubernetes Operator"
  state: "STOPPED"
  proxy:
    virtual_hosts:
      - path: "/k8s-basic"
    groups:
      - endpoints:
          - name: "Default"
            target: "https://api.gravitee.io/echo"
""" | kubectl apply -f -
----

To start the API again, change the `state` property value back to `STARTED`.

=== Updating your API

To update the API, follow the example below - it shows a simple update of the API name, path, and endpoint target.

NOTE: Before you proceed, ensure that the steps above have been completed.


[,shell]
----
echo """
apiVersion: gravitee.io/v1alpha1
kind: ApiDefinition
metadata:
  name: basic-api-example
  namespace: apim-example
spec:
  name: gko-example-updated
  contextRef:
    name: apim-example-context
    namespace: apim-example
  version: "1.0.0"
  description: "Basic api managed by Gravitee Kubernetes Operator"
  proxy:
    virtual_hosts:
      - path: "/k8s-basic-updated"
    groups:
      - endpoints:
          - name: "Default"
            target: "https://api.gravitee.io/whattimeisit"
""" | kubectl apply -f -
----

=== Deleting your API

NOTE: Before you proceed, ensure that the steps above have been completed.

The example below shows a simple delettion of the API definition:

[,shell]
----
kubectl -n apim-example delete apidefinitions.gravitee.io basic-api-example
----
{'fraction_non_alphanumeric': 0.10465668804941791, 'fraction_numerical': 0.007127583749109052, 'mean_word_length': 3.463944856839873, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6101321', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2567, 'n_words': 886}