Skip to content

Commit

Permalink
🍲 cooking...
Browse files Browse the repository at this point in the history
  • Loading branch information
zcubbs committed Sep 17, 2023
1 parent e9c602f commit 865cd50
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 129 deletions.
19 changes: 14 additions & 5 deletions cmd/cli/cmd/cook/cook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
configPath string
recipePath string
)

// Cmd represents the cook command
Expand All @@ -18,19 +18,28 @@ var Cmd = &cobra.Command{
Short: "cook commands",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
style.PrintColoredHeader("Cooking the cluster")
must.Succeed(progress.RunTask(cook(), true))
},
}

func cook() func() error {
return func() error {
return recipe.Cook(configPath)
return recipe.Cook(recipePath,
recipe.Hooks{
Pre: func() error {
style.PrintColoredHeader("Cooking the cluster")
return nil
},
Post: func() error {
return nil
},
},
)
}
}

func init() {
Cmd.Flags().StringVarP(&configPath, "config", "c", "./recipe.yaml", "yaml config file path (default is ./recipe.yaml)")
Cmd.Flags().StringVarP(&recipePath, "recipe", "r", "./recipe.yaml", "yaml config file path (default is ./recipe.yaml)")

_ = Cmd.MarkFlagRequired("config")
_ = Cmd.MarkFlagRequired("recipe")
}
91 changes: 91 additions & 0 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package argocd

import (
"context"
"fmt"
"github.com/zcubbs/x/helm"
"github.com/zcubbs/x/kubernetes"
"time"
)

const (
argocdChartName = "argocd"
argocdHelmRepoName = "argocd"
argocdHelmRepoURL = "https://argoproj.github.io/argo-helm"
argocdChartVersion = "latest"
argocdNamespace = "argocd"
)

const (
argocdServerDeploymentName = "argo-cd-argocd-server"
argocdRepoServerDeploymentName = "argo-cd-argocd-repo-server"
argocdRedisDeploymentName = "argo-cd-argocd-redis"
argocdDexServerDeploymentName = "argo-cd-argocd-dex-server"
argocdApplicationsetControllerDeploymentName = "argo-cd-argocd-applicationset-controller"
argocdNotificationsControllerDeploymentName = "argo-cd-argocd-notifications-controller"
)

func Install(values Values, kubeconfig string, debug bool) error {
if err := validateValues(values); err != nil {
return err
}

vals := map[string]interface{}{
"configs.params.server\\.insecure": values.Insecure,
}

err := helm.Install(helm.Chart{
Name: argocdChartName,
Repo: argocdHelmRepoName,
URL: argocdHelmRepoURL,
Version: values.ChartVersion,
Values: vals,
ValuesFiles: nil,
Namespace: argocdNamespace,
}, kubeconfig, debug)
if err != nil {
return err
}

// wait for argocd server to be ready
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
err = kubernetes.IsDeploymentReady(
ctxWithTimeout,
kubeconfig,
argocdNamespace,
[]string{
argocdServerDeploymentName,
argocdRepoServerDeploymentName,
argocdRedisDeploymentName,
argocdDexServerDeploymentName,
argocdApplicationsetControllerDeploymentName,
argocdNotificationsControllerDeploymentName,
},
debug,
)
if err != nil {
return fmt.Errorf("failed to wait for argocd server to be ready \n %w", err)
}

return nil
}

func Uninstall(kubeconfig string, debug bool) error {
return helm.Uninstall(helm.Chart{
Name: argocdChartName,
Namespace: argocdNamespace,
}, kubeconfig, debug)
}

type Values struct {
Insecure bool
ChartVersion string
}

func validateValues(values Values) error {
if values.ChartVersion == "" {
values.ChartVersion = argocdChartVersion
}
return nil
}
157 changes: 33 additions & 124 deletions pkg/recipe/cook.go
Original file line number Diff line number Diff line change
@@ -1,163 +1,72 @@
package recipe

import (
"github.com/zcubbs/hotpot/pkg/traefik"
"github.com/zcubbs/x/helm"
"github.com/zcubbs/x/k3s"
)

const (
// CertResolver is the name of the cert-manager resolver
CertResolver = "certResolver"
)

type Hooks struct {
Pre PreHook
Post PostHook
}
type PreHook func() error
type PostHook func() error

// Cook runs recipe
func Cook(cfgPath string, hooks ...PreHook) error {
func Cook(recipePath string, hooks ...Hooks) error {
// load config
cfg, err := Load(cfgPath)
recipe, err := Load(recipePath)
if err != nil {
return err
}

// validate config
if err := validate(cfg); err != nil {
if err := validate(recipe); err != nil {
return err
}

// preheat hooks
for _, hook := range hooks {
if err := hook(); err != nil {
if err := hook.Pre(); err != nil {
return err
}
}

if err := checkPrerequisites(cfg); err != nil {
return err
}

if err := installK3s(cfg); err != nil {
return err
}

if err := installHelm(cfg); err != nil {
return err
}

if err := installCertManager(cfg); err != nil {
return err
}

if err := installTraefik(cfg); err != nil {
return err
}

if err := installArgocd(cfg); err != nil {
return err
}

if err := configureArgocdRepos(cfg); err != nil {
return err
}

if err := configureArgocdProjects(cfg); err != nil {
return err
}

if err := configureArgocdApps(cfg); err != nil {
return err
}

if err := printKubeconfig(cfg); err != nil {
return err
}

return nil
}

func checkPrerequisites(_ *Recipe) error {
return nil
}

func installK3s(cfg *Recipe) error {
k3sCfg := cfg.Ingredients.K3s
return k3s.Install(k3s.Config{
Disable: k3sCfg.Disable,
TlsSan: k3sCfg.TlsSan,
DataDir: k3sCfg.DataDir,
DefaultLocalStoragePath: k3sCfg.DefaultLocalStoragePath,
WriteKubeconfigMode: k3sCfg.WriteKubeconfigMode,
}, cfg.Debug)
}

func installHelm(cfg *Recipe) error {
ok, err := helm.IsHelmInstalled()
if err != nil {
// add steps
if err := add(recipe,
step{f: checkPrerequisites, c: true},
step{f: installK3s, c: recipe.Ingredients.K3s.Enabled},
step{f: installHelm, c: true},
step{f: installCertManager, c: recipe.Ingredients.CertManager.Enabled},
step{f: installTraefik, c: recipe.Ingredients.Traefik.Enabled},
step{f: installArgocd, c: recipe.Ingredients.ArgoCD.Enabled},
step{f: configureArgocdRepos, c: recipe.Ingredients.ArgoCD.Enabled},
step{f: configureArgocdProjects, c: recipe.Ingredients.ArgoCD.Enabled},
step{f: configureArgocdApps, c: recipe.Ingredients.ArgoCD.Enabled},
step{f: printKubeconfig, c: recipe.Debug},
); err != nil {
return err
}

if !ok {
err = helm.InstallCli(cfg.Debug)
if err != nil {
// post cook hooks
for _, hook := range hooks {
if err := hook.Post(); err != nil {
return err
}
}

return nil
}

func installCertManager(_ *Recipe) error {
return nil
}

func installTraefik(cfg *Recipe) error {
var ingressProvider string
if cfg.Ingredients.CertManager.Enabled {
ingressProvider = CertResolver
func add(r *Recipe, steps ...step) error {
for _, step := range steps {
if !step.c {
continue
}
if err := step.f(r); err != nil {
return err
}
}
traefikCfg := cfg.Ingredients.Traefik
return traefik.Install(
traefik.Values{
AdditionalArguments: nil,
IngressProvider: ingressProvider,
DnsProvider: traefikCfg.DnsChallengeProvider,
DnsResolverEmail: traefikCfg.DnsChallengeResolverEmail,
EnableDashboard: traefikCfg.EnableDashboard,
EnableAccessLog: traefikCfg.EnableAccessLog,
DebugLog: traefikCfg.Debug,
EndpointsWeb: traefikCfg.EndpointsWeb,
EndpointsWebsecure: traefikCfg.EndpointsWebsecure,
ServersTransportInsecureSkipVerify: traefikCfg.TransportInsecure,
ForwardedHeaders: traefikCfg.ForwardHeaders,
ForwardedHeadersInsecure: traefikCfg.ForwardHeadersInsecure,
ForwardedHeadersTrustedIPs: traefikCfg.ForwardHeadersTrustedIPs,
ProxyProtocol: traefikCfg.ProxyProtocol,
ProxyProtocolInsecure: traefikCfg.ProxyProtocolInsecure,
ProxyProtocolTrustedIPs: traefikCfg.ProxyProtocolTrustedIPs,
DnsTZ: traefikCfg.DnsChallengeTZ,
},
cfg.Kubeconfig,
cfg.Debug,
)
}

func installArgocd(cfg *Recipe) error {
return nil
}

func configureArgocdRepos(cfg *Recipe) error {
return nil
}

func configureArgocdProjects(cfg *Recipe) error {
return nil
}

func configureArgocdApps(cfg *Recipe) error {
return nil
}

func printKubeconfig(cfg *Recipe) error {
return nil
}
Loading

0 comments on commit 865cd50

Please sign in to comment.