|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "helm.sh/helm/v3/pkg/action" |
| 11 | + "helm.sh/helm/v3/pkg/chart/loader" |
| 12 | + "helm.sh/helm/v3/pkg/cli" |
| 13 | + |
| 14 | + cmdd "capipe/cmd" |
| 15 | +) |
| 16 | + |
| 17 | +// add a GitOps Controller |
| 18 | +var AppStack1 string |
| 19 | +var apiKey string |
| 20 | +var clusterName string |
| 21 | + |
| 22 | +var addAppStack1Cmd = &cobra.Command{ |
| 23 | + Use: "appstack1", |
| 24 | + Short: "Install ArgoCD, Crossplane, OPA, Datadog", |
| 25 | + Long: `Install The Following App Stack: ArgoCD, Crossplane, OPA, Datadog`, |
| 26 | + Run: func(cmd *cobra.Command, args []string) { |
| 27 | + |
| 28 | + // |
| 29 | + // ArgoCD |
| 30 | + // |
| 31 | + |
| 32 | + var ( |
| 33 | + chartName = "argo/argo-cd" |
| 34 | + releaseName = "argocd" |
| 35 | + namespace = "argocd" |
| 36 | + ) |
| 37 | + |
| 38 | + // Call upon the CLI package |
| 39 | + settings := cli.New() |
| 40 | + |
| 41 | + // Create a new instance of the configuration |
| 42 | + config := new(action.Configuration) |
| 43 | + |
| 44 | + // Collect local Helm information |
| 45 | + if err := config.Init(settings.RESTClientGetter(), namespace, os.Getenv("HELM_DRIVER"), log.Printf); err != nil { |
| 46 | + log.Printf("%+v", err) |
| 47 | + } |
| 48 | + |
| 49 | + // Create a new instance of the `Install` action, which is similar to running `helm install` |
| 50 | + client := action.NewInstall(config) |
| 51 | + |
| 52 | + // Set metadata |
| 53 | + client.CreateNamespace = true |
| 54 | + client.Namespace = namespace |
| 55 | + client.ReleaseName = releaseName |
| 56 | + |
| 57 | + // Find the ArgoCD Helm Chart |
| 58 | + ch, err := client.LocateChart(chartName, settings) |
| 59 | + if err != nil { |
| 60 | + fmt.Println(err) |
| 61 | + } |
| 62 | + |
| 63 | + // Load the chart to install |
| 64 | + chart, err := loader.Load(ch) |
| 65 | + if err != nil { |
| 66 | + log.Println(err) |
| 67 | + } |
| 68 | + |
| 69 | + // Install Chart |
| 70 | + results, err := client.Run(chart, nil) |
| 71 | + if err != nil { |
| 72 | + log.Printf("%+v", err) |
| 73 | + } |
| 74 | + |
| 75 | + fmt.Println(results) |
| 76 | + |
| 77 | + // |
| 78 | + // Crossplane |
| 79 | + // |
| 80 | + |
| 81 | + var ( |
| 82 | + chartNameCrossplane = "crossplane-stable/crossplane" |
| 83 | + releaseNameCrossplane = "crossplane" |
| 84 | + namespaceCrossplane = "crossplane" |
| 85 | + ) |
| 86 | + |
| 87 | + // Call upon the CLI package |
| 88 | + settingsCrossplane := cli.New() |
| 89 | + |
| 90 | + // Create a new instance of the configuration |
| 91 | + configCrossplane := new(action.Configuration) |
| 92 | + |
| 93 | + // Collect local Helm information |
| 94 | + if err := configCrossplane.Init(settingsCrossplane.RESTClientGetter(), namespaceCrossplane, os.Getenv("HELM_DRIVER"), log.Printf); err != nil { |
| 95 | + log.Printf("%+v", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Create a new instance of the `Install` action, which is similar to running `helm install` |
| 99 | + clientCrossplane := action.NewInstall(configCrossplane) |
| 100 | + |
| 101 | + // Set metadata |
| 102 | + clientCrossplane.CreateNamespace = true |
| 103 | + clientCrossplane.Namespace = namespaceCrossplane |
| 104 | + clientCrossplane.ReleaseName = releaseNameCrossplane |
| 105 | + |
| 106 | + // Find the crossplane Helm Chart |
| 107 | + chCrossplane, err := clientCrossplane.LocateChart(chartNameCrossplane, settingsCrossplane) |
| 108 | + if err != nil { |
| 109 | + fmt.Println(err) |
| 110 | + } |
| 111 | + |
| 112 | + // Load the chart to install |
| 113 | + chartCrossplane, err := loader.Load(chCrossplane) |
| 114 | + if err != nil { |
| 115 | + log.Println(err) |
| 116 | + } |
| 117 | + |
| 118 | + // Install Chart |
| 119 | + resultsCrossplane, err := clientCrossplane.Run(chartCrossplane, nil) |
| 120 | + if err != nil { |
| 121 | + log.Printf("%+v", err) |
| 122 | + } |
| 123 | + |
| 124 | + fmt.Println(resultsCrossplane) |
| 125 | + |
| 126 | + // |
| 127 | + // OPA |
| 128 | + // |
| 129 | + |
| 130 | + var ( |
| 131 | + chartNameOPA = "gatekeeper/gatekeeper" |
| 132 | + releaseNameOPA = "opa" |
| 133 | + namespaceOPA = "gatekeeper-system" |
| 134 | + argsOPA = map[string]interface{}{ |
| 135 | + // comma seperated values to set |
| 136 | + "set": "name-template=gatekeeper", |
| 137 | + } |
| 138 | + ) |
| 139 | + |
| 140 | + // Call upon the CLI package |
| 141 | + settingsOPA := cli.New() |
| 142 | + |
| 143 | + // Create a new instance of the configuration |
| 144 | + configOPA := new(action.Configuration) |
| 145 | + |
| 146 | + // Collect local Helm information |
| 147 | + if err := configOPA.Init(settingsOPA.RESTClientGetter(), namespaceOPA, os.Getenv("HELM_DRIVER"), log.Printf); err != nil { |
| 148 | + log.Printf("%+v", err) |
| 149 | + } |
| 150 | + |
| 151 | + // Create a new instance of the `Install` action, which is similar to running `helm install` |
| 152 | + clientOPA := action.NewInstall(configOPA) |
| 153 | + |
| 154 | + // Set metadata |
| 155 | + clientOPA.CreateNamespace = true |
| 156 | + clientOPA.Namespace = namespaceOPA |
| 157 | + clientOPA.ReleaseName = releaseNameOPA |
| 158 | + |
| 159 | + // Find the OPA Helm Chart |
| 160 | + chOPA, err := clientOPA.LocateChart(chartNameOPA, settingsOPA) |
| 161 | + if err != nil { |
| 162 | + fmt.Println(err) |
| 163 | + } |
| 164 | + |
| 165 | + // Load the chart to install |
| 166 | + chartOPA, err := loader.Load(chOPA) |
| 167 | + if err != nil { |
| 168 | + log.Println(err) |
| 169 | + } |
| 170 | + |
| 171 | + // Install Chart |
| 172 | + resultsOPA, err := clientOPA.Run(chartOPA, argsOPA) |
| 173 | + if err != nil { |
| 174 | + log.Printf("%+v", err) |
| 175 | + } |
| 176 | + |
| 177 | + fmt.Println(resultsOPA) |
| 178 | + |
| 179 | + // |
| 180 | + // Datadog |
| 181 | + // |
| 182 | + |
| 183 | + var ( |
| 184 | + chartNameDatadog = "datadog/datadog" |
| 185 | + releaseNameDatadog = "datadog" |
| 186 | + namespaceDatadog = "datadog" |
| 187 | + ) |
| 188 | + |
| 189 | + // Call upon the CLI package |
| 190 | + settingsDatadog := cli.New() |
| 191 | + |
| 192 | + // Create a new instance of the configuration |
| 193 | + configDatadog := new(action.Configuration) |
| 194 | + |
| 195 | + // Collect local Helm information |
| 196 | + if err := configDatadog.Init(settingsDatadog.RESTClientGetter(), namespaceDatadog, os.Getenv("HELM_DRIVER"), log.Printf); err != nil { |
| 197 | + log.Printf("%+v", err) |
| 198 | + } |
| 199 | + |
| 200 | + // Create a new instance of the `Install` action, which is similar to running `helm install` |
| 201 | + clientDatadog := action.NewInstall(configDatadog) |
| 202 | + |
| 203 | + // Set metadata |
| 204 | + clientDatadog.CreateNamespace = true |
| 205 | + clientDatadog.Namespace = namespaceDatadog |
| 206 | + clientDatadog.ReleaseName = releaseNameDatadog |
| 207 | + |
| 208 | + // Find the Helm Chart |
| 209 | + chDatadog, err := clientDatadog.LocateChart(chartNameDatadog, settingsDatadog) |
| 210 | + if err != nil { |
| 211 | + fmt.Println(err) |
| 212 | + } |
| 213 | + |
| 214 | + // Load the chart to install |
| 215 | + chartDatadog, err := loader.Load(chDatadog) |
| 216 | + if err != nil { |
| 217 | + log.Println(err) |
| 218 | + } |
| 219 | + |
| 220 | + valsDatadog := map[string]interface{}{ |
| 221 | + "datadog": map[string]interface{}{ |
| 222 | + "site": "datadoghq.com", |
| 223 | + |
| 224 | + "clusterName": clusterName, |
| 225 | + |
| 226 | + "kubeStateMetricsEnabled": true, |
| 227 | + |
| 228 | + "kubeStateMetricsCore": map[string]interface{}{ |
| 229 | + "enabled": true, |
| 230 | + }, |
| 231 | + |
| 232 | + "apiKey": apiKey, |
| 233 | + |
| 234 | + "logs": map[string]interface{}{ |
| 235 | + "enabled": true, |
| 236 | + "containerCollectAll": true, |
| 237 | + }, |
| 238 | + |
| 239 | + "processAgent": map[string]interface{}{ |
| 240 | + "enabled": true, |
| 241 | + }, |
| 242 | + |
| 243 | + "clusterAgent": map[string]interface{}{ |
| 244 | + "replicas": "LoadBalancer", |
| 245 | + "createPodDisruptionBudget": true, |
| 246 | + }, |
| 247 | + |
| 248 | + "autoscaling": map[string]interface{}{ |
| 249 | + "enabled": true, |
| 250 | + "minReplicas": 2, |
| 251 | + }, |
| 252 | + }, |
| 253 | + } |
| 254 | + |
| 255 | + // Install Chart |
| 256 | + // Install Chart |
| 257 | + resultsDatadog, err := clientDatadog.Run(chartDatadog, valsDatadog) |
| 258 | + if err != nil { |
| 259 | + log.Printf("%+v", err) |
| 260 | + } |
| 261 | + |
| 262 | + fmt.Println(resultsDatadog) |
| 263 | + |
| 264 | + }, |
| 265 | +} |
| 266 | + |
| 267 | +func init() { |
| 268 | + cmdd.RootCmd.AddCommand(addAppStack1Cmd) |
| 269 | + |
| 270 | + addAppStack1Cmd.PersistentFlags().StringVarP(&apiKey, "apikey", "a", "", "Enter your Datadog API key") |
| 271 | + addAppStack1Cmd.PersistentFlags().StringVarP(&clusterName, "clustername", "n", "", "Enter the name of the cluster to be associated with Datadog") |
| 272 | +} |
0 commit comments