Skip to content

Commit

Permalink
Specify env for build deploy pipeline job (#1222)
Browse files Browse the repository at this point in the history
* Added toEnvironment option for build deploy pipeline job

* Set toEnvironment option for build deploy pipeline job

* Fixed toEnvironment option

* Fixed annotation

* Fixed an option description
  • Loading branch information
satr authored Nov 11, 2024
1 parent 8ec3eae commit 524efbf
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 78 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ deploy-pipeline:
az acr login --name $(CONTAINER_REPO)
docker buildx build -t $(DOCKER_REGISTRY)/radix-pipeline:$(VERSION) -t $(DOCKER_REGISTRY)/radix-pipeline:$(BRANCH)-$(VERSION) -t $(DOCKER_REGISTRY)/radix-pipeline:$(TAG) --platform linux/arm64,linux/amd64 -f pipeline.Dockerfile --push .

.PHONY: deploy-pipeline-arm64
deploy-pipeline-arm64:
az acr login --name $(CONTAINER_REPO)
docker buildx build -t $(DOCKER_REGISTRY)/radix-pipeline:$(VERSION) -t $(DOCKER_REGISTRY)/radix-pipeline:$(BRANCH)-$(VERSION) -t $(DOCKER_REGISTRY)/radix-pipeline:$(TAG) --platform linux/arm64 -f pipeline.Dockerfile --push .

.PHONY: build-operator
build-operator:
docker build -t $(DOCKER_REGISTRY)/radix-operator:$(VERSION) -t $(DOCKER_REGISTRY)/radix-operator:$(BRANCH)-$(VERSION) -t $(DOCKER_REGISTRY)/radix-operator:$(TAG) -f operator.Dockerfile .
Expand Down
4 changes: 2 additions & 2 deletions charts/radix-operator/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
name: radix-operator
version: 1.45.1
appVersion: 1.65.1
version: 1.46.0
appVersion: 1.66.0
kubeVersion: ">=1.24.0"
description: Radix Operator
keywords:
Expand Down
2 changes: 1 addition & 1 deletion pipeline-runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func setPipelineArgsFromArguments(cmd *cobra.Command, pipelineArgs *model.Pipeli
cmd.Flags().StringVar(&pipelineArgs.CommitID, defaults.RadixCommitIdEnvironmentVariable, "", "Commit ID to build from")
cmd.Flags().StringVar(&pipelineArgs.DeploymentName, defaults.RadixPromoteDeploymentEnvironmentVariable, "", "Radix deployment name")
cmd.Flags().StringVar(&pipelineArgs.FromEnvironment, defaults.RadixPromoteFromEnvironmentEnvironmentVariable, "", "Radix application environment name to promote from")
cmd.Flags().StringVar(&pipelineArgs.ToEnvironment, defaults.RadixPromoteToEnvironmentEnvironmentVariable, "", "Radix application environment name to promote to")
cmd.Flags().StringVar(&pipelineArgs.ToEnvironment, defaults.RadixPipelineJobToEnvironmentEnvironmentVariable, "", "Radix application environment name to build-deploy or promote to")
cmd.Flags().StringVar(&pipelineArgs.TektonPipeline, defaults.RadixTektonPipelineImageEnvironmentVariable, "", "Radix Tekton docker image")
cmd.Flags().StringVar(&pipelineArgs.ImageBuilder, defaults.RadixImageBuilderEnvironmentVariable, "", "Radix Image Builder docker image")
cmd.Flags().StringVar(&pipelineArgs.BuildKitImageBuilder, defaults.RadixBuildKitImageBuilderEnvironmentVariable, "", "Radix Build Kit Image Builder container image")
Expand Down
6 changes: 6 additions & 0 deletions pipeline-runner/model/pipelineInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ func (info *PipelineInfo) SetApplicationConfig(applicationConfig *application.Ap
targetEnvironments = append(targetEnvironments, info.PipelineArguments.ToEnvironment)
}

// For build and build-deploy pipeline
if (info.IsPipelineType(radixv1.Build) || info.IsPipelineType(radixv1.BuildDeploy)) &&
len(info.PipelineArguments.ToEnvironment) > 0 {
targetEnvironments = []string{info.PipelineArguments.ToEnvironment}
}

info.TargetEnvironments = targetEnvironments
}

Expand Down
2 changes: 1 addition & 1 deletion pipeline-runner/steps/preparepipeline/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (cli *PreparePipelinesStepImplementation) getPreparePipelinesJobConfig(pipe
Value: pipelineInfo.PipelineArguments.FromEnvironment,
},
{
Name: defaults.RadixPromoteToEnvironmentEnvironmentVariable,
Name: defaults.RadixPipelineJobToEnvironmentEnvironmentVariable,
Value: pipelineInfo.PipelineArguments.ToEnvironment,
},
{
Expand Down
2 changes: 1 addition & 1 deletion pipeline-runner/steps/runpipeline/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (step *RunPipelinesStepImplementation) getRunTektonPipelinesJobConfig(pipel
Value: pipelineInfo.PipelineArguments.FromEnvironment,
},
{
Name: defaults.RadixPromoteToEnvironmentEnvironmentVariable,
Name: defaults.RadixPipelineJobToEnvironmentEnvironmentVariable,
Value: pipelineInfo.PipelineArguments.ToEnvironment,
},
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/defaults/environment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ const (
// RadixPromoteFromEnvironmentEnvironmentVariable Name of Radix environment for pipeline promote from
RadixPromoteFromEnvironmentEnvironmentVariable = "FROM_ENVIRONMENT"

// RadixPromoteToEnvironmentEnvironmentVariable Name of Radix environment for pipeline promote to
RadixPromoteToEnvironmentEnvironmentVariable = "TO_ENVIRONMENT"
// RadixPipelineJobToEnvironmentEnvironmentVariable Name of Radix environment for pipeline build-deploy or promote to
RadixPipelineJobToEnvironmentEnvironmentVariable = "TO_ENVIRONMENT"

// RadixPromoteSourceDeploymentCommitHashEnvironmentVariable Git commit hash of source deployment in promote jobs
RadixPromoteSourceDeploymentCommitHashEnvironmentVariable = "SOURCE_DEPLOYMENT_GIT_COMMIT_HASH"
Expand Down
55 changes: 34 additions & 21 deletions pkg/apis/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ func (job *Job) syncStatuses(ctx context.Context, ra *v1.RadixApplication) (stop
return true, nil
}

appRadixJobs, err := job.getRadixJobs(ctx)
existingRadixJobs, err := job.getRadixJobs(ctx)
if err != nil {
err = fmt.Errorf("failed to get all RadixJobs: %v", err)
return false, err
}
if job.isOtherJobRunningOnBranchOrEnvironment(ra, appRadixJobs) {
if job.isOtherJobRunningOnBranchOrEnvironment(ra, existingRadixJobs) {
err = job.queueJob(ctx)
if err != nil {
return false, err
Expand All @@ -169,34 +169,41 @@ func (job *Job) syncStatuses(ctx context.Context, ra *v1.RadixApplication) (stop
return
}

func (job *Job) isOtherJobRunningOnBranchOrEnvironment(ra *v1.RadixApplication, appRadixJobs []v1.RadixJob) bool {
func (job *Job) isOtherJobRunningOnBranchOrEnvironment(ra *v1.RadixApplication, existingRadixJobs []v1.RadixJob) bool {
isJobActive := func(rj *v1.RadixJob) bool {
return rj.Status.Condition == v1.JobWaiting || rj.Status.Condition == v1.JobRunning
}

jobTargetEnvironments := job.getTargetEnvironments(ra)
for _, rj := range appRadixJobs {
if rj.GetName() == job.radixJob.GetName() || !isJobActive(&rj) {
for _, existingRadixJob := range existingRadixJobs {
if existingRadixJob.GetName() == job.radixJob.GetName() || !isJobActive(&existingRadixJob) {
continue
}
switch rj.Spec.PipeLineType {
switch existingRadixJob.Spec.PipeLineType {
case v1.BuildDeploy, v1.Build:
if len(jobTargetEnvironments) > 0 {
rjTargetBranches := applicationconfig.GetTargetEnvironments(rj.Spec.Build.Branch, ra)
for _, rjEnvName := range rjTargetBranches {
if _, ok := jobTargetEnvironments[rjEnvName]; ok {
existingJobTargetEnvironments := applicationconfig.GetTargetEnvironments(existingRadixJob.Spec.Build.Branch, ra)
for _, existingJobTargetEnvironment := range existingJobTargetEnvironments {
if _, ok := jobTargetEnvironments[existingJobTargetEnvironment]; ok {
return true
}
}
} else if job.radixJob.Spec.Build.Branch == rj.Spec.Build.Branch {
return true
continue
}
if job.radixJob.Spec.Build.Branch == existingRadixJob.Spec.Build.Branch {
if len(job.radixJob.Spec.Build.ToEnvironment) == 0 {
return true
}
if _, ok := jobTargetEnvironments[existingRadixJob.Spec.Build.ToEnvironment]; ok {
return true
}
}
case v1.Deploy:
if _, ok := jobTargetEnvironments[rj.Spec.Deploy.ToEnvironment]; ok {
if _, ok := jobTargetEnvironments[existingRadixJob.Spec.Deploy.ToEnvironment]; ok {
return true
}
case v1.Promote:
if _, ok := jobTargetEnvironments[rj.Spec.Promote.ToEnvironment]; ok {
if _, ok := jobTargetEnvironments[existingRadixJob.Spec.Promote.ToEnvironment]; ok {
return true
}
}
Expand All @@ -207,14 +214,18 @@ func (job *Job) isOtherJobRunningOnBranchOrEnvironment(ra *v1.RadixApplication,
func (job *Job) getTargetEnvironments(ra *v1.RadixApplication) map[string]struct{} {
targetEnvs := make(map[string]struct{})
if job.radixJob.Spec.PipeLineType == v1.BuildDeploy || job.radixJob.Spec.PipeLineType == v1.Build {
if ra != nil {
return slice.Reduce(applicationconfig.GetTargetEnvironments(job.radixJob.Spec.Build.Branch, ra),
targetEnvs, func(acc map[string]struct{}, envName string) map[string]struct{} {
acc[envName] = struct{}{}
return acc
})
if ra == nil {
return targetEnvs
}
} else if job.radixJob.Spec.PipeLineType == v1.Deploy {
return slice.Reduce(applicationconfig.GetTargetEnvironments(job.radixJob.Spec.Build.Branch, ra),
targetEnvs, func(acc map[string]struct{}, envName string) map[string]struct{} {
if len(job.radixJob.Spec.Build.ToEnvironment) == 0 || envName == job.radixJob.Spec.Build.ToEnvironment {
acc[envName] = struct{}{}
}
return acc
})
}
if job.radixJob.Spec.PipeLineType == v1.Deploy {
targetEnvs[job.radixJob.Spec.Deploy.ToEnvironment] = struct{}{}
} else if job.radixJob.Spec.PipeLineType == v1.Promote {
targetEnvs[job.radixJob.Spec.Promote.ToEnvironment] = struct{}{}
Expand Down Expand Up @@ -251,7 +262,9 @@ func (job *Job) getTargetEnv(ra *v1.RadixApplication, rj *v1.RadixJob) (targetEn

for _, env := range ra.Spec.Environments {
if len(env.Build.From) > 0 && branch.MatchesPattern(env.Build.From, rj.Spec.Build.Branch) {
targetEnvs = append(targetEnvs, env.Name)
if len(rj.Spec.Build.ToEnvironment) == 0 || env.Name == rj.Spec.Build.ToEnvironment {
targetEnvs = append(targetEnvs, env.Name)
}
}
}
return targetEnvs
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/job/job_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func (h *history) getRadixJobsMapToRadixDeployments(ctx context.Context, appName
func getRadixJobEnvs(rj radixv1.RadixJob, envsMap map[string][]string) []string {
switch rj.Spec.PipeLineType {
case radixv1.BuildDeploy:
if len(rj.Spec.Build.ToEnvironment) > 0 {
return []string{rj.Spec.Build.ToEnvironment}
}
return envsMap[rj.Spec.Build.Branch]
case radixv1.Deploy:
return []string{rj.Spec.Deploy.ToEnvironment}
Expand Down
4 changes: 3 additions & 1 deletion pkg/apis/job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ func (s *RadixJobTestSuite) TestObjectSynced_StatusMissing_StatusFromAnnotation(
}

func (s *RadixJobTestSuite) TestObjectSynced_PipelineJobCreated() {
appName, jobName, branch, deploymentName, commitID, imageTag, pipelineTag := "anyapp", "anyjobname", "anybranch", "anydeploy", "anycommit", "anyimagetag", "anypipelinetag"
appName, jobName, branch, envName, deploymentName, commitID, imageTag, pipelineTag := "anyapp", "anyjobname", "anybranch", "anyenv", "anydeploy", "anycommit", "anyimagetag", "anypipelinetag"
config := getConfigWithPipelineJobsHistoryLimit(3)
rj, err := s.applyJobWithSync(utils.NewJobBuilder().
WithJobName(jobName).
WithAppName(appName).
WithBranch(branch).
WithToEnvironment(envName).
WithCommitID(commitID).
WithPushImage(true).
WithImageTag(imageTag).
Expand Down Expand Up @@ -234,6 +235,7 @@ func (s *RadixJobTestSuite) TestObjectSynced_PipelineJobCreated() {
fmt.Sprintf("--RADIX_PIPELINE_GIT_CLONE_BASH_IMAGE=%s", s.config.bashImage),
fmt.Sprintf("--IMAGE_TAG=%s", imageTag),
fmt.Sprintf("--BRANCH=%s", branch),
fmt.Sprintf("--TO_ENVIRONMENT=%s", envName),
fmt.Sprintf("--COMMIT_ID=%s", commitID),
"--PUSH_IMAGE=1",
},
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/job/kubejob.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (job *Job) getPipelineJobArguments(ctx context.Context, appName, jobName st
case radixv1.BuildDeploy, radixv1.Build:
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixImageTagEnvironmentVariable, jobSpec.Build.ImageTag))
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixBranchEnvironmentVariable, jobSpec.Build.Branch))
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixPipelineJobToEnvironmentEnvironmentVariable, jobSpec.Build.ToEnvironment))
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixCommitIdEnvironmentVariable, jobSpec.Build.CommitID))
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixPushImageEnvironmentVariable, getPushImageTag(jobSpec.Build.PushImage)))
if jobSpec.Build.OverrideUseBuildCache != nil {
Expand All @@ -204,9 +205,9 @@ func (job *Job) getPipelineJobArguments(ctx context.Context, appName, jobName st
case radixv1.Promote:
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixPromoteDeploymentEnvironmentVariable, jobSpec.Promote.DeploymentName))
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixPromoteFromEnvironmentEnvironmentVariable, jobSpec.Promote.FromEnvironment))
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixPromoteToEnvironmentEnvironmentVariable, jobSpec.Promote.ToEnvironment))
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixPipelineJobToEnvironmentEnvironmentVariable, jobSpec.Promote.ToEnvironment))
case radixv1.Deploy:
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixPromoteToEnvironmentEnvironmentVariable, jobSpec.Deploy.ToEnvironment))
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixPipelineJobToEnvironmentEnvironmentVariable, jobSpec.Deploy.ToEnvironment))
args = append(args, fmt.Sprintf("--%s=%s", defaults.RadixCommitIdEnvironmentVariable, jobSpec.Deploy.CommitID))
for componentName, imageTagName := range jobSpec.Deploy.ImageTagNames {
args = append(args, fmt.Sprintf("--%s=%s=%s", defaults.RadixImageTagNameEnvironmentVariable, componentName, imageTagName))
Expand Down
Loading

0 comments on commit 524efbf

Please sign in to comment.