Kubernetes is an open source container orchestration system for automating the deployment, scaling, and management of computer applications. This software was originally designed by Google and is now maintained by the Cloud Native Computing Foundation.

Prerequisite

  1. kubernetes manifest
  2. jq installed
  3. yq installed

Kubernetes is a portable platform, meaning it can run on various infrastructures like on-premises servers, hybrid environments, and cloud platforms.

Because kubernetes is portable, we possible to migrate the yaml manifest like from GKE to onprem, from GKE to EKS, etc.

But if we export manifest directly, there will be several things that need to be deleted.

example if we export yaml manifest

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    branch: refs/heads/development
    commit: fae2707c3aed43ed527f50d8d911u5g9b113b13d
    createdBy: tkjpedia
    deployTimestamp: "1706096688902"
    deployment.kubernetes.io/revision: "159"
    jobName: deploy_development
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"name":"test-deployment"},"name":"test-deployment","namespace":"tkjpedia-dev"},"spec":{"replicas":1,"selector":{"matchLabels":{"name":"test-deployment"}},"template":{"metadata":{"labels":{"name":"test-deployment"}},"spec":{"containers":[{"image":"ghcr.io/tkjpedia/test-deployment-development:fae2707c3aed43ed527f50d8d911u5g9b113b13d","name":"test-deployment","ports":[{"containerPort":80,"protocol":"TCP"}],"resources":{"limits":{"cpu":"30m","memory":"128Mi"},"requests":{"cpu":"1m","memory":"64Mi"}}}],"imagePullSecrets":[{"name":"ghcr"}]}}}}
    provider: GitHub
    repository: tkjpedia/test-deployment
    run: "7639612587"
    runUri: https://github.com/tkjpedia/test-deployment/actions/runs/7639612587
    workflow: Github Actions Workflows
  creationTimestamp: "2023-05-11T15:20:53Z"
  generation: 311
  labels:
    k8slens-edit-resource-version: v1
    name: test-deployment
  name: test-deployment
  namespace: tkjpedia-dev
  resourceVersion: "33670469"
  uid: ae602625-3562-4648-8027-f29508ed9533
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      name: test-deployment
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: "2023-12-28T06:49:02Z"
      creationTimestamp: null
      labels:
        name: test-deployment
    spec:
      containers:
        - image: ghcr.io/tkjpedia/test-deployment-development:fae2707c3aed43ed527f50d8d911u5g9b113b13d
          imagePullPolicy: Always
          name: test-deployment
          ports:
            - containerPort: 80
              protocol: TCP
          resources:
            limits:
              cpu: 30m
              memory: 128Mi
            requests:
              cpu: 1m
              memory: 64Mi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
        - name: ghcr
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
    - lastTransitionTime: "2023-12-28T07:08:27Z"
      lastUpdateTime: "2023-12-28T07:08:27Z"
      message: Deployment has minimum availability.
      reason: MinimumReplicasAvailable
      status: "True"
      type: Available
    - lastTransitionTime: "2024-01-12T14:30:01Z"
      lastUpdateTime: "2024-01-24T11:45:03Z"
      message: ReplicaSet "test-deployment-5ff88bdcff" has successfully progressed.
      reason: NewReplicaSetAvailable
      status: "True"
      type: Progressing
  observedGeneration: 311
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

we no need annotation and status parameter, then we should delete it. perhaps we can delete it manually, but what if we have many yaml kubernetes manifest? we can use jq and yq.

jq is a lightweight and powerful command-line tool designed specifically for working with JSON data in Linux environments, yq is a lightweight and portable command-line tool designed for processing YAML, JSON, and XML data in Linux environments. you should install jq and yq in your pc.

Export kubernetes yaml manifest

k get deployment -n tkjpedia-dev test-deployment -ojson > test-deployment.json

export to json format

Delete unused parameters using jq

we can define what we want to delete in jq like this

jq 'del(.metadata.resourceVersion,.metadata.uid,.metadata.selfLink,.metadata.creationTimestamp,.metadata.annotations,.metadata.generation,.metadata.ownerReferences,.status)'

its mean we delete this parameter

  1. .metadata.resourceVersion
  2. .metadata.uid
  3. .metadata.selfLink
  4. .metadata.creationTimestamp
  5. .metadata.annotations
  6. .metadata.generation
  7. .metadata.ownerReferences
  8. .status

command:

cat test-deployment.json | jq 'del(.metadata.resourceVersion,.metadata.uid,.metadata.selfLink,.metadata.creationTimestamp,.metadata.annotations,.metadata.generation,.metadata.ownerReferences,.status)'

output

{
  "apiVersion": "apps/v1",
  "kind": "Deployment",
  "metadata": {
    "labels": {
      "k8slens-edit-resource-version": "v1",
      "name": "test-deployment"
    },
    "name": "test-deployment",
    "namespace": "tkjpedia-dev"
  },
  "spec": {
    "progressDeadlineSeconds": 600,
    "replicas": 1,
    "revisionHistoryLimit": 10,
    "selector": {
      "matchLabels": {
        "name": "test-deployment"
      }
    },
    "strategy": {
      "rollingUpdate": {
        "maxSurge": "25%",
        "maxUnavailable": "25%"
      },
      "type": "RollingUpdate"
    },
    "template": {
      "metadata": {
        "annotations": {
          "kubectl.kubernetes.io/restartedAt": "2023-12-28T06:49:02Z"
        },
        "creationTimestamp": null,
        "labels": {
          "name": "test-deployment"
        }
      },
      "spec": {
        "containers": [
          {
            "image": "ghcr.io/tkjpedia/test-deployment-development:fae2707c3aed43ed527f50d8d911u5g9b113b13d",
            "imagePullPolicy": "Always",
            "name": "test-deployment",
            "ports": [
              {
                "containerPort": 80,
                "protocol": "TCP"
              }
            ],
            "resources": {
              "limits": {
                "cpu": "30m",
                "memory": "128Mi"
              },
              "requests": {
                "cpu": "1m",
                "memory": "64Mi"
              }
            },
            "terminationMessagePath": "/dev/termination-log",
            "terminationMessagePolicy": "File"
          }
        ],
        "dnsPolicy": "ClusterFirst",
        "imagePullSecrets": [
          {
            "name": "ghcr"
          }
        ],
        "restartPolicy": "Always",
        "schedulerName": "default-scheduler",
        "securityContext": {},
        "terminationGracePeriodSeconds": 30
      }
    }
  }
}

annotation and status all gone 😀

lets convert json to yaml using yq

yq eval . --prettyPrint

one command

cat test-deployment.json | jq 'del(.metadata.resourceVersion,.metadata.uid,.metadata.selfLink,.metadata.creationTimestamp,.metadata.annotations,.metadata.generation,.metadata.ownerReferences,.status)' | yq eval . --prettyPrint

output

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    k8slens-edit-resource-version: v1
    name: test-deployment
  name: test-deployment
  namespace: tkjpedia-dev
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      name: test-deployment
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      annotations:
        kubectl.kubernetes.io/restartedAt: "2023-12-28T06:49:02Z"
      creationTimestamp: null
      labels:
        name: test-deployment
    spec:
      containers:
        - image: ghcr.io/tkjpedia/test-deployment-development:fae2707c3aed43ed527f50d8d911u5g9b113b13d
          imagePullPolicy: Always
          name: test-deployment
          ports:
            - containerPort: 80
              protocol: TCP
          resources:
            limits:
              cpu: 30m
              memory: 128Mi
            requests:
              cpu: 1m
              memory: 64Mi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      imagePullSecrets:
        - name: ghcr
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

Thank you..