first version

This commit is contained in:
2024-04-25 22:15:40 +03:00
commit 4c795c172a
880 changed files with 62224 additions and 0 deletions

105
pkg/templates/funcs.go Normal file
View 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
}

101
pkg/templates/templates.go Normal file
View File

@@ -0,0 +1,101 @@
package templates
import (
"bytes"
"embed"
"kube-forge/pkg/config"
"kube-forge/pkg/resources"
"os"
"path/filepath"
"strings"
"text/template"
)
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"},
}
var HELM_APPS_TEMPLATES = [...]string{
"templates/helm-apps/releases/additional-modules/longhorn.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 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() {
config := config.GetConfig()
for _, templateData := range K8S_TEMPLATES {
var templateFile = templateData[0]
var outFile = filepath.Join(config.DataDir, 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)
}
}
}
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")
}
func ApplyTemplates() {
applyTemplates()
}