Skip to content

Commit

Permalink
Add extraOpts option to helm releases
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
arnarg committed Oct 22, 2024
1 parent c92819f commit c154b74
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 1 deletion.
10 changes: 9 additions & 1 deletion modules/applications/helm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ in {
Whether or not to include CRDs in the helm release.
'';
};
extraOpts = mkOption {
type = with types; listOf str;
default = [];
example = ["--no-hooks"];
description = ''
Extra options to pass to `helm template` that is run when rendering the helm chart.
'';
};
transformer = mkOption {
type = with types; functionTo (listOf (attrsOf anything));
default = nixidyDefaults.helm.transformer;
Expand All @@ -70,7 +78,7 @@ in {

config = {
objects = with lib;
pipe {inherit (config) name namespace chart values includeCRDs;} [
pipe {inherit (config) name namespace chart values includeCRDs extraOpts;} [
helm.buildHelmChart
builtins.readFile
kube.fromYAML
Expand Down
1 change: 1 addition & 0 deletions tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
./helm/with-values.nix
./helm/transformer.nix
./helm/resource-override.nix
./helm/extra-opts.nix
./kustomize/base.nix
./kustomize/overlay.nix
./kustomize/resource-override.nix
Expand Down
21 changes: 21 additions & 0 deletions tests/helm/chart/templates/job-hook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: batch/v1
kind: Job
metadata:
name: job-hook
annotations:
helm.sh/hook: post-install,post-upgrade
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
template:
metadata:
labels:
{{- include "chart.labels" . | nindent 8 }}
spec:
containers:
- name: {{ .Chart.Name }}-job-hook
image: busybox
command:
- 'sh'
- '-c'
- 'echo The hook Job is running'
128 changes: 128 additions & 0 deletions tests/helm/extra-opts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
lib,
config,
...
}: let
apps = config.applications;
in {
# Create an application with a helm chart
# with `extraOpts` set
applications.test1.helm.releases.test1 = {
chart = ./chart;
extraOpts = ["--no-hooks"];
};

test = with lib; {
name = "helm chart with extra opts";
description = "Create an application with Helm chart and adding extra opts.";
assertions = [
{
description = "Deployment should be rendered correctly.";

expression =
findFirst
(x: x.kind == "Deployment" && x.metadata.name == "test1-chart")
null
apps.test1.objects;

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";
"app.kubernetes.io/version" = "1.16.0";
"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";
"app.kubernetes.io/version" = "1.16.0";
"helm.sh/chart" = "chart-0.1.0";
};
spec.containers = [
{
name = "chart";
image = "nginx:latest";
ports = [
{
name = "http";
containerPort = 80;
protocol = "TCP";
}
];
}
];
};
};
};
}

{
description = "Service should be rendered correctly.";

expression =
findFirst
(x: x.kind == "Service" && x.metadata.name == "test1-chart")
null
apps.test1.objects;

expected = {
apiVersion = "v1";
kind = "Service";
metadata = {
name = "test1-chart";
namespace = "test1";
labels = {
"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 = {
type = "ClusterIP";
ports = [
{
port = 80;
targetPort = "http";
protocol = "TCP";
name = "http";
}
];
selector = {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/name" = "chart";
};
};
};
}

{
description = "Job hook should not be rendered at all.";

expression =
findFirst
(x: x.kind == "Job" && x.metadata.name == "job-hook")
null
apps.test1.objects;

expected = null;
}
];
};
}
51 changes: 51 additions & 0 deletions tests/helm/no-values.nix
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,57 @@ in {
};
};
}

{
description = "Job hook should be rendered correctly.";

expression =
findFirst
(x: x.kind == "Job" && x.metadata.name == "job-hook")
null
apps.test1.objects;

expected = {
apiVersion = "batch/v1";
kind = "Job";
metadata = {
name = "job-hook";
namespace = "test1";
annotations = {
"helm.sh/hook" = "post-install,post-upgrade";
};
labels = {
"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 = {
template = {
metadata.labels = {
"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 = [
{
name = "chart-job-hook";
image = "busybox";
command = [
"sh"
"-c"
"echo The hook Job is running"
];
}
];
};
};
};
}
];
};
}
49 changes: 49 additions & 0 deletions tests/helm/transformer.nix
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,55 @@ in {
};
};
}

{
description = "Job hook should be rendered correctly.";

expression =
findFirst
(x: x.kind == "Job" && x.metadata.name == "job-hook")
null
apps.test1.objects;

expected = {
apiVersion = "batch/v1";
kind = "Job";
metadata = {
name = "job-hook";
namespace = "test1";
annotations = {
"helm.sh/hook" = "post-install,post-upgrade";
};
labels = {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
};
};
spec = {
template = {
metadata.labels = {
"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 = [
{
name = "chart-job-hook";
image = "busybox";
command = [
"sh"
"-c"
"echo The hook Job is running"
];
}
];
};
};
};
}
];
};
}
51 changes: 51 additions & 0 deletions tests/helm/with-values.nix
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,57 @@ in {
spec.ca.secretName = "ca-key-pair";
};
}

{
description = "Job hook should be rendered correctly.";

expression =
findFirst
(x: x.kind == "Job" && x.metadata.name == "job-hook")
null
apps.test1.objects;

expected = {
apiVersion = "batch/v1";
kind = "Job";
metadata = {
name = "job-hook";
namespace = "test1";
annotations = {
"helm.sh/hook" = "post-install,post-upgrade";
};
labels = {
"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 = {
template = {
metadata.labels = {
"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 = [
{
name = "chart-job-hook";
image = "busybox";
command = [
"sh"
"-c"
"echo The hook Job is running"
];
}
];
};
};
};
}
];
};
}

0 comments on commit c154b74

Please sign in to comment.