add auto init/unseal for vault

This commit is contained in:
2024-05-06 00:00:18 +03:00
parent 772b8ceb57
commit e6689a1dda
21 changed files with 779 additions and 114 deletions

View File

@@ -1,23 +1,11 @@
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/docker-secrets-generator.yml.tmpl",
"templates/helm-apps/releases/additional-modules/longhorn.yml.tmpl",
@@ -41,49 +29,6 @@ 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{}
@@ -98,7 +43,3 @@ func GetHelmAppsConfigData() (string, string) {
}
return strings.Join(repositoriesTemplateResults, "\n"), strings.Join(helmAppsTemplateResults, "\n")
}
func ApplyTemplates() {
applyTemplates()
}

View 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[:])
}

View File

@@ -0,0 +1,14 @@
package templates
import (
"fmt"
"kube-forge/pkg/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
pkg/templates/utility.go Normal file
View File

@@ -0,0 +1,54 @@
package templates
import (
"bytes"
"embed"
"kube-forge/pkg/config"
"kube-forge/pkg/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)
}
}
}