Skip to content

Commit

Permalink
Updated refs, fixed unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satr committed Dec 19, 2023
1 parent 939a04f commit 50e7f54
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ Dockerfile.dev

# binary created by goland with default settings
main
swaggerui/statik.go
23 changes: 23 additions & 0 deletions api/privateimagehubs/internal/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/equinor/radix-operator/pkg/apis/kube"
operatorutils "github.com/equinor/radix-operator/pkg/apis/utils"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
)

// UpdatePrivateImageHubsSecretsPassword update secret password
Expand All @@ -34,3 +35,25 @@ func UpdatePrivateImageHubsSecretsPassword(kubeUtil *kube.Kube, appName, server,
}
return fmt.Errorf("private image hub secret does not contain config for server %s", server)
}

// GetPendingPrivateImageHubSecrets returns a list of private image hubs where secret value is not set
func GetPendingPrivateImageHubSecrets(kubeUtil *kube.Kube, appName string) ([]string, error) {
pendingSecrets := []string{}
ns := operatorutils.GetAppNamespace(appName)
secret, err := kubeUtil.GetSecret(ns, defaults.PrivateImageHubSecretName)
if err != nil && !errors.IsNotFound(err) {
return nil, err
}

imageHubs, err := applicationconfig.GetImageHubSecretValue(secret.Data[corev1.DockerConfigJsonKey])
if err != nil {
return nil, err
}

for key, imageHub := range imageHubs {
if imageHub.Password == "" {
pendingSecrets = append(pendingSecrets, key)
}
}
return pendingSecrets, nil
}
29 changes: 1 addition & 28 deletions api/privateimagehubs/privateimagehubs_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import (
"github.com/equinor/radix-api/api/privateimagehubs/models"
"github.com/equinor/radix-api/api/utils"
sharedModels "github.com/equinor/radix-api/models"
"github.com/equinor/radix-operator/pkg/apis/applicationconfig"
"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"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
)

// PrivateImageHubHandler Instance variables
Expand All @@ -39,7 +34,7 @@ func (ph PrivateImageHubHandler) GetPrivateImageHubs(ctx context.Context, appNam
if err != nil {
return []models.ImageHubSecret{}, nil
}
pendingImageHubSecrets, err := getPendingPrivateImageHubSecrets(ph.kubeUtil, appName)
pendingImageHubSecrets, err := internal.GetPendingPrivateImageHubSecrets(ph.kubeUtil, appName)
if err != nil {
return nil, err
}
Expand All @@ -62,28 +57,6 @@ func (ph PrivateImageHubHandler) UpdatePrivateImageHubValue(appName, server, pas
return internal.UpdatePrivateImageHubsSecretsPassword(ph.kubeUtil, appName, server, password)
}

// getPendingPrivateImageHubSecrets returns a list of private image hubs where secret value is not set
func getPendingPrivateImageHubSecrets(kubeUtil *kube.Kube, appName string) ([]string, error) {
pendingSecrets := []string{}
ns := operatorutils.GetAppNamespace(appName)
secret, err := kubeUtil.GetSecret(ns, defaults.PrivateImageHubSecretName)
if err != nil && !errors.IsNotFound(err) {
return nil, err
}

imageHubs, err := applicationconfig.GetImageHubSecretValue(secret.Data[corev1.DockerConfigJsonKey])
if err != nil {
return nil, err
}

for key, imageHub := range imageHubs {
if imageHub.Password == "" {
pendingSecrets = append(pendingSecrets, key)
}
}
return pendingSecrets, nil
}

func getImageHubSecretStatus(pendingImageHubSecrets []string, server string) models.ImageHubSecretStatus {
for _, val := range pendingImageHubSecrets {
if val == server {
Expand Down
36 changes: 21 additions & 15 deletions api/privateimagehubs/privateimagehubs_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package privateimagehubs
package privateimagehubs_test

import (
"context"
Expand Down Expand Up @@ -27,8 +27,9 @@ const (
)

func Test_WithPrivateImageHubSet_SecretsCorrectly_NoImageHubs(t *testing.T) {
client, _, kubeUtil := applyRadixAppWithPrivateImageHub(radixv1.PrivateImageHubEntries{})
pendingSecrets, _ := applicationconfig.GetPendingPrivateImageHubSecrets(kubeUtil, "any-app")
client, _, kubeUtil, err := applyRadixAppWithPrivateImageHub(radixv1.PrivateImageHubEntries{})
require.NoError(t, err)
pendingSecrets, _ := internal.GetPendingPrivateImageHubSecrets(kubeUtil, "any-app")

secret, _ := client.CoreV1().Secrets("any-app-app").Get(context.TODO(), defaults.PrivateImageHubSecretName, metav1.GetOptions{})

Expand All @@ -41,53 +42,58 @@ func Test_WithPrivateImageHubSet_SecretsCorrectly_NoImageHubs(t *testing.T) {
}

func Test_WithPrivateImageHubSet_SecretsCorrectly_SetPassword(t *testing.T) {
client, _, kubeUtil := applyRadixAppWithPrivateImageHub(radixv1.PrivateImageHubEntries{
client, _, kubeUtil, err := applyRadixAppWithPrivateImageHub(radixv1.PrivateImageHubEntries{
"privaterepodeleteme.azurecr.io": &radixv1.RadixPrivateImageHubCredential{
Username: "814607e6-3d71-44a7-8476-50e8b281abbc",
Email: "radix@equinor.com",
},
})
pendingSecrets, _ := applicationconfig.GetPendingPrivateImageHubSecrets(kubeUtil, "any-app")
require.NoError(t, err)
pendingSecrets, _ := internal.GetPendingPrivateImageHubSecrets(kubeUtil, "any-app")

assert.Equal(t, "privaterepodeleteme.azurecr.io", pendingSecrets[0])

if err := internal.UpdatePrivateImageHubsSecretsPassword(kubeUtil, "any-app", "privaterepodeleteme.azurecr.io", "a-password"); err != nil {
require.NoError(t, err)
}
secret, _ := client.CoreV1().Secrets("any-app-app").Get(context.TODO(), defaults.PrivateImageHubSecretName, metav1.GetOptions{})
pendingSecrets, _ = applicationconfig.GetPendingPrivateImageHubSecrets(kubeUtil, "any-app")
pendingSecrets, _ = internal.GetPendingPrivateImageHubSecrets(kubeUtil, "any-app")

assert.Equal(t,
"{\"auths\":{\"privaterepodeleteme.azurecr.io\":{\"username\":\"814607e6-3d71-44a7-8476-50e8b281abbc\",\"password\":\"a-password\",\"email\":\"radix@equinor.com\",\"auth\":\"ODE0NjA3ZTYtM2Q3MS00NGE3LTg0NzYtNTBlOGIyODFhYmJjOmEtcGFzc3dvcmQ=\"}}}",
string(secret.Data[corev1.DockerConfigJsonKey]))
assert.Equal(t, 0, len(pendingSecrets))
}

func applyRadixAppWithPrivateImageHub(privateImageHubs radixv1.PrivateImageHubEntries) (kubernetes.Interface, *applicationconfig.ApplicationConfig, *kube.Kube) {
tu, client, kubeUtil, radixClient := setupTest()
func applyRadixAppWithPrivateImageHub(privateImageHubs radixv1.PrivateImageHubEntries) (kubernetes.Interface, *applicationconfig.ApplicationConfig, *kube.Kube, error) {
tu, client, kubeUtil, radixClient, err := setupTest()
if err != nil {
return nil, nil, nil, err
}
appBuilder := utils.ARadixApplication().
WithAppName("any-app").
WithEnvironment("dev", "master")
for key, config := range privateImageHubs {
appBuilder.WithPrivateImageRegistry(key, config.Username, config.Email)
}

err := applyApplicationWithSync(tu, client, kubeUtil, radixClient, appBuilder)
if err != nil {
return nil, nil, nil
if err := applyApplicationWithSync(tu, client, kubeUtil, radixClient, appBuilder); err != nil {
return nil, nil, nil, err
}
appConfig := getAppConfig(client, kubeUtil, radixClient, appBuilder)
return client, appConfig, kubeUtil
return client, appConfig, kubeUtil, nil
}

func setupTest() (*test.Utils, kubernetes.Interface, *kube.Kube, radixclient.Interface) {
func setupTest() (*test.Utils, kubernetes.Interface, *kube.Kube, radixclient.Interface, error) {
kubeClient := kubefake.NewSimpleClientset()
radixClient := radixfake.NewSimpleClientset()
secretproviderclient := secretproviderfake.NewSimpleClientset()
kubeUtil, _ := kube.New(kubeClient, radixClient, secretproviderclient)
handlerTestUtils := test.NewTestUtils(kubeClient, radixClient, secretproviderclient)
handlerTestUtils.CreateClusterPrerequisites(clusterName, "0.0.0.0", "anysubid")
return &handlerTestUtils, kubeClient, kubeUtil, radixClient
if err := handlerTestUtils.CreateClusterPrerequisites(clusterName, "0.0.0.0", "anysubid"); err != nil {
return nil, nil, nil, nil, err
}
return &handlerTestUtils, kubeClient, kubeUtil, radixClient, nil
}

func applyApplicationWithSync(tu *test.Utils, client kubernetes.Interface, kubeUtil *kube.Kube,
Expand Down
19 changes: 3 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/equinor/radix-api
go 1.21

require (
github.com/equinor/radix-common v1.6.2
github.com/equinor/radix-common v1.7.0
github.com/equinor/radix-job-scheduler v1.8.4
github.com/equinor/radix-operator v1.99.1000-0.20231212112235-911f68b1106d
github.com/equinor/radix-operator v1.47.0
github.com/evanphx/json-patch/v5 v5.6.0
github.com/go-swagger/go-swagger v0.30.5
github.com/golang-jwt/jwt/v4 v4.5.0
Expand All @@ -16,6 +16,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/prometheus-operator/prometheus-operator/pkg/client v0.64.1
github.com/prometheus/client_golang v1.16.0
github.com/rakyll/statik v0.1.7
github.com/rs/cors v1.8.3
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
Expand All @@ -35,15 +36,9 @@ require (
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.0 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/vcs v1.13.3 // indirect
github.com/alecthomas/gometalinter v3.0.0+incompatible // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blendle/zapdriver v1.3.1 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -58,22 +53,19 @@ require (
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/dep v0.5.4 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/cel-go v0.12.7 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-containerregistry v0.16.1 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jmank88/nuts v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand All @@ -82,10 +74,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/nightlyone/lockfile v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -94,7 +83,6 @@ require (
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/prometheus/statsd_exporter v0.21.0 // indirect
github.com/sdboyer/constext v0.0.0-20170321163424-836a14457353 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -120,7 +108,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 50e7f54

Please sign in to comment.