kube-forge v2: embede python with ansible (kubespray) and cobra as cli building tool

This commit is contained in:
2025-06-22 00:37:47 +03:00
parent c2df2abc78
commit 4142a9db61
830 changed files with 874 additions and 2047 deletions

View File

@@ -2,10 +2,19 @@ package config
import (
"fmt"
"kube-forge/internal/files"
"path/filepath"
"github.com/ilyakaznacheev/cleanenv"
)
var (
ConfigFile string
Output string
Password string
Verbose bool
)
type Host struct {
Hostname string `yaml:"hostname"`
Ip string `yaml:"ip"`
@@ -15,10 +24,12 @@ type Host struct {
}
type Config struct {
WorkDir string
KubeconfigFile string `yaml:"kubeconfig_file" env-default:"k8s-admin.conf"`
Verbose bool
Credentials struct {
BaseDir string
InventoryDir string
Verbose bool
KubeconfigFile string
InstallationName string `yaml:"installation_name" env-default:"cluster.local"`
Credentials struct {
User string `yaml:"user"`
Password string `yaml:"password"`
AskSudoPassword bool `yaml:"ask_sudo_password"`
@@ -36,21 +47,37 @@ type Config struct {
var instance *Config
func CreateConfig(configPath string, workDir string, password string) *Config {
func createConfig(configFile string, out string, password string, verbose bool) *Config {
instance = &Config{}
instance.WorkDir = workDir
if err := cleanenv.ReadConfig(configPath, instance); err != nil {
if err := cleanenv.ReadConfig(configFile, instance); err != nil {
helper, _ := cleanenv.GetDescription(instance, nil)
panic(fmt.Sprintf("%s\n%s", helper, err))
}
instance.BaseDir = files.CreateTempDir()
instance.InventoryDir = filepath.Join(instance.BaseDir, "inventory")
files.CreateDirWithAllParents(instance.InventoryDir)
if out != "" {
files.CreateDirWithAllParents(out)
instance.KubeconfigFile = filepath.Join(out, "k8s-admin.conf")
} else {
instance.KubeconfigFile = filepath.Join(files.GetWorkingDir(), "k8s-admin.conf")
}
if password != "" {
instance.Credentials.Password = password
}
instance.Verbose = verbose
return instance
}
func GetConfig() *Config {
if instance == nil {
createConfig(ConfigFile, Output, Password, Verbose)
}
return instance
}