github.com/jaylevin/jenkins-library@v1.230.4/cmd/helmExecute_generated.go (about) 1 // Code generated by piper's step-generator. DO NOT EDIT. 2 3 package cmd 4 5 import ( 6 "fmt" 7 "os" 8 "time" 9 10 "github.com/SAP/jenkins-library/pkg/config" 11 "github.com/SAP/jenkins-library/pkg/log" 12 "github.com/SAP/jenkins-library/pkg/splunk" 13 "github.com/SAP/jenkins-library/pkg/telemetry" 14 "github.com/SAP/jenkins-library/pkg/validation" 15 "github.com/spf13/cobra" 16 ) 17 18 type helmExecuteOptions struct { 19 AdditionalParameters []string `json:"additionalParameters,omitempty"` 20 ChartPath string `json:"chartPath,omitempty"` 21 TargetRepositoryURL string `json:"targetRepositoryURL,omitempty"` 22 TargetRepositoryName string `json:"targetRepositoryName,omitempty"` 23 TargetRepositoryUser string `json:"targetRepositoryUser,omitempty"` 24 TargetRepositoryPassword string `json:"targetRepositoryPassword,omitempty"` 25 HelmDeployWaitSeconds int `json:"helmDeployWaitSeconds,omitempty"` 26 HelmValues []string `json:"helmValues,omitempty"` 27 Image string `json:"image,omitempty"` 28 KeepFailedDeployments bool `json:"keepFailedDeployments,omitempty"` 29 KubeConfig string `json:"kubeConfig,omitempty"` 30 KubeContext string `json:"kubeContext,omitempty"` 31 Namespace string `json:"namespace,omitempty"` 32 DockerConfigJSON string `json:"dockerConfigJSON,omitempty"` 33 HelmCommand string `json:"helmCommand,omitempty" validate:"possible-values=upgrade lint install test uninstall dependency publish"` 34 AppVersion string `json:"appVersion,omitempty"` 35 Dependency string `json:"dependency,omitempty" validate:"possible-values=build list update"` 36 PackageDependencyUpdate bool `json:"packageDependencyUpdate,omitempty"` 37 DumpLogs bool `json:"dumpLogs,omitempty"` 38 FilterTest string `json:"filterTest,omitempty"` 39 CustomTLSCertificateLinks []string `json:"customTlsCertificateLinks,omitempty"` 40 Publish bool `json:"publish,omitempty"` 41 Version string `json:"version,omitempty"` 42 } 43 44 // HelmExecuteCommand Executes helm3 functionality as the package manager for Kubernetes. 45 func HelmExecuteCommand() *cobra.Command { 46 const STEP_NAME = "helmExecute" 47 48 metadata := helmExecuteMetadata() 49 var stepConfig helmExecuteOptions 50 var startTime time.Time 51 var logCollector *log.CollectorHook 52 var splunkClient *splunk.Splunk 53 telemetryClient := &telemetry.Telemetry{} 54 55 var createHelmExecuteCmd = &cobra.Command{ 56 Use: STEP_NAME, 57 Short: "Executes helm3 functionality as the package manager for Kubernetes.", 58 Long: `Alpha version: please expect incompatible changes 59 60 Executes helm functionality as the package manager for Kubernetes. 61 62 * [Helm](https://helm.sh/) is the package manager for Kubernetes. 63 * [Helm documentation https://helm.sh/docs/intro/using_helm/ and best practies https://helm.sh/docs/chart_best_practices/conventions/] 64 * [Helm Charts] (https://artifacthub.io/) 65 ` + "`" + `` + "`" + `` + "`" + ` 66 Available Commands: 67 ` + "`" + `upgrade` + "`" + `, ` + "`" + `lint` + "`" + `, ` + "`" + `install` + "`" + `, ` + "`" + `test` + "`" + `, ` + "`" + `uninstall` + "`" + `, ` + "`" + `dependency` + "`" + `, ` + "`" + `publish` + "`" + ` 68 69 upgrade upgrade a release 70 lint examine a chart for possible issues 71 install install a chart 72 test run tests for a release 73 uninstall uninstall a release 74 dependency package a chart directory into a chart archive 75 publish package and puslish a release 76 77 ` + "`" + `` + "`" + `` + "`" + ` 78 79 Note: piper supports only helm3 version, since helm2 is deprecated.`, 80 PreRunE: func(cmd *cobra.Command, _ []string) error { 81 startTime = time.Now() 82 log.SetStepName(STEP_NAME) 83 log.SetVerbose(GeneralConfig.Verbose) 84 85 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 86 87 path, _ := os.Getwd() 88 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 89 log.RegisterHook(fatalHook) 90 91 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 92 if err != nil { 93 log.SetErrorCategory(log.ErrorConfiguration) 94 return err 95 } 96 log.RegisterSecret(stepConfig.TargetRepositoryUser) 97 log.RegisterSecret(stepConfig.TargetRepositoryPassword) 98 log.RegisterSecret(stepConfig.KubeConfig) 99 log.RegisterSecret(stepConfig.DockerConfigJSON) 100 101 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 102 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 103 log.RegisterHook(&sentryHook) 104 } 105 106 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 107 splunkClient = &splunk.Splunk{} 108 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 109 log.RegisterHook(logCollector) 110 } 111 112 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 113 if err != nil { 114 return err 115 } 116 if err = validation.ValidateStruct(stepConfig); err != nil { 117 log.SetErrorCategory(log.ErrorConfiguration) 118 return err 119 } 120 121 return nil 122 }, 123 Run: func(_ *cobra.Command, _ []string) { 124 stepTelemetryData := telemetry.CustomData{} 125 stepTelemetryData.ErrorCode = "1" 126 handler := func() { 127 config.RemoveVaultSecretFiles() 128 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 129 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 130 stepTelemetryData.PiperCommitHash = GitCommit 131 telemetryClient.SetData(&stepTelemetryData) 132 telemetryClient.Send() 133 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 134 splunkClient.Send(telemetryClient.GetData(), logCollector) 135 } 136 } 137 log.DeferExitHandler(handler) 138 defer handler() 139 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 140 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 141 splunkClient.Initialize(GeneralConfig.CorrelationID, 142 GeneralConfig.HookConfig.SplunkConfig.Dsn, 143 GeneralConfig.HookConfig.SplunkConfig.Token, 144 GeneralConfig.HookConfig.SplunkConfig.Index, 145 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 146 } 147 helmExecute(stepConfig, &stepTelemetryData) 148 stepTelemetryData.ErrorCode = "0" 149 log.Entry().Info("SUCCESS") 150 }, 151 } 152 153 addHelmExecuteFlags(createHelmExecuteCmd, &stepConfig) 154 return createHelmExecuteCmd 155 } 156 157 func addHelmExecuteFlags(cmd *cobra.Command, stepConfig *helmExecuteOptions) { 158 cmd.Flags().StringSliceVar(&stepConfig.AdditionalParameters, "additionalParameters", []string{}, "Defines additional parameters for Helm like \"helm install [NAME] [CHART] [flags]\".") 159 cmd.Flags().StringVar(&stepConfig.ChartPath, "chartPath", os.Getenv("PIPER_chartPath"), "Defines the chart path for helm. chartPath is mandatory for install/upgrade/publish commands.") 160 cmd.Flags().StringVar(&stepConfig.TargetRepositoryURL, "targetRepositoryURL", os.Getenv("PIPER_targetRepositoryURL"), "URL of the target repository where the compiled helm .tgz archive shall be uploaded - typically provided by the CI/CD environment.") 161 cmd.Flags().StringVar(&stepConfig.TargetRepositoryName, "targetRepositoryName", os.Getenv("PIPER_targetRepositoryName"), "set the chart repository. The value is required for install/upgrade/uninstall commands.") 162 cmd.Flags().StringVar(&stepConfig.TargetRepositoryUser, "targetRepositoryUser", os.Getenv("PIPER_targetRepositoryUser"), "Username for the char repository where the compiled helm .tgz archive shall be uploaded - typically provided by the CI/CD environment.") 163 cmd.Flags().StringVar(&stepConfig.TargetRepositoryPassword, "targetRepositoryPassword", os.Getenv("PIPER_targetRepositoryPassword"), "Password for the target repository where the compiled helm .tgz archive shall be uploaded - typically provided by the CI/CD environment.") 164 cmd.Flags().IntVar(&stepConfig.HelmDeployWaitSeconds, "helmDeployWaitSeconds", 300, "Number of seconds before helm deploy returns.") 165 cmd.Flags().StringSliceVar(&stepConfig.HelmValues, "helmValues", []string{}, "List of helm values as YAML file reference or URL (as per helm parameter description for `-f` / `--values`)") 166 cmd.Flags().StringVar(&stepConfig.Image, "image", os.Getenv("PIPER_image"), "Full name of the image to be deployed.") 167 cmd.Flags().BoolVar(&stepConfig.KeepFailedDeployments, "keepFailedDeployments", false, "Defines whether a failed deployment will be purged") 168 cmd.Flags().StringVar(&stepConfig.KubeConfig, "kubeConfig", os.Getenv("PIPER_kubeConfig"), "Defines the path to the \"kubeconfig\" file.") 169 cmd.Flags().StringVar(&stepConfig.KubeContext, "kubeContext", os.Getenv("PIPER_kubeContext"), "Defines the context to use from the \"kubeconfig\" file.") 170 cmd.Flags().StringVar(&stepConfig.Namespace, "namespace", `default`, "Defines the target Kubernetes namespace for the deployment.") 171 cmd.Flags().StringVar(&stepConfig.DockerConfigJSON, "dockerConfigJSON", os.Getenv("PIPER_dockerConfigJSON"), "Path to the file `.docker/config.json` - this is typically provided by your CI/CD system. You can find more details about the Docker credentials in the [Docker documentation](https://docs.docker.com/engine/reference/commandline/login/).") 172 cmd.Flags().StringVar(&stepConfig.HelmCommand, "helmCommand", os.Getenv("PIPER_helmCommand"), "Helm: defines the command `upgrade`, `lint`, `install`, `test`, `uninstall`, `dependency`, `publish`.") 173 cmd.Flags().StringVar(&stepConfig.AppVersion, "appVersion", os.Getenv("PIPER_appVersion"), "set the appVersion on the chart to this version") 174 cmd.Flags().StringVar(&stepConfig.Dependency, "dependency", os.Getenv("PIPER_dependency"), "manage a chart's dependencies") 175 cmd.Flags().BoolVar(&stepConfig.PackageDependencyUpdate, "packageDependencyUpdate", false, "update dependencies from \"Chart.yaml\" to dir \"charts/\" before packaging") 176 cmd.Flags().BoolVar(&stepConfig.DumpLogs, "dumpLogs", false, "dump the logs from test pods (this runs after all tests are complete, but before any cleanup)") 177 cmd.Flags().StringVar(&stepConfig.FilterTest, "filterTest", os.Getenv("PIPER_filterTest"), "specify tests by attribute (currently `name`) using attribute=value syntax or `!attribute=value` to exclude a test (can specify multiple or separate values with commas `name=test1,name=test2`)") 178 cmd.Flags().StringSliceVar(&stepConfig.CustomTLSCertificateLinks, "customTlsCertificateLinks", []string{}, "List of download links to custom TLS certificates. This is required to ensure trusted connections to instances with repositories (like nexus) when publish flag is set to true.") 179 cmd.Flags().BoolVar(&stepConfig.Publish, "publish", false, "Configures helm to run the deploy command to publish artifacts to a repository.") 180 cmd.Flags().StringVar(&stepConfig.Version, "version", os.Getenv("PIPER_version"), "Defines the artifact version to use from helm package/publish commands.") 181 182 cmd.MarkFlagRequired("image") 183 } 184 185 // retrieve step metadata 186 func helmExecuteMetadata() config.StepData { 187 var theMetaData = config.StepData{ 188 Metadata: config.StepMetadata{ 189 Name: "helmExecute", 190 Aliases: []config.Alias{}, 191 Description: "Executes helm3 functionality as the package manager for Kubernetes.", 192 }, 193 Spec: config.StepSpec{ 194 Inputs: config.StepInputs{ 195 Secrets: []config.StepSecrets{ 196 {Name: "dockerCredentialsId", Type: "jenkins"}, 197 {Name: "dockerConfigJsonCredentialsId", Description: "Jenkins 'Secret file' credentials ID containing Docker config.json (with registry credential(s)).", Type: "jenkins"}, 198 }, 199 Resources: []config.StepResources{ 200 {Name: "deployDescriptor", Type: "stash"}, 201 }, 202 Parameters: []config.StepParameters{ 203 { 204 Name: "additionalParameters", 205 ResourceRef: []config.ResourceReference{}, 206 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 207 Type: "[]string", 208 Mandatory: false, 209 Aliases: []config.Alias{{Name: "helmDeploymentParameters"}}, 210 Default: []string{}, 211 }, 212 { 213 Name: "chartPath", 214 ResourceRef: []config.ResourceReference{}, 215 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 216 Type: "string", 217 Mandatory: false, 218 Aliases: []config.Alias{{Name: "helmChartPath"}}, 219 Default: os.Getenv("PIPER_chartPath"), 220 }, 221 { 222 Name: "targetRepositoryURL", 223 ResourceRef: []config.ResourceReference{ 224 { 225 Name: "commonPipelineEnvironment", 226 Param: "custom/helmRepositoryURL", 227 }, 228 229 { 230 Name: "commonPipelineEnvironment", 231 Param: "custom/repositoryUrl", 232 }, 233 }, 234 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 235 Type: "string", 236 Mandatory: false, 237 Aliases: []config.Alias{}, 238 Default: os.Getenv("PIPER_targetRepositoryURL"), 239 }, 240 { 241 Name: "targetRepositoryName", 242 ResourceRef: []config.ResourceReference{}, 243 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 244 Type: "string", 245 Mandatory: false, 246 Aliases: []config.Alias{}, 247 Default: os.Getenv("PIPER_targetRepositoryName"), 248 }, 249 { 250 Name: "targetRepositoryUser", 251 ResourceRef: []config.ResourceReference{ 252 { 253 Name: "commonPipelineEnvironment", 254 Param: "custom/helmRepositoryUsername", 255 }, 256 257 { 258 Name: "commonPipelineEnvironment", 259 Param: "custom/repositoryUsername", 260 }, 261 }, 262 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 263 Type: "string", 264 Mandatory: false, 265 Aliases: []config.Alias{}, 266 Default: os.Getenv("PIPER_targetRepositoryUser"), 267 }, 268 { 269 Name: "targetRepositoryPassword", 270 ResourceRef: []config.ResourceReference{ 271 { 272 Name: "commonPipelineEnvironment", 273 Param: "custom/helmRepositoryPassword", 274 }, 275 276 { 277 Name: "commonPipelineEnvironment", 278 Param: "custom/repositoryPassword", 279 }, 280 }, 281 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 282 Type: "string", 283 Mandatory: false, 284 Aliases: []config.Alias{}, 285 Default: os.Getenv("PIPER_targetRepositoryPassword"), 286 }, 287 { 288 Name: "helmDeployWaitSeconds", 289 ResourceRef: []config.ResourceReference{}, 290 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 291 Type: "int", 292 Mandatory: false, 293 Aliases: []config.Alias{}, 294 Default: 300, 295 }, 296 { 297 Name: "helmValues", 298 ResourceRef: []config.ResourceReference{}, 299 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 300 Type: "[]string", 301 Mandatory: false, 302 Aliases: []config.Alias{}, 303 Default: []string{}, 304 }, 305 { 306 Name: "image", 307 ResourceRef: []config.ResourceReference{ 308 { 309 Name: "commonPipelineEnvironment", 310 Param: "container/imageNameTag", 311 }, 312 }, 313 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 314 Type: "string", 315 Mandatory: true, 316 Aliases: []config.Alias{{Name: "deployImage"}}, 317 Default: os.Getenv("PIPER_image"), 318 }, 319 { 320 Name: "keepFailedDeployments", 321 ResourceRef: []config.ResourceReference{}, 322 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 323 Type: "bool", 324 Mandatory: false, 325 Aliases: []config.Alias{}, 326 Default: false, 327 }, 328 { 329 Name: "kubeConfig", 330 ResourceRef: []config.ResourceReference{ 331 { 332 Name: "kubeConfigFileCredentialsId", 333 Type: "secret", 334 }, 335 336 { 337 Name: "kubeConfigFileVaultSecretName", 338 Type: "vaultSecretFile", 339 Default: "kube-config", 340 }, 341 }, 342 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 343 Type: "string", 344 Mandatory: false, 345 Aliases: []config.Alias{}, 346 Default: os.Getenv("PIPER_kubeConfig"), 347 }, 348 { 349 Name: "kubeContext", 350 ResourceRef: []config.ResourceReference{}, 351 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 352 Type: "string", 353 Mandatory: false, 354 Aliases: []config.Alias{}, 355 Default: os.Getenv("PIPER_kubeContext"), 356 }, 357 { 358 Name: "namespace", 359 ResourceRef: []config.ResourceReference{}, 360 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 361 Type: "string", 362 Mandatory: false, 363 Aliases: []config.Alias{{Name: "helmDeploymentNamespace"}}, 364 Default: `default`, 365 }, 366 { 367 Name: "dockerConfigJSON", 368 ResourceRef: []config.ResourceReference{ 369 { 370 Name: "dockerConfigJsonCredentialsId", 371 Type: "secret", 372 }, 373 374 { 375 Name: "dockerConfigFileVaultSecretName", 376 Type: "vaultSecretFile", 377 Default: "docker-config", 378 }, 379 }, 380 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 381 Type: "string", 382 Mandatory: false, 383 Aliases: []config.Alias{}, 384 Default: os.Getenv("PIPER_dockerConfigJSON"), 385 }, 386 { 387 Name: "helmCommand", 388 ResourceRef: []config.ResourceReference{}, 389 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 390 Type: "string", 391 Mandatory: false, 392 Aliases: []config.Alias{}, 393 Default: os.Getenv("PIPER_helmCommand"), 394 }, 395 { 396 Name: "appVersion", 397 ResourceRef: []config.ResourceReference{}, 398 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 399 Type: "string", 400 Mandatory: false, 401 Aliases: []config.Alias{}, 402 Default: os.Getenv("PIPER_appVersion"), 403 }, 404 { 405 Name: "dependency", 406 ResourceRef: []config.ResourceReference{}, 407 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 408 Type: "string", 409 Mandatory: false, 410 Aliases: []config.Alias{}, 411 Default: os.Getenv("PIPER_dependency"), 412 }, 413 { 414 Name: "packageDependencyUpdate", 415 ResourceRef: []config.ResourceReference{}, 416 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 417 Type: "bool", 418 Mandatory: false, 419 Aliases: []config.Alias{}, 420 Default: false, 421 }, 422 { 423 Name: "dumpLogs", 424 ResourceRef: []config.ResourceReference{}, 425 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 426 Type: "bool", 427 Mandatory: false, 428 Aliases: []config.Alias{}, 429 Default: false, 430 }, 431 { 432 Name: "filterTest", 433 ResourceRef: []config.ResourceReference{}, 434 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 435 Type: "string", 436 Mandatory: false, 437 Aliases: []config.Alias{}, 438 Default: os.Getenv("PIPER_filterTest"), 439 }, 440 { 441 Name: "customTlsCertificateLinks", 442 ResourceRef: []config.ResourceReference{}, 443 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 444 Type: "[]string", 445 Mandatory: false, 446 Aliases: []config.Alias{}, 447 Default: []string{}, 448 }, 449 { 450 Name: "publish", 451 ResourceRef: []config.ResourceReference{}, 452 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 453 Type: "bool", 454 Mandatory: false, 455 Aliases: []config.Alias{}, 456 Default: false, 457 }, 458 { 459 Name: "version", 460 ResourceRef: []config.ResourceReference{}, 461 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 462 Type: "string", 463 Mandatory: false, 464 Aliases: []config.Alias{}, 465 Default: os.Getenv("PIPER_version"), 466 }, 467 }, 468 }, 469 Containers: []config.Container{ 470 {Image: "dtzar/helm-kubectl:3.8.0", WorkingDir: "/config", Options: []config.Option{{Name: "-u", Value: "0"}}}, 471 }, 472 }, 473 } 474 return theMetaData 475 }