Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for extraVolumes/extraVolumeMounts #583

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ install-go-tools: mod-download
go install github.com/golang/mock/[email protected]
go install github.com/onsi/ginkgo/v2/[email protected]
go install golang.org/x/tools/cmd/goimports
go install sigs.k8s.io/kind/cmd/[email protected]

# proto compiler installation
PROTOC_VERSION:=3.15.8
Expand Down Expand Up @@ -74,18 +75,26 @@ generate-changelog:
# set TEST_PKG to run a specific test package
.PHONY: run-tests
run-tests:
PATH=$(DEPSGOBIN):$$PATH ginkgo -r -failFast -trace -progress \
-progress \
PATH=$(DEPSGOBIN):$$PATH ginkgo -r --fail-fast -trace \
--show-node-events \
-compilers=4 \
-skipPackage=$(SKIP_PACKAGES) $(TEST_PKG) \
$(GINKGO_FLAGS) \
--skip-package=$(SKIP_PACKAGES) $(TEST_PKG) \
-failOnPending \
-randomizeAllSpecs \
-randomizeSuites \
-keepGoing
$(DEPSGOBIN)/goimports -w .

run-test:
PATH=$(DEPSGOBIN):$$PATH ginkgo $(GINKGO_FLAGS) $(TEST_PKG)
test-clusters:
@kind create cluster --name skv2-test-master 2> /dev/null || true
@kind create cluster --name skv2-test-remote 2> /dev/null || true

# CI workflow for running tests
run-all: REMOTE_CLUSTER_CONTEXT ?= kind-skv2-test-remote
run-all: test-clusters
@go test ./...
@goimports -w .

#----------------------------------------------------------------------------------
# Third Party License Management
Expand Down
6 changes: 6 additions & 0 deletions changelog/v0.40.7/issue-582.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
changelog:
- type: NEW_FEATURE
issueLink: https://github.com/solo-io/skv2/issues/582
description: >
Support extraVolumes/extraVolumeMounts fields which can be passed in by the user from helm values.
skipCI: "false"
234 changes: 234 additions & 0 deletions codegen/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,240 @@ var _ = Describe("Cmd", func() {
"encoding/protobuf/cue/cue.proto",
}

{
type TestEntry struct {
name string
values any
staticVolumes []v1.Volume
conditionalVolumes []ConditionalVolume
staticVolumeMounts []v1.VolumeMount
conditionalVolumeMounts []ConditionalVolumeMount
}

DescribeTable(
"extraVolume/extraVolumeMounts",
Ordered, func(entry TestEntry, expectedVolumes, expectedVolumeMounts int) {
cmd := &Command{
Chart: &Chart{
Data: Data{
ApiVersion: "v1",
Description: "",
Name: "Painting Operator",
Version: "v0.0.1",
Home: "https://docs.solo.io/skv2/latest",
Sources: []string{
"https://github.com/solo-io/skv2",
},
},
Operators: []Operator{{
Name: "painter",
Deployment: Deployment{
Container: Container{
Image: Image{
Tag: "v0.0.0",
Repository: "painter",
Registry: "quay.io/solo-io",
PullPolicy: "IfNotPresent",
},
VolumeMounts: entry.staticVolumeMounts,
ConditionalVolumeMounts: entry.conditionalVolumeMounts,
},
Volumes: entry.staticVolumes,
ConditionalVolumes: entry.conditionalVolumes,
},
}},
},
ManifestRoot: fmt.Sprintf("codegen/test/chart/%s", entry.name),
}
Expect(cmd.Execute()).NotTo(HaveOccurred(), "failed to execute command")

manifests := helmTemplate(fmt.Sprintf("./test/chart/%s", entry.name), entry.values)

var (
renderedDeployment *appsv1.Deployment
decoder = kubeyaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(manifests), 4096)
)
for {
var deployment appsv1.Deployment
if err := decoder.Decode(&deployment); errors.Is(err, io.EOF) {
break
}

if deployment.GetName() == "painter" && deployment.Kind == "Deployment" {
renderedDeployment = &deployment
break
}
}

Expect(renderedDeployment.Spec.Template.Spec.Volumes).To(HaveLen(expectedVolumes))

Expect(renderedDeployment.Spec.Template.Spec.Containers).To(HaveLen(1))

Expect(renderedDeployment.Spec.Template.Spec.Containers[0].VolumeMounts).To(HaveLen(expectedVolumes))
},
Entry(
"empty with no volumes",
TestEntry{
name: "extra-volumes",
values: map[string]any{
"painter": map[string]any{
"enabled": true,
"extraVolumes": []v1.Volume{{
Name: "extra-certs",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "extra-secret",
},
},
}},
"extraVolumeMounts": []v1.VolumeMount{{
Name: "extra-certs",
MountPath: "/etc/ssl/certs",
}},
},
},
},
0,
0,
),
Entry(
"with static volumes",
TestEntry{
name: "static-volumes",
values: map[string]any{
"painter": map[string]any{
"enabled": true,
"extraVolumes": []v1.Volume{{
Name: "extra-certs",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "extra-secret",
},
},
}},
"extraVolumeMounts": []v1.VolumeMount{{
Name: "extra-certs",
MountPath: "/etc/ssl/extra",
}},
},
},
staticVolumes: []v1.Volume{{
Name: "static-certs",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "static-secret",
},
},
}},
staticVolumeMounts: []v1.VolumeMount{{
Name: "static-certs",
MountPath: "/var/run/secret/static",
}},
},
2,
2,
),
Entry(
"with conditional volumes",
TestEntry{
name: "conditional-volumes",
values: map[string]any{
"painter": map[string]any{
"enabled": true,
"extraVolumes": []v1.Volume{{
Name: "extra-certs",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "extra-secret",
},
},
}},
"extraVolumeMounts": []v1.VolumeMount{{
Name: "extra-certs",
MountPath: "/etc/ssl/extra",
}},
},
},
conditionalVolumes: []ConditionalVolume{{
Condition: ".Values.painter.enabled",
Volume: v1.Volume{
Name: "conditional-certs",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "conditional-secret",
},
},
},
}},
conditionalVolumeMounts: []ConditionalVolumeMount{{
Condition: ".Values.painter.enabled",
VolumeMount: v1.VolumeMount{
Name: "conditional-certs",
MountPath: "/var/run/secret/conditional",
},
}},
},
2,
2,
),
Entry(
"with all volumes",
TestEntry{
name: "all-volumes",
values: map[string]any{
"painter": map[string]any{
"enabled": true,
"extraVolumes": []v1.Volume{{
Name: "extra-certs",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "extra-secret",
},
},
}},
"extraVolumeMounts": []v1.VolumeMount{{
Name: "extra-certs",
MountPath: "/etc/ssl/extra",
}},
},
},
staticVolumes: []v1.Volume{{
Name: "static-certs",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "static-secret",
},
},
}},
conditionalVolumes: []ConditionalVolume{{
Condition: ".Values.painter.enabled",
Volume: v1.Volume{
Name: "conditional-certs",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: "conditional-secret",
},
},
},
}},
staticVolumeMounts: []v1.VolumeMount{{
Name: "static-certs",
MountPath: "/var/run/secret/static",
}},
conditionalVolumeMounts: []ConditionalVolumeMount{{
Condition: ".Values.painter.enabled",
VolumeMount: v1.VolumeMount{
Name: "conditional-certs",
MountPath: "/var/run/secret/conditional",
},
}},
},
3,
3,
),
)
}

