Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply externa alias #1215

Merged
merged 34 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a95613f
Adding deploy-config step
satr Oct 7, 2024
71ea2c7
Merge remote-tracking branch 'origin/master' into apply-externa-alias
satr Oct 8, 2024
2fa942a
Added deploy conf step
satr Oct 8, 2024
70c893c
Added deploy conf step to apply config pipeline
satr Oct 8, 2024
2040019
Taken only applicable envs to deploy config
satr Oct 9, 2024
d51e8fc
Added unit-tests
satr Oct 9, 2024
230f8d7
Adding tests
satr Oct 11, 2024
b2ed44d
Merge remote-tracking branch 'origin/master' into apply-externa-alias
satr Oct 11, 2024
f64a3f9
Adding tests
satr Oct 14, 2024
112fc84
Adding tests
satr Oct 14, 2024
85b1556
Fixed method
satr Oct 14, 2024
e18c0a1
Fixed method
satr Oct 14, 2024
eb0a73d
Creating unit-tests
satr Oct 15, 2024
d9f6c35
Creating unit-tests
satr Oct 15, 2024
05b2209
Creating unit-tests
satr Oct 15, 2024
7d22d9a
Creating unit-tests
satr Oct 15, 2024
99317ab
Creating unit-tests
satr Oct 16, 2024
ff9a346
Creating unit-tests
satr Oct 17, 2024
4d8ff9f
Creating unit-tests
satr Oct 17, 2024
5995446
Creating unit-tests
satr Oct 17, 2024
537a476
Merge remote-tracking branch 'origin/master' into apply-externa-alias
satr Oct 17, 2024
1ea9e31
Creating unit-tests
satr Oct 17, 2024
e284a05
Corrected copied RD and unit tests
satr Oct 21, 2024
849acb3
Merge remote-tracking branch 'origin/master' into apply-externa-alias
satr Oct 21, 2024
04c5f3e
Merge remote-tracking branch 'origin/master' into apply-externa-alias
satr Oct 22, 2024
3e477e5
Merge remote-tracking branch 'origin/master' into apply-externa-alias
satr Oct 22, 2024
03ab04f
Updated version
satr Oct 22, 2024
2ec0f90
suggested refactor of deploy-config step
nilsgstrabo Oct 24, 2024
66c1217
implement handler and external dns updater
nilsgstrabo Oct 25, 2024
008d2e1
misc changes
nilsgstrabo Oct 25, 2024
39bd61d
ignore environment without active RD
nilsgstrabo Oct 28, 2024
c8c1e01
Merge pull request #1219 from equinor/review-suggestions-apply-extern…
nilsgstrabo Oct 28, 2024
f4993a7
Fixed unit-tests
satr Oct 28, 2024
659ddbb
Update pipeline-runner/steps/deployconfig/internal/handler.go
satr Oct 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,32 @@
"--RADIX_RESERVED_DNS_ALIASES=grafana,prometheus,www"
]
},
{
"name": "Launch-applyconfig-pipeline",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/pipeline-runner/main.go",
"env": {},
"args": [
"--RADIX_APP=oauth-demo",
"--JOB_NAME=radix-pipeline-20231030091802-mfzoz",
"--PIPELINE_TYPE=apply-config",
"--RADIX_TEKTON_IMAGE=radix-tekton:main-latest",
"--RADIX_PIPELINE_GIT_CLONE_NSLOOKUP_IMAGE=radixdevcache.azurecr.io/alpine:3.20",
"--RADIX_PIPELINE_GIT_CLONE_GIT_IMAGE=radixdevcache.azurecr.io/alpine/git:2.45.2",
"--RADIX_PIPELINE_GIT_CLONE_BASH_IMAGE=radixdevcache.azurecr.io/bash:5.2",
"--DEBUG=true",
"--RADIX_FILE_NAME=/workspace/radixconfig.yaml",
"--RADIX_CLUSTER_TYPE=development",
"--RADIX_ZONE=dev",
"--RADIX_CLUSTERNAME=weekly-43",
"--RADIX_CONTAINER_REGISTRY=radixdev.azurecr.io",
"--RADIX_RESERVED_APP_DNS_ALIASES=api=radix-api,canary=radix-canary-golang,console=radix-web-console,cost-api=radix-cost-allocation-api,webhook=radix-github-webhook",
"--RADIX_RESERVED_DNS_ALIASES=grafana,prometheus,www",
"--APPLY_CONFIG_DEPLOY_EXTERNALDNS=true"
]
},
{
"name": "Launch-operator",
"type": "go",
Expand Down
4 changes: 2 additions & 2 deletions charts/radix-operator/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
name: radix-operator
version: 1.44.0
appVersion: 1.64.0
version: 1.45.0
appVersion: 1.65.0
kubeVersion: ">=1.24.0"
description: Radix Operator
keywords:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/cert-manager/cert-manager v1.14.2
github.com/equinor/radix-common v1.9.4
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.6.0
github.com/kedacore/keda/v2 v2.15.1
github.com/pkg/errors v0.9.1
Expand Down Expand Up @@ -50,7 +51,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/hashicorp/hcl v1.0.1-vault-5 // indirect
github.com/imdario/mergo v0.3.16 // indirect
Expand Down
2 changes: 1 addition & 1 deletion pipeline-runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func setPipelineArgsFromArguments(cmd *cobra.Command, pipelineArgs *model.Pipeli
cmd.Flags().StringVar(&pipelineArgs.GitCloneNsLookupImage, defaults.RadixGitCloneNsLookupImageEnvironmentVariable, "alpine:latest", "Container image with nslookup used by git clone init containers")
cmd.Flags().StringVar(&pipelineArgs.GitCloneGitImage, defaults.RadixGitCloneGitImageEnvironmentVariable, "alpine/git:latest", "Container image with git used by git clone init containers")
cmd.Flags().StringVar(&pipelineArgs.GitCloneBashImage, defaults.RadixGitCloneBashImageEnvironmentVariable, "bash:latest", "Container image with bash used by git clone init containers")

cmd.Flags().BoolVar(&pipelineArgs.ApplyConfigOptions.DeployExternalDNS, defaults.RadixPipelineApplyConfigDeployExternalDNSFlag, false, "Deploy changes to External DNS configuration with the 'apply-config' pipeline")
// TODO: Remove when both pipeline and operator is released. This flag is only to prevent errors when deprecated flag is passed
cmd.Flags().String("USE_CACHE", "0", "Use cache")

Expand Down
6 changes: 6 additions & 0 deletions pipeline-runner/model/pipelineInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ type Builder struct {
ResourcesRequestsMemory string
}

type ApplyConfigOptions struct {
DeployExternalDNS bool
}

// PipelineArguments Holds arguments for the pipeline
type PipelineArguments struct {
PipelineType string
Expand Down Expand Up @@ -115,6 +119,8 @@ type PipelineArguments struct {
// Name of secret with .dockerconfigjson key containing docker auths. Optional.
// Used to authenticate external container registries when using buildkit to build dockerfiles.
ExternalContainerRegistryDefaultAuthSecret string

ApplyConfigOptions ApplyConfigOptions
}

// InitPipeline Initialize pipeline with step implementations
Expand Down
11 changes: 8 additions & 3 deletions pipeline-runner/model/pipelineInfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/equinor/radix-operator/pkg/apis/pipeline"
v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
applyConfigStep = &model.DefaultStepImplementation{StepType: pipeline.ApplyConfigStep, SuccessMessage: "config applied"}
deployConfigStep = &model.DefaultStepImplementation{StepType: pipeline.DeployConfigStep, SuccessMessage: "config deployed"}
buildStep = &model.DefaultStepImplementation{StepType: pipeline.BuildStep, SuccessMessage: "built"}
deployStep = &model.DefaultStepImplementation{StepType: pipeline.DeployStep, SuccessMessage: "deployed"}
prepareTektonPipelineStep = &model.DefaultStepImplementation{StepType: pipeline.PreparePipelinesStep,
Expand Down Expand Up @@ -59,14 +61,17 @@ func Test_BuildAndDefaultNoPushOnlyPipeline(t *testing.T) {
}

func Test_ApplyConfigPipeline(t *testing.T) {
pipelineType, _ := pipeline.GetPipelineFromName(string(v1.ApplyConfig))
pipelineType, err := pipeline.GetPipelineFromName(string(v1.ApplyConfig))
require.NoError(t, err, "Failed to get pipeline type. Error %v", err)

p, _ := model.InitPipeline(pipelineType, getPipelineArguments(), prepareTektonPipelineStep, applyConfigStep)
p, err := model.InitPipeline(pipelineType, getPipelineArguments(), prepareTektonPipelineStep, applyConfigStep, deployConfigStep)
require.NoError(t, err, "Failed to create pipeline. Error %v", err)
assert.Equal(t, v1.ApplyConfig, p.Definition.Type)
assert.False(t, p.PipelineArguments.PushImage)
assert.Equal(t, 2, len(p.Steps))
assert.Equal(t, 3, len(p.Steps))
assert.Equal(t, "pipelines prepared", p.Steps[0].SucceededMsg())
assert.Equal(t, "config applied", p.Steps[1].SucceededMsg())
assert.Equal(t, "config deployed", p.Steps[2].SucceededMsg())
}

func Test_GetImageTagNamesFromArgs(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pipeline-runner/pipelines/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/equinor/radix-operator/pipeline-runner/steps/applyconfig"
"github.com/equinor/radix-operator/pipeline-runner/steps/build"
"github.com/equinor/radix-operator/pipeline-runner/steps/deploy"
"github.com/equinor/radix-operator/pipeline-runner/steps/deployconfig"
"github.com/equinor/radix-operator/pipeline-runner/steps/preparepipeline"
"github.com/equinor/radix-operator/pipeline-runner/steps/promote"
"github.com/equinor/radix-operator/pipeline-runner/steps/runpipeline"
Expand Down Expand Up @@ -125,6 +126,7 @@ func (cli *PipelineRunner) initStepImplementations(ctx context.Context, registra
stepImplementations = append(stepImplementations, build.NewBuildStep(nil))
stepImplementations = append(stepImplementations, runpipeline.NewRunPipelinesStep(nil))
stepImplementations = append(stepImplementations, deploy.NewDeployStep(watcher.NewNamespaceWatcherImpl(cli.kubeclient), watcher.NewRadixDeploymentWatcher(cli.radixclient, time.Minute*5)))
stepImplementations = append(stepImplementations, deployconfig.NewDeployConfigStep(watcher.NewRadixDeploymentWatcher(cli.radixclient, time.Minute*5)))
stepImplementations = append(stepImplementations, promote.NewPromoteStep())

for _, stepImplementation := range stepImplementations {
Expand Down
4 changes: 2 additions & 2 deletions pipeline-runner/steps/applyconfig/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/kube"
"github.com/equinor/radix-operator/pkg/apis/pipeline"
application2 "github.com/equinor/radix-operator/pkg/apis/pipeline/application"
pipelineApplication "github.com/equinor/radix-operator/pkg/apis/pipeline/application"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
validate "github.com/equinor/radix-operator/pkg/apis/radixvalidators"
operatorutils "github.com/equinor/radix-operator/pkg/apis/utils"
Expand Down Expand Up @@ -76,7 +76,7 @@ func (cli *ApplyConfigStepImplementation) Run(ctx context.Context, pipelineInfo
if !ok {
return fmt.Errorf("failed load RadixApplication from ConfigMap")
}
ra, err := application2.CreateRadixApplication(ctx, cli.GetRadixclient(), pipelineInfo.PipelineArguments.DNSConfig, configFileContent)
ra, err := pipelineApplication.CreateRadixApplication(ctx, cli.GetRadixclient(), pipelineInfo.PipelineArguments.DNSConfig, configFileContent)
if err != nil {
return err
}
Expand Down
30 changes: 9 additions & 21 deletions pipeline-runner/steps/deploy/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"github.com/equinor/radix-operator/pipeline-runner/internal/watcher"
"github.com/equinor/radix-operator/pipeline-runner/model"
"github.com/equinor/radix-operator/pipeline-runner/steps/internal"
"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/pipeline"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils"
"github.com/rs/zerolog/log"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -72,10 +71,9 @@ func (cli *DeployStepImplementation) deploy(ctx context.Context, pipelineInfo *m
}

func (cli *DeployStepImplementation) deployToEnv(ctx context.Context, appName, envName string, pipelineInfo *model.PipelineInfo) error {
defaultEnvVars := getDefaultEnvVars(pipelineInfo)

if commitID, ok := defaultEnvVars[defaults.RadixCommitHashEnvironmentVariable]; !ok || len(commitID) == 0 {
defaultEnvVars[defaults.RadixCommitHashEnvironmentVariable] = pipelineInfo.PipelineArguments.CommitID // Commit ID specified by job arguments
commitID, gitTags := pipelineInfo.GitCommitHash, pipelineInfo.GitTags
if len(commitID) == 0 {
commitID = pipelineInfo.PipelineArguments.CommitID // Commit ID specified by job arguments
}

radixApplicationHash, err := internal.CreateRadixApplicationHash(pipelineInfo.RadixApplication)
Expand All @@ -99,9 +97,10 @@ func (cli *DeployStepImplementation) deployToEnv(ctx context.Context, appName, e
pipelineInfo.PipelineArguments.JobName,
pipelineInfo.PipelineArguments.ImageTag,
pipelineInfo.PipelineArguments.Branch,
commitID,
gitTags,
pipelineInfo.DeployEnvironmentComponentImages[envName],
envName,
defaultEnvVars,
radixApplicationHash,
buildSecretHash,
pipelineInfo.PrepareBuildContext,
Expand All @@ -122,23 +121,12 @@ func (cli *DeployStepImplementation) deployToEnv(ctx context.Context, appName, e
return fmt.Errorf("failed to apply Radix deployment for app %s to environment %s. %w", appName, envName, err)
}

if err := cli.radixDeploymentWatcher.WaitForActive(ctx, namespace, radixDeploymentName); err != nil {
if err = cli.radixDeploymentWatcher.WaitForActive(ctx, namespace, radixDeploymentName); err != nil {
log.Ctx(ctx).Error().Err(err).Msgf("Failed to activate Radix deployment %s in environment %s. Deleting deployment", radixDeploymentName, envName)
if err := cli.GetRadixclient().RadixV1().RadixDeployments(radixDeployment.GetNamespace()).Delete(context.Background(), radixDeploymentName, metav1.DeleteOptions{}); err != nil {
log.Ctx(ctx).Error().Err(err).Msgf("Failed to delete Radix deployment")
if deleteErr := cli.GetRadixclient().RadixV1().RadixDeployments(radixDeployment.GetNamespace()).Delete(context.Background(), radixDeploymentName, metav1.DeleteOptions{}); deleteErr != nil && !k8serrors.IsNotFound(deleteErr) {
log.Ctx(ctx).Error().Err(deleteErr).Msgf("Failed to delete Radix deployment")
}
return err
}
return nil
}

func getDefaultEnvVars(pipelineInfo *model.PipelineInfo) radixv1.EnvVarsMap {
gitCommitHash := pipelineInfo.GitCommitHash
gitTags := pipelineInfo.GitTags

envVarsMap := make(radixv1.EnvVarsMap)
envVarsMap[defaults.RadixCommitHashEnvironmentVariable] = gitCommitHash
envVarsMap[defaults.RadixGitTagsEnvironmentVariable] = gitTags

return envVarsMap
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package internal

import (
"fmt"
"slices"

"github.com/equinor/radix-common/utils/slice"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
)

// ExternalDNSFeatureProvider handles external DNS configuration
type ExternalDNSFeatureProvider struct{}

func (d *ExternalDNSFeatureProvider) IsEnabledForEnvironment(envName string, ra radixv1.RadixApplication, activeRd radixv1.RadixDeployment) bool {
if slices.ContainsFunc(ra.Spec.DNSExternalAlias, func(alias radixv1.ExternalAlias) bool { return alias.Environment == envName }) {
return true
}

if slices.ContainsFunc(activeRd.Spec.Components, func(comp radixv1.RadixDeployComponent) bool { return len(comp.GetExternalDNS()) > 0 }) {
return true
}

return false
}

func (d *ExternalDNSFeatureProvider) Mutate(target, source radixv1.RadixDeployment) error {
for i, targetComp := range target.Spec.Components {
sourceComp, found := slice.FindFirst(source.Spec.Components, func(c radixv1.RadixDeployComponent) bool { return c.Name == targetComp.Name })
if !found {
return fmt.Errorf("component %s not found in active deployment", targetComp.Name)
}
target.Spec.Components[i].ExternalDNS = sourceComp.GetExternalDNS()
}

return nil
}
14 changes: 14 additions & 0 deletions pipeline-runner/steps/deployconfig/internal/feature_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package internal

import (
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
)

// FeatureProvider provides methods for checking and mutating Radix features
type FeatureProvider interface {
// Check if feature is enabled for the specified environment by inspecting RadixApplication and active RadixDeployment (if set)
IsEnabledForEnvironment(envName string, ra radixv1.RadixApplication, activeRd radixv1.RadixDeployment) bool

// Mutates target with fields from source
Mutate(target, source radixv1.RadixDeployment) error
}
Loading
Loading