Skip to content

Commit

Permalink
Merge branch 'master' into apply-config-with-dns-option
Browse files Browse the repository at this point in the history
  • Loading branch information
satr authored Nov 11, 2024
2 parents b41c919 + e0551d1 commit f64469a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 32 deletions.
6 changes: 0 additions & 6 deletions api/defaults/environment_variables.go

This file was deleted.

1 change: 0 additions & 1 deletion api/deployments/models/deployment_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func NewDeploymentBuilder() DeploymentBuilder {

func (b *deploymentBuilder) WithRadixDeployment(rd *v1.RadixDeployment) DeploymentBuilder {
jobName := rd.Labels[kube.RadixJobNameLabel]

b.withComponentSummariesFromRadixDeployment(rd).
withEnvironment(rd.Spec.Environment).
withNamespace(rd.GetNamespace()).
Expand Down
1 change: 1 addition & 0 deletions api/environmentvariables/env_vars_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

const (
clusterName = "AnyClusterName"
clusterType = "development"
appName = "any-app"
environmentName = "dev"
componentName = "backend"
Expand Down
23 changes: 19 additions & 4 deletions api/environmentvariables/env_vars_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"sort"
"strings"

"github.com/equinor/radix-operator/pkg/apis/deployment"
"github.com/rs/zerolog/log"

envvarsmodels "github.com/equinor/radix-api/api/environmentvariables/models"
"github.com/equinor/radix-api/models"
"github.com/equinor/radix-common/utils/slice"
"github.com/equinor/radix-operator/pkg/apis/kube"
v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
crdUtils "github.com/equinor/radix-operator/pkg/apis/utils"
"github.com/rs/zerolog/log"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -68,7 +69,7 @@ func (eh *envVarsHandler) GetComponentEnvVars(ctx context.Context, appName strin
if err != nil {
return nil, err
}
envVars, err := deployment.GetEnvironmentVariables(ctx, eh.kubeUtil, appName, rd, radixDeployComponent)
envVars, err := eh.getDeploymentEnvironmentVariables(ctx, appName, rd.Spec.Environment, radixDeployComponent)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -164,6 +165,20 @@ func (eh *envVarsHandler) ChangeEnvVar(ctx context.Context, appName, envName, co
return eh.kubeUtil.ApplyEnvVarsMetadataConfigMap(ctx, namespace, envVarsMetadataConfigMap, envVarsMetadataMap)
}

func (eh *envVarsHandler) getDeploymentEnvironmentVariables(ctx context.Context, appName, envName string, radixDeployComponent v1.RadixCommonDeployComponent) ([]corev1.EnvVar, error) {
deployment, err := eh.accounts.UserAccount.Client.AppsV1().Deployments(crdUtils.GetEnvironmentNamespace(appName, envName)).Get(ctx, radixDeployComponent.GetName(), metav1.GetOptions{})
if err != nil {
return nil, err
}
componentContainer, ok := slice.FindFirst(deployment.Spec.Template.Spec.Containers, func(container corev1.Container) bool { return container.Name == radixDeployComponent.GetName() })
if !ok {
return nil, fmt.Errorf("container %s not found in deployment", radixDeployComponent.GetName())
}
return slice.FindAll(componentContainer.Env, func(envVar corev1.EnvVar) bool {
return envVar.ValueFrom == nil || envVar.ValueFrom.SecretKeyRef == nil
}), nil
}

func getComponent(rd *v1.RadixDeployment, componentName string) v1.RadixCommonDeployComponent {
for _, component := range rd.Spec.Components {
if strings.EqualFold(component.Name, componentName) {
Expand Down
59 changes: 39 additions & 20 deletions api/environmentvariables/env_vars_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

envvarsmodels "github.com/equinor/radix-api/api/environmentvariables/models"
"github.com/equinor/radix-api/models"
"github.com/equinor/radix-common/utils/slice"
"github.com/equinor/radix-operator/pkg/apis/defaults"
"github.com/equinor/radix-operator/pkg/apis/kube"
operatorutils "github.com/equinor/radix-operator/pkg/apis/utils"
"github.com/stretchr/testify/assert"
Expand All @@ -30,7 +32,7 @@ func Test_GetEnvVars(t *testing.T) {
handler := envVarsHandler{
kubeUtil: commonTestUtils.GetKubeUtil(),
inClusterClient: nil,
accounts: models.Accounts{},
accounts: models.Accounts{UserAccount: models.Account{Client: kubeClient}},
}

_, err = kubeUtil.GetConfigMap(context.Background(), namespace, kube.GetEnvVarsConfigMapName(componentName))
Expand All @@ -43,11 +45,14 @@ func Test_GetEnvVars(t *testing.T) {

assert.NoError(t, err)
assert.NotEmpty(t, envVars)
assert.Len(t, envVars, 2)
assert.Equal(t, "VAR1", envVars[0].Name)
assert.Equal(t, "val1", envVars[0].Value)
assert.Equal(t, "VAR2", envVars[1].Name)
assert.Equal(t, "val2", envVars[1].Value)
envVarMap := convertToMap(envVars)
assert.Equal(t, "val1", envVarMap["VAR1"], "Invalid or missing VAR1")
assert.Equal(t, "val2", envVarMap["VAR2"], "Invalid or missing VAR2")
_, existsSecret1 := envVarMap["SECRET1"]
assert.False(t, existsSecret1, "Unexpected secret SECRET1 among env-vars")
_, existsSecret2 := envVarMap["SECRET2"]
assert.False(t, existsSecret2, "Unexpected secret SECRET2 among env-vars")
assertRadixEnvVars(t, envVarMap)
})
}

Expand All @@ -69,7 +74,7 @@ func Test_ChangeGetEnvVars(t *testing.T) {
handler := envVarsHandler{
kubeUtil: commonTestUtils.GetKubeUtil(),
inClusterClient: nil,
accounts: models.Accounts{},
accounts: models.Accounts{UserAccount: models.Account{Client: kubeClient}},
}

_, err = kubeUtil.GetConfigMap(context.Background(), namespace, kube.GetEnvVarsConfigMapName(componentName))
Expand All @@ -95,13 +100,11 @@ func Test_ChangeGetEnvVars(t *testing.T) {
envVars, err := handler.GetComponentEnvVars(context.Background(), appName, environmentName, componentName)
assert.NoError(t, err)
assert.NotEmpty(t, envVars)
assert.Len(t, envVars, 3)
assert.Equal(t, "VAR1", envVars[0].Name)
assert.Equal(t, "val1", envVars[0].Value)
assert.Equal(t, "VAR2", envVars[1].Name)
assert.Equal(t, "new-val2", envVars[1].Value)
assert.Equal(t, "VAR3", envVars[2].Name)
assert.Equal(t, "new-val3", envVars[2].Value)
envVarMap := convertToMap(envVars)
assert.Equal(t, "val1", envVarMap["VAR1"], "Invalid or missing VAR1")
assert.Equal(t, "new-val2", envVarMap["VAR2"], "Invalid or missing VAR2")
assert.Equal(t, "new-val3", envVarMap["VAR3"], "Invalid or missing VAR3")
assertRadixEnvVars(t, envVarMap)
})
t.Run("Skipped changing not-existing env vars", func(t *testing.T) {
t.Parallel()
Expand All @@ -118,7 +121,7 @@ func Test_ChangeGetEnvVars(t *testing.T) {
handler := envVarsHandler{
kubeUtil: commonTestUtils.GetKubeUtil(),
inClusterClient: nil,
accounts: models.Accounts{},
accounts: models.Accounts{UserAccount: models.Account{Client: kubeClient}},
}

_, err = kubeUtil.GetConfigMap(context.Background(), namespace, kube.GetEnvVarsConfigMapName(componentName))
Expand All @@ -144,10 +147,26 @@ func Test_ChangeGetEnvVars(t *testing.T) {
envVars, err := handler.GetComponentEnvVars(context.Background(), appName, environmentName, componentName)
assert.NoError(t, err)
assert.NotEmpty(t, envVars)
assert.Len(t, envVars, 2)
assert.Equal(t, "VAR1", envVars[0].Name)
assert.Equal(t, "val1", envVars[0].Value)
assert.Equal(t, "VAR2", envVars[1].Name)
assert.Equal(t, "new-val2", envVars[1].Value)
envVarMap := convertToMap(envVars)
assert.Equal(t, "val1", envVarMap["VAR1"], "Invalid or missing VAR1")
assert.Equal(t, "new-val2", envVarMap["VAR2"], "Invalid or missing VAR2")
assertRadixEnvVars(t, envVarMap)
})
}

func convertToMap(envVars []envvarsmodels.EnvVar) map[string]string {
return slice.Reduce(envVars, make(map[string]string), func(acc map[string]string, envVar envvarsmodels.EnvVar) map[string]string {
acc[envVar.Name] = envVar.Value
return acc
})
}

func assertRadixEnvVars(t *testing.T, envVarMap map[string]string) {
assert.Equal(t, appName, envVarMap[defaults.RadixAppEnvironmentVariable], "Invalid or missing RADIX_APP env-var")
assert.Equal(t, environmentName, envVarMap[defaults.EnvironmentnameEnvironmentVariable], "Invalid or missing RADIX_ENVIRONMENT env-var")
assert.Equal(t, clusterName, envVarMap[defaults.ClusternameEnvironmentVariable], "Invalid or missing RADIX_CLUSTERNAME env-var")
assert.Equal(t, clusterType, envVarMap[defaults.RadixClusterTypeEnvironmentVariable], "Invalid or missing RADIX_CLUSTER_TYPE env-var")
assert.Equal(t, componentName, envVarMap[defaults.RadixComponentEnvironmentVariable], "Invalid or missing RADIX_COMPONENT env-var")
assert.Equal(t, "any.container.registry", envVarMap[defaults.ContainerRegistryEnvironmentVariable], "Invalid or missing RADIX_CONTAINER_REGISTRY env-var")
assert.Equal(t, "dev.radix.equinor.com", envVarMap[defaults.RadixDNSZoneEnvironmentVariable], "Invalid or missing RADIX_DNS_ZONE env-var")
}
4 changes: 3 additions & 1 deletion api/models/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/equinor/radix-api/api/utils/predicate"
"github.com/equinor/radix-api/api/utils/tlsvalidation"
"github.com/equinor/radix-common/utils/slice"
"github.com/equinor/radix-operator/pkg/apis/kube"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
operatorutils "github.com/equinor/radix-operator/pkg/apis/utils"
radixlabels "github.com/equinor/radix-operator/pkg/apis/utils/labels"
Expand Down Expand Up @@ -59,11 +60,12 @@ func buildComponent(
WithHorizontalScalingSummary(GetHpaSummary(ra.Name, radixComponent.GetName(), hpaList, scaledObjects)).
WithExternalDNS(getComponentExternalDNS(ra.Name, radixComponent, secretList, certs, certRequests, tlsValidator))

componentPods := slice.FindAll(podList, predicate.IsPodForComponent(ra.Name, radixComponent.GetName()))
var kd *appsv1.Deployment
if depl, ok := slice.FindFirst(deploymentList, predicate.IsDeploymentForComponent(ra.Name, radixComponent.GetName())); ok {
kd = &depl
}
componentPods := append(slice.FindAll(podList, predicate.IsPodForComponent(ra.Name, radixComponent.GetName())),
slice.FindAll(podList, predicate.IsPodForAuxComponent(ra.Name, radixComponent.GetName(), kube.RadixJobTypeManagerAux))...)

if rd.Status.ActiveTo.IsZero() {
builder.WithPodNames(slice.Map(componentPods, func(pod corev1.Pod) string { return pod.Name }))
Expand Down

0 comments on commit f64469a

Please sign in to comment.