Describe("image pull secrets", Ordered, func() {
BeforeAll(func() {
cmd := &Command{
Expand Down
16 changes: 14 additions & 2 deletions codegen/templates/chart/operator-deployment.yamltmpl
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,15 @@ spec:
[[- end ]]
[[- if $volumes ]]
volumes:
[[ toYaml $volumes | indent 6 ]]
[[- toYaml $volumes | nindent 6 ]]
{{- if [[ (opVar $operator) ]].extraVolumes }}
{{- tpl (toYaml [[ (opVar $operator) ]].extraVolumes) . | nindent 6 }}
{{- end }}
[[- else if $conditionalVolumes ]]
volumes:
{{- if [[ (opVar $operator) ]].extraVolumes }}
{{- tpl (toYaml [[ (opVar $operator) ]].extraVolumes) . | nindent 6 }}
{{- end }}
[[- end ]]
[[- range $v := $conditionalVolumes ]]
{{- if [[ $v.Condition ]] }}
Expand Down Expand Up @@ -178,9 +184,15 @@ spec:
{{- end }}
[[- if $container.VolumeMounts ]]
volumeMounts:
[[ toYaml $container.VolumeMounts | indent 8 ]]
[[- toYaml $container.VolumeMounts | nindent 8 ]]
{{- if [[ (opVar $operator) ]].extraVolumeMounts }}
{{- tpl (toYaml [[ (opVar $operator) ]].extraVolumeMounts) . | nindent 8 }}
{{- end }}
[[- else if $container.ConditionalVolumeMounts ]]
volumeMounts:
{{- if [[ (opVar $operator) ]].extraVolumeMounts }}
{{- tpl (toYaml [[ (opVar $operator) ]].extraVolumeMounts) . | nindent 8 }}
{{- end }}
[[- end ]]
[[- range $v := $container.ConditionalVolumeMounts ]]
{{- if [[ $v.Condition ]] }}
Expand Down
6 changes: 6 additions & 0 deletions codegen/test/chart-no-desc/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ spec:
volumes:
- emptyDir: {}
name: paint
{{- if $.Values.painter.extraVolumes }}
{{- tpl (toYaml $.Values.painter.extraVolumes) . | nindent 6 }}
{{- end }}
containers:
{{- $painter := $.Values.painter }}
{{- $painterImage := $painter.image }}
Expand Down Expand Up @@ -116,6 +119,9 @@ spec:
volumeMounts:
- mountPath: /etc/paint
name: paint
{{- if $.Values.painter.extraVolumeMounts }}
{{- tpl (toYaml $.Values.painter.extraVolumeMounts) . | nindent 8 }}
{{- end }}
resources:
{{- if $palette.resources }}
{{ toYaml $palette.resources | indent 10}}
Expand Down
8 changes: 8 additions & 0 deletions codegen/test/chart/all-volumes/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Code generated by skv2. DO NOT EDIT.

apiVersion: v1
home: https://docs.solo.io/skv2/latest
name: Painting Operator
sources:
- https://github.com/solo-io/skv2
version: v0.0.1
Loading
Loading