Skip to content

Commit

Permalink
Added error for getComponentSources, renamed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
satr committed Mar 1, 2024
1 parent 6d247df commit 1483e98
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
22 changes: 17 additions & 5 deletions pipeline-runner/steps/apply_radixconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,21 @@ 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
}
}

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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pipeline-runner/steps/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
28 changes: 14 additions & 14 deletions pipeline-runner/steps/build_acr.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,34 @@ 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)
for _, componentImage := range buildComponentImages {
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
Expand All @@ -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)
Expand Down Expand Up @@ -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(),
},
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 1483e98

Please sign in to comment.