> For the complete documentation index, see [llms.txt](https://docs.cennso.com/cennso/cmc/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/cennso/cmc/latest/releases/migration_to_1.2.md).

# Migration guide from Cennso Agent-based CMC installation to CMC Extension 1.2

With Cennso 2.10.0, Cennso Management Console (CMC) uses Grafana based on a Cennso extension instead of plain Management Console Crossplane composition. A migration procedure is required to keep the existing configuration, such as authentication SSO settings, users, or manually created dashboards.

> [!NOTE]
> Before upgrading Cennso Management Console, upgrade Management Cluster to Cennso 2.10.0 and make sure that the `cmc-1.2.2` Cennso package is installed on your cluster. If you cannot find it, contact Cennso maintainers.
>
> ```bash
> kubectl get packages cmc-1.2.2
> ```

## Migration steps

1. Get the name of the Management Console Helm release, Grafana's Deployment, and Grafana's PVC:

   ```bash
   export MC_RELEASE_NAME=$(kubectl get managementconsoles management-console -n cennso-system -o jsonpath='{.spec.resourceRef.name}')
   export GRAFANA_DEPLOYMENT=$(kubectl get deployment -n cennso-system -l app.kubernetes.io/instance=$MC_RELEASE_NAME -o jsonpath="{.items[*].metadata.name}")
   export GRAFANA_PVC=$(kubectl get pvc -n cennso-system -l app.kubernetes.io/instance=$MC_RELEASE_NAME -o jsonpath="{.items[*].metadata.name}")
   ```

2. Copy the administrator credentials and Grafana Service Account token for use in the next steps:

   ```bash
   export CMC_ADMIN_PASSWORD=$(kubectl get secret management-console -n cennso-system -o jsonpath="{.data.admin-password}")
   export CMC_SERVICE_ACCOUNT_TOKEN=$(kubectl get secret grafana-operator-token -n cennso-system -o jsonpath="{.data.token}")
   ```

3. To ensure data integrity, shut down your Grafana instance before backing up the SQLite database by scaling down Deployment replicas to 0 in the Management Console composition instance:

   ```bash
   kubectl patch deployment $GRAFANA_DEPLOYMENT -n cennso-system --type='merge' -p '{"spec":{"replicas":0}}'
   ```

4. Create a temporary pod to retrieve data from Grafana's PVC:

   ```bash
   kubectl apply -n cennso-system -f - <<EOF
   apiVersion: v1
   kind: Pod
   metadata:
     name: grafana-db-migration
     namespace: cennso-system
   spec:
     containers:
       - name: grafana-db-migration
         image: alpine:3.21
         command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
         volumeMounts:
         - name: volume1
           mountPath: "/mnt/volume1"
     volumes:
     - name: volume1
       persistentVolumeClaim:
         claimName: ${GRAFANA_PVC}
     restartPolicy: Never
   EOF
   ```

5. Copy the Grafana database file to your host machine using the pod's name from the previous step:

   ```bash
   kubectl cp -n cennso-system grafana-db-migration:/mnt/volume1/grafana.db ./grafana.db
   ```

6. Edit database permissions:

   ```bash
   sudo chmod u=rw,g=rw,o=r ./grafana.db
   sudo chown 472 ./grafana.db
   ```

7. Remove the temporary pod:

   ```bash
   kubectl delete pod -n cennso-system grafana-db-migration
   ```

8. Using the [script](https://docs.cennso.com/api/assets/cennso/cmc/latest/assets/upgrade-1.2/create-cmc-installation-manifest.sh), create a `ClusterInstallation` resource to apply in the next steps:

9. Delete the old Management Console instance to clean up old managed resources:

   ```bash
   kubectl delete managementconsoles management-console -n cennso-system
   ```

10. Wait until all resources of the old Management Console instance are removed:

    ```bash
    kubectl get xmanagementconsoles
    ```

    If you didn't receive any data, the old Management Console should be removed.

11. Create a new CMC instance using the previously created `ClusterInstallation` resource:

    ```bash
    kubectl apply -f ./cmc-installation.yaml
    ```

12. Wait for the new Grafana's PVC readiness:

    ```bash
    kubectl get pvc cmc -n cennso-system
    ```

13. Scale the CMC instance to 0 replicas:

    ```bash
    kubectl scale deployment cmc -n cennso-system --replicas=0
    ```

14. Edit the newly created secrets using the old values saved in step 2:

    ```bash
    kubectl patch secret cmc -n cennso-system -p "{\"data\": {\"admin-password\": \"${CMC_ADMIN_PASSWORD}\"}}"
    ```

    If the secret `cmc-grafana-operator-token` has already been created, replace its token:

    ```bash
    kubectl get secret cmc-grafana-operator-token -n cennso-system
    ```

    If you see it, then:

    ```bash
    kubectl patch secret cmc-grafana-operator-token -n cennso-system -p "{\"data\": {\"token\": \"${CMC_SERVICE_ACCOUNT_TOKEN}\"}}"
    ```

15. Attach the new PVC to the temporary pod:

    ```bash
    kubectl apply -n cennso-system -f - <<EOF
    apiVersion: v1
    kind: Pod
    metadata:
      name: grafana-db-migration
      namespace: cennso-system
    spec:
      containers:
        - name: grafana-db-migration
          image: alpine:3.21
          command: [ "/bin/sh", "-c", "tail -f /dev/null" ]
          volumeMounts:
          - name: volume1
            mountPath: "/mnt/volume1"
      volumes:
      - name: volume1
        persistentVolumeClaim:
          claimName: cmc
      restartPolicy: Never
    EOF
    ```

16. Using the pod's name from the previous step, copy the Grafana database file into the new volume:

    ```bash
    kubectl cp -n cennso-system ./grafana.db grafana-db-migration:/mnt/volume1/grafana.db
    ```

17. Remove the temporary pod:

    ```bash
    kubectl delete pod -n cennso-system grafana-db-migration
    ```

18. Scale the CMC instance to 1 replica:

    ```bash
    kubectl scale deployment cmc -n cennso-system --replicas=1
    ```

19. Wait for the CMC's Pod readiness:

    ```bash
    kubectl get pod -n cennso-system | grep cmc
    ```

    If Pod has a `Ready` status, you can access the CMC instance at the same address as before.

20. Remove unccessary files:

    ```bash
    rm -f ./cmc-installation.yaml
    rm -f ./grafana.db
    ```

21. Remove the leftover resources coming from the old Management Console composition:

    ```bash
    kubectl get configmaps -n cennso-system | grep ^management-console- | awk '{print $1}' | xargs kubectl delete configmaps -n cennso-system
    kubectl get jobs -n cennso-system | grep ^management-console- | awk '{print $1}' | xargs kubectl delete jobs -n cennso-system
    kubectl get clusterroles | grep ^management-console- | awk '{print $1}' | xargs kubectl delete clusterroles
    kubectl get clusterrolebindings | grep ^management-console- | awk '{print $1}' | xargs kubectl delete clusterrolebindings
    kubectl get serviceaccounts -n cennso-system | grep ^management-console- | awk '{print $1}' | xargs kubectl delete serviceaccounts -n cennso-system
    kubectl get grafanas.grafana.integreatly.org -n cennso-system | grep ^management-console- | awk '{print $1}' | xargs kubectl delete grafanas.grafana.integreatly.org -n cennso-system
    kubectl delete secrets grafana-operator-token -n cennso-system
    ```

    > [!NOTE]
    > Because some resources may no longer exist, an error may occur during removal.
