From 34bd64f4b3a49e8217c34d5930de357a41547304 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Tue, 30 Nov 2021 17:29:42 +0100 Subject: [PATCH] Fixup storage activation --- generator/main.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ helm/deployment.go | 16 +++++++++------- helm/storage.go | 29 +++++++++++++++++++++++++++++ main.go | 42 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 helm/storage.go diff --git a/generator/main.go b/generator/main.go index 54229be..fdeaba5 100644 --- a/generator/main.go +++ b/generator/main.go @@ -22,6 +22,7 @@ var Ingresses = make(map[string]*helm.Ingress, 0) // Values is kept in memory to create a values.yaml file. var Values = make(map[string]map[string]interface{}) +var VolumeValues = make(map[string]map[string]map[string]interface{}) var dependScript = ` OK=0 @@ -74,6 +75,45 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) { ContainerPort: port, }) } + + volumes := make([]map[string]interface{}, 0) + mountPoints := make([]interface{}, 0) + for _, volume := range s.Volumes { + parts := strings.Split(volume, ":") + volname := parts[0] + volepath := parts[1] + if strings.HasPrefix(volname, ".") || strings.HasPrefix(volname, "/") { + Redf("You cannot, at this time, have local volume in %s service", name) + os.Exit(1) + } + + pvc := helm.NewPVC(name, volname) + ret = append(ret, pvc) + volumes = append(volumes, map[string]interface{}{ + "name": volname, + "persistentVolumeClaim": map[string]string{ + "claimName": "{{ .Release.Name }}-" + volname, + }, + }) + mountPoints = append(mountPoints, map[string]interface{}{ + "name": volname, + "mountPath": volepath, + }) + + Yellow("Generate volume values for ", volname) + locker.Lock() + if _, ok := VolumeValues[name]; !ok { + VolumeValues[name] = make(map[string]map[string]interface{}) + } + VolumeValues[name][volname] = map[string]interface{}{ + "enabled": false, + "capacity": "1Gi", + } + locker.Unlock() + } + container.VolumeMounts = mountPoints + + o.Spec.Template.Spec.Volumes = volumes o.Spec.Template.Spec.Containers = []*helm.Container{container} o.Spec.Selector = map[string]interface{}{ @@ -119,6 +159,10 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) { ret = append(ret, ks) } + if len(VolumeValues[name]) > 0 { + Values[name]["persistence"] = VolumeValues[name] + } + Green("Done deployment ", name) return diff --git a/helm/deployment.go b/helm/deployment.go index 57cef9c..033ca58 100644 --- a/helm/deployment.go +++ b/helm/deployment.go @@ -38,11 +38,12 @@ type ContainerPort struct { } type Container struct { - Name string `yaml:"name,omitempty"` - Image string `yaml:"image"` - Ports []*ContainerPort `yaml:"ports,omitempty"` - Env []Value `yaml:"env,omitempty"` - Command []string `yaml:"command,omitempty"` + Name string `yaml:"name,omitempty"` + Image string `yaml:"image"` + Ports []*ContainerPort `yaml:"ports,omitempty"` + Env []Value `yaml:"env,omitempty"` + Command []string `yaml:"command,omitempty"` + VolumeMounts []interface{} `yaml:"volumeMounts,omitempty"` } func NewContainer(name, image string, environment, labels map[string]string) *Container { @@ -71,8 +72,9 @@ func NewContainer(name, image string, environment, labels map[string]string) *Co } type PodSpec struct { - InitContainers []*Container `yaml:"initContainers,omitempty"` - Containers []*Container `yaml:"containers"` + InitContainers []*Container `yaml:"initContainers,omitempty"` + Containers []*Container `yaml:"containers"` + Volumes []map[string]interface{} `yaml:"volumes,omitempty"` } type PodTemplate struct { diff --git a/helm/storage.go b/helm/storage.go new file mode 100644 index 0000000..8f2b976 --- /dev/null +++ b/helm/storage.go @@ -0,0 +1,29 @@ +package helm + +type Storage struct { + *K8sBase `yaml:",inline"` + Spec *PVCSpec +} + +func NewPVC(name, storageName string) *Storage { + pvc := &Storage{} + pvc.K8sBase = NewBase() + pvc.K8sBase.Kind = "PersistentVolumeClaim" + pvc.K8sBase.Metadata.Labels[K+"/pvc-name"] = storageName + pvc.K8sBase.ApiVersion = "v1" + pvc.K8sBase.Metadata.Name = "{{ .Release.Name }}-" + storageName + pvc.Spec = &PVCSpec{ + Resouces: map[string]interface{}{ + "requests": map[string]string{ + "capacity": "{{ .Values." + name + ".persistence." + storageName + ".capacity }}", + }, + }, + AccessModes: []string{"ReadWriteOnce"}, + } + return pvc +} + +type PVCSpec struct { + Resouces map[string]interface{} `yaml:"resources"` + AccessModes []string `yaml:"accessModes"` +} diff --git a/main.go b/main.go index 0014fac..941dde5 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "flag" "helm-compose/compose" "helm-compose/generator" @@ -53,9 +54,42 @@ func main() { kind = strings.ToLower(kind) fname := filepath.Join(templatesDir, n+"."+kind+".yaml") fp, _ := os.Create(fname) - enc := yaml.NewEncoder(fp) - enc.SetIndent(2) - enc.Encode(c) + switch c := c.(type) { + case *helm.Storage: + volname := c.K8sBase.Metadata.Labels[helm.K+"/pvc-name"] + fp.WriteString("{{ if .Values." + n + ".persistence." + volname + ".enabled }}\n") + enc := yaml.NewEncoder(fp) + enc.SetIndent(2) + enc.Encode(c) + fp.WriteString("{{- end -}}") + case *helm.Deployment: + var buff []byte + buffer := bytes.NewBuffer(buff) + enc := yaml.NewEncoder(buffer) + enc.SetIndent(2) + enc.Encode(c) + _content := string(buffer.Bytes()) + content := strings.Split(string(_content), "\n") + dataname := "" + component := c.Spec.Selector["matchLabels"].(map[string]string)[helm.K+"/component"] + for _, line := range content { + if strings.Contains(line, "name:") { + dataname = strings.Split(line, ":")[1] + dataname = strings.TrimSpace(dataname) + } else if strings.Contains(line, "persistentVolumeClaim") { + line = " {{- if .Values." + component + ".persistence." + dataname + ".enabled }}\n" + line + } else if strings.Contains(line, "claimName") { + line += "\n {{ else }}" + line += "\n emptyDir: {}" + line += "\n {{- end }}" + } + fp.WriteString(line + "\n") + } + default: + enc := yaml.NewEncoder(fp) + enc.SetIndent(2) + enc.Encode(c) + } fp.Close() } } @@ -67,7 +101,7 @@ func main() { enc.SetIndent(2) fp.WriteString("{{- if .Values." + name + ".ingress.enabled -}}\n") enc.Encode(ing) - fp.WriteString("\n{{- end -}}") + fp.WriteString("{{- end -}}") fp.Close() }