Refactorisation to writers in generator package
This commit is contained in:
17
generator/writers/configmap.go
Normal file
17
generator/writers/configmap.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package writers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func BuildConfigMap(c interface{}, kind, servicename, name, templatesDir string) {
|
||||
fname := filepath.Join(templatesDir, servicename+"."+name+"."+kind+".yaml")
|
||||
fp, _ := os.Create(fname)
|
||||
enc := yaml.NewEncoder(fp)
|
||||
enc.SetIndent(2)
|
||||
enc.Encode(c)
|
||||
fp.Close()
|
||||
}
|
40
generator/writers/deployment.go
Normal file
40
generator/writers/deployment.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package writers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"katenary/helm"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func BuildDeployment(deployment *helm.Deployment, name, templatesDir string) {
|
||||
kind := "deployment"
|
||||
fname := filepath.Join(templatesDir, name+"."+kind+".yaml")
|
||||
fp, _ := os.Create(fname)
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
enc := yaml.NewEncoder(buffer)
|
||||
enc.SetIndent(2)
|
||||
enc.Encode(deployment)
|
||||
_content := string(buffer.Bytes())
|
||||
content := strings.Split(string(_content), "\n")
|
||||
dataname := ""
|
||||
component := deployment.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")
|
||||
}
|
||||
fp.Close()
|
||||
|
||||
}
|
40
generator/writers/ingress.go
Normal file
40
generator/writers/ingress.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package writers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"katenary/helm"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func BuildIngress(ingress *helm.Ingress, name, templatesDir string) {
|
||||
kind := "ingress"
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
fname := filepath.Join(templatesDir, name+"."+kind+".yaml")
|
||||
enc := yaml.NewEncoder(buffer)
|
||||
enc.SetIndent(2)
|
||||
buffer.WriteString("{{- if .Values." + name + ".ingress.enabled -}}\n")
|
||||
enc.Encode(ingress)
|
||||
buffer.WriteString("{{- end -}}")
|
||||
|
||||
fp, _ := os.Create(fname)
|
||||
content := string(buffer.Bytes())
|
||||
lines := strings.Split(content, "\n")
|
||||
for _, l := range lines {
|
||||
if strings.Contains(l, "ingressClassName") {
|
||||
p := strings.Split(l, ":")
|
||||
condition := p[1]
|
||||
condition = strings.ReplaceAll(condition, "'", "")
|
||||
condition = strings.ReplaceAll(condition, "{{", "")
|
||||
condition = strings.ReplaceAll(condition, "}}", "")
|
||||
condition = strings.TrimSpace(condition)
|
||||
condition = "{{- if " + condition + " }}"
|
||||
l = " " + condition + "\n" + l + "\n {{- end }}"
|
||||
}
|
||||
fp.WriteString(l + "\n")
|
||||
}
|
||||
fp.Close()
|
||||
}
|
23
generator/writers/service.go
Normal file
23
generator/writers/service.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package writers
|
||||
|
||||
import (
|
||||
"katenary/helm"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func BuildService(service *helm.Service, name, templatesDir string) {
|
||||
kind := "service"
|
||||
suffix := ""
|
||||
if service.Spec.Type == "NodePort" {
|
||||
suffix = "-external"
|
||||
}
|
||||
fname := filepath.Join(templatesDir, name+suffix+"."+kind+".yaml")
|
||||
fp, _ := os.Create(fname)
|
||||
enc := yaml.NewEncoder(fp)
|
||||
enc.SetIndent(2)
|
||||
enc.Encode(service)
|
||||
fp.Close()
|
||||
}
|
21
generator/writers/storage.go
Normal file
21
generator/writers/storage.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package writers
|
||||
|
||||
import (
|
||||
"katenary/helm"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func BuildStorage(storage *helm.Storage, name, templatesDir string) {
|
||||
kind := "pvc"
|
||||
fname := filepath.Join(templatesDir, name+"."+kind+".yaml")
|
||||
fp, _ := os.Create(fname)
|
||||
volname := storage.K8sBase.Metadata.Labels[helm.K+"/pvc-name"]
|
||||
fp.WriteString("{{ if .Values." + name + ".persistence." + volname + ".enabled }}\n")
|
||||
enc := yaml.NewEncoder(fp)
|
||||
enc.SetIndent(2)
|
||||
enc.Encode(storage)
|
||||
fp.WriteString("{{- end -}}")
|
||||
}
|
Reference in New Issue
Block a user