Skip to content

Commit

Permalink
Update removeLabels to also filter spec.template.metadata.labels
Browse files Browse the repository at this point in the history
Closes #27
  • Loading branch information
arnarg committed Nov 7, 2024
1 parent 149d263 commit af18c4e
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 17 deletions.
59 changes: 46 additions & 13 deletions lib/kube.nix
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,50 @@
# List of labels that should be removed
labels:
# Kubernetes manifest
manifest:
manifest
// {
metadata =
manifest.metadata
// (
if manifest.metadata ? labels
then {
labels = removeAttrs manifest.metadata.labels labels;
}
else {}
);
};
manifest: let
updateFunc = old: removeAttrs old labels;

hasLabelPath = p: res: (lib.attrByPath p null res) != null;

specialTemplateKinds = [
"Deployment"
"ReplicaSet"
"StatefulSet"
"DaemonSet"
"Job"
];
in
lib.attrsets.updateManyAttrsByPath (
# If metadata.labels is present, it should be filtered
(lib.optional (manifest.metadata ? labels) {
path = ["metadata" "labels"];
update = updateFunc;
})
# If it's one of the special kinds with
# spec.template.metadata.labels, that should be filtered
# too
++ (lib.optional (
(lib.any (k: manifest.kind == k) specialTemplateKinds)
&& (hasLabelPath ["spec" "template" "metadata" "labels"] manifest)
) {
path = ["spec" "template" "metadata" "labels"];
update = updateFunc;
})
# CronJob needs to be filtered differently too
++ (
lib.optionals
(manifest.kind == "CronJob")
(
(lib.optional (hasLabelPath ["spec" "jobTemplate" "metadata" "labels"] manifest) {
path = ["spec" "jobTemplate" "metadata" "labels"];
update = updateFunc;
})
++ (lib.optional (hasLabelPath ["spec" "jobTemplate" "spec" "template" "metadata" "labels"] manifest) {
path = ["spec" "jobTemplate" "spec" "template" "metadata" "labels"];
update = updateFunc;
})
)
)
)
manifest;
}
100 changes: 100 additions & 0 deletions lib/tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,106 @@ in {
};
};
};
testNoLabels = {
expr = lib.kube.removeLabels ["helm.sh/chart"] {
apiVersion = "apps/v1";
kind = "Deployment";
metadata = {
name = "argocd";
};
};
expected = {
apiVersion = "apps/v1";
kind = "Deployment";
metadata = {
name = "argocd";
};
};
};
testSpecialTemplateLabels = {
expr = lib.kube.removeLabels ["helm.sh/chart"] {
apiVersion = "apps/v1";
kind = "Deployment";
metadata = {
name = "test1-chart";
namespace = "test1";
labels = {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
"helm.sh/chart" = "chart-0.1.0";
};
};
spec = {
replicas = 1;
selector.matchLabels = {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/name" = "chart";
};
template = {
metadata.labels = {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
"helm.sh/chart" = "chart-0.1.0";
};
spec.containers = [
{
name = "chart";
image = "nginx:latest";
ports = [
{
name = "http";
containerPort = 80;
protocol = "TCP";
}
];
}
];
};
};
};
expected = {
apiVersion = "apps/v1";
kind = "Deployment";
metadata = {
name = "test1-chart";
namespace = "test1";
labels = {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
};
};
spec = {
replicas = 1;
selector.matchLabels = {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/name" = "chart";
};
template = {
metadata.labels = {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
};
spec.containers = [
{
name = "chart";
image = "nginx:latest";
ports = [
{
name = "http";
containerPort = 80;
protocol = "TCP";
}
];
}
];
};
};
};
};
};
};
}
4 changes: 0 additions & 4 deletions tests/helm/transformer.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ in {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
"app.kubernetes.io/version" = "1.16.0";
"helm.sh/chart" = "chart-0.1.0";
};
spec.containers = [
{
Expand Down Expand Up @@ -142,8 +140,6 @@ in {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
"app.kubernetes.io/version" = "1.16.0";
"helm.sh/chart" = "chart-0.1.0";
};
spec.containers = [
{
Expand Down

0 comments on commit af18c4e

Please sign in to comment.