diff --git a/cmd/helmChart.go b/cmd/helmChart.go index 542b8faff..8bf0a81da 100644 --- a/cmd/helmChart.go +++ b/cmd/helmChart.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "sort" + "strings" "github.com/kubeshark/kubeshark/config" "github.com/kubeshark/kubeshark/kubernetes" @@ -257,6 +258,34 @@ func template(object interface{}, mappings map[string]interface{}) (template int return } +func handlePVCManifest(manifest string) string { + return fmt.Sprintf("{{- if .Values.tap.persistentstorage }}\n%s{{- end }}", manifest) +} + +func handleDaemonSetManifest(manifest string) string { + lines := strings.Split(manifest, "\n") + + for i, line := range lines { + if strings.TrimSpace(line) == "- mountPath: /app/data" { + lines[i] = fmt.Sprintf("{{- if .Values.tap.persistentstorage }}\n%s", line) + } + + if strings.TrimSpace(line) == "name: kubeshark-persistent-volume" { + lines[i] = fmt.Sprintf("%s\n{{- end }}", line) + } + + if strings.TrimSpace(line) == "- name: kubeshark-persistent-volume" { + lines[i] = fmt.Sprintf("{{- if .Values.tap.persistentstorage }}\n%s", line) + } + + if strings.TrimSpace(line) == "claimName: kubeshark-persistent-volume-claim" { + lines[i] = fmt.Sprintf("%s\n{{- end }}", line) + } + } + + return strings.Join(lines, "\n") +} + func dumpHelmChart(objects map[string]interface{}) error { folder := filepath.Join(".", "helm-chart") templatesFolder := filepath.Join(folder, "templates") @@ -285,6 +314,14 @@ func dumpHelmChart(objects map[string]interface{}) error { return err } + if filename == "08-persistent-volume-claim.yaml" { + manifest = handlePVCManifest(manifest) + } + + if filename == "09-worker-daemon-set.yaml" { + manifest = handleDaemonSetManifest(manifest) + } + path := filepath.Join(templatesFolder, filename) err = os.WriteFile(path, []byte(manifestHeader+manifest), 0644) if err != nil { diff --git a/cmd/manifests.go b/cmd/manifests.go index b12c662bb..51f882ee7 100644 --- a/cmd/manifests.go +++ b/cmd/manifests.go @@ -104,6 +104,8 @@ func generateManifests() ( err error, ) { config.Config.License = "" + persistentStorage := config.Config.Tap.PersistentStorage + config.Config.Tap.PersistentStorage = true var kubernetesProvider *kubernetes.Provider kubernetesProvider, err = getKubernetesProviderForCli(true, true) @@ -171,6 +173,8 @@ func generateManifests() ( return } + config.Config.Tap.PersistentStorage = persistentStorage + return } diff --git a/helm-chart/templates/08-persistent-volume-claim.yaml b/helm-chart/templates/08-persistent-volume-claim.yaml index be0264288..52f6b13d8 100644 --- a/helm-chart/templates/08-persistent-volume-claim.yaml +++ b/helm-chart/templates/08-persistent-volume-claim.yaml @@ -1,5 +1,6 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY KUBESHARK CLI. DO NOT EDIT! --- +{{- if .Values.tap.persistentstorage }} apiVersion: v1 kind: PersistentVolumeClaim metadata: @@ -18,3 +19,4 @@ spec: storage: '{{ .Values.tap.storagelimit }}' storageClassName: '{{ .Values.tap.storageclass }}' status: {} +{{- end }} \ No newline at end of file diff --git a/helm-chart/templates/09-worker-daemon-set.yaml b/helm-chart/templates/09-worker-daemon-set.yaml index 52155082e..47853f6f6 100644 --- a/helm-chart/templates/09-worker-daemon-set.yaml +++ b/helm-chart/templates/09-worker-daemon-set.yaml @@ -67,6 +67,10 @@ spec: - mountPath: /sys name: sys readOnly: true +{{- if .Values.tap.persistentstorage }} + - mountPath: /app/data + name: kubeshark-persistent-volume +{{- end }} dnsPolicy: ClusterFirstWithHostNet hostNetwork: true serviceAccountName: kubeshark-service-account @@ -83,3 +87,8 @@ spec: - hostPath: path: /sys name: sys +{{- if .Values.tap.persistentstorage }} + - name: kubeshark-persistent-volume + persistentVolumeClaim: + claimName: kubeshark-persistent-volume-claim +{{- end }} diff --git a/manifests/09-worker-daemon-set.yaml b/manifests/09-worker-daemon-set.yaml index 82ed36d27..53c7ae0fd 100644 --- a/manifests/09-worker-daemon-set.yaml +++ b/manifests/09-worker-daemon-set.yaml @@ -67,6 +67,8 @@ spec: - mountPath: /sys name: sys readOnly: true + - mountPath: /app/data + name: kubeshark-persistent-volume dnsPolicy: ClusterFirstWithHostNet hostNetwork: true serviceAccountName: kubeshark-service-account @@ -83,3 +85,6 @@ spec: - hostPath: path: /sys name: sys + - name: kubeshark-persistent-volume + persistentVolumeClaim: + claimName: kubeshark-persistent-volume-claim