Skip to content

Commit 70decda

Browse files
Merge pull request #15 from christianh814/dev
v0.0.5 Update
2 parents 0e313fa + be12b6d commit 70decda

File tree

13 files changed

+4826
-361
lines changed

13 files changed

+4826
-361
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ This project is a Proof of Concept centered around getting a GitOps
55
aware Kubernetes Platform on Day 0. The installer aims to:
66

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

cmd/argo/argo.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"github.com/christianh814/gokp/cmd/capi"
1111
"github.com/christianh814/gokp/cmd/utils"
1212
"k8s.io/client-go/tools/clientcmd"
13-
"sigs.k8s.io/kustomize/api/krusty"
14-
"sigs.k8s.io/kustomize/kyaml/filesys"
1513
)
1614

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

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

7775
return true, nil
7876
}
79-
80-
// RunKustomize runs kustomize on a specific dir and outputs it to a YAML to use for later
81-
func RunKustomize(dir string, outfile string) (bool, error) {
82-
// set up where to run kustomize, how to write it, and which file to create
83-
kustomizeDir := dir
84-
fSys := filesys.MakeFsOnDisk()
85-
writer, _ := os.Create(outfile)
86-
87-
// The default options are fine for our use case
88-
k := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
89-
90-
// Run Kustomize
91-
m, err := k.Run(fSys, kustomizeDir)
92-
if err != nil {
93-
return false, err
94-
}
95-
96-
// Convert to YAML
97-
yml, err := m.AsYaml()
98-
if err != nil {
99-
return false, err
100-
}
101-
102-
// Write YAML out
103-
_, err = writer.Write(yml)
104-
if err != nil {
105-
return false, err
106-
}
107-
108-
// If we're here, we should be okay
109-
return false, nil
110-
}

cmd/createCluster_aws.go

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/christianh814/gokp/cmd/argo"
77
"github.com/christianh814/gokp/cmd/capi"
88
"github.com/christianh814/gokp/cmd/export"
9+
"github.com/christianh814/gokp/cmd/flux"
910

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

52+
// Set GitOps Controller
53+
gitOpsController, _ := cmd.Flags().GetString("gitops-controller")
54+
5155
// Grab AWS related flags
5256
awsRegion, _ := cmd.Flags().GetString("aws-region")
5357
awsAccessKey, _ := cmd.Flags().GetString("aws-access-key")
@@ -63,7 +67,7 @@ doesn't create one for you).`,
6367
tcpName := "gokp-bootstrapper"
6468

6569
// Run PreReq Checks
66-
_, err = utils.CheckPreReqs(gokpartifacts)
70+
_, err = utils.CheckPreReqs(gokpartifacts, gitOpsController)
6771
if err != nil {
6872
log.Fatal(err)
6973
}
@@ -98,15 +102,26 @@ doesn't create one for you).`,
98102
log.Fatal(err)
99103
}
100104

