Skip to content

Commit

Permalink
renamed methods and interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsgstrabo committed Sep 16, 2024
1 parent a479079 commit 97e16f4
Show file tree
Hide file tree
Showing 10 changed files with 195 additions and 156 deletions.
60 changes: 30 additions & 30 deletions pipeline-runner/internal/jobs/build/acr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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),
Expand All @@ -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),
Expand All @@ -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,
Expand All @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pipeline-runner/internal/jobs/build/acr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
66 changes: 33 additions & 33 deletions pipeline-runner/internal/jobs/build/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -73,49 +73,49 @@ 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),
labels.ForComponentName(c.componentImage.ComponentName),
)
}

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,
Expand Down Expand Up @@ -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),
Expand All @@ -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)",
Expand Down Expand Up @@ -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),
Expand All @@ -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"}),
Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pipeline-runner/internal/jobs/build/buildkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pipeline-runner/internal/jobs/build/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading

0 comments on commit 97e16f4

Please sign in to comment.