This is a Kubernetes operator that allows you to inherit labels from a namespace to the resources within it. The operator only supports the following resources for now:
- pods
- configmaps
The operator is built using the Kubebuilder framework. It watches for the Namespace resources and updates the labels of the resources within the namespace based on the labels of the namespace. It watches for the Namespace resources and updates the labels of the resources within the namespace based on the labels of the namespace.
The Operator watches the Custom Resource Definition (CRD) inheritors.labels.theisferre
. A sample CR is as follows:
apiVersion: labels.theisferre/v1
kind: Inheritor
metadata:
name: inheritor-sample
spec:
selectors:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: inheritor
includeLabels:
- app
- foo
This CR is used to specify the namespaces that need to be watched and the labels that need to be inherited. The operator watches for the namespaces that match the namespaceSelector
and updates the labels of the resources within the namespace based on the includeLabels
.
- Create a KIND cluster
kind create cluster
- Build and load the Docker image into the KIND cluster
make docker-build
kind load docker-image label-inheritance-operator:0.0.1
- Deploy the operator to the KIND cluster
make deploy IMG=label-inheritance-operator:0.0.1
- Install the CRDs
make install
- Create namespace
inheritor
and a sample pod
kubectl create namespace inheritor
kubectl label namespace inheritor app=sample foo=bar
kubectl apply -f config/samples/pod.yaml
- Apply the sample Custom Resource (CR)
The sample CR is in the config/samples
directory. It listens for the namespace inheritor
and inherits the labels app
and foo
to the resources within the namespace.
kubectl apply -f config/samples/labels_v1_inheritor.yaml
- Verify that the labels are inherited by the pod
kubectl get pod -n inheritor -o jsonpath='{.items[*].metadata.labels}'
To run the tests, run the following command:
make test