How to remove version from status.storedVersions while Manually upgrading the existing objects to a new stored version in k8s custome resources

12/23/2019

How to remove version from 'status.storedVersions' while Manually upgrading the existing objects to a new stored version in kubernetes custom resources

apiVersion for CRD: apiextensions.k8s.io/v1beta1

Using aws EKS

CRD YAML

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  # name must match the spec fields below, and be in the form: <plural>.<group>
  name: crontabs.stable.example.com
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: stable.example.com
  # list of versions supported by this CustomResourceDefinition
  versions:
    - name: v1
      # Each version can be enabled/disabled by Served flag.
      served: true
      # One and only one version must be marked as the storage version.
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                image:
                  type: string
                replicas:
                  type: integer
  # either Namespaced or Cluster
  scope: Namespaced
  names:
    # plural name to be used in the URL: /apis/<group>/<version>/<plural>
    plural: crontabs
    # singular name to be used as an alias on the CLI and for display
    singular: crontab
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    kind: CronTab
    # shortNames allow shorter string to match your resource on the CLI
    shortNames:
    - ct

And upgrading the CRD to v2

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  # name must match the spec fields below, and be in the form: <plural>.<group>
  name: crontabs.stable.example.com
spec:
  # group name to use for REST API: /apis/<group>/<version>
  group: stable.example.com
  # list of versions supported by this CustomResourceDefinition
  versions:
    - name: v2
      # Each version can be enabled/disabled by Served flag.
      served: true
      # One and only one version must be marked as the storage version.
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                image:
                  type: string
                replicas:
                  type: integer
    - name: v1
      # Each version can be enabled/disabled by Served flag.
      served: false
      # One and only one version must be marked as the storage version.
      storage: false
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                image:
                  type: string
                replicas:
                  type: integer
  # either Namespaced or Cluster
  scope: Namespaced
  names:
    # plural name to be used in the URL: /apis/<group>/<version>/<plural>
    plural: crontabs
    # singular name to be used as an alias on the CLI and for display
    singular: crontab
    # kind is normally the CamelCased singular type. Your resource manifests use this.
    kind: CronTab
    # shortNames allow shorter string to match your resource on the CLI
    shortNames:
    - ct

After disabling version v1 I want remove it from the versions list to do that I need to remove it from status.storedVersions how can i do that the kubernetes document does not give details about that

-- DK_
kubernetes
kubernetes-custom-resources

2 Answers

1/29/2020

Ive tried many various options on Minikube, Kubeadm, GKE but everywhere output was the same.

Unfortunately it is impossible using kubectl and docs Remove the old version from the CustomResourceDefinition spec.versions list is bit missleading.

Once a definition falls into specVersion it's impossible to remove it. storage: true puts it int the storedVersions list.

It was well described in this Github thread, especially comment from @sebgl.

IUC correctly the way CRDs are validated, it seems impossible to easily update a CRD in order to remove an old version.

When a CRD is updated:

  • the list of expected storedVersions is built from status.StoredVersions in the previous CRD + newer storedVersions in the new CRD
  • this list of storedVersions is passed to a validation function that checks all expected storedVersions are still present in the new CRD

All served versions in the history of the CRD end up in the Status. Any update to the CRD Status is impossible. Any update to the CRD versions that would consist in removing a version that exists in the status is impossible.

However, he also mentioned it can be done by using operator-lifecycle-manager.

Programmatically updating the CRD can be found here. This would bypass validation of CRD storedVersion.

-- PjoterS
Source: StackOverflow

1/31/2020

There is also another another way of removing status.specVersion using cluster API.

Fields under status are modified via the status sub resource:

curl -d '[{ "op": "replace", "path":"/status/storedVersions", "value": ["v2"] }]' \
  -H "Content-Type: application/json-patch+json" \
  -X PATCH \
 http://localhost:8080/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions/crontabs.stable.example.com/status

To access API you need to first connected to cluster doc

-- lukaszberwid
Source: StackOverflow