github.com/xgoffin/jenkins-library@v1.154.0/cmd/gitopsUpdateDeployment_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 gitopsUpdateDeploymentOptions struct { 19 BranchName string `json:"branchName,omitempty"` 20 CommitMessage string `json:"commitMessage,omitempty"` 21 ServerURL string `json:"serverUrl,omitempty"` 22 Username string `json:"username,omitempty"` 23 Password string `json:"password,omitempty"` 24 FilePath string `json:"filePath,omitempty"` 25 ContainerName string `json:"containerName,omitempty"` 26 ContainerRegistryURL string `json:"containerRegistryUrl,omitempty"` 27 ContainerImageNameTag string `json:"containerImageNameTag,omitempty"` 28 ChartPath string `json:"chartPath,omitempty"` 29 HelmValues []string `json:"helmValues,omitempty"` 30 DeploymentName string `json:"deploymentName,omitempty"` 31 Tool string `json:"tool,omitempty" validate:"possible-values=kubectl helm kustomize"` 32 } 33 34 // GitopsUpdateDeploymentCommand Updates Kubernetes Deployment Manifest in an Infrastructure Git Repository 35 func GitopsUpdateDeploymentCommand() *cobra.Command { 36 const STEP_NAME = "gitopsUpdateDeployment" 37 38 metadata := gitopsUpdateDeploymentMetadata() 39 var stepConfig gitopsUpdateDeploymentOptions 40 var startTime time.Time 41 var logCollector *log.CollectorHook 42 var splunkClient *splunk.Splunk 43 telemetryClient := &telemetry.Telemetry{} 44 45 var createGitopsUpdateDeploymentCmd = &cobra.Command{ 46 Use: STEP_NAME, 47 Short: "Updates Kubernetes Deployment Manifest in an Infrastructure Git Repository", 48 Long: `This step allows you to update the deployment manifest for Kubernetes in a git repository. 49 50 It can for example be used for GitOps scenarios where the update of the manifests triggers an update of the corresponding deployment in Kubernetes. 51 52 As of today, it supports the update of deployment yaml files via kubectl patch, update a whole helm template and kustomize. 53 54 For *kubectl* the container inside the yaml must be described within the following hierarchy: ` + "`" + `{"spec":{"template":{"spec":{"containers":[{...}]}}}}` + "`" + ` 55 For *helm* the whole template is generated into a single file (` + "`" + `filePath` + "`" + `) and uploaded into the repository. 56 For *kustomize* the ` + "`" + `images` + "`" + ` section will be update with the current image.`, 57 PreRunE: func(cmd *cobra.Command, _ []string) error { 58 startTime = time.Now() 59 log.SetStepName(STEP_NAME) 60 log.SetVerbose(GeneralConfig.Verbose) 61 62 GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens) 63 64 path, _ := os.Getwd() 65 fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path} 66 log.RegisterHook(fatalHook) 67 68 err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile) 69 if err != nil { 70 log.SetErrorCategory(log.ErrorConfiguration) 71 return err 72 } 73 log.RegisterSecret(stepConfig.Username) 74 log.RegisterSecret(stepConfig.Password) 75 76 if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 { 77 sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID) 78 log.RegisterHook(&sentryHook) 79 } 80 81 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 82 splunkClient = &splunk.Splunk{} 83 logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID} 84 log.RegisterHook(logCollector) 85 } 86 87 validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages()) 88 if err != nil { 89 return err 90 } 91 if err = validation.ValidateStruct(stepConfig); err != nil { 92 log.SetErrorCategory(log.ErrorConfiguration) 93 return err 94 } 95 96 return nil 97 }, 98 Run: func(_ *cobra.Command, _ []string) { 99 stepTelemetryData := telemetry.CustomData{} 100 stepTelemetryData.ErrorCode = "1" 101 handler := func() { 102 config.RemoveVaultSecretFiles() 103 stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds()) 104 stepTelemetryData.ErrorCategory = log.GetErrorCategory().String() 105 stepTelemetryData.PiperCommitHash = GitCommit 106 telemetryClient.SetData(&stepTelemetryData) 107 telemetryClient.Send() 108 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 109 splunkClient.Send(telemetryClient.GetData(), logCollector) 110 } 111 } 112 log.DeferExitHandler(handler) 113 defer handler() 114 telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME) 115 if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 { 116 splunkClient.Initialize(GeneralConfig.CorrelationID, 117 GeneralConfig.HookConfig.SplunkConfig.Dsn, 118 GeneralConfig.HookConfig.SplunkConfig.Token, 119 GeneralConfig.HookConfig.SplunkConfig.Index, 120 GeneralConfig.HookConfig.SplunkConfig.SendLogs) 121 } 122 gitopsUpdateDeployment(stepConfig, &stepTelemetryData) 123 stepTelemetryData.ErrorCode = "0" 124 log.Entry().Info("SUCCESS") 125 }, 126 } 127 128 addGitopsUpdateDeploymentFlags(createGitopsUpdateDeploymentCmd, &stepConfig) 129 return createGitopsUpdateDeploymentCmd 130 } 131 132 func addGitopsUpdateDeploymentFlags(cmd *cobra.Command, stepConfig *gitopsUpdateDeploymentOptions) { 133 cmd.Flags().StringVar(&stepConfig.BranchName, "branchName", `master`, "The name of the branch where the changes should get pushed into.") 134 cmd.Flags().StringVar(&stepConfig.CommitMessage, "commitMessage", os.Getenv("PIPER_commitMessage"), "The commit message of the commit that will be done to do the changes.") 135 cmd.Flags().StringVar(&stepConfig.ServerURL, "serverUrl", `https://github.com`, "GitHub server url to the repository.") 136 cmd.Flags().StringVar(&stepConfig.Username, "username", os.Getenv("PIPER_username"), "User name for git authentication") 137 cmd.Flags().StringVar(&stepConfig.Password, "password", os.Getenv("PIPER_password"), "Password/token for git authentication.") 138 cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Relative path in the git repository to the deployment descriptor file that shall be updated. For different tools this has different semantics:\n\n * `kubectl` - path to the `deployment.yaml` that should be patched. Supports globbing.\n * `helm` - path where the helm chart will be generated into. Here no globbing is supported.\n * `kustomize` - path to the `kustomization.yaml`. Supports globbing.\n") 139 cmd.Flags().StringVar(&stepConfig.ContainerName, "containerName", os.Getenv("PIPER_containerName"), "The name of the container to update") 140 cmd.Flags().StringVar(&stepConfig.ContainerRegistryURL, "containerRegistryUrl", os.Getenv("PIPER_containerRegistryUrl"), "http(s) url of the Container registry where the image is located") 141 cmd.Flags().StringVar(&stepConfig.ContainerImageNameTag, "containerImageNameTag", os.Getenv("PIPER_containerImageNameTag"), "Container image name with version tag to annotate in the deployment configuration.") 142 cmd.Flags().StringVar(&stepConfig.ChartPath, "chartPath", os.Getenv("PIPER_chartPath"), "Defines the chart path for deployments using helm. Globbing is supported to merge multiple charts into one resource.yaml that will be commited.") 143 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`)") 144 cmd.Flags().StringVar(&stepConfig.DeploymentName, "deploymentName", os.Getenv("PIPER_deploymentName"), "Defines the name of the deployment. In case of `kustomize` this is the name or alias of the image in the `kustomization.yaml`") 145 cmd.Flags().StringVar(&stepConfig.Tool, "tool", `kubectl`, "Defines the tool which should be used to update the deployment description.") 146 147 cmd.MarkFlagRequired("branchName") 148 cmd.MarkFlagRequired("serverUrl") 149 cmd.MarkFlagRequired("username") 150 cmd.MarkFlagRequired("password") 151 cmd.MarkFlagRequired("filePath") 152 cmd.MarkFlagRequired("containerRegistryUrl") 153 cmd.MarkFlagRequired("containerImageNameTag") 154 cmd.MarkFlagRequired("tool") 155 } 156 157 // retrieve step metadata 158 func gitopsUpdateDeploymentMetadata() config.StepData { 159 var theMetaData = config.StepData{ 160 Metadata: config.StepMetadata{ 161 Name: "gitopsUpdateDeployment", 162 Aliases: []config.Alias{}, 163 Description: "Updates Kubernetes Deployment Manifest in an Infrastructure Git Repository", 164 }, 165 Spec: config.StepSpec{ 166 Inputs: config.StepInputs{ 167 Secrets: []config.StepSecrets{ 168 {Name: "gitHttpsCredentialsId", Description: "Jenkins 'Username with password' credentials ID containing username/password for http access to your git repository.", Type: "jenkins"}, 169 }, 170 Resources: []config.StepResources{ 171 {Name: "deployDescriptor", Type: "stash"}, 172 }, 173 Parameters: []config.StepParameters{ 174 { 175 Name: "branchName", 176 ResourceRef: []config.ResourceReference{}, 177 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 178 Type: "string", 179 Mandatory: true, 180 Aliases: []config.Alias{}, 181 Default: `master`, 182 }, 183 { 184 Name: "commitMessage", 185 ResourceRef: []config.ResourceReference{}, 186 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 187 Type: "string", 188 Mandatory: false, 189 Aliases: []config.Alias{}, 190 Default: os.Getenv("PIPER_commitMessage"), 191 }, 192 { 193 Name: "serverUrl", 194 ResourceRef: []config.ResourceReference{}, 195 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 196 Type: "string", 197 Mandatory: true, 198 Aliases: []config.Alias{{Name: "githubServerUrl"}}, 199 Default: `https://github.com`, 200 }, 201 { 202 Name: "username", 203 ResourceRef: []config.ResourceReference{ 204 { 205 Name: "gitHttpsCredentialsId", 206 Param: "username", 207 Type: "secret", 208 }, 209 210 { 211 Name: "gitHttpsCredentialVaultSecretName", 212 Type: "vaultSecret", 213 Default: "gitHttpsCredential", 214 }, 215 }, 216 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 217 Type: "string", 218 Mandatory: true, 219 Aliases: []config.Alias{}, 220 Default: os.Getenv("PIPER_username"), 221 }, 222 { 223 Name: "password", 224 ResourceRef: []config.ResourceReference{ 225 { 226 Name: "gitHttpsCredentialsId", 227 Param: "password", 228 Type: "secret", 229 }, 230 231 { 232 Name: "gitHttpsCredentialVaultSecretName", 233 Type: "vaultSecret", 234 Default: "gitHttpsCredential", 235 }, 236 }, 237 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 238 Type: "string", 239 Mandatory: true, 240 Aliases: []config.Alias{}, 241 Default: os.Getenv("PIPER_password"), 242 }, 243 { 244 Name: "filePath", 245 ResourceRef: []config.ResourceReference{}, 246 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 247 Type: "string", 248 Mandatory: true, 249 Aliases: []config.Alias{}, 250 Default: os.Getenv("PIPER_filePath"), 251 }, 252 { 253 Name: "containerName", 254 ResourceRef: []config.ResourceReference{}, 255 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 256 Type: "string", 257 Mandatory: false, 258 Aliases: []config.Alias{}, 259 Default: os.Getenv("PIPER_containerName"), 260 }, 261 { 262 Name: "containerRegistryUrl", 263 ResourceRef: []config.ResourceReference{ 264 { 265 Name: "commonPipelineEnvironment", 266 Param: "container/registryUrl", 267 }, 268 }, 269 Scope: []string{"GENERAL", "PARAMETERS", "STAGES", "STEPS"}, 270 Type: "string", 271 Mandatory: true, 272 Aliases: []config.Alias{{Name: "dockerRegistryUrl"}}, 273 Default: os.Getenv("PIPER_containerRegistryUrl"), 274 }, 275 { 276 Name: "containerImageNameTag", 277 ResourceRef: []config.ResourceReference{ 278 { 279 Name: "commonPipelineEnvironment", 280 Param: "container/imageNameTag", 281 }, 282 }, 283 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 284 Type: "string", 285 Mandatory: true, 286 Aliases: []config.Alias{{Name: "image", Deprecated: true}, {Name: "containerImage"}}, 287 Default: os.Getenv("PIPER_containerImageNameTag"), 288 }, 289 { 290 Name: "chartPath", 291 ResourceRef: []config.ResourceReference{}, 292 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 293 Type: "string", 294 Mandatory: false, 295 Aliases: []config.Alias{{Name: "helmChartPath"}}, 296 Default: os.Getenv("PIPER_chartPath"), 297 }, 298 { 299 Name: "helmValues", 300 ResourceRef: []config.ResourceReference{}, 301 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 302 Type: "[]string", 303 Mandatory: false, 304 Aliases: []config.Alias{}, 305 Default: []string{}, 306 }, 307 { 308 Name: "deploymentName", 309 ResourceRef: []config.ResourceReference{}, 310 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 311 Type: "string", 312 Mandatory: false, 313 Aliases: []config.Alias{{Name: "helmDeploymentName"}}, 314 Default: os.Getenv("PIPER_deploymentName"), 315 }, 316 { 317 Name: "tool", 318 ResourceRef: []config.ResourceReference{}, 319 Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, 320 Type: "string", 321 Mandatory: true, 322 Aliases: []config.Alias{}, 323 Default: `kubectl`, 324 }, 325 }, 326 }, 327 Containers: []config.Container{ 328 {Image: "dtzar/helm-kubectl:3.8.0", WorkingDir: "/config", Options: []config.Option{{Name: "-u", Value: "0"}}, Conditions: []config.Condition{{ConditionRef: "strings-equal", Params: []config.Param{{Name: "tool", Value: "helm"}}}}}, 329 {Image: "dtzar/helm-kubectl:3.8.0", WorkingDir: "/config", Options: []config.Option{{Name: "-u", Value: "0"}}, Conditions: []config.Condition{{ConditionRef: "strings-equal", Params: []config.Param{{Name: "tool", Value: "kubectl"}}}}}, 330 {Image: "nekottyo/kustomize-kubeval:kustomizev4", WorkingDir: "/config", Options: []config.Option{{Name: "-u", Value: "0"}}, Conditions: []config.Condition{{ConditionRef: "strings-equal", Params: []config.Param{{Name: "tool", Value: "kustomize"}}}}}, 331 }, 332 }, 333 } 334 return theMetaData 335 }