From 97e16f4a26d8bb533f60cc7a7a7881baa7a366c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Gustav=20Str=C3=A5b=C3=B8?= Date: Mon, 16 Sep 2024 10:24:09 +0200 Subject: [PATCH] renamed methods and interface --- pipeline-runner/internal/jobs/build/acr.go | 60 ++++++------ .../internal/jobs/build/acr_test.go | 2 +- .../internal/jobs/build/buildkit.go | 66 +++++++------- .../internal/jobs/build/buildkit_test.go | 2 +- .../internal/jobs/build/interface.go | 4 +- .../internal/jobs/build/internal/kubejob.go | 50 ++++++++++ .../internal/jobs/build/kubejob.go | 91 ++++++++----------- .../internal/jobs/build/mock/job.go | 34 +++---- pipeline-runner/steps/build/build_test.go | 36 ++++---- pipeline-runner/steps/build/step.go | 6 +- 10 files changed, 195 insertions(+), 156 deletions(-) create mode 100644 pipeline-runner/internal/jobs/build/internal/kubejob.go diff --git a/pipeline-runner/internal/jobs/build/acr.go b/pipeline-runner/internal/jobs/build/acr.go index 902ab1f24..672863869 100644 --- a/pipeline-runner/internal/jobs/build/acr.go +++ b/pipeline-runner/internal/jobs/build/acr.go @@ -9,6 +9,7 @@ import ( "github.com/equinor/radix-common/utils/pointers" "github.com/equinor/radix-common/utils/slice" internalgit "github.com/equinor/radix-operator/pipeline-runner/internal/git" + "github.com/equinor/radix-operator/pipeline-runner/internal/jobs/build/internal" "github.com/equinor/radix-operator/pipeline-runner/model" "github.com/equinor/radix-operator/pkg/apis/defaults" "github.com/equinor/radix-operator/pkg/apis/pipeline" @@ -26,29 +27,28 @@ const ( acrHomePath = "/home/radix-image-builder" ) -func NewACR() Interface { +func NewACR() JobsBuilder { return &acr{} } type acr struct{} -func (c *acr) GetJobs(useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, componentImages []pipeline.BuildComponentImage, buildSecrets []string) []batchv1.Job { - kubeJob := kubeJobBuilder{ - source: &acrJobSource{ - pipelineArgs: pipelineArgs, - componentImages: componentImages, - cloneURL: cloneURL, - gitCommitHash: gitCommitHash, - gitTags: gitTags, - buildSecrets: buildSecrets, - }, +func (c *acr) BuildJobs(useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, componentImages []pipeline.BuildComponentImage, buildSecrets []string) []batchv1.Job { + props := &acrKubeJobProps{ + pipelineArgs: pipelineArgs, + componentImages: componentImages, + cloneURL: cloneURL, + gitCommitHash: gitCommitHash, + gitTags: gitTags, + buildSecrets: buildSecrets, } - return []batchv1.Job{kubeJob.GetJob()} + + return []batchv1.Job{internal.BuildKubeJob(props)} } -var _ kubeJobBuilderSource = &acrJobSource{} +var _ internal.KubeJobProps = &acrKubeJobProps{} -type acrJobSource struct { +type acrKubeJobProps struct { pipelineArgs model.PipelineArguments componentImages []pipeline.BuildComponentImage cloneURL string @@ -57,42 +57,42 @@ type acrJobSource struct { buildSecrets []string } -func (c *acrJobSource) JobName() string { +func (c *acrKubeJobProps) JobName() string { hash := strings.ToLower(utils.RandStringStrSeed(5, c.pipelineArgs.JobName)) return getJobName(time.Now(), c.pipelineArgs.ImageTag, hash) } -func (c *acrJobSource) JobLabels() map[string]string { +func (c *acrKubeJobProps) JobLabels() map[string]string { return getCommonJobLabels(c.pipelineArgs.AppName, c.pipelineArgs.JobName, c.pipelineArgs.ImageTag) } -func (c *acrJobSource) JobAnnotations() map[string]string { +func (c *acrKubeJobProps) JobAnnotations() map[string]string { return getCommonJobAnnotations(c.pipelineArgs.Branch, c.componentImages...) } -func (c *acrJobSource) PodLabels() map[string]string { +func (c *acrKubeJobProps) PodLabels() map[string]string { return getCommonPodLabels(c.pipelineArgs.JobName) } -func (c *acrJobSource) PodAnnotations() map[string]string { +func (c *acrKubeJobProps) PodAnnotations() map[string]string { return getCommonPodAnnotations() } -func (c *acrJobSource) PodTolerations() []corev1.Toleration { +func (c *acrKubeJobProps) PodTolerations() []corev1.Toleration { return getCommonPodTolerations() } -func (c *acrJobSource) PodAffinity() *corev1.Affinity { +func (c *acrKubeJobProps) PodAffinity() *corev1.Affinity { return getCommonPodAffinity(&radixv1.Runtime{Architecture: radixv1.RuntimeArchitectureArm64}) } -func (*acrJobSource) PodSecurityContext() *corev1.PodSecurityContext { +func (*acrKubeJobProps) PodSecurityContext() *corev1.PodSecurityContext { return securitycontext.Pod( securitycontext.WithPodFSGroup(1000), securitycontext.WithPodSeccompProfile(corev1.SeccompProfileTypeRuntimeDefault)) } -func (c *acrJobSource) PodVolumes() []corev1.Volume { +func (c *acrKubeJobProps) PodVolumes() []corev1.Volume { volumes := getCommonPodVolumes(c.componentImages) volumes = append(volumes, @@ -117,16 +117,16 @@ func (c *acrJobSource) PodVolumes() []corev1.Volume { return volumes } -func (c *acrJobSource) PodInitContainers() []corev1.Container { +func (c *acrKubeJobProps) PodInitContainers() []corev1.Container { cloneCfg := internalgit.CloneConfigFromPipelineArgs(c.pipelineArgs) return getCommonPodInitContainers(c.cloneURL, c.pipelineArgs.Branch, cloneCfg) } -func (c *acrJobSource) PodContainers() []corev1.Container { +func (c *acrKubeJobProps) PodContainers() []corev1.Container { return slice.Map(c.componentImages, c.getPodContainer) } -func (c *acrJobSource) getPodContainer(componentImage pipeline.BuildComponentImage) corev1.Container { +func (c *acrKubeJobProps) getPodContainer(componentImage pipeline.BuildComponentImage) corev1.Container { return corev1.Container{ Name: componentImage.ContainerName, Image: fmt.Sprintf("%s/%s", c.pipelineArgs.ContainerRegistry, c.pipelineArgs.ImageBuilder), @@ -137,7 +137,7 @@ func (c *acrJobSource) getPodContainer(componentImage pipeline.BuildComponentIma } } -func (*acrJobSource) getPodContainerSecurityContext() *corev1.SecurityContext { +func (*acrKubeJobProps) getPodContainerSecurityContext() *corev1.SecurityContext { return securitycontext.Container( securitycontext.WithContainerDropAllCapabilities(), securitycontext.WithContainerSeccompProfileType(corev1.SeccompProfileTypeRuntimeDefault), @@ -147,7 +147,7 @@ func (*acrJobSource) getPodContainerSecurityContext() *corev1.SecurityContext { ) } -func (c *acrJobSource) getPodContainerVolumeMounts(componentImage pipeline.BuildComponentImage) []corev1.VolumeMount { +func (c *acrKubeJobProps) getPodContainerVolumeMounts(componentImage pipeline.BuildComponentImage) []corev1.VolumeMount { volumeMounts := getCommonPodContainerVolumeMounts(componentImage) volumeMounts = append(volumeMounts, @@ -167,7 +167,7 @@ func (c *acrJobSource) getPodContainerVolumeMounts(componentImage pipeline.Build return volumeMounts } -func (c *acrJobSource) getPodContainerEnvVars(componentImage pipeline.BuildComponentImage) []corev1.EnvVar { +func (c *acrKubeJobProps) getPodContainerEnvVars(componentImage pipeline.BuildComponentImage) []corev1.EnvVar { var push string if c.pipelineArgs.PushImage { push = "--push" @@ -237,7 +237,7 @@ func (c *acrJobSource) getPodContainerEnvVars(componentImage pipeline.BuildCompo return envVars } -func (c *acrJobSource) getPodContainerBuildSecretEnvVars() []corev1.EnvVar { +func (c *acrKubeJobProps) getPodContainerBuildSecretEnvVars() []corev1.EnvVar { return slice.Map(c.buildSecrets, func(secret string) corev1.EnvVar { return corev1.EnvVar{ Name: defaults.BuildSecretPrefix + secret, diff --git a/pipeline-runner/internal/jobs/build/acr_test.go b/pipeline-runner/internal/jobs/build/acr_test.go index 8f06f6743..28ee4ce01 100644 --- a/pipeline-runner/internal/jobs/build/acr_test.go +++ b/pipeline-runner/internal/jobs/build/acr_test.go @@ -61,7 +61,7 @@ func assertACRJobSpec(t *testing.T, pushImage bool) { buildSecrets := []string{"secret1", "secret2"} sut := build.NewACR() - jobs := sut.GetJobs(false, args, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets) + jobs := sut.BuildJobs(false, args, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets) require.Len(t, jobs, 1) job := jobs[0] diff --git a/pipeline-runner/internal/jobs/build/buildkit.go b/pipeline-runner/internal/jobs/build/buildkit.go index 0214eaea8..4c9061478 100644 --- a/pipeline-runner/internal/jobs/build/buildkit.go +++ b/pipeline-runner/internal/jobs/build/buildkit.go @@ -8,6 +8,7 @@ import ( "github.com/equinor/radix-common/utils/pointers" internalgit "github.com/equinor/radix-operator/pipeline-runner/internal/git" + "github.com/equinor/radix-operator/pipeline-runner/internal/jobs/build/internal" "github.com/equinor/radix-operator/pipeline-runner/model" "github.com/equinor/radix-operator/pkg/apis/defaults" "github.com/equinor/radix-operator/pkg/apis/pipeline" @@ -29,41 +30,40 @@ const ( defaultExternalRegistruAuthPath = "/radix-default-external-registry-auth" ) -func NewBuildKit() Interface { +func NewBuildKit() JobsBuilder { return &buildKit{} } type buildKit struct{} -func (c *buildKit) GetJobs(useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, componentImages []pipeline.BuildComponentImage, buildSecrets []string) []batchv1.Job { +func (c *buildKit) BuildJobs(useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, componentImages []pipeline.BuildComponentImage, buildSecrets []string) []batchv1.Job { var jobs []batchv1.Job for _, componentImage := range componentImages { - job := c.constructJob(componentImage, useBuildCache, pipelineArgs, cloneURL, gitCommitHash, gitTags, buildSecrets) + job := c.buildJob(componentImage, useBuildCache, pipelineArgs, cloneURL, gitCommitHash, gitTags, buildSecrets) jobs = append(jobs, job) } return jobs } -func (c *buildKit) constructJob(componentImage pipeline.BuildComponentImage, useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, buildSecrets []string) batchv1.Job { - kubeJob := kubeJobBuilder{ - source: &buildKitJobSource{ - pipelineArgs: pipelineArgs, - componentImage: componentImage, - cloneURL: cloneURL, - gitCommitHash: gitCommitHash, - gitTags: gitTags, - buildSecrets: buildSecrets, - useBuildCache: useBuildCache, - }, +func (c *buildKit) buildJob(componentImage pipeline.BuildComponentImage, useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, buildSecrets []string) batchv1.Job { + props := &buildKitKubeJobProps{ + pipelineArgs: pipelineArgs, + componentImage: componentImage, + cloneURL: cloneURL, + gitCommitHash: gitCommitHash, + gitTags: gitTags, + buildSecrets: buildSecrets, + useBuildCache: useBuildCache, } - return kubeJob.GetJob() + + return internal.BuildKubeJob(props) } -var _ kubeJobBuilderSource = &buildKitJobSource{} +var _ internal.KubeJobProps = &buildKitKubeJobProps{} -type buildKitJobSource struct { +type buildKitKubeJobProps struct { pipelineArgs model.PipelineArguments componentImage pipeline.BuildComponentImage cloneURL string @@ -73,12 +73,12 @@ type buildKitJobSource struct { useBuildCache bool } -func (c *buildKitJobSource) JobName() string { +func (c *buildKitKubeJobProps) JobName() string { hash := strings.ToLower(utils.RandStringStrSeed(5, fmt.Sprintf("%s-%s-%s", c.pipelineArgs.JobName, c.componentImage.EnvName, c.componentImage.ComponentName))) return getJobName(time.Now(), c.pipelineArgs.ImageTag, hash) } -func (c *buildKitJobSource) JobLabels() map[string]string { +func (c *buildKitKubeJobProps) JobLabels() map[string]string { return labels.Merge( getCommonJobLabels(c.pipelineArgs.AppName, c.pipelineArgs.JobName, c.pipelineArgs.ImageTag), labels.ForEnvironmentName(c.componentImage.EnvName), @@ -86,36 +86,36 @@ func (c *buildKitJobSource) JobLabels() map[string]string { ) } -func (c *buildKitJobSource) JobAnnotations() map[string]string { +func (c *buildKitKubeJobProps) JobAnnotations() map[string]string { return getCommonJobAnnotations(c.pipelineArgs.Branch, c.componentImage) } -func (c *buildKitJobSource) PodLabels() map[string]string { +func (c *buildKitKubeJobProps) PodLabels() map[string]string { return getCommonPodLabels(c.pipelineArgs.JobName) } -func (c *buildKitJobSource) PodAnnotations() map[string]string { +func (c *buildKitKubeJobProps) PodAnnotations() map[string]string { annotations := getCommonPodAnnotations() annotations[fmt.Sprintf("container.apparmor.security.beta.kubernetes.io/%s", c.componentImage.ContainerName)] = "unconfined" return annotations } -func (c *buildKitJobSource) PodTolerations() []corev1.Toleration { +func (c *buildKitKubeJobProps) PodTolerations() []corev1.Toleration { return getCommonPodTolerations() } -func (c *buildKitJobSource) PodAffinity() *corev1.Affinity { +func (c *buildKitKubeJobProps) PodAffinity() *corev1.Affinity { return getCommonPodAffinity(c.componentImage.Runtime) } -func (*buildKitJobSource) PodSecurityContext() *corev1.PodSecurityContext { +func (*buildKitKubeJobProps) PodSecurityContext() *corev1.PodSecurityContext { return securitycontext.Pod( securitycontext.WithPodFSGroup(1000), securitycontext.WithPodSeccompProfile(corev1.SeccompProfileTypeRuntimeDefault), securitycontext.WithPodRunAsNonRoot(pointers.Ptr(false))) } -func (c *buildKitJobSource) PodVolumes() []corev1.Volume { +func (c *buildKitKubeJobProps) PodVolumes() []corev1.Volume { volumes := getCommonPodVolumes([]pipeline.BuildComponentImage{c.componentImage}) volumes = append(volumes, @@ -182,12 +182,12 @@ func (c *buildKitJobSource) PodVolumes() []corev1.Volume { return volumes } -func (c *buildKitJobSource) PodInitContainers() []corev1.Container { +func (c *buildKitKubeJobProps) PodInitContainers() []corev1.Container { cloneCfg := internalgit.CloneConfigFromPipelineArgs(c.pipelineArgs) return getCommonPodInitContainers(c.cloneURL, c.pipelineArgs.Branch, cloneCfg) } -func (c *buildKitJobSource) PodContainers() []corev1.Container { +func (c *buildKitKubeJobProps) PodContainers() []corev1.Container { container := corev1.Container{ Name: c.componentImage.ContainerName, Image: fmt.Sprintf("%s/%s", c.pipelineArgs.ContainerRegistry, c.pipelineArgs.BuildKitImageBuilder), @@ -202,7 +202,7 @@ func (c *buildKitJobSource) PodContainers() []corev1.Container { return []corev1.Container{container} } -func (c *buildKitJobSource) getPodContainerArgs() []string { +func (c *buildKitKubeJobProps) getPodContainerArgs() []string { args := []string{ "--registry", c.pipelineArgs.ContainerRegistry, "--registry-username", "$(BUILDAH_USERNAME)", @@ -249,7 +249,7 @@ func (c *buildKitJobSource) getPodContainerArgs() []string { return args } -func (c *buildKitJobSource) getPodContainerResources() corev1.ResourceRequirements { +func (c *buildKitKubeJobProps) getPodContainerResources() corev1.ResourceRequirements { return corev1.ResourceRequirements{ Requests: map[corev1.ResourceName]resource.Quantity{ corev1.ResourceCPU: resource.MustParse(c.pipelineArgs.Builder.ResourcesRequestsCPU), @@ -261,7 +261,7 @@ func (c *buildKitJobSource) getPodContainerResources() corev1.ResourceRequiremen } } -func (c *buildKitJobSource) getPodContainerSecurityContext() *corev1.SecurityContext { +func (c *buildKitKubeJobProps) getPodContainerSecurityContext() *corev1.SecurityContext { return securitycontext.Container( securitycontext.WithContainerDropAllCapabilities(), securitycontext.WithContainerCapabilities([]corev1.Capability{"SETUID", "SETGID", "SETFCAP"}), @@ -274,7 +274,7 @@ func (c *buildKitJobSource) getPodContainerSecurityContext() *corev1.SecurityCon ) } -func (c *buildKitJobSource) getPodContainerEnvVars() []corev1.EnvVar { +func (c *buildKitKubeJobProps) getPodContainerEnvVars() []corev1.EnvVar { envVars := []corev1.EnvVar{ { Name: "BUILDAH_USERNAME", @@ -317,7 +317,7 @@ func (c *buildKitJobSource) getPodContainerEnvVars() []corev1.EnvVar { return envVars } -func (c *buildKitJobSource) getPodContainerVolumeMounts() []corev1.VolumeMount { +func (c *buildKitKubeJobProps) getPodContainerVolumeMounts() []corev1.VolumeMount { volumeMounts := getCommonPodContainerVolumeMounts(c.componentImage) volumeMounts = append(volumeMounts, diff --git a/pipeline-runner/internal/jobs/build/buildkit_test.go b/pipeline-runner/internal/jobs/build/buildkit_test.go index 417569f3b..f62fc0e65 100644 --- a/pipeline-runner/internal/jobs/build/buildkit_test.go +++ b/pipeline-runner/internal/jobs/build/buildkit_test.go @@ -70,7 +70,7 @@ func assertBuildKitJobSpec(t *testing.T, useCache, pushImage bool, buildSecrets } sut := build.NewBuildKit() - jobs := sut.GetJobs(useCache, args, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets) + jobs := sut.BuildJobs(useCache, args, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets) require.Len(t, jobs, len(componentImages)) for _, ci := range componentImages { diff --git a/pipeline-runner/internal/jobs/build/interface.go b/pipeline-runner/internal/jobs/build/interface.go index 26fd22c50..bede1e3ef 100644 --- a/pipeline-runner/internal/jobs/build/interface.go +++ b/pipeline-runner/internal/jobs/build/interface.go @@ -6,6 +6,6 @@ import ( batchv1 "k8s.io/api/batch/v1" ) -type Interface interface { - GetJobs(useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, componentImages []pipeline.BuildComponentImage, buildSecrets []string) []batchv1.Job +type JobsBuilder interface { + BuildJobs(useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, componentImages []pipeline.BuildComponentImage, buildSecrets []string) []batchv1.Job } diff --git a/pipeline-runner/internal/jobs/build/internal/kubejob.go b/pipeline-runner/internal/jobs/build/internal/kubejob.go new file mode 100644 index 000000000..1b3676ae1 --- /dev/null +++ b/pipeline-runner/internal/jobs/build/internal/kubejob.go @@ -0,0 +1,50 @@ +package internal + +import ( + "github.com/equinor/radix-common/utils/pointers" + batchv1 "k8s.io/api/batch/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +type KubeJobProps interface { + JobName() string + JobLabels() map[string]string + JobAnnotations() map[string]string + PodLabels() map[string]string + PodAnnotations() map[string]string + PodTolerations() []corev1.Toleration + PodAffinity() *corev1.Affinity + PodSecurityContext() *corev1.PodSecurityContext + PodVolumes() []corev1.Volume + PodInitContainers() []corev1.Container + PodContainers() []corev1.Container +} + +func BuildKubeJob(props KubeJobProps) batchv1.Job { + return batchv1.Job{ + ObjectMeta: metav1.ObjectMeta{ + Name: props.JobName(), + Labels: props.JobLabels(), + Annotations: props.JobAnnotations(), + }, + Spec: batchv1.JobSpec{ + BackoffLimit: pointers.Ptr[int32](0), + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: props.PodLabels(), + Annotations: props.PodAnnotations(), + }, + Spec: corev1.PodSpec{ + RestartPolicy: corev1.RestartPolicyNever, + Affinity: props.PodAffinity(), + Tolerations: props.PodTolerations(), + SecurityContext: props.PodSecurityContext(), + Volumes: props.PodVolumes(), + InitContainers: props.PodInitContainers(), + Containers: props.PodContainers(), + }, + }, + }, + } +} diff --git a/pipeline-runner/internal/jobs/build/kubejob.go b/pipeline-runner/internal/jobs/build/kubejob.go index f50274610..5cf010408 100644 --- a/pipeline-runner/internal/jobs/build/kubejob.go +++ b/pipeline-runner/internal/jobs/build/kubejob.go @@ -1,54 +1,43 @@ package build -import ( - "github.com/equinor/radix-common/utils/pointers" - batchv1 "k8s.io/api/batch/v1" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) +// type kubeJobProps interface { +// JobName() string +// JobLabels() map[string]string +// JobAnnotations() map[string]string +// PodLabels() map[string]string +// PodAnnotations() map[string]string +// PodTolerations() []corev1.Toleration +// PodAffinity() *corev1.Affinity +// PodSecurityContext() *corev1.PodSecurityContext +// PodVolumes() []corev1.Volume +// PodInitContainers() []corev1.Container +// PodContainers() []corev1.Container +// } -type kubeJobBuilderSource interface { - JobName() string - JobLabels() map[string]string - JobAnnotations() map[string]string - PodLabels() map[string]string - PodAnnotations() map[string]string - PodTolerations() []corev1.Toleration - PodAffinity() *corev1.Affinity - PodSecurityContext() *corev1.PodSecurityContext - PodVolumes() []corev1.Volume - PodInitContainers() []corev1.Container - PodContainers() []corev1.Container -} - -type kubeJobBuilder struct { - source kubeJobBuilderSource -} - -func (b *kubeJobBuilder) GetJob() batchv1.Job { - return batchv1.Job{ - ObjectMeta: metav1.ObjectMeta{ - Name: b.source.JobName(), - Labels: b.source.JobLabels(), - Annotations: b.source.JobAnnotations(), - }, - Spec: batchv1.JobSpec{ - BackoffLimit: pointers.Ptr[int32](0), - Template: corev1.PodTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Labels: b.source.PodLabels(), - Annotations: b.source.PodAnnotations(), - }, - Spec: corev1.PodSpec{ - RestartPolicy: corev1.RestartPolicyNever, - Affinity: b.source.PodAffinity(), - Tolerations: b.source.PodTolerations(), - SecurityContext: b.source.PodSecurityContext(), - Volumes: b.source.PodVolumes(), - InitContainers: b.source.PodInitContainers(), - Containers: b.source.PodContainers(), - }, - }, - }, - } -} +// func buildKubeJob(props kubeJobProps) batchv1.Job { +// return batchv1.Job{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: props.JobName(), +// Labels: props.JobLabels(), +// Annotations: props.JobAnnotations(), +// }, +// Spec: batchv1.JobSpec{ +// BackoffLimit: pointers.Ptr[int32](0), +// Template: corev1.PodTemplateSpec{ +// ObjectMeta: metav1.ObjectMeta{ +// Labels: props.PodLabels(), +// Annotations: props.PodAnnotations(), +// }, +// Spec: corev1.PodSpec{ +// RestartPolicy: corev1.RestartPolicyNever, +// Affinity: props.PodAffinity(), +// Tolerations: props.PodTolerations(), +// SecurityContext: props.PodSecurityContext(), +// Volumes: props.PodVolumes(), +// InitContainers: props.PodInitContainers(), +// Containers: props.PodContainers(), +// }, +// }, +// }, +// } +// } diff --git a/pipeline-runner/internal/jobs/build/mock/job.go b/pipeline-runner/internal/jobs/build/mock/job.go index 2cb090342..213ab622b 100644 --- a/pipeline-runner/internal/jobs/build/mock/job.go +++ b/pipeline-runner/internal/jobs/build/mock/job.go @@ -13,39 +13,39 @@ import ( v1 "k8s.io/api/batch/v1" ) -// MockInterface is a mock of Interface interface. -type MockInterface struct { +// MockJobsBuilder is a mock of JobsBuilder interface. +type MockJobsBuilder struct { ctrl *gomock.Controller - recorder *MockInterfaceMockRecorder + recorder *MockJobsBuilderMockRecorder } -// MockInterfaceMockRecorder is the mock recorder for MockInterface. -type MockInterfaceMockRecorder struct { - mock *MockInterface +// MockJobsBuilderMockRecorder is the mock recorder for MockJobsBuilder. +type MockJobsBuilderMockRecorder struct { + mock *MockJobsBuilder } -// NewMockInterface creates a new mock instance. -func NewMockInterface(ctrl *gomock.Controller) *MockInterface { - mock := &MockInterface{ctrl: ctrl} - mock.recorder = &MockInterfaceMockRecorder{mock} +// NewMockJobsBuilder creates a new mock instance. +func NewMockJobsBuilder(ctrl *gomock.Controller) *MockJobsBuilder { + mock := &MockJobsBuilder{ctrl: ctrl} + mock.recorder = &MockJobsBuilderMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockInterface) EXPECT() *MockInterfaceMockRecorder { +func (m *MockJobsBuilder) EXPECT() *MockJobsBuilderMockRecorder { return m.recorder } -// GetJobs mocks base method. -func (m *MockInterface) GetJobs(useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, componentImages []pipeline.BuildComponentImage, buildSecrets []string) []v1.Job { +// BuildJobs mocks base method. +func (m *MockJobsBuilder) BuildJobs(useBuildCache bool, pipelineArgs model.PipelineArguments, cloneURL, gitCommitHash, gitTags string, componentImages []pipeline.BuildComponentImage, buildSecrets []string) []v1.Job { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetJobs", useBuildCache, pipelineArgs, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets) + ret := m.ctrl.Call(m, "BuildJobs", useBuildCache, pipelineArgs, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets) ret0, _ := ret[0].([]v1.Job) return ret0 } -// GetJobs indicates an expected call of GetJobs. -func (mr *MockInterfaceMockRecorder) GetJobs(useBuildCache, pipelineArgs, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets interface{}) *gomock.Call { +// BuildJobs indicates an expected call of BuildJobs. +func (mr *MockJobsBuilderMockRecorder) BuildJobs(useBuildCache, pipelineArgs, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetJobs", reflect.TypeOf((*MockInterface)(nil).GetJobs), useBuildCache, pipelineArgs, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BuildJobs", reflect.TypeOf((*MockJobsBuilder)(nil).BuildJobs), useBuildCache, pipelineArgs, cloneURL, gitCommitHash, gitTags, componentImages, buildSecrets) } diff --git a/pipeline-runner/steps/build/build_test.go b/pipeline-runner/steps/build/build_test.go index c7e8022b2..93058222f 100644 --- a/pipeline-runner/steps/build/build_test.go +++ b/pipeline-runner/steps/build/build_test.go @@ -47,8 +47,8 @@ const ( ) func createbuildJobFactoryMock(m *mock.Mock) build.BuildJobFactory { - return func(useBuildKit bool) internalbuild.Interface { - return m.MethodCalled(buildJobFactoryMockMethodName, useBuildKit).Get(0).(internalbuild.Interface) + return func(useBuildKit bool) internalbuild.JobsBuilder { + return m.MethodCalled(buildJobFactoryMockMethodName, useBuildKit).Get(0).(internalbuild.JobsBuilder) } } @@ -168,8 +168,8 @@ func (s *buildTestSuite) Test_WithBuildSecrets_Validation() { m.AssertNotCalled(s.T(), buildJobFactoryMockMethodName, mock.Anything) // secret correctly set - jobBuilder := buildjobmock.NewMockInterface(gomock.NewController(s.T())) - jobBuilder.EXPECT().GetJobs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) + jobBuilder := buildjobmock.NewMockJobsBuilder(gomock.NewController(s.T())) + jobBuilder.EXPECT().BuildJobs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) m.On(buildJobFactoryMockMethodName, mock.Anything).Return(jobBuilder) pipelineInfo.BuildSecret = &corev1.Secret{Data: map[string][]byte{secretName: []byte("anyvalue")}} err = cli.Run(context.Background(), pipelineInfo) @@ -201,8 +201,8 @@ func (s *buildTestSuite) Test_AppWithoutBuildSecrets_Validation() { RadixApplication: ra, } - jobBuilder := buildjobmock.NewMockInterface(gomock.NewController(s.T())) - jobBuilder.EXPECT().GetJobs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) + jobBuilder := buildjobmock.NewMockJobsBuilder(gomock.NewController(s.T())) + jobBuilder.EXPECT().BuildJobs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) m.On(buildJobFactoryMockMethodName, mock.Anything).Return(jobBuilder) err := cli.Run(context.Background(), pipelineInfo) s.NoError(err) @@ -245,12 +245,12 @@ func (s *buildTestSuite) Test_JobsCreated() { }}, } - jobBuilder := buildjobmock.NewMockInterface(gomock.NewController(s.T())) + jobBuilder := buildjobmock.NewMockJobsBuilder(gomock.NewController(s.T())) jobsToReturn := []batchv1.Job{ {ObjectMeta: metav1.ObjectMeta{Name: "job1", Namespace: utils.GetAppNamespace(appName)}}, {ObjectMeta: metav1.ObjectMeta{Name: "job2", Namespace: utils.GetAppNamespace(appName)}}, } - jobBuilder.EXPECT().GetJobs( + jobBuilder.EXPECT().BuildJobs( gomock.Any(), pipelineInfo.PipelineArguments, cloneUrl, @@ -981,10 +981,10 @@ func (s *buildTestSuite) Test_BuildChangedComponents() { s.Require().NoError(applyStep.Run(context.Background(), &pipelineInfo)) // Run build step - jobsBuilder := buildjobmock.NewMockInterface(s.ctrl) + jobsBuilder := buildjobmock.NewMockJobsBuilder(s.ctrl) m.On(buildJobFactoryMockMethodName, false).Return(jobsBuilder).Times(1) expectedComponentImages := slices.Concat(maps.Values(pipelineInfo.BuildComponentImages)...) - jobsBuilder.EXPECT().GetJobs(false, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.InAnyOrder(expectedComponentImages), gomock.InAnyOrder([]string{})).Return([]batchv1.Job{{}}).Times(1) + jobsBuilder.EXPECT().BuildJobs(false, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.InAnyOrder(expectedComponentImages), gomock.InAnyOrder([]string{})).Return([]batchv1.Job{{}}).Times(1) s.Require().NoError(buildStep.Run(context.Background(), &pipelineInfo)) s.Require().NoError(deployStep.Run(context.Background(), &pipelineInfo)) // jobs, _ := s.kubeClient.BatchV1().Jobs(utils.GetAppNamespace(appName)).List(context.Background(), metav1.ListOptions{}) @@ -1467,7 +1467,7 @@ func (s *buildTestSuite) Test_DetectComponentsToBuild() { s.Require().NoError(applyStep.Run(context.Background(), &pipelineInfo)) // Run build step - jobsBuilder := buildjobmock.NewMockInterface(s.ctrl) + jobsBuilder := buildjobmock.NewMockJobsBuilder(s.ctrl) m.On(buildJobFactoryMockMethodName, false).Return(jobsBuilder).Times(1) if len(test.expectedJobContainers) > 0 { var expectedBuildSecrets []string @@ -1475,7 +1475,7 @@ func (s *buildTestSuite) Test_DetectComponentsToBuild() { expectedBuildSecrets = ra.Spec.Build.Secrets } expectedComponentImages := slices.Concat(maps.Values(pipelineInfo.BuildComponentImages)...) - jobsBuilder.EXPECT().GetJobs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.InAnyOrder(expectedComponentImages), gomock.InAnyOrder(expectedBuildSecrets)).Return([]batchv1.Job{{}}).Times(1) + jobsBuilder.EXPECT().BuildJobs(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.InAnyOrder(expectedComponentImages), gomock.InAnyOrder(expectedBuildSecrets)).Return([]batchv1.Job{{}}).Times(1) jobWaiter.EXPECT().Wait(gomock.Any()).Return(nil).Times(1) } s.Require().NoError(buildStep.Run(context.Background(), &pipelineInfo)) @@ -1739,9 +1739,9 @@ func (s *buildTestSuite) Test_BuildJobSpec_WithBuildSecrets() { s.Require().NoError(applyStep.Run(context.Background(), &pipeline)) // Run build step - jobsBuilder := buildjobmock.NewMockInterface(s.ctrl) + jobsBuilder := buildjobmock.NewMockJobsBuilder(s.ctrl) m.On(buildJobFactoryMockMethodName, false).Return(jobsBuilder).Times(1) - jobsBuilder.EXPECT().GetJobs(false, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.InAnyOrder([]string{"SECRET1", "SECRET2"})).Return([]batchv1.Job{{}}).Times(1) + jobsBuilder.EXPECT().BuildJobs(false, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.InAnyOrder([]string{"SECRET1", "SECRET2"})).Return([]batchv1.Job{{}}).Times(1) jobWaiter.EXPECT().Wait(gomock.Any()).Return(nil).Times(1) s.Require().NoError(buildStep.Run(context.Background(), &pipeline)) // jobs, _ := s.kubeClient.BatchV1().Jobs(utils.GetAppNamespace(appName)).List(context.Background(), metav1.ListOptions{}) @@ -1810,10 +1810,10 @@ func (s *buildTestSuite) Test_BuildJobSpec_BuildKit() { s.Require().NoError(applyStep.Run(context.Background(), &pipeline)) // Run build step - jobsBuilder := buildjobmock.NewMockInterface(s.ctrl) + jobsBuilder := buildjobmock.NewMockJobsBuilder(s.ctrl) m.On(buildJobFactoryMockMethodName, true).Return(jobsBuilder).Times(1) jobWaiter.EXPECT().Wait(gomock.Any()).Return(nil).AnyTimes() - jobsBuilder.EXPECT().GetJobs(true, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) + jobsBuilder.EXPECT().BuildJobs(true, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) s.Require().NoError(buildStep.Run(context.Background(), &pipeline)) // jobs, _ := s.kubeClient.BatchV1().Jobs(utils.GetAppNamespace(appName)).List(context.Background(), metav1.ListOptions{}) @@ -1968,10 +1968,10 @@ func (s *buildTestSuite) Test_BuildJobSpec_OverrideUseBuildCacheInBuildKit() { s.Require().NoError(applyStep.Run(context.Background(), &pipeline)) // Run build step - jobsBuilder := buildjobmock.NewMockInterface(s.ctrl) + jobsBuilder := buildjobmock.NewMockJobsBuilder(s.ctrl) m.On(buildJobFactoryMockMethodName, true).Return(jobsBuilder).Times(1) jobWaiter.EXPECT().Wait(gomock.Any()).Return(nil).AnyTimes() - jobsBuilder.EXPECT().GetJobs(ts.expectedUseBuildCache, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) + jobsBuilder.EXPECT().BuildJobs(ts.expectedUseBuildCache, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) s.Require().NoError(buildStep.Run(context.Background(), &pipeline)) // jobs, _ := s.kubeClient.BatchV1().Jobs(utils.GetAppNamespace(appName)).List(context.Background(), metav1.ListOptions{}) // s.Require().Len(jobs.Items, 1) diff --git a/pipeline-runner/steps/build/step.go b/pipeline-runner/steps/build/step.go index 6649548fd..a8347ad23 100644 --- a/pipeline-runner/steps/build/step.go +++ b/pipeline-runner/steps/build/step.go @@ -26,7 +26,7 @@ import ( "k8s.io/client-go/kubernetes" ) -type BuildJobFactory func(useBuildKit bool) internalbuild.Interface +type BuildJobFactory func(useBuildKit bool) internalbuild.JobsBuilder // BuildStepImplementation Step to build docker image type BuildStepImplementation struct { @@ -44,7 +44,7 @@ func WithBuildJobFactory(factory BuildJobFactory) Option { } } -func defaultBuildJobFactory(useBuildKit bool) internalbuild.Interface { +func defaultBuildJobFactory(useBuildKit bool) internalbuild.JobsBuilder { if useBuildKit { return internalbuild.NewBuildKit() } @@ -124,7 +124,7 @@ func (step *BuildStepImplementation) getBuildJobs(pipelineInfo *model.PipelineIn } imagesToBuild := slices.Concat(maps.Values(pipelineInfo.BuildComponentImages)...) return step.buildJobFactory(pipelineInfo.IsUsingBuildKit()). - GetJobs( + BuildJobs( pipelineInfo.IsUsingBuildCache(), pipelineInfo.PipelineArguments, rr.Spec.CloneURL,