Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
anneliawa committed Oct 27, 2023
2 parents b89d269 + 78d4216 commit fe1059b
Show file tree
Hide file tree
Showing 21 changed files with 1,242 additions and 596 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ mocks:
mockgen -source ./pkg/apis/batch/syncer.go -destination ./pkg/apis/batch/syncer_mock.go -package batch
mockgen -source ./pkg/apis/batch/syncerfactory.go -destination ./pkg/apis/batch/syncerfactory_mock.go -package batch
mockgen -source ./radix-operator/common/handler.go -destination ./radix-operator/common/handler_mock.go -package common
mockgen -source ./pipeline-runner/wait/job.go -destination ./pipeline-runner/wait/job_mock.go -package wait

.PHONY: build-pipeline
build-pipeline:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package internal

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions pipeline-runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
pipe "github.com/equinor/radix-operator/pipeline-runner/pipelines"
"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/pipeline"
"github.com/equinor/radix-operator/pkg/apis/radix/v1"
v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -71,7 +71,7 @@ func prepareRunner(pipelineArgs *model.PipelineArguments) (*pipe.PipelineRunner,
return nil, err
}

pipelineRunner := pipe.InitRunner(client, radixClient, prometheusOperatorClient, secretProviderClient, pipelineDefinition, pipelineArgs.AppName)
pipelineRunner := pipe.NewRunner(client, radixClient, prometheusOperatorClient, secretProviderClient, pipelineDefinition, pipelineArgs.AppName)

err = pipelineRunner.PrepareRun(pipelineArgs)
if err != nil {
Expand Down
15 changes: 7 additions & 8 deletions pipeline-runner/model/pipelineInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package model

import (
"fmt"
"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/utils/conditions"
"path/filepath"
"strings"
"time"

"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/utils/conditions"

"github.com/equinor/radix-common/utils/maps"
application "github.com/equinor/radix-operator/pkg/apis/applicationconfig"
"github.com/equinor/radix-operator/pkg/apis/pipeline"
Expand Down Expand Up @@ -47,6 +49,7 @@ type PipelineInfo struct {

// Holds information on the images referred to by their respective components
ComponentImages map[string]pipeline.ComponentImage

// Prepare pipeline job build context
PrepareBuildContext *PrepareBuildContext
StopPipeline bool
Expand Down Expand Up @@ -351,10 +354,6 @@ func getDockerfileName(name string) string {
}

func getContext(sourceFolder string) string {
sourceFolder = strings.Trim(sourceFolder, ".")
sourceFolder = strings.Trim(sourceFolder, "/")
if sourceFolder == "" {
return fmt.Sprintf("%s/", git.Workspace)
}
return fmt.Sprintf("%s/%s/", git.Workspace, sourceFolder)
sourceRoot := filepath.Join("/", sourceFolder)
return fmt.Sprintf("%s/", filepath.Join(git.Workspace, sourceRoot))
}
382 changes: 26 additions & 356 deletions pipeline-runner/model/pipelineInfo_test.go

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions pipeline-runner/pipelines/app.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package onpush
package pipelines

import (
"context"
Expand Down Expand Up @@ -32,9 +32,8 @@ type PipelineRunner struct {
pipelineInfo *model.PipelineInfo
}

// InitRunner constructor
func InitRunner(kubeclient kubernetes.Interface, radixclient radixclient.Interface, prometheusOperatorClient monitoring.Interface, secretsstorevclient secretsstorevclient.Interface, definition *pipeline.Definition, appName string) PipelineRunner {

// NewRunner constructor
func NewRunner(kubeclient kubernetes.Interface, radixclient radixclient.Interface, prometheusOperatorClient monitoring.Interface, secretsstorevclient secretsstorevclient.Interface, definition *pipeline.Definition, appName string) PipelineRunner {
kubeUtil, _ := kube.New(kubeclient, radixclient, secretsstorevclient)
handler := PipelineRunner{
definition: definition,
Expand Down Expand Up @@ -106,10 +105,10 @@ func (cli *PipelineRunner) TearDown() {

func (cli *PipelineRunner) initStepImplementations(registration *v1.RadixRegistration) []model.Step {
stepImplementations := make([]model.Step, 0)
stepImplementations = append(stepImplementations, steps.NewPreparePipelinesStep())
stepImplementations = append(stepImplementations, steps.NewPreparePipelinesStep(nil))
stepImplementations = append(stepImplementations, steps.NewApplyConfigStep())
stepImplementations = append(stepImplementations, steps.NewBuildStep())
stepImplementations = append(stepImplementations, steps.NewRunPipelinesStep())
stepImplementations = append(stepImplementations, steps.NewBuildStep(nil))
stepImplementations = append(stepImplementations, steps.NewRunPipelinesStep(nil))
stepImplementations = append(stepImplementations, steps.NewDeployStep(kube.NewNamespaceWatcherImpl(cli.kubeclient)))
stepImplementations = append(stepImplementations, steps.NewPromoteStep())

Expand Down
5 changes: 3 additions & 2 deletions pipeline-runner/pipelines/app_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package onpush
package pipelines_test

import (
"testing"

"github.com/equinor/radix-operator/pipeline-runner/model"
"github.com/equinor/radix-operator/pipeline-runner/pipelines"
"github.com/equinor/radix-operator/pkg/apis/pipeline"
v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
commonTest "github.com/equinor/radix-operator/pkg/apis/test"
Expand All @@ -27,7 +28,7 @@ func setupTest(t *testing.T) (*kubernetes.Clientset, *radix.Clientset, *secretpr
func TestPrepare_NoRegistration_NotValid(t *testing.T) {
kubeclient, radixclient, secretproviderclient, _ := setupTest(t)
pipelineDefinition, _ := pipeline.GetPipelineFromName(string(v1.BuildDeploy))
cli := InitRunner(kubeclient, radixclient, &monitoring.Clientset{}, secretproviderclient, pipelineDefinition, "any-app")
cli := pipelines.NewRunner(kubeclient, radixclient, &monitoring.Clientset{}, secretproviderclient, pipelineDefinition, "any-app")

err := cli.PrepareRun(&model.PipelineArguments{})
assert.Error(t, err)
Expand Down
14 changes: 8 additions & 6 deletions pipeline-runner/steps/apply_radixconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ func (cli *ApplyConfigStepImplementation) ErrorMsg(err error) string {

// Run Override of default step method
func (cli *ApplyConfigStepImplementation) Run(pipelineInfo *model.PipelineInfo) error {
// Get radix application from config map
// Get pipeline info from configmap created by prepare pipeline step
namespace := utils.GetAppNamespace(cli.GetAppName())
configMap, err := cli.GetKubeutil().GetConfigMap(namespace, pipelineInfo.RadixConfigMapName)
if err != nil {
return err
}

// Read build context info from configmap
pipelineInfo.PrepareBuildContext, err = getPrepareBuildContextContent(configMap)
if err != nil {
return err
}

// Read radixconfig from configmap
configFileContent, ok := configMap.Data[pipelineDefaults.PipelineConfigMapContent]
if !ok {
return fmt.Errorf("failed load RadixApplication from ConfigMap")
Expand All @@ -81,11 +88,6 @@ func (cli *ApplyConfigStepImplementation) Run(pipelineInfo *model.PipelineInfo)
// Set back to pipeline
pipelineInfo.SetApplicationConfig(applicationConfig)

pipelineInfo.PrepareBuildContext, err = getPrepareBuildContextContent(configMap)
if err != nil {
return err
}

if pipelineInfo.PipelineArguments.PipelineType == string(v1.BuildDeploy) {
gitCommitHash, gitTags := cli.getHashAndTags(namespace, pipelineInfo)
err = validate.GitTagsContainIllegalChars(gitTags)
Expand Down
28 changes: 23 additions & 5 deletions pipeline-runner/steps/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,41 @@ import (
"fmt"

"github.com/equinor/radix-operator/pipeline-runner/model"
pipelinewait "github.com/equinor/radix-operator/pipeline-runner/wait"
jobUtil "github.com/equinor/radix-operator/pkg/apis/job"
"github.com/equinor/radix-operator/pkg/apis/kube"
"github.com/equinor/radix-operator/pkg/apis/pipeline"
v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils"
radixclient "github.com/equinor/radix-operator/pkg/client/clientset/versioned"
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// BuildStepImplementation Step to build docker image
type BuildStepImplementation struct {
stepType pipeline.StepType
model.DefaultStepImplementation
jobWaiter pipelinewait.JobCompletionWaiter
}

// NewBuildStep Constructor
func NewBuildStep() model.Step {
return &BuildStepImplementation{
stepType: pipeline.BuildStep,
// NewBuildStep Constructor.
// jobWaiter is optional and will be set by Init(...) function if nil.
func NewBuildStep(jobWaiter pipelinewait.JobCompletionWaiter) model.Step {
step := &BuildStepImplementation{
stepType: pipeline.BuildStep,
jobWaiter: jobWaiter,
}

return step
}

func (step *BuildStepImplementation) Init(kubeclient kubernetes.Interface, radixclient radixclient.Interface, kubeutil *kube.Kube, prometheusOperatorClient monitoring.Interface, rr *v1.RadixRegistration) {
step.DefaultStepImplementation.Init(kubeclient, radixclient, kubeutil, prometheusOperatorClient, rr)
if step.jobWaiter == nil {
step.jobWaiter = pipelinewait.NewJobCompletionWaiter(kubeclient)
}
}

Expand Down Expand Up @@ -84,7 +102,7 @@ func (cli *BuildStepImplementation) Run(pipelineInfo *model.PipelineInfo) error
return err
}

return cli.GetKubeutil().WaitForCompletionOf(job)
return cli.jobWaiter.Wait(job)
}

func needToBuildComponents(componentImages map[string]pipeline.ComponentImage) bool {
Expand Down
59 changes: 31 additions & 28 deletions pipeline-runner/steps/build_acr.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,19 @@ func getACRBuildJobVolumes(defaultMode *int32, buildSecrets []corev1.EnvVar) []c
},
},
}
if len(buildSecrets) == 0 {
return volumes
}
volumes = append(volumes,
corev1.Volume{
Name: defaults.BuildSecretsName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: defaults.BuildSecretsName,

if len(buildSecrets) > 0 {
volumes = append(volumes,
corev1.Volume{
Name: defaults.BuildSecretsName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: defaults.BuildSecretsName,
},
},
},
})
})
}

return volumes
}

Expand Down Expand Up @@ -278,7 +279,7 @@ func createACRBuildContainers(appName string, pipelineInfo *model.PipelineInfo,
if isUsingBuildKit(pipelineInfo) {
containerCommand = getBuildahContainerCommand(pipelineInfo, containerRegistry, secretMountsArgsString,
componentImage.Context, componentImage.Dockerfile, componentImage.ImagePath,
clusterTypeImage, clusterNameImage)
clusterTypeImage, clusterNameImage, pushImage)
container.Command = containerCommand
container.Resources.Requests = map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse(pipelineInfo.PipelineArguments.Builder.ResourcesRequestsCPU),
Expand Down Expand Up @@ -318,26 +319,28 @@ func getBuildAcrJobContainerVolumeMounts(azureServicePrincipleContext string, bu
return volumeMounts
}

func getBuildahContainerCommand(pipelineInfo *model.PipelineInfo, containerImageRegistry, secretArgsString, context, dockerFileName, imageTag, clusterTypeImageTag, clusterNameImageTag string) []string {
return []string{
"/bin/bash",
"-c",
fmt.Sprintf("/usr/bin/buildah login --username ${BUILDAH_USERNAME} --password ${BUILDAH_PASSWORD} %s && "+
"/usr/bin/buildah build --storage-driver=vfs --isolation=chroot "+
"--jobs 0 %s --file %s%s "+
"--build-arg RADIX_GIT_COMMIT_HASH=\"${RADIX_GIT_COMMIT_HASH}\" "+
"--build-arg RADIX_GIT_TAGS=\"${RADIX_GIT_TAGS}\" "+
"--build-arg BRANCH=\"${BRANCH}\" "+
"--build-arg TARGET_ENVIRONMENTS=\"${TARGET_ENVIRONMENTS}\" "+
"--tag %s --tag %s --tag %s %s && "+
func getBuildahContainerCommand(pipelineInfo *model.PipelineInfo, containerImageRegistry, secretArgsString, context, dockerFileName, imageTag, clusterTypeImageTag, clusterNameImageTag string, pushImage bool) []string {
cmd := fmt.Sprintf("/usr/bin/buildah login --username ${BUILDAH_USERNAME} --password ${BUILDAH_PASSWORD} %s && "+
"/usr/bin/buildah build --storage-driver=vfs --isolation=chroot "+
"--jobs 0 %s --file %s%s "+
"--build-arg RADIX_GIT_COMMIT_HASH=\"${RADIX_GIT_COMMIT_HASH}\" "+
"--build-arg RADIX_GIT_TAGS=\"${RADIX_GIT_TAGS}\" "+
"--build-arg BRANCH=\"${BRANCH}\" "+
"--build-arg TARGET_ENVIRONMENTS=\"${TARGET_ENVIRONMENTS}\" "+
"--tag %s --tag %s --tag %s %s",
containerImageRegistry, secretArgsString, context, dockerFileName,
imageTag, clusterTypeImageTag, clusterNameImageTag,
context)

if pushImage {
cmd = fmt.Sprintf("%s && "+
"/usr/bin/buildah push --storage-driver=vfs %s && "+
"/usr/bin/buildah push --storage-driver=vfs %s && "+
"/usr/bin/buildah push --storage-driver=vfs %s",
containerImageRegistry, secretArgsString, context, dockerFileName,
imageTag, clusterTypeImageTag, clusterNameImageTag,
context,
imageTag, clusterTypeImageTag, clusterNameImageTag),
cmd, imageTag, clusterTypeImageTag, clusterNameImageTag)
}

return []string{"/bin/bash", "-c", cmd}
}

func isUsingBuildKit(pipelineInfo *model.PipelineInfo) bool {
Expand Down
Loading

0 comments on commit fe1059b

Please sign in to comment.