101-
// Create repo dir structure. Including Argo CD install YAMLs and base YAMLs. Push initial dir structure out
102-
_, err = templates.CreateRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
103-
if err != nil {
104-
log.Fatal(err)
105+
// Create repo dir structure based on which gitops controller that was chosen
106+
if gitOpsController == "argocd" {
107+
// Create repo dir structure. Including Argo CD install YAMLs and base YAMLs. Push initial dir structure out
108+
_, err = templates.CreateArgoRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
109+
if err != nil {
110+
log.Fatal(err)
111+
}
112+
} else if gitOpsController == "fluxcd" {
113+
// Create repo dir structure. Including Flux CD install YAMLs and base YAMLs. Push initial dir structure out
114+
_, err = templates.CreateFluxRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
115+
if err != nil {
116+
log.Fatal(err)
117+
}
118+
} else {
119+
log.Fatal("unknown gitops controller")
105120
}
106121

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

121-
// Install Argo CD on the newly created cluster
122-
// Deploy applications/applicationsets
123-
log.Info("Deploying Argo CD GitOps Controller")
124-
_, err = argo.BootstrapArgoCD(&clusterName, WorkDir, CapiCfg)
125-
if err != nil {
126-
log.Fatal(err)
136+
// Deplopy the GitOps controller that was chosen
137+
if gitOpsController == "argocd" {
138+
// Install Argo CD on the newly created cluster with applications/applicationsets
139+
log.Info("Deploying Argo CD GitOps Controller")
140+
_, err = argo.BootstrapArgoCD(&clusterName, WorkDir, CapiCfg)
141+
if err != nil {
142+
log.Fatal(err)
143+
}
144+
} else if gitOpsController == "fluxcd" {
145+
// Install Flux CD on the newly created cluster with all it's components
146+
log.Info("Deploying Flux CD GitOps Controller")
147+
_, err = flux.BootstrapFluxCD(&clusterName, WorkDir, CapiCfg)
148+
if err != nil {
149+
log.Fatal(err)
150+
}
151+
} else {
152+
log.Fatal("unknown gitops controller")
127153
}
128154

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

152-
notNeededDirs := []string{
177+
notNeeded := []string{
153178
"argocd-install-output",
154179
"capi-install-yamls-output",
155180
"cni-output",
156-
}
157-
158-
for _, notNeededDir := range notNeededDirs {
159-
err = os.RemoveAll(gokpartifacts + "/" + notNeededDir)
160-
if err != nil {
161-
log.Fatal(err)
162-
}
163-
}
164-
165-
notNeededFiles := []string{
181+
"fluxcd-install-output",
166182
"argocd-install.yaml",
183+
"flux-install.yaml",
167184
"cni.yaml",
168185
"install-cluster.yaml",
169186
"kind.kubeconfig",
170187
}
171188

172-
for _, notNeededFile := range notNeededFiles {
173-
err = os.Remove(gokpartifacts + "/" + notNeededFile)
189+
for _, notNeededthing := range notNeeded {
190+
err = os.RemoveAll(gokpartifacts + "/" + notNeededthing)
174191
if err != nil {
175192
log.Fatal(err)
176193
}
@@ -185,6 +202,9 @@ doesn't create one for you).`,
185202
func init() {
186203
createClusterCmd.AddCommand(awscreateCmd)
187204

205+
// GitOps Controller Flag
206+
awscreateCmd.Flags().String("gitops-controller", "argocd", "The GitOps Controller to use for this cluster.")
207+
188208
// Repo specific flags
189209
awscreateCmd.Flags().String("github-token", "", "GitHub token to use.")
190210
awscreateCmd.Flags().String("cluster-name", "", "Name of your cluster.")

cmd/createCluster_development.go

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/christianh814/gokp/cmd/argo"
77
"github.com/christianh814/gokp/cmd/capi"
88
"github.com/christianh814/gokp/cmd/export"
9+
"github.com/christianh814/gokp/cmd/flux"
910
"github.com/christianh814/gokp/cmd/github"
1011
"github.com/christianh814/gokp/cmd/kind"
1112
"github.com/christianh814/gokp/cmd/templates"
@@ -42,6 +43,9 @@ so beware. This create a local cluster for testing. PRE-PRE-ALPHA.`,
4243
clusterName, _ := cmd.Flags().GetString("cluster-name")
4344
privateRepo, _ := cmd.Flags().GetBool("private-repo")
4445

46+
// Set GitOps Controller
47+
gitOpsController, _ := cmd.Flags().GetString("gitops-controller")
48+
4549
// HA request
4650
createHaCluster, _ := cmd.Flags().GetBool("ha")
4751

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

5559
// Run PreReq Checks
56-
_, err = utils.CheckPreReqs(gokpartifacts)
60+
_, err = utils.CheckPreReqs(gokpartifacts, gitOpsController)
5761
if err != nil {
5862
log.Fatal(err)
5963
}
@@ -77,15 +81,26 @@ so beware. This create a local cluster for testing. PRE-PRE-ALPHA.`,
7781
log.Fatal(err)
7882
}
7983

80-
// Create repo dir structure. Including Argo CD install YAMLs and base YAMLs. Push initial dir structure out
81-
_, err = templates.CreateRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
82-
if err != nil {
83-
log.Fatal(err)
84+
// Create repo dir structure based on which gitops controller that was chosen
85+
if gitOpsController == "argocd" {
86+
// Create repo dir structure. Including Argo CD install YAMLs and base YAMLs. Push initial dir structure out
87+
_, err = templates.CreateArgoRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
88+
if err != nil {
89+
log.Fatal(err)
90+
}
91+
} else if gitOpsController == "fluxcd" {
92+
// Create repo dir structure. Including Flux CD install YAMLs and base YAMLs. Push initial dir structure out
93+
_, err = templates.CreateFluxRepoSkel(&clusterName, WorkDir, ghToken, gitopsrepo, &privateRepo)
94+
if err != nil {
95+
log.Fatal(err)
96+
}
97+
} else {
98+
log.Fatal("unknown gitops controller")
8499
}
85100

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

100-
// Install Argo CD on the newly created cluster
101-
// Deploy applications/applicationsets
102-
log.Info("Deploying Argo CD GitOps Controller")
103-
_, err = argo.BootstrapArgoCD(&clusterName, WorkDir, CapiCfg)
104-
if err != nil {
105-
log.Fatal(err)
115+
// Deplopy the GitOps controller that was chosen
116+
if gitOpsController == "argocd" {
117+
// Install Argo CD on the newly created cluster with applications/applicationsets
118+
log.Info("Deploying Argo CD GitOps Controller")
119+
_, err = argo.BootstrapArgoCD(&clusterName, WorkDir, CapiCfg)
120+
if err != nil {
121+
log.Fatal(err)
122+
}
123+
} else if gitOpsController == "fluxcd" {
124+
// Install Flux CD on the newly created cluster with all it's components
125+
log.Info("Deploying Flux CD GitOps Controller")
126+
_, err = flux.BootstrapFluxCD(&clusterName, WorkDir, CapiCfg)
127+
if err != nil {
128+
log.Fatal(err)
129+
}
130+
} else {
131+
log.Fatal("unknown gitops controller")
106132
}
107133

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

123-
notNeededDirs := []string{
149+
notNeeded := []string{
124150
"argocd-install-output",
125151
"capi-install-yamls-output",
126152
"cni-output",
127-
}
128-
129-
for _, notNeededDir := range notNeededDirs {
130-
err = os.RemoveAll(gokpartifacts + "/" + notNeededDir)
131-
if err != nil {
132-
log.Fatal(err)
133-
}
134-
}
135-
136-
notNeededFiles := []string{
153+
"fluxcd-install-output",
137154
"argocd-install.yaml",
155+
"flux-install.yaml",
138156
"cni.yaml",
139157
"install-cluster.yaml",
140158
"kind.kubeconfig",
141159
"kindconfig.yaml",
142160
}
143161

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

177+
// GitOps Controller Flag
178+
developmentClusterCmd.Flags().String("gitops-controller", "argocd", "The GitOps Controller to use for this cluster.")
179+
159180
// Repo Specific Flags
160181
developmentClusterCmd.Flags().String("github-token", "", "GitHub token to use.")
161182
developmentClusterCmd.Flags().String("cluster-name", "", "Name of your cluster.")

cmd/export/export.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var FuncMap = template.FuncMap{
2525
}
2626

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

3131
//Create client and dynamtic client
@@ -87,8 +87,10 @@ func ExportClusterYaml(capicfg string, repodir string) (bool, error) {
8787
// generate the kustomization.yaml file based on the template
8888
cskf := struct {
8989
ClusterScopedYamls []string
90+
GitOpsController string
9091
}{
9192
ClusterScopedYamls: clusterScopedYamlFiles,
93+
GitOpsController: gitOpsController,
9294
}
9395
_, err = WriteTemplateWithFunc(ClusterScopedKustomizeFile, repodir+"/cluster/core/cluster/kustomization.yaml", cskf, FuncMap)
9496
if err != nil {
@@ -173,9 +175,11 @@ func ExportClusterYaml(capicfg string, repodir string) (bool, error) {
173175

174176
// generate the kustomization.yaml file based on the template
175177
nskf := struct {
176-
NsScopedYamls []string
178+
NsScopedYamls []string
179+
GitOpsController string
177180
}{
178-
NsScopedYamls: nsScopedYamlFiles,
181+
NsScopedYamls: nsScopedYamlFiles,
182+
GitOpsController: gitOpsController,
179183
}
180184
_, err = WriteTemplateWithFunc(NameSpacedScopedKustomizeFile, repodir+"/cluster/core/"+ns.Name+"/kustomization.yaml", nskf, FuncMap)
181185
if err != nil {

cmd/export/exporttypes.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ var ClusterScopedKustomizeFile string = `apiVersion: kustomize.config.k8s.io/v1b
99
kind: Kustomization
1010
1111
12+
{{- if eq .GitOpsController "argocd"}}
1213
commonAnnotations:
1314
argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
1415
argocd.argoproj.io/sync-options: Validate=false
16+
{{ end }}
1517
1618
resources:
1719
{{- range $ClusterScopedYaml := .ClusterScopedYamls }}
@@ -23,8 +25,10 @@ var NameSpacedScopedKustomizeFile string = `apiVersion: kustomize.config.k8s.io/
2325
kind: Kustomization
2426
2527
28+
{{- if eq .GitOpsController "argocd"}}
2629
commonAnnotations:
2730
argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
31+
{{ end }}
2832
2933
resources:
3034
{{- range $NsScopedYaml := .NsScopedYamls }}

0 commit comments

Comments
 (0)