Convert manifest between API versions using kubectl convert plugin

kubernetes plugins convert unused-volumes

2 min read | by Jordi Prats

We can install the kubectl convert plugin to help us convert manifest between different API versions: For example, from an Ingress using the extensions/v1beta1 API version to networking.k8s.io/v1

We are going to use the following Ingress to test it out:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: beta-ingress
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internal
    alb.ingress.kubernetes.io/target-type: ip
spec:
  rules:
    - http:
        paths:
          - backend:
              serviceName: example
              servicePort: 8080
            path: /*

Install kubectl convert

To install the kubectl convert pluguin we can execute the following:

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl-convert"
sudo install -o root -g root -m 0755 kubectl-convert /usr/local/bin/kubectl-convert

Converting a manifest

If we try to apply this manifest on a kubernetes cluster v1.22+ we are going to get an error like the following since this API version have been removed:

$ kubectl apply -f bet-ingress.yaml 
error: unable to recognize "beta-ingress.yaml": no matches for kind "Ingress" in version "extensions/v1beta1"

We can use kubectl convert to transform it to a different version that our Kubernetes supports, for example networking.k8s.io/v1. To do so we need to set the --output-version option as follows:

$ kubectl convert -f beta-ingress.yaml --output-version networking.k8s.io/v1
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    alb.ingress.kubernetes.io/scheme: internal
    alb.ingress.kubernetes.io/target-type: ip
    kubernetes.io/ingress.class: alb
  creationTimestamp: null
  name: beta-ingress
spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: example
            port:
              number: 8080
        path: /*
        pathType: ImplementationSpecific
status:
  loadBalancer: {}

We can directly inject the resulting manifest to the cluster:

$ kubectl convert -f bet-ingress.yaml --output-version networking.k8s.io/v1 | kubectl apply -f -
ingress.networking.k8s.io/beta-ingress created

But this might or might not provide the expected results, it's always advisable to review the deprecation guide and use this tool as and aid rather than something that we use to blindly convert outdated manifest.


Posted on 16/06/2022