Open stang opened 5 years ago
Example repository with workaround discussed in pull request #17 - https://github.com/rkwaysltd/k8slab-kube-libsonnet
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.
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.
Currently,
Deployment
'sselector
is populated based ondeployment.spec.template.metadata.labels
, which itself comes from thedeployment.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 animmutable 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
andselector
(eg: I think thatksonnet-lib
uses the{app: metadata.name}
).