Fixup storage activation

This commit is contained in:
2021-11-30 17:29:42 +01:00
parent b586b1eac8
commit 34bd64f4b3
4 changed files with 120 additions and 11 deletions

View File

@@ -22,6 +22,7 @@ var Ingresses = make(map[string]*helm.Ingress, 0)
// Values is kept in memory to create a values.yaml file. // Values is kept in memory to create a values.yaml file.
var Values = make(map[string]map[string]interface{}) var Values = make(map[string]map[string]interface{})
var VolumeValues = make(map[string]map[string]map[string]interface{})
var dependScript = ` var dependScript = `
OK=0 OK=0
@@ -74,6 +75,45 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) {
ContainerPort: port, 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.Template.Spec.Containers = []*helm.Container{container}
o.Spec.Selector = map[string]interface{}{ o.Spec.Selector = map[string]interface{}{
@@ -119,6 +159,10 @@ func CreateReplicaObject(name string, s compose.Service) (ret []interface{}) {
ret = append(ret, ks) ret = append(ret, ks)
} }
if len(VolumeValues[name]) > 0 {
Values[name]["persistence"] = VolumeValues[name]
}
Green("Done deployment ", name) Green("Done deployment ", name)
return return

View File

@@ -38,11 +38,12 @@ type ContainerPort struct {
} }
type Container struct { type Container struct {
Name string `yaml:"name,omitempty"` Name string `yaml:"name,omitempty"`
Image string `yaml:"image"` Image string `yaml:"image"`
Ports []*ContainerPort `yaml:"ports,omitempty"` Ports []*ContainerPort `yaml:"ports,omitempty"`
Env []Value `yaml:"env,omitempty"` Env []Value `yaml:"env,omitempty"`
Command []string `yaml:"command,omitempty"` Command []string `yaml:"command,omitempty"`
VolumeMounts []interface{} `yaml:"volumeMounts,omitempty"`
} }
func NewContainer(name, image string, environment, labels map[string]string) *Container { 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 { type PodSpec struct {
InitContainers []*Container `yaml:"initContainers,omitempty"` InitContainers []*Container `yaml:"initContainers,omitempty"`
Containers []*Container `yaml:"containers"` Containers []*Container `yaml:"containers"`
Volumes []map[string]interface{} `yaml:"volumes,omitempty"`
} }
type PodTemplate struct { type PodTemplate struct {

29
helm/storage.go Normal file
View File

@@ -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"`
}

42
main.go
View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"flag" "flag"
"helm-compose/compose" "helm-compose/compose"
"helm-compose/generator" "helm-compose/generator"
@@ -53,9 +54,42 @@ func main() {
kind = strings.ToLower(kind) kind = strings.ToLower(kind)
fname := filepath.Join(templatesDir, n+"."+kind+".yaml") fname := filepath.Join(templatesDir, n+"."+kind+".yaml")
fp, _ := os.Create(fname) fp, _ := os.Create(fname)
enc := yaml.NewEncoder(fp) switch c := c.(type) {
enc.SetIndent(2) case *helm.Storage:
enc.Encode(c) 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() fp.Close()
} }
} }
@@ -67,7 +101,7 @@ func main() {
enc.SetIndent(2) enc.SetIndent(2)
fp.WriteString("{{- if .Values." + name + ".ingress.enabled -}}\n") fp.WriteString("{{- if .Values." + name + ".ingress.enabled -}}\n")
enc.Encode(ing) enc.Encode(ing)
fp.WriteString("\n{{- end -}}") fp.WriteString("{{- end -}}")
fp.Close() fp.Close()
} }