Skip to content

Commit

Permalink
Merge pull request #15 from christianh814/dev
Browse files Browse the repository at this point in the history
v0.0.5 Update
  • Loading branch information
christianh814 authored Nov 17, 2021
2 parents 0e313fa + be12b6d commit 70decda
Show file tree
Hide file tree
Showing 13 changed files with 4,826 additions and 361 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ This project is a Proof of Concept centered around getting a GitOps
aware Kubernetes Platform on Day 0. The installer aims to:

* Install an HA Kubernetes cluster (AWS or Docker)
* Install Argo CD
* Configure Argo CD in an opinionated way
* Install the chosen GitOps controller (Argo CD or Flux CD)
* Configure the chosen GitOps controller in an opinionated way
* Export all YAML into a Git repo (GitHub only currently)
* Deliver a "ready to go with GitOps" cluster.

Expand Down
36 changes: 1 addition & 35 deletions cmd/argo/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/christianh814/gokp/cmd/capi"
"github.com/christianh814/gokp/cmd/utils"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

// BootstrapArgoCD installs ArgoCD on a given cluster with the provided Kustomize-ed dir
Expand All @@ -26,7 +24,7 @@ func BootstrapArgoCD(clustername *string, workdir string, capicfg string) (bool,

// generate the ArgoCD Install YAML
argocdyaml := workdir + "/" + "argocd-install.yaml"
_, err := RunKustomize(overlay, argocdyaml)
_, err := utils.RunKustomize(overlay, argocdyaml)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -76,35 +74,3 @@ func BootstrapArgoCD(clustername *string, workdir string, capicfg string) (bool,

return true, nil
}

// RunKustomize runs kustomize on a specific dir and outputs it to a YAML to use for later
func RunKustomize(dir string, outfile string) (bool, error) {
// set up where to run kustomize, how to write it, and which file to create
kustomizeDir := dir
fSys := filesys.MakeFsOnDisk()
writer, _ := os.Create(outfile)

// The default options are fine for our use case
k := krusty.MakeKustomizer(krusty.MakeDefaultOptions())

// Run Kustomize
m, err := k.Run(fSys, kustomizeDir)
if err != nil {
return false, err
}

// Convert to YAML
yml, err := m.AsYaml()
if err != nil {
return false, err
}

// Write YAML out
_, err = writer.Write(yml)
if err != nil {
return false, err
}

// If we're here, we should be okay
return false, nil
}
72 changes: 46 additions & 26 deletions cmd/createCluster_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/christianh814/gokp/cmd/argo"
"github.com/christianh814/gokp/cmd/capi"
"github.com/christianh814/gokp/cmd/export"
"github.com/christianh814/gokp/cmd/flux"

"github.com/christianh814/gokp/cmd/github"
"github.com/christianh814/gokp/cmd/kind"
Expand Down Expand Up @@ -48,6 +49,9 @@ doesn't create one for you).`,
clusterName, _ := cmd.Flags().GetString("cluster-name")
privateRepo, _ := cmd.Flags().GetBool("private-repo")

// Set GitOps Controller
gitOpsController, _ := cmd.Flags().GetString("gitops-controller")

// Grab AWS related flags
awsRegion, _ := cmd.Flags().GetString("aws-region")
awsAccessKey, _ := cmd.Flags().GetString("aws-access-key")
Expand All @@ -63,7 +67,7 @@ doesn't create one for you).`,
tcpName := "gokp-bootstrapper"

// Run PreReq Checks
_, err = utils.CheckPreReqs(gokpartifacts)
_, err = utils.CheckPreReqs(gokpartifacts, gitOpsController)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -98,15 +102,26 @@ doesn't create one for you).`,
log.Fatal(err)
}

// Create repo dir structure. Including Argo CD install YAMLs and base YAMLs. Push initial dir structure out
_, err = templates.CreateRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
if err != nil {
log.Fatal(err)
// Create repo dir structure based on which gitops controller that was chosen
if gitOpsController == "argocd" {
// Create repo dir structure. Including Argo CD install YAMLs and base YAMLs. Push initial dir structure out
_, err = templates.CreateArgoRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
if err != nil {
log.Fatal(err)
}
} else if gitOpsController == "fluxcd" {
// Create repo dir structure. Including Flux CD install YAMLs and base YAMLs. Push initial dir structure out
_, err = templates.CreateFluxRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal("unknown gitops controller")
}

// Export/Create Cluster YAML to the Repo, Make sure kustomize is used for the core components
log.Info("Exporting Cluster YAML")
_, err = export.ExportClusterYaml(CapiCfg, WorkDir+"/"+clusterName)
_, err = export.ExportClusterYaml(CapiCfg, WorkDir+"/"+clusterName, gitOpsController)
if err != nil {
log.Fatal(err)
}
Expand All @@ -118,16 +133,26 @@ doesn't create one for you).`,
log.Fatal(err)
}

