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

20
cmd/apply-modules.go Normal file
View File

@@ -0,0 +1,20 @@
package cmd
import (
"kube-forge/internal/templates"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(applyModulesCmd)
}
var applyModulesCmd = &cobra.Command{
Use: "apply-modules",
Short: "Apply only additional modules",
Run: func(cmd *cobra.Command, args []string) {
templates.ApplyK8sTemplates()
installAndConfigureModules()
},
}

22
cmd/apply.go Normal file
View File

@@ -0,0 +1,22 @@
package cmd
import (
"kube-forge/internal/kubespray"
"kube-forge/internal/templates"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(applyCmd)
}
var applyCmd = &cobra.Command{
Use: "apply",
Short: "Apply configuration",
Run: func(cmd *cobra.Command, args []string) {
templates.ApplyK8sTemplates()
kubespray.InstallCluster("")
installAndConfigureModules()
},
}

View File

@@ -1,54 +1,9 @@
package main
import (
"flag"
"kube-forge/internal/additional"
"kube-forge/internal/config"
"kube-forge/internal/csi"
"kube-forge/internal/kubespray"
"kube-forge/internal/logging"
"kube-forge/internal/templates"
"os"
"kube-forge/cmd"
)
func installAndConfigureModules() {
csi.ApplyCharts()
additional.ApplyCharts()
}
func main() {
var password, configPath, workDir string
var verbose bool
flag.StringVar(&password, "p", "", "Password to access hosts")
flag.StringVar(&configPath, "c", "/etc/kube-forge/config.yaml", "Path to config file")
flag.StringVar(&workDir, "d", "/var/lib/kube-forge", "Path to kube-forge work dir")
flag.BoolVar(&verbose, "v", false, "Enable verbose logging")
flag.Parse()
config := config.CreateConfig(configPath, workDir, password)
config.Verbose = verbose
templates.ApplyK8sTemplates()
for _, cmd := range os.Args {
switch cmd {
case "apply":
kubespray.InstallCluster("")
installAndConfigureModules()
return
case "apply-modules":
installAndConfigureModules()
return
case "upgrade":
kubespray.UpgradeCluster("")
return
case "scale":
kubespray.ScaleCluster()
return
case "reset":
kubespray.ResetCluster()
return
}
}
logging.Log.Error("No such command\nAvailable commands: apply, apply-modules, upgrade, scale, reset")
cmd.Execute()
}

21
cmd/reset.go Normal file
View File

@@ -0,0 +1,21 @@
package cmd
import (
"kube-forge/internal/kubespray"
"kube-forge/internal/templates"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(resetCmd)
}
var resetCmd = &cobra.Command{
Use: "reset",
Short: "Reset cluster",
Run: func(cmd *cobra.Command, args []string) {
templates.ApplyK8sTemplates()
kubespray.ResetCluster()
},
}

44
cmd/root.go Normal file
View File

@@ -0,0 +1,44 @@
package cmd
import (
"fmt"
"kube-forge/internal/additional"
"kube-forge/internal/config"
"kube-forge/internal/csi"
"os"
"github.com/spf13/cobra"
)
func installAndConfigureModules() {
csi.ApplyCharts()
additional.ApplyCharts()
}
func init() {
rootCmd.PersistentFlags().StringVarP(&config.ConfigFile, "config", "c", "config.yaml", "path to configuration file for cluster")
rootCmd.PersistentFlags().StringVarP(&config.Output, "output", "o", "", "directory to store k8s admin config")
rootCmd.PersistentFlags().StringVarP(&config.Password, "password", "p", "", "password to access hosts via SSH")
rootCmd.PersistentFlags().BoolVarP(&config.Verbose, "verbose", "v", false, "enable verbose logging")
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
var (
rootCmd = &cobra.Command{
Use: "kube-forge <subcommand>",
Short: "Kube-forge will help you to deploy K8s clusters",
Long: `An easy to use K8s installer`,
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.MinimumNArgs(0)(cmd, args); err != nil {
return err
}
return nil
},
}
)

21
cmd/scale.go Normal file
View File

@@ -0,0 +1,21 @@
package cmd
import (
"kube-forge/internal/kubespray"
"kube-forge/internal/templates"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(scaleCmd)
}
var scaleCmd = &cobra.Command{
Use: "scale",
Short: "Scale cluster. Use when adding new nodes",
Run: func(cmd *cobra.Command, args []string) {
templates.ApplyK8sTemplates()
kubespray.ScaleCluster()
},
}

21
cmd/upgrade.go Normal file
View File

@@ -0,0 +1,21 @@
package cmd
import (
"kube-forge/internal/kubespray"
"kube-forge/internal/templates"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(upgradeCmd)
}
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Upgrade cluster. Use when changing version of K8s",
Run: func(cmd *cobra.Command, args []string) {
templates.ApplyK8sTemplates()
kubespray.UpgradeCluster("")
},
}

15
cmd/utils.go Normal file
View File

@@ -0,0 +1,15 @@
package cmd
import (
"log"
"os/user"
)
func GetUserHomeDir() string {
usr, err := user.Current()
if err != nil {
log.Fatalf("cant get user home directory: %v", err)
}
homePath := usr.HomeDir
return homePath
}