bitnami / kube-libsonnet

Bitnami's jsonnet library for building Kubernetes manifests
https://bitnami.com
Apache License 2.0
174 stars 50 forks source link

Deployment's selector should be immutable #16

Open stang opened 5 years ago

stang commented 5 years ago

Currently, Deployment's selector is populated based on deployment.spec.template.metadata.labels, which itself comes from the deployment.metadata.labels.

This works fine as long as the deployment.metadata.labels are not modified between two deployments. In the other cases, it will raise an immutable field error.

https://github.com/bitnami-labs/kube-libsonnet/blob/master/kube.libsonnet#L385-L387

Ultimately, pod labels can have for a wider range of use-cases which should not be tight to the Deployment selector.

An alternative approach would be to use a deterministic value to populate the the deployment.spec.template.metadata.labels and selector (eg: I think that ksonnet-lib uses the {app: metadata.name}).

kupson commented 4 years ago

Example repository with workaround discussed in pull request #17 - https://github.com/rkwaysltd/k8slab-kube-libsonnet

ghostsquad commented 3 years ago

I'm doing something to allow labels to differ between the deployment itself and the pod template

_Object(apiVersion, kind, name):: super._Object(apiVersion, kind, name) {
    local this = self,
    metadata+: {
      labels_:: { app: std.join('-', std.split(this.metadata.name, ':')) },
      labels: std.prune(self.labels_ + lm.obj.lookupAll(self, "self_labels_", {})),
    },
},

Deployment(name, namespace):: super.Deployment(name) {
    local dp = self,

    metadata+: {
      namespace: namespace,
      pod_only_labels_:: {},
      self_labels_:: {},

      labels: std.prune(self.labels_ + self.self_labels_),
    },
    spec+: {
      template+: {
        metadata: {
          labels: std.prune(dp.metadata.labels_ + dp.metadata.pod_only_labels_),
        },
        spec+: $.PodSpec,
      },
    },
  },

my library is an overlay of bitnami's library.

ghostsquad commented 3 years ago

the bitnami library is technically doing this correctly, in that, the select points to the pod labels. Which matches the documentation.

selector: {
        matchLabels: deployment.spec.template.metadata.labels,
      },

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#selector

.spec.selector must match .spec.template.metadata.labels, or it will be rejected by the API.

The problem I found is that I needed a way to have the deployment labels differ from the pod labels.