// Install Argo CD on the newly created cluster
// Deploy applications/applicationsets
log.Info("Deploying Argo CD GitOps Controller")
_, err = argo.BootstrapArgoCD(&clusterName, WorkDir, CapiCfg)
if err != nil {
log.Fatal(err)
// Deplopy the GitOps controller that was chosen
if gitOpsController == "argocd" {
// Install Argo CD on the newly created cluster with applications/applicationsets
log.Info("Deploying Argo CD GitOps Controller")
_, err = argo.BootstrapArgoCD(&clusterName, WorkDir, CapiCfg)
if err != nil {
log.Fatal(err)
}
} else if gitOpsController == "fluxcd" {
// Install Flux CD on the newly created cluster with all it's components
log.Info("Deploying Flux CD GitOps Controller")
_, err = flux.BootstrapFluxCD(&clusterName, WorkDir, CapiCfg)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal("unknown gitops controller")
}

// MOVE from kind to capi instance
// uses the kubeconfig files of "src ~> dest"
log.Info("Moving CAPI Artifacts to: " + clusterName)
_, err = capi.MoveMgmtCluster(KindCfg, CapiCfg)
if err != nil {
Expand All @@ -149,28 +174,20 @@ doesn't create one for you).`,
log.Fatal(err)
}

notNeededDirs := []string{
notNeeded := []string{
"argocd-install-output",
"capi-install-yamls-output",
"cni-output",
}

for _, notNeededDir := range notNeededDirs {
err = os.RemoveAll(gokpartifacts + "/" + notNeededDir)
if err != nil {
log.Fatal(err)
}
}

notNeededFiles := []string{
"fluxcd-install-output",
"argocd-install.yaml",
"flux-install.yaml",
"cni.yaml",
"install-cluster.yaml",
"kind.kubeconfig",
}

for _, notNeededFile := range notNeededFiles {
err = os.Remove(gokpartifacts + "/" + notNeededFile)
for _, notNeededthing := range notNeeded {
err = os.RemoveAll(gokpartifacts + "/" + notNeededthing)
if err != nil {
log.Fatal(err)
}
Expand All @@ -185,6 +202,9 @@ doesn't create one for you).`,
func init() {
createClusterCmd.AddCommand(awscreateCmd)

// GitOps Controller Flag
awscreateCmd.Flags().String("gitops-controller", "argocd", "The GitOps Controller to use for this cluster.")

// Repo specific flags
awscreateCmd.Flags().String("github-token", "", "GitHub token to use.")
awscreateCmd.Flags().String("cluster-name", "", "Name of your cluster.")
Expand Down
71 changes: 46 additions & 25 deletions cmd/createCluster_development.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/christianh814/gokp/cmd/argo"
"github.com/christianh814/gokp/cmd/capi"
"github.com/christianh814/gokp/cmd/export"
"github.com/christianh814/gokp/cmd/flux"
"github.com/christianh814/gokp/cmd/github"
"github.com/christianh814/gokp/cmd/kind"
"github.com/christianh814/gokp/cmd/templates"
Expand Down Expand Up @@ -42,6 +43,9 @@ so beware. This create a local cluster for testing. PRE-PRE-ALPHA.`,
clusterName, _ := cmd.Flags().GetString("cluster-name")
privateRepo, _ := cmd.Flags().GetBool("private-repo")

// Set GitOps Controller
gitOpsController, _ := cmd.Flags().GetString("gitops-controller")

// HA request
createHaCluster, _ := cmd.Flags().GetBool("ha")

Expand All @@ -53,7 +57,7 @@ so beware. This create a local cluster for testing. PRE-PRE-ALPHA.`,
tcpName := "gokp-bootstrapper"

// Run PreReq Checks
_, err = utils.CheckPreReqs(gokpartifacts)
_, err = utils.CheckPreReqs(gokpartifacts, gitOpsController)
if err != nil {
log.Fatal(err)
}
Expand All @@ -77,15 +81,26 @@ so beware. This create a local cluster for testing. PRE-PRE-ALPHA.`,
log.Fatal(err)
}

// Create repo dir structure. Including Argo CD install YAMLs and base YAMLs. Push initial dir structure out
_, err = templates.CreateRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
if err != nil {
log.Fatal(err)
// Create repo dir structure based on which gitops controller that was chosen
if gitOpsController == "argocd" {
// Create repo dir structure. Including Argo CD install YAMLs and base YAMLs. Push initial dir structure out
_, err = templates.CreateArgoRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
if err != nil {
log.Fatal(err)
}
} else if gitOpsController == "fluxcd" {
// Create repo dir structure. Including Flux CD install YAMLs and base YAMLs. Push initial dir structure out
_, err = templates.CreateFluxRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal("unknown gitops controller")
}

// Export/Create Cluster YAML to the Repo, Make sure kustomize is used for the core components
log.Info("Exporting Cluster YAML")
_, err = export.ExportClusterYaml(CapiCfg, WorkDir+"/"+clusterName)
_, err = export.ExportClusterYaml(CapiCfg, WorkDir+"/"+clusterName, gitOpsController)
if err != nil {
log.Fatal(err)
}
Expand All @@ -97,12 +112,23 @@ so beware. This create a local cluster for testing. PRE-PRE-ALPHA.`,
log.Fatal(err)
}

// Install Argo CD on the newly created cluster
// Deploy applications/applicationsets
log.Info("Deploying Argo CD GitOps Controller")
_, err = argo.BootstrapArgoCD(&clusterName, WorkDir, CapiCfg)
if err != nil {
log.Fatal(err)
// Deplopy the GitOps controller that was chosen
if gitOpsController == "argocd" {
// Install Argo CD on the newly created cluster with applications/applicationsets
log.Info("Deploying Argo CD GitOps Controller")
_, err = argo.BootstrapArgoCD(&clusterName, WorkDir, CapiCfg)
if err != nil {
log.Fatal(err)
}
} else if gitOpsController == "fluxcd" {
// Install Flux CD on the newly created cluster with all it's components
log.Info("Deploying Flux CD GitOps Controller")
_, err = flux.BootstrapFluxCD(&clusterName, WorkDir, CapiCfg)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal("unknown gitops controller")
}

// Delete local Kind Cluster
Expand All @@ -120,29 +146,21 @@ so beware. This create a local cluster for testing. PRE-PRE-ALPHA.`,
log.Fatal(err)
}

notNeededDirs := []string{
notNeeded := []string{
"argocd-install-output",
"capi-install-yamls-output",
"cni-output",
}

for _, notNeededDir := range notNeededDirs {
err = os.RemoveAll(gokpartifacts + "/" + notNeededDir)
if err != nil {
log.Fatal(err)
}
}

notNeededFiles := []string{
"fluxcd-install-output",
"argocd-install.yaml",
"flux-install.yaml",
"cni.yaml",
"install-cluster.yaml",
"kind.kubeconfig",
"kindconfig.yaml",
}

for _, notNeededFile := range notNeededFiles {
err = os.Remove(gokpartifacts + "/" + notNeededFile)
for _, notNeededthing := range notNeeded {
err = os.RemoveAll(gokpartifacts + "/" + notNeededthing)
if err != nil {
log.Fatal(err)
}
Expand All @@ -156,6 +174,9 @@ so beware. This create a local cluster for testing. PRE-PRE-ALPHA.`,
func init() {
createClusterCmd.AddCommand(developmentClusterCmd)

// GitOps Controller Flag
developmentClusterCmd.Flags().String("gitops-controller", "argocd", "The GitOps Controller to use for this cluster.")

// Repo Specific Flags
developmentClusterCmd.Flags().String("github-token", "", "GitHub token to use.")
developmentClusterCmd.Flags().String("cluster-name", "", "Name of your cluster.")
Expand Down
10 changes: 7 additions & 3 deletions cmd/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var FuncMap = template.FuncMap{
}

// ExportClusterYaml exports the given clusters YAML into the directory
func ExportClusterYaml(capicfg string, repodir string) (bool, error) {
func ExportClusterYaml(capicfg string, repodir string, gitOpsController string) (bool, error) {
/* repodir == workdir + clustername */

//Create client and dynamtic client
Expand Down Expand Up @@ -87,8 +87,10 @@ func ExportClusterYaml(capicfg string, repodir string) (bool, error) {
// generate the kustomization.yaml file based on the template
cskf := struct {
ClusterScopedYamls []string
GitOpsController string
}{
ClusterScopedYamls: clusterScopedYamlFiles,
GitOpsController: gitOpsController,
}
_, err = WriteTemplateWithFunc(ClusterScopedKustomizeFile, repodir+"/cluster/core/cluster/kustomization.yaml", cskf, FuncMap)
if err != nil {
Expand Down Expand Up @@ -173,9 +175,11 @@ func ExportClusterYaml(capicfg string, repodir string) (bool, error) {

// generate the kustomization.yaml file based on the template
nskf := struct {
NsScopedYamls []string
NsScopedYamls []string
GitOpsController string
}{
NsScopedYamls: nsScopedYamlFiles,
NsScopedYamls: nsScopedYamlFiles,
GitOpsController: gitOpsController,
}
_, err = WriteTemplateWithFunc(NameSpacedScopedKustomizeFile, repodir+"/cluster/core/"+ns.Name+"/kustomization.yaml", nskf, FuncMap)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/export/exporttypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ var ClusterScopedKustomizeFile string = `apiVersion: kustomize.config.k8s.io/v1b
kind: Kustomization
{{- if eq .GitOpsController "argocd"}}
commonAnnotations:
argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
argocd.argoproj.io/sync-options: Validate=false
{{ end }}
resources:
{{- range $ClusterScopedYaml := .ClusterScopedYamls }}
Expand All @@ -23,8 +25,10 @@ var NameSpacedScopedKustomizeFile string = `apiVersion: kustomize.config.k8s.io/
kind: Kustomization
{{- if eq .GitOpsController "argocd"}}
commonAnnotations:
argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
{{ end }}
resources:
{{- range $NsScopedYaml := .NsScopedYamls }}
Expand Down
Loading

0 comments on commit 70decda

Please sign in to comment.