diff --git a/api/krusty/fnplugin_test.go b/api/krusty/fnplugin_test.go index e4d5fa83bd5..0422b8f51a2 100644 --- a/api/krusty/fnplugin_test.go +++ b/api/krusty/fnplugin_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + . "sigs.k8s.io/kustomize/api/krusty" kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -630,3 +631,181 @@ metadata: name: env `) } + +func TestFnContainerMounts(t *testing.T) { + skipIfNoDocker(t) + + th := kusttest_test.MakeHarness(t) + o := th.MakeOptionsPluginsEnabled() + fSys := filesys.MakeFsOnDisk() + b := MakeKustomizer(&o) + tmpDir, err := filesys.NewTmpConfirmedDir() + assert.NoError(t, err) + + path, err := os.Getwd() + assert.NoError(t, err) + chartPath := filepath.Join(path, "testdata", "charts") + + assert.NoError(t, fSys.WriteFile(filepath.Join(tmpDir.String(), "kustomization.yaml"), []byte(` +generators: + - |- + apiVersion: v1alpha1 + kind: RenderHelmChart + metadata: + name: demo + annotations: + config.kubernetes.io/function: | + container: + image: gcr.io/kpt-fn/render-helm-chart:v0.1.0 + mounts: + - type: "bind" + src: "`+chartPath+`" + dst: "/tmp/charts" + helmCharts: + - name: helloworld-chart + releaseName: test + valuesFile: /tmp/charts/helloworld-values/values.yaml +`))) + m, err := b.Run( + fSys, + tmpDir.String()) + assert.NoError(t, err) + yml, err := m.AsYaml() + assert.NoError(t, err) + assert.Equal(t, `apiVersion: v1 +kind: ServiceAccount +metadata: + labels: + app.kubernetes.io/instance: test + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: helloworld-chart + app.kubernetes.io/version: 1.16.0 + helm.sh/chart: helloworld-chart-0.1.0 + name: test-helloworld-chart +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app.kubernetes.io/instance: test + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: helloworld-chart + app.kubernetes.io/version: 1.16.0 + helm.sh/chart: helloworld-chart-0.1.0 + name: test-helloworld-chart +spec: + ports: + - name: http + port: 80 + protocol: TCP + targetPort: http + selector: + app.kubernetes.io/instance: test + app.kubernetes.io/name: helloworld-chart + type: ClusterIP +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app.kubernetes.io/instance: test + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: helloworld-chart + app.kubernetes.io/version: 1.16.0 + helm.sh/chart: helloworld-chart-0.1.0 + name: test-helloworld-chart +spec: + replicas: 5 + selector: + matchLabels: + app.kubernetes.io/instance: test + app.kubernetes.io/name: helloworld-chart + template: + metadata: + labels: + app.kubernetes.io/instance: test + app.kubernetes.io/name: helloworld-chart + spec: + containers: + - image: nginx:1.16.0 + imagePullPolicy: Always + livenessProbe: + httpGet: + path: / + port: http + name: helloworld-chart + ports: + - containerPort: 80 + name: http + protocol: TCP + readinessProbe: + httpGet: + path: / + port: http + resources: {} + securityContext: {} + securityContext: {} + serviceAccountName: test-helloworld-chart +--- +apiVersion: v1 +kind: Pod +metadata: + annotations: + helm.sh/hook: test-success + labels: + app.kubernetes.io/instance: test + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: helloworld-chart + app.kubernetes.io/version: 1.16.0 + helm.sh/chart: helloworld-chart-0.1.0 + name: test-helloworld-chart-test-connection +spec: + containers: + - args: + - test-helloworld-chart:80 + command: + - wget + image: busybox + name: wget + restartPolicy: Never +`, string(yml)) + + assert.NoError(t, fSys.RemoveAll(tmpDir.String())) +} + +func TestFnContainerMountsLoadRestrictions(t *testing.T) { + skipIfNoDocker(t) + + th := kusttest_test.MakeHarness(t) + o := th.MakeOptionsPluginsEnabled() + fSys := filesys.MakeFsOnDisk() + b := MakeKustomizer(&o) + tmpDir, err := filesys.NewTmpConfirmedDir() + assert.NoError(t, err) + + path, err := os.Getwd() + assert.NoError(t, err) + chartPath := filepath.Join("../", path, "testdata", "charts") + + assert.NoError(t, fSys.WriteFile(filepath.Join(tmpDir.String(), "kustomization.yaml"), []byte(` +generators: + - |- + apiVersion: v1alpha1 + kind: RenderHelmChart + metadata: + name: demo + annotations: + config.kubernetes.io/function: | + container: + image: gcr.io/kpt-fn/render-helm-chart:v0.1.0 + mounts: + - type: "bind" + src: "`+chartPath+`" + dst: "/tmp/charts" +`))) + _, err = b.Run( + fSys, + tmpDir.String()) + assert.Error(t, err) + assert.Equal(t, err.Error(), "couldn't execute function: declarative mount paths must be under the current directory") +} diff --git a/api/krusty/testdata/charts/helloworld-chart/Chart.yaml b/api/krusty/testdata/charts/helloworld-chart/Chart.yaml new file mode 100644 index 00000000000..de4012af9da --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-chart/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: helloworld-chart +description: A Helm chart for Kubernetes +type: application +version: 0.1.0 +appVersion: 1.16.0 diff --git a/api/krusty/testdata/charts/helloworld-chart/templates/NOTES.txt b/api/krusty/testdata/charts/helloworld-chart/templates/NOTES.txt new file mode 100644 index 00000000000..af17e53fbc9 --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-chart/templates/NOTES.txt @@ -0,0 +1,21 @@ +1. Get the application URL by running these commands: +{{- if .Values.ingress.enabled }} +{{- range $host := .Values.ingress.hosts }} + {{- range .paths }} + http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ . }} + {{- end }} +{{- end }} +{{- else if contains "NodePort" .Values.service.type }} + export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "helloworld-chart.fullname" . }}) + export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}") + echo http://$NODE_IP:$NODE_PORT +{{- else if contains "LoadBalancer" .Values.service.type }} + NOTE: It may take a few minutes for the LoadBalancer IP to be available. + You can watch the status of by running 'kubectl get --namespace {{ .Release.Namespace }} svc -w {{ include "helloworld-chart.fullname" . }}' + export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "helloworld-chart.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}") + echo http://$SERVICE_IP:{{ .Values.service.port }} +{{- else if contains "ClusterIP" .Values.service.type }} + export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app.kubernetes.io/name={{ include "helloworld-chart.name" . }},app.kubernetes.io/instance={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}") + echo "Visit http://127.0.0.1:8080 to use your application" + kubectl --namespace {{ .Release.Namespace }} port-forward $POD_NAME 8080:80 +{{- end }} diff --git a/api/krusty/testdata/charts/helloworld-chart/templates/_helpers.tpl b/api/krusty/testdata/charts/helloworld-chart/templates/_helpers.tpl new file mode 100644 index 00000000000..3a62dfa0f29 --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-chart/templates/_helpers.tpl @@ -0,0 +1,63 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "helloworld-chart.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "helloworld-chart.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "helloworld-chart.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Common labels +*/}} +{{- define "helloworld-chart.labels" -}} +helm.sh/chart: {{ include "helloworld-chart.chart" . }} +{{ include "helloworld-chart.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end -}} + +{{/* +Selector labels +*/}} +{{- define "helloworld-chart.selectorLabels" -}} +app.kubernetes.io/name: {{ include "helloworld-chart.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end -}} + +{{/* +Create the name of the service account to use +*/}} +{{- define "helloworld-chart.serviceAccountName" -}} +{{- if .Values.serviceAccount.create -}} + {{ default (include "helloworld-chart.fullname" .) .Values.serviceAccount.name }} +{{- else -}} + {{ default "default" .Values.serviceAccount.name }} +{{- end -}} +{{- end -}} diff --git a/api/krusty/testdata/charts/helloworld-chart/templates/deployment.yaml b/api/krusty/testdata/charts/helloworld-chart/templates/deployment.yaml new file mode 100644 index 00000000000..ca686e89ec4 --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-chart/templates/deployment.yaml @@ -0,0 +1,55 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "helloworld-chart.fullname" . }} + labels: + {{- include "helloworld-chart.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "helloworld-chart.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "helloworld-chart.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "helloworld-chart.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/api/krusty/testdata/charts/helloworld-chart/templates/ingress.yaml b/api/krusty/testdata/charts/helloworld-chart/templates/ingress.yaml new file mode 100644 index 00000000000..76e1dd91d79 --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-chart/templates/ingress.yaml @@ -0,0 +1,41 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "helloworld-chart.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "helloworld-chart.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: +{{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} +{{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ . }} + backend: + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} +{{- end }} diff --git a/api/krusty/testdata/charts/helloworld-chart/templates/service.yaml b/api/krusty/testdata/charts/helloworld-chart/templates/service.yaml new file mode 100644 index 00000000000..a80cf8cd648 --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-chart/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "helloworld-chart.fullname" . }} + labels: + {{- include "helloworld-chart.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "helloworld-chart.selectorLabels" . | nindent 4 }} diff --git a/api/krusty/testdata/charts/helloworld-chart/templates/serviceaccount.yaml b/api/krusty/testdata/charts/helloworld-chart/templates/serviceaccount.yaml new file mode 100644 index 00000000000..bf9a8e15446 --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-chart/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "helloworld-chart.serviceAccountName" . }} + labels: + {{- include "helloworld-chart.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end -}} diff --git a/api/krusty/testdata/charts/helloworld-chart/templates/tests/test-connection.yaml b/api/krusty/testdata/charts/helloworld-chart/templates/tests/test-connection.yaml new file mode 100644 index 00000000000..a71342a0d02 --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-chart/templates/tests/test-connection.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: "{{ include "helloworld-chart.fullname" . }}-test-connection" + labels: + {{- include "helloworld-chart.labels" . | nindent 4 }} + annotations: + "helm.sh/hook": test-success +spec: + containers: + - name: wget + image: busybox + command: ['wget'] + args: ['{{ include "helloworld-chart.fullname" . }}:{{ .Values.service.port }}'] + restartPolicy: Never diff --git a/api/krusty/testdata/charts/helloworld-chart/values.yaml b/api/krusty/testdata/charts/helloworld-chart/values.yaml new file mode 100644 index 00000000000..e81d4bf57dd --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-chart/values.yaml @@ -0,0 +1,27 @@ +replicaCount: 1 +image: + repository: nginx + pullPolicy: IfNotPresent +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" +serviceAccount: + create: true + annotations: {} + name: +podSecurityContext: {} +securityContext: {} +service: + type: ClusterIP + port: 80 +ingress: + enabled: false + annotations: {} + hosts: + - host: chart-example.local + paths: [] + tls: [] +resources: {} +nodeSelector: {} +tolerations: [] +affinity: {} diff --git a/api/krusty/testdata/charts/helloworld-values/values.yaml b/api/krusty/testdata/charts/helloworld-values/values.yaml new file mode 100644 index 00000000000..d178c72a8ad --- /dev/null +++ b/api/krusty/testdata/charts/helloworld-values/values.yaml @@ -0,0 +1,68 @@ +# Default values for helloworld-chart. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 5 + +image: + repository: nginx + pullPolicy: Always + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: + +podSecurityContext: {} +# fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true +# runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: [] + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m +# memory: 128Mi + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/kyaml/fn/runtime/runtimeutil/functiontypes.go b/kyaml/fn/runtime/runtimeutil/functiontypes.go index 83e7ff0eca0..39cb241959f 100644 --- a/kyaml/fn/runtime/runtimeutil/functiontypes.go +++ b/kyaml/fn/runtime/runtimeutil/functiontypes.go @@ -136,9 +136,6 @@ type FunctionSpec struct { // ExecSpec is the spec for running a function as an executable Exec ExecSpec `json:"exec,omitempty" yaml:"exec,omitempty"` - - // Mounts are the storage or directories to mount into the container - StorageMounts []StorageMount `json:"mounts,omitempty" yaml:"mounts,omitempty"` } type ExecSpec struct { @@ -208,9 +205,7 @@ func GetFunctionSpec(n *yaml.RNode) *FunctionSpec { if err != nil { return nil } - if fn := getFunctionSpecFromAnnotation(n, meta); fn != nil { - fn.StorageMounts = []StorageMount{} return fn } diff --git a/kyaml/runfn/runfn.go b/kyaml/runfn/runfn.go index eda2c290259..c7c79eefb14 100644 --- a/kyaml/runfn/runfn.go +++ b/kyaml/runfn/runfn.go @@ -468,11 +468,26 @@ func (r *RunFns) ffp(spec runtimeutil.FunctionSpec, api *yaml.RNode, currentUser if err != nil { return nil, err } + + // Storage mounts can either come from kustomize fn run --mounts, + // or from the declarative function mounts field. + // In the latter case, we ensure that the mount path is subject + // to kustomize's load restrictions. + storageMounts := spec.Container.StorageMounts + for _, mount := range storageMounts { + if strings.HasPrefix(filepath.Clean(mount.Src), "../") { + return nil, fmt.Errorf("declarative mount paths must be under the current directory") //nolint:goerr113 + } + } + if len(storageMounts) == 0 { + storageMounts = r.StorageMounts + } + c := container.NewContainer( runtimeutil.ContainerSpec{ Image: spec.Container.Image, Network: spec.Container.Network, - StorageMounts: r.StorageMounts, + StorageMounts: storageMounts, Env: spec.Container.Env, }, uidgid, diff --git a/plugin/builtin/hashtransformer/HashTransformer_test.go b/plugin/builtin/hashtransformer/HashTransformer_test.go index 3d62541ac04..27972250cdf 100644 --- a/plugin/builtin/hashtransformer/HashTransformer_test.go +++ b/plugin/builtin/hashtransformer/HashTransformer_test.go @@ -6,7 +6,7 @@ package main_test import ( "testing" - "sigs.k8s.io/kustomize/api/testutils/kusttest" + kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" ) func TestHashTransformer(t *testing.T) {