From 1483e98cfe084be01d213287484a34a1cc28ada5 Mon Sep 17 00:00:00 2001 From: Sergey Smolnikov Date: Fri, 1 Mar 2024 15:39:55 +0100 Subject: [PATCH] Added error for getComponentSources, renamed methods --- pipeline-runner/steps/apply_radixconfig.go | 22 +++++++++++++---- pipeline-runner/steps/build.go | 2 +- pipeline-runner/steps/build_acr.go | 28 +++++++++++----------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/pipeline-runner/steps/apply_radixconfig.go b/pipeline-runner/steps/apply_radixconfig.go index f6777d4ed..cc75fc063 100644 --- a/pipeline-runner/steps/apply_radixconfig.go +++ b/pipeline-runner/steps/apply_radixconfig.go @@ -254,7 +254,11 @@ func (cli *ApplyConfigStepImplementation) getEnvironmentComponentImageSource(pip } enabledComponents := slice.FindAll(appComponents, func(rcc radixv1.RadixCommonComponent) bool { return rcc.GetEnabledForEnvironment(envName) }) - if componentImageSources := getComponentSources(envName, enabledComponents, mustBuildComponent, currentRd); len(componentImageSources) > 0 { + componentImageSources, err := cli.getComponentSources(envName, enabledComponents, mustBuildComponent, currentRd) + if err != nil { + return nil, err + } + if len(componentImageSources) > 0 { environmentComponentImageSources[envName] = componentImageSources } } @@ -262,8 +266,9 @@ func (cli *ApplyConfigStepImplementation) getEnvironmentComponentImageSource(pip return environmentComponentImageSources, nil } -func getComponentSources(envName string, components []radixv1.RadixCommonComponent, mustBuildComponent func(comp radixv1.RadixCommonComponent) bool, currentRadixDeployment *radixv1.RadixDeployment) []componentImageSource { +func (cli *ApplyConfigStepImplementation) getComponentSources(envName string, components []radixv1.RadixCommonComponent, mustBuildComponent func(comp radixv1.RadixCommonComponent) bool, currentRadixDeployment *radixv1.RadixDeployment) ([]componentImageSource, error) { componentSource := make([]componentImageSource, 0) + var errs []error for _, component := range components { imageSource := componentImageSource{ComponentName: component.GetName()} if image := component.GetImageForEnvironment(envName); len(image) > 0 { @@ -273,16 +278,23 @@ func getComponentSources(envName string, components []radixv1.RadixCommonCompone imageSource.ImageSource = fromBuild imageSource.Source = component.GetSourceForEnvironment(envName) } else { - imageSource.ImageSource = fromDeployment deployComponent := currentRadixDeployment.GetCommonComponentByName(component.GetName()) if commonutils.IsNil(deployComponent) { - continue // component not found in current deployment + // if the component, existing in the current RadixApplication, does not exist in the active RadixDeployment, + // it is supposed that the radixconfig was changed and this component should have an imageSource with `imageSource.ImageSource` fromImagePath or fromBuild + errs = append(errs, errors.Errorf("missing the component %s in the active deployment %s, but it exist in the application %s and it has to be built and deployed or deployed with its image", + component.GetName(), currentRadixDeployment.Name, cli.GetAppName())) + continue } + imageSource.ImageSource = fromDeployment imageSource.Image = deployComponent.GetImage() } componentSource = append(componentSource, imageSource) } - return componentSource + if len(errs) > 0 { + return nil, stderrors.Join(errs...) + } + return componentSource, nil } // Get component build information used by build job diff --git a/pipeline-runner/steps/build.go b/pipeline-runner/steps/build.go index 382333107..134c5c149 100644 --- a/pipeline-runner/steps/build.go +++ b/pipeline-runner/steps/build.go @@ -84,7 +84,7 @@ func (step *BuildStepImplementation) Run(pipelineInfo *model.PipelineInfo) error return err } - jobs, err := step.buildACRBuildJobs(pipelineInfo, buildSecrets) + jobs, err := step.buildContainerImageBuildingJobs(pipelineInfo, buildSecrets) if err != nil { return err } diff --git a/pipeline-runner/steps/build_acr.go b/pipeline-runner/steps/build_acr.go index d21caccfd..3ac02bf52 100644 --- a/pipeline-runner/steps/build_acr.go +++ b/pipeline-runner/steps/build_acr.go @@ -32,26 +32,26 @@ const ( azureServicePrincipleContext = "/radix-image-builder/.azure" ) -func (step *BuildStepImplementation) buildACRBuildJobs(pipelineInfo *model.PipelineInfo, buildSecrets []corev1.EnvVar) ([]*batchv1.Job, error) { +func (step *BuildStepImplementation) buildContainerImageBuildingJobs(pipelineInfo *model.PipelineInfo, buildSecrets []corev1.EnvVar) ([]*batchv1.Job, error) { rr := step.GetRegistration() if isUsingBuildKit(pipelineInfo) { - return step.buildACRBuildJobsForBuildKit(rr, pipelineInfo, buildSecrets) + return step.buildContainerImageBuildingJobsForBuildKit(rr, pipelineInfo, buildSecrets) } - return step.buildACRBuildJobsForACRTasks(rr, pipelineInfo, buildSecrets) + return step.buildContainerImageBuildingJobsForACRTasks(rr, pipelineInfo, buildSecrets) } -func (step *BuildStepImplementation) buildACRBuildJobsForACRTasks(rr *v1.RadixRegistration, pipelineInfo *model.PipelineInfo, buildSecrets []corev1.EnvVar) ([]*batchv1.Job, error) { +func (step *BuildStepImplementation) buildContainerImageBuildingJobsForACRTasks(rr *v1.RadixRegistration, pipelineInfo *model.PipelineInfo, buildSecrets []corev1.EnvVar) ([]*batchv1.Job, error) { var buildComponentImages []pipeline.BuildComponentImage for _, envComponentImages := range pipelineInfo.BuildComponentImages { buildComponentImages = append(buildComponentImages, envComponentImages...) } log.Debug("build a build-job") hash := strings.ToLower(utils.RandStringStrSeed(5, pipelineInfo.PipelineArguments.JobName)) - job := buildACRBuildJob(rr, pipelineInfo, buildSecrets, hash, buildComponentImages...) + job := buildContainerImageBuildingJob(rr, pipelineInfo, buildSecrets, hash, buildComponentImages...) return []*batchv1.Job{job}, nil } -func (step *BuildStepImplementation) buildACRBuildJobsForBuildKit(rr *v1.RadixRegistration, pipelineInfo *model.PipelineInfo, buildSecrets []corev1.EnvVar) ([]*batchv1.Job, error) { +func (step *BuildStepImplementation) buildContainerImageBuildingJobsForBuildKit(rr *v1.RadixRegistration, pipelineInfo *model.PipelineInfo, buildSecrets []corev1.EnvVar) ([]*batchv1.Job, error) { var jobs []*batchv1.Job for envName, buildComponentImages := range pipelineInfo.BuildComponentImages { log.Debugf("build a build-kit jobs for the env %s", envName) @@ -59,7 +59,7 @@ func (step *BuildStepImplementation) buildACRBuildJobsForBuildKit(rr *v1.RadixRe log.Debugf("build a job for the image %s", componentImage.ImageName) hash := strings.ToLower(utils.RandStringStrSeed(5, fmt.Sprintf("%s-%s-%s", pipelineInfo.PipelineArguments.JobName, envName, componentImage.ComponentName))) - job := buildACRBuildJob(rr, pipelineInfo, buildSecrets, hash, componentImage) + job := buildContainerImageBuildingJob(rr, pipelineInfo, buildSecrets, hash, componentImage) job.ObjectMeta.Labels[kube.RadixEnvLabel] = envName job.ObjectMeta.Labels[kube.RadixComponentLabel] = componentImage.ComponentName @@ -69,13 +69,13 @@ func (step *BuildStepImplementation) buildACRBuildJobsForBuildKit(rr *v1.RadixRe return jobs, nil } -func buildACRBuildJob(rr *v1.RadixRegistration, pipelineInfo *model.PipelineInfo, buildSecrets []corev1.EnvVar, hash string, buildComponentImages ...pipeline.BuildComponentImage) *batchv1.Job { +func buildContainerImageBuildingJob(rr *v1.RadixRegistration, pipelineInfo *model.PipelineInfo, buildSecrets []corev1.EnvVar, hash string, buildComponentImages ...pipeline.BuildComponentImage) *batchv1.Job { appName := rr.Name branch := pipelineInfo.PipelineArguments.Branch imageTag := pipelineInfo.PipelineArguments.ImageTag pipelineJobName := pipelineInfo.PipelineArguments.JobName initContainers := git.CloneInitContainers(rr.Spec.CloneURL, branch, pipelineInfo.PipelineArguments.ContainerSecurityContext) - buildContainers := createACRBuildContainers(appName, pipelineInfo, buildComponentImages, buildSecrets) + buildContainers := createContainerImageBuildingContainers(appName, pipelineInfo, buildComponentImages, buildSecrets) timestamp := time.Now().Format("20060102150405") defaultMode, backOffLimit := int32(256), int32(0) componentImagesAnnotation, _ := json.Marshal(buildComponentImages) @@ -118,7 +118,7 @@ func buildACRBuildJob(rr *v1.RadixRegistration, pipelineInfo *model.PipelineInfo InitContainers: initContainers, Containers: buildContainers, SecurityContext: buildPodSecurityContext, - Volumes: getACRBuildJobVolumes(&defaultMode, buildSecrets), + Volumes: getContainerImageBuildingJobVolumes(&defaultMode, buildSecrets), Affinity: utils.GetPipelineJobPodSpecAffinity(), Tolerations: utils.GetPipelineJobPodSpecTolerations(), }, @@ -128,7 +128,7 @@ func buildACRBuildJob(rr *v1.RadixRegistration, pipelineInfo *model.PipelineInfo return job } -func getACRBuildJobVolumes(defaultMode *int32, buildSecrets []corev1.EnvVar) []corev1.Volume { +func getContainerImageBuildingJobVolumes(defaultMode *int32, buildSecrets []corev1.EnvVar) []corev1.Volume { volumes := []corev1.Volume{ { Name: git.BuildContextVolumeName, @@ -175,7 +175,7 @@ func getACRBuildJobVolumes(defaultMode *int32, buildSecrets []corev1.EnvVar) []c return volumes } -func createACRBuildContainers(appName string, pipelineInfo *model.PipelineInfo, buildComponentImages []pipeline.BuildComponentImage, buildSecrets []corev1.EnvVar) []corev1.Container { +func createContainerImageBuildingContainers(appName string, pipelineInfo *model.PipelineInfo, buildComponentImages []pipeline.BuildComponentImage, buildSecrets []corev1.EnvVar) []corev1.Container { var containers []corev1.Container imageTag := pipelineInfo.PipelineArguments.ImageTag clusterType := pipelineInfo.PipelineArguments.Clustertype @@ -205,7 +205,7 @@ func createACRBuildContainers(appName string, pipelineInfo *model.PipelineInfo, Command: command, ImagePullPolicy: corev1.PullAlways, Env: envVars, - VolumeMounts: getBuildAcrJobContainerVolumeMounts(buildSecrets, isUsingBuildKit(pipelineInfo)), + VolumeMounts: getContainerImageBuildingJobVolumeMounts(buildSecrets, isUsingBuildKit(pipelineInfo)), SecurityContext: buildContainerSecContext, Resources: resources, } @@ -382,7 +382,7 @@ func getStandardEnvVars(appName string, pipelineInfo *model.PipelineInfo, compon return envVars } -func getBuildAcrJobContainerVolumeMounts(buildSecrets []corev1.EnvVar, mountPrivateImageHubAuth bool) []corev1.VolumeMount { +func getContainerImageBuildingJobVolumeMounts(buildSecrets []corev1.EnvVar, mountPrivateImageHubAuth bool) []corev1.VolumeMount { volumeMounts := []corev1.VolumeMount{ { Name: git.BuildContextVolumeName,