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