Skip to content

Commit

Permalink
Merge pull request #552 from equinor/master
Browse files Browse the repository at this point in the history
Added commit ID and tags to promote job and job models (#550)
  • Loading branch information
satr authored Oct 19, 2023
2 parents 99a7def + 019e0d4 commit dc1d296
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 85 deletions.
44 changes: 31 additions & 13 deletions api/applications/applications_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ import (
"testing"
"time"

"github.com/equinor/radix-api/models"
"github.com/equinor/radix-common/utils/pointers"

applicationModels "github.com/equinor/radix-api/api/applications/models"
environmentModels "github.com/equinor/radix-api/api/environments/models"
jobModels "github.com/equinor/radix-api/api/jobs/models"
controllertest "github.com/equinor/radix-api/api/test"
"github.com/equinor/radix-api/api/utils"
"github.com/equinor/radix-api/models"
radixhttp "github.com/equinor/radix-common/net/http"
radixutils "github.com/equinor/radix-common/utils"
"github.com/equinor/radix-common/utils/pointers"
"github.com/equinor/radix-operator/pkg/apis/applicationconfig"
"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/kube"
jobPipeline "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/radixvalidators"
Expand Down Expand Up @@ -1393,26 +1393,44 @@ func TestHandleTriggerPipeline_Deploy_JobHasCorrectParameters(t *testing.T) {
}

func TestHandleTriggerPipeline_Promote_JobHasCorrectParameters(t *testing.T) {
_, controllerTestUtils, _, radixclient, _, _ := setupTest(true, true)

appName := "an-app"
commonTestUtils, controllerTestUtils, _, radixclient, _, _ := setupTest(true, true)

const (
appName = "an-app"
commitId = "475f241c-478b-49da-adfb-3c336aaab8d2"
deploymentName = "a-deployment"
fromEnvironment = "origin"
toEnvironment = "target"
)

parameters := applicationModels.PipelineParametersPromote{
FromEnvironment: "origin",
ToEnvironment: "target",
DeploymentName: "a-deployment",
FromEnvironment: fromEnvironment,
ToEnvironment: toEnvironment,
DeploymentName: deploymentName,
}
_, err := commonTestUtils.ApplyDeployment(builders.
ARadixDeployment().
WithAppName(appName).
WithDeploymentName(deploymentName).
WithEnvironment(fromEnvironment).
WithLabel(kube.RadixCommitLabel, commitId).
WithCondition(v1.DeploymentInactive))
require.NoError(t, err)

registerAppParam := buildApplicationRegistrationRequest(anApplicationRegistration().WithName(appName).Build(), false)
<-controllerTestUtils.ExecuteRequestWithParameters("POST", "/api/v1/applications", registerAppParam)
responseChannel := controllerTestUtils.ExecuteRequestWithParameters("POST", fmt.Sprintf("/api/v1/applications/%s/pipelines/%s", appName, v1.Promote), parameters)
<-responseChannel

appNamespace := fmt.Sprintf("%s-app", appName)
jobs, _ := getJobsInNamespace(radixclient, appNamespace)
jobs, err := getJobsInNamespace(radixclient, appNamespace)
require.NoError(t, err)

assert.Equal(t, jobs[0].Spec.Promote.FromEnvironment, "origin")
assert.Equal(t, jobs[0].Spec.Promote.ToEnvironment, "target")
assert.Equal(t, jobs[0].Spec.Promote.DeploymentName, "a-deployment")
require.Len(t, jobs, 1)
assert.Equal(t, jobs[0].Spec.Promote.FromEnvironment, fromEnvironment)
assert.Equal(t, jobs[0].Spec.Promote.ToEnvironment, toEnvironment)
assert.Equal(t, jobs[0].Spec.Promote.DeploymentName, deploymentName)
assert.Equal(t, jobs[0].Spec.Promote.CommitID, commitId)
}

func TestIsDeployKeyValid(t *testing.T) {
Expand Down
33 changes: 22 additions & 11 deletions api/applications/applications_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"

applicationModels "github.com/equinor/radix-api/api/applications/models"
"github.com/equinor/radix-api/api/deployments"
"github.com/equinor/radix-api/api/environments"
Expand All @@ -19,10 +22,11 @@ import (
"github.com/equinor/radix-operator/pkg/apis/applicationconfig"
"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/defaults/k8s"
"github.com/equinor/radix-operator/pkg/apis/kube"
jobPipeline "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/radixvalidators"
crdUtils "github.com/equinor/radix-operator/pkg/apis/utils"
operatorUtils "github.com/equinor/radix-operator/pkg/apis/utils"
log "github.com/sirupsen/logrus"
authorizationapi "k8s.io/api/authorization/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -31,8 +35,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"net/http"
"strings"
)

type patch struct {
Expand Down Expand Up @@ -66,7 +68,7 @@ func NewApplicationHandler(accounts models.Accounts, config ApplicationHandlerCo
}

func getApiNamespace(config ApplicationHandlerConfig) string {
if namespace := crdUtils.GetEnvironmentNamespace(config.AppName, config.EnvironmentName); len(namespace) > 0 {
if namespace := operatorUtils.GetEnvironmentNamespace(config.AppName, config.EnvironmentName); len(namespace) > 0 {
return namespace
}
panic("missing RADIX_APP or RADIX_ENVIRONMENT environment variables")
Expand Down Expand Up @@ -288,7 +290,7 @@ func (ah *ApplicationHandler) ModifyRegistrationDetails(ctx context.Context, app
}

if patchRequest.Repository != nil && *patchRequest.Repository != "" {
cloneURL := crdUtils.GetGithubCloneURLFromRepo(*patchRequest.Repository)
cloneURL := operatorUtils.GetGithubCloneURLFromRepo(*patchRequest.Repository)
updatedRegistration.Spec.CloneURL = cloneURL
payload = append(payload, patch{Op: "replace", Path: "/spec/cloneURL", Value: cloneURL})
runUpdate = true
Expand Down Expand Up @@ -433,6 +435,15 @@ func (ah *ApplicationHandler) TriggerPipelinePromote(ctx context.Context, appNam
return nil, err
}

radixDeployment, err := kubequery.GetRadixDeploymentByName(ctx, ah.accounts.UserAccount.RadixClient, appName, fromEnvironment, deploymentName)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil, fmt.Errorf("deployment %s not found in the app %s, environment %s", deploymentName, appName, fromEnvironment)
}
return nil, fmt.Errorf("failed to get deployment %s for the app %s, environment %s: %v", deploymentName, appName, fromEnvironment, err)
}

jobParameters.CommitID = radixDeployment.GetLabels()[kube.RadixCommitLabel]
jobSummary, err := ah.jobHandler.HandleStartPipelineJob(ctx, appName, pipeline, jobParameters)
if err != nil {
return nil, err
Expand Down Expand Up @@ -601,23 +612,23 @@ func (ah *ApplicationHandler) RegenerateDeployKey(ctx context.Context, appName s

if regenerateDeployKeyAndSecretData.PrivateKey != "" {
// Deriving the public key from the private key in order to test it for validity
_, err := crdUtils.DeriveDeployKeyFromPrivateKey(regenerateDeployKeyAndSecretData.PrivateKey)
_, err := operatorUtils.DeriveDeployKeyFromPrivateKey(regenerateDeployKeyAndSecretData.PrivateKey)
if err != nil {
return fmt.Errorf("failed to derive public key from private key: %v", err)
}
existingSecret, err := ah.getUserAccount().Client.CoreV1().Secrets(crdUtils.GetAppNamespace(appName)).Get(ctx, defaults.GitPrivateKeySecretName, metav1.GetOptions{})
existingSecret, err := ah.getUserAccount().Client.CoreV1().Secrets(operatorUtils.GetAppNamespace(appName)).Get(ctx, defaults.GitPrivateKeySecretName, metav1.GetOptions{})
if err != nil {
return err
}
newSecret := existingSecret.DeepCopy()
newSecret.Data[defaults.GitPrivateKeySecretKey] = []byte(regenerateDeployKeyAndSecretData.PrivateKey)
_, err = ah.getUserAccount().Client.CoreV1().Secrets(crdUtils.GetAppNamespace(appName)).Update(ctx, newSecret, metav1.UpdateOptions{})
_, err = ah.getUserAccount().Client.CoreV1().Secrets(operatorUtils.GetAppNamespace(appName)).Update(ctx, newSecret, metav1.UpdateOptions{})
if err != nil {
return err
}
} else {
// Deleting the secret with the private key. This triggers the RR to be reconciled and the new key to be generated
err = ah.getUserAccount().Client.CoreV1().Secrets(crdUtils.GetAppNamespace(appName)).Delete(ctx, defaults.GitPrivateKeySecretName, metav1.DeleteOptions{})
err = ah.getUserAccount().Client.CoreV1().Secrets(operatorUtils.GetAppNamespace(appName)).Delete(ctx, defaults.GitPrivateKeySecretName, metav1.DeleteOptions{})
if err != nil {
return err
}
Expand All @@ -627,7 +638,7 @@ func (ah *ApplicationHandler) RegenerateDeployKey(ctx context.Context, appName s
}

func (ah *ApplicationHandler) GetDeployKeyAndSecret(ctx context.Context, appName string) (*applicationModels.DeployKeyAndSecret, error) {
cm, err := ah.getUserAccount().Client.CoreV1().ConfigMaps(crdUtils.GetAppNamespace(appName)).Get(ctx, defaults.GitPublicKeyConfigMapName, metav1.GetOptions{})
cm, err := ah.getUserAccount().Client.CoreV1().ConfigMaps(operatorUtils.GetAppNamespace(appName)).Get(ctx, defaults.GitPublicKeyConfigMapName, metav1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return nil, err
}
Expand Down Expand Up @@ -674,7 +685,7 @@ func (ah *ApplicationHandler) validateUserIsMemberOfAdGroups(ctx context.Context
}
name := fmt.Sprintf("access-validation-%s", appName)
labels := map[string]string{"radix-access-validation": "true"}
configMapName := fmt.Sprintf("%s-%s", name, strings.ToLower(crdUtils.RandString(6)))
configMapName := fmt.Sprintf("%s-%s", name, strings.ToLower(operatorUtils.RandString(6)))
role, err := createRoleToGetConfigMap(ctx, ah.accounts.ServiceAccount.Client, ah.namespace, name, labels, configMapName)
if err != nil {
return err
Expand Down
8 changes: 6 additions & 2 deletions api/buildstatus/models/buildstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

// embed https://golang.org/pkg/embed/ - For embedding a single file, a variable of type []byte or string is often best
//
//go:embed badges/build-status.svg
var defaultBadgeTemplate string

Expand Down Expand Up @@ -86,9 +87,12 @@ func (rbs *pipelineBadge) getBadge(condition v1.RadixJobCondition, pipeline v1.R
}

svgTemplate := template.New("status-badge.svg")
svgTemplate.Parse(rbs.badgeTemplate)
_, err := svgTemplate.Parse(rbs.badgeTemplate)
if err != nil {
return nil, err
}
var buff bytes.Buffer
err := svgTemplate.Execute(&buff, &badgeData)
err = svgTemplate.Execute(&buff, &badgeData)
if err != nil {
return nil, errors.New("failed to create SVG template")
}
Expand Down
9 changes: 7 additions & 2 deletions api/environments/environment_controller_secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
operatorutils "github.com/equinor/radix-operator/pkg/apis/utils"
radixclient "github.com/equinor/radix-operator/pkg/client/clientset/versioned"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -872,12 +873,14 @@ func (s *externalDnsAliasSecretTestSuite) SetupTest() {

s.appName, s.componentName, s.environmentName, s.alias = "any-app", "backend", "dev", "cdn.myalias.com"

s.deployment, _ = s.commonTestUtils.ApplyDeployment(operatorutils.
deployment, err := s.commonTestUtils.ApplyDeployment(operatorutils.
ARadixDeployment().
WithAppName(s.appName).
WithEnvironment(s.environmentName).
WithComponents(operatorutils.NewDeployComponentBuilder().WithName(s.componentName).WithDNSExternalAlias(s.alias)).
WithImageTag("master"))
require.NoError(s.T(), err)
s.deployment = deployment

s.commonTestUtils.ApplyApplication(operatorutils.
ARadixApplication().
Expand Down Expand Up @@ -1232,7 +1235,9 @@ func (s *secretHandlerTestSuite) prepareTestRun(scenario *getSecretScenario, app
ObjectMeta: metav1.ObjectMeta{Name: envNamespace},
Spec: v1.RadixEnvironmentSpec{AppName: appName, EnvName: envName},
}
radixClient.RadixV1().RadixEnvironments().Create(context.Background(), re, metav1.CreateOptions{})
_, err := radixClient.RadixV1().RadixEnvironments().Create(context.Background(), re, metav1.CreateOptions{})
require.NoError(s.T(), err)

radixDeployment := v1.RadixDeployment{
ObjectMeta: metav1.ObjectMeta{Name: deploymentName},
Spec: v1.RadixDeploymentSpec{
Expand Down
Loading

0 comments on commit dc1d296

Please sign in to comment.