update vault integration
This commit is contained in:
105
internal/templates/funcs.go
Normal file
105
internal/templates/funcs.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
func funcMap() template.FuncMap {
|
||||
f := sprig.TxtFuncMap()
|
||||
delete(f, "env")
|
||||
delete(f, "expandenv")
|
||||
|
||||
extra := template.FuncMap{
|
||||
"toToml": toTOML,
|
||||
"toYaml": toYAML,
|
||||
"fromYaml": fromYAML,
|
||||
"fromYamlArray": fromYAMLArray,
|
||||
"toJson": toJSON,
|
||||
"fromJson": fromJSON,
|
||||
"fromJsonArray": fromJSONArray,
|
||||
|
||||
"include": func(string, interface{}) string { return "not implemented" },
|
||||
"tpl": func(string, interface{}) interface{} { return "not implemented" },
|
||||
"required": func(string, interface{}) (interface{}, error) { return "not implemented", nil },
|
||||
"lookup": func(string, string, string, string) (map[string]interface{}, error) {
|
||||
return map[string]interface{}{}, nil
|
||||
},
|
||||
}
|
||||
|
||||
for k, v := range extra {
|
||||
f[k] = v
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func toYAML(v interface{}) string {
|
||||
data, err := yaml.Marshal(v)
|
||||
if err != nil {
|
||||
// Swallow errors inside of a template.
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSuffix(string(data), "\n")
|
||||
}
|
||||
|
||||
func fromYAML(str string) map[string]interface{} {
|
||||
m := map[string]interface{}{}
|
||||
|
||||
if err := yaml.Unmarshal([]byte(str), &m); err != nil {
|
||||
m["Error"] = err.Error()
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func fromYAMLArray(str string) []interface{} {
|
||||
a := []interface{}{}
|
||||
|
||||
if err := yaml.Unmarshal([]byte(str), &a); err != nil {
|
||||
a = []interface{}{err.Error()}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func toTOML(v interface{}) string {
|
||||
b := bytes.NewBuffer(nil)
|
||||
e := toml.NewEncoder(b)
|
||||
err := e.Encode(v)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func toJSON(v interface{}) string {
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
// Swallow errors inside of a template.
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func fromJSON(str string) map[string]interface{} {
|
||||
m := make(map[string]interface{})
|
||||
|
||||
if err := json.Unmarshal([]byte(str), &m); err != nil {
|
||||
m["Error"] = err.Error()
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func fromJSONArray(str string) []interface{} {
|
||||
a := []interface{}{}
|
||||
|
||||
if err := json.Unmarshal([]byte(str), &a); err != nil {
|
||||
a = []interface{}{err.Error()}
|
||||
}
|
||||
return a
|
||||
}
|
||||
45
internal/templates/helm_apps.go
Normal file
45
internal/templates/helm_apps.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"kube-forge/internal/config"
|
||||
"kube-forge/internal/resources"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var HELM_APPS_TEMPLATES = [...]string{
|
||||
"templates/helm-apps/releases/additional-modules/docker-secrets-generator.yml.tmpl",
|
||||
"templates/helm-apps/releases/additional-modules/longhorn.yml.tmpl",
|
||||
"templates/helm-apps/releases/additional-modules/cert-manager.yml.tmpl",
|
||||
"templates/helm-apps/releases/additional-modules/ingress-nginx.yml.tmpl",
|
||||
"templates/helm-apps/releases/additional-modules/secrets-store-csi-driver.yml.tmpl",
|
||||
"templates/helm-apps/releases/observability/fluent-operator.yml.tmpl",
|
||||
"templates/helm-apps/releases/observability/opentelemetry-operator.yml.tmpl",
|
||||
"templates/helm-apps/releases/observability/tempo.yml.tmpl",
|
||||
"templates/helm-apps/releases/observability/loki.yml.tmpl",
|
||||
"templates/helm-apps/releases/observability/observability.yml.tmpl",
|
||||
"templates/helm-apps/releases/registry/harbor-certificate-generator.yml.tmpl",
|
||||
"templates/helm-apps/releases/registry/harbor.yml.tmpl",
|
||||
"templates/helm-apps/releases/cicd/argo-cd.yml.tmpl",
|
||||
"templates/helm-apps/releases/cicd/argo-rollouts.yml.tmpl",
|
||||
"templates/helm-apps/releases/cicd/keel.yml.tmpl",
|
||||
"templates/helm-apps/releases/cicd/argo-cd-ingress.yml.tmpl",
|
||||
"templates/helm-apps/releases/secrets-storage/vault.yml.tmpl",
|
||||
}
|
||||
var HELM_REPOSITORIES_TEMPLATES = [...]string{
|
||||
"templates/helm-apps/repositories/repositories.yml.tmpl",
|
||||
}
|
||||
|
||||
func GetHelmAppsConfigData() (string, string) {
|
||||
cfg := config.GetConfig()
|
||||
helmAppsTemplateResults := []string{}
|
||||
for _, templateFile := range HELM_APPS_TEMPLATES {
|
||||
template := getTemplateFromEmbedFSFolder(resources.Templates, templateFile)
|
||||
helmAppsTemplateResults = append(helmAppsTemplateResults, executeTemplateToString(template, cfg))
|
||||
}
|
||||
repositoriesTemplateResults := []string{}
|
||||
for _, templateFile := range HELM_REPOSITORIES_TEMPLATES {
|
||||
template := getTemplateFromEmbedFSFolder(resources.Templates, templateFile)
|
||||
repositoriesTemplateResults = append(repositoriesTemplateResults, executeTemplateToString(template, cfg))
|
||||
}
|
||||
return strings.Join(repositoriesTemplateResults, "\n"), strings.Join(helmAppsTemplateResults, "\n")
|
||||
}
|
||||
12
internal/templates/kubespray.go
Normal file
12
internal/templates/kubespray.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package templates
|
||||
|
||||
var K8S_TEMPLATES = [...][2]string{
|
||||
{"templates/kubespray/inventory/hosts.tmpl", "kubespray/inventory/hosts"},
|
||||
{"templates/kubespray/inventory/group_vars/all.yml.tmpl", "kubespray/inventory/group_vars/all.yml"},
|
||||
{"templates/kubespray/inventory/group_vars/k8s_cluster/addons.yml.tmpl", "kubespray/inventory/group_vars/k8s_cluster/addons.yml"},
|
||||
{"templates/kubespray/inventory/group_vars/k8s_cluster/k8s-cluster.yml.tmpl", "kubespray/inventory/group_vars/k8s_cluster/k8s-cluster.yml"},
|
||||
}
|
||||
|
||||
func ApplyK8sTemplates() {
|
||||
applyTemplates(K8S_TEMPLATES[:])
|
||||
}
|
||||
14
internal/templates/secrets_storage.go
Normal file
14
internal/templates/secrets_storage.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"kube-forge/internal/config"
|
||||
)
|
||||
|
||||
func ApplyVaultInitKeysTemplate() {
|
||||
config := config.GetConfig()
|
||||
var VAULT_INIT_KEYS_TEMPLATE = [...][2]string{
|
||||
{"templates/secrets-storage/vault-keys.json.tmpl", fmt.Sprintf("%s/vault-keys.json", config.WorkDir)},
|
||||
}
|
||||
applyTemplates(VAULT_INIT_KEYS_TEMPLATE[:])
|
||||
}
|
||||
54
internal/templates/utility.go
Normal file
54
internal/templates/utility.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"kube-forge/internal/config"
|
||||
"kube-forge/internal/resources"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func executeTemplateToString(template *template.Template, config *config.Config) string {
|
||||
templateResult := &bytes.Buffer{}
|
||||
err := template.Execute(templateResult, config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
templateResultString := templateResult.String()
|
||||
return templateResultString
|
||||
}
|
||||
|
||||
func getTemplateFromEmbedFSFolder(embedFS embed.FS, templateFile string) *template.Template {
|
||||
templateData, err := embedFS.ReadFile(templateFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
templateDataString := string(templateData)
|
||||
template, err := template.New("tmpl").Funcs(funcMap()).Parse(templateDataString)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return template
|
||||
}
|
||||
|
||||
func applyTemplates(templates [][2]string) {
|
||||
config := config.GetConfig()
|
||||
for _, templateData := range templates {
|
||||
var templateFile = templateData[0]
|
||||
var outFile = filepath.Join(config.WorkDir, templateData[1])
|
||||
|
||||
tmpl := getTemplateFromEmbedFSFolder(resources.Templates, templateFile)
|
||||
|
||||
file, err := os.Create(outFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
err = tmpl.Execute(file, config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user