Skip to content

Commit

Permalink
Update operator with keda support (#629)
Browse files Browse the repository at this point in the history
* initialize kedaClient

* fix hpa metrics

* log request errors as warnings

* insert HPA for controller test

* remove unused constant
  • Loading branch information
Richard87 authored Jun 3, 2024
1 parent b5342e1 commit 52e034f
Show file tree
Hide file tree
Showing 36 changed files with 1,000 additions and 780 deletions.
8 changes: 4 additions & 4 deletions api/alerting/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (h *handler) updateRadixAlertFromAlertingConfig(ctx context.Context, radixA
if err != nil {
return nil, err
}
if err := h.updateConfigSecret(*configSecret, &config); err != nil {
if err := h.updateConfigSecret(ctx, *configSecret, &config); err != nil {
return nil, err
}
}
Expand All @@ -152,7 +152,7 @@ func (h *handler) updateRadixAlertFromAlertingConfig(ctx context.Context, radixA
return h.applyRadixAlert(ctx, &radixAlert)
}

func (h *handler) updateConfigSecret(secret corev1.Secret, config *alertModels.UpdateAlertingConfig) error {
func (h *handler) updateConfigSecret(ctx context.Context, secret corev1.Secret, config *alertModels.UpdateAlertingConfig) error {
if secret.Data == nil {
secret.Data = make(map[string][]byte)
}
Expand All @@ -163,8 +163,8 @@ func (h *handler) updateConfigSecret(secret corev1.Secret, config *alertModels.U
}
}

kubeUtil, _ := kube.New(h.accounts.UserAccount.Client, h.accounts.UserAccount.RadixClient, h.accounts.UserAccount.SecretProviderClient)
_, err := kubeUtil.ApplySecret(h.namespace, &secret)
kubeUtil, _ := kube.New(h.accounts.UserAccount.Client, h.accounts.UserAccount.RadixClient, h.accounts.UserAccount.KedaClient, h.accounts.UserAccount.SecretProviderClient)
_, err := kubeUtil.ApplySecret(ctx, h.namespace, &secret)
return err
}

Expand Down
4 changes: 3 additions & 1 deletion api/alerting/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/equinor/radix-operator/pkg/apis/kube"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
radixfake "github.com/equinor/radix-operator/pkg/client/clientset/versioned/fake"
kedafake "github.com/kedacore/keda/v2/pkg/generated/clientset/versioned/fake"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
corev1 "k8s.io/api/core/v1"
Expand All @@ -30,9 +31,10 @@ type HandlerTestSuite struct {
func (s *HandlerTestSuite) SetupTest() {
kubeClient := kubefake.NewSimpleClientset()
radixClient := radixfake.NewSimpleClientset()
kedaClient := kedafake.NewSimpleClientset()
secretProviderClient := secretproviderfake.NewSimpleClientset()
certClient := certclientfake.NewSimpleClientset()
s.accounts = models.NewAccounts(kubeClient, radixClient, secretProviderClient, nil, certClient, kubeClient, radixClient, secretProviderClient, nil, certClient, "", radixmodels.Impersonation{})
s.accounts = models.NewAccounts(kubeClient, radixClient, kedaClient, secretProviderClient, nil, certClient, kubeClient, radixClient, kedaClient, secretProviderClient, nil, certClient, "", radixmodels.Impersonation{})
}

func TestHandlerTestSuite(t *testing.T) {
Expand Down
150 changes: 80 additions & 70 deletions api/applications/applications_controller_test.go

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions api/applications/applications_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ func (ah *ApplicationHandler) RegisterApplication(ctx context.Context, applicati
return nil, err
}

err = ah.isValidRegistrationInsert(radixRegistration)
err = ah.isValidRegistrationInsert(ctx, radixRegistration)
if err != nil {
return nil, err
}

if !applicationRegistrationRequest.AcknowledgeWarnings {
if upsertResponse, err := ah.getRegistrationInsertResponseForWarnings(radixRegistration); upsertResponse != nil || err != nil {
if upsertResponse, err := ah.getRegistrationInsertResponseForWarnings(ctx, radixRegistration); upsertResponse != nil || err != nil {
return upsertResponse, err
}
}
Expand All @@ -182,8 +182,8 @@ func (ah *ApplicationHandler) RegisterApplication(ctx context.Context, applicati
}, nil
}

func (ah *ApplicationHandler) getRegistrationInsertResponseForWarnings(radixRegistration *v1.RadixRegistration) (*applicationModels.ApplicationRegistrationUpsertResponse, error) {
warnings, err := ah.getRegistrationInsertWarnings(radixRegistration)
func (ah *ApplicationHandler) getRegistrationInsertResponseForWarnings(ctx context.Context, radixRegistration *v1.RadixRegistration) (*applicationModels.ApplicationRegistrationUpsertResponse, error) {
warnings, err := ah.getRegistrationInsertWarnings(ctx, radixRegistration)
if err != nil {
return nil, err
}
Expand All @@ -193,8 +193,8 @@ func (ah *ApplicationHandler) getRegistrationInsertResponseForWarnings(radixRegi
return nil, nil
}

func (ah *ApplicationHandler) getRegistrationUpdateResponseForWarnings(radixRegistration *v1.RadixRegistration) (*applicationModels.ApplicationRegistrationUpsertResponse, error) {
warnings, err := ah.getRegistrationUpdateWarnings(radixRegistration)
func (ah *ApplicationHandler) getRegistrationUpdateResponseForWarnings(ctx context.Context, radixRegistration *v1.RadixRegistration) (*applicationModels.ApplicationRegistrationUpsertResponse, error) {
warnings, err := ah.getRegistrationUpdateWarnings(ctx, radixRegistration)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -245,7 +245,7 @@ func (ah *ApplicationHandler) ChangeRegistrationDetails(ctx context.Context, app

needToRevalidateWarnings := updatedRegistration.Spec.CloneURL != currentRegistration.Spec.CloneURL
if needToRevalidateWarnings && !applicationRegistrationRequest.AcknowledgeWarnings {
if upsertResponse, err := ah.getRegistrationUpdateResponseForWarnings(radixRegistration); upsertResponse != nil || err != nil {
if upsertResponse, err := ah.getRegistrationUpdateResponseForWarnings(ctx, radixRegistration); upsertResponse != nil || err != nil {
return upsertResponse, err
}
}
Expand Down Expand Up @@ -348,7 +348,7 @@ func (ah *ApplicationHandler) ModifyRegistrationDetails(ctx context.Context, app

needToRevalidateWarnings := currentRegistration.Spec.CloneURL != updatedRegistration.Spec.CloneURL
if needToRevalidateWarnings && !applicationRegistrationPatchRequest.AcknowledgeWarnings {
if upsertResponse, err := ah.getRegistrationUpdateResponseForWarnings(updatedRegistration); upsertResponse != nil || err != nil {
if upsertResponse, err := ah.getRegistrationUpdateResponseForWarnings(ctx, updatedRegistration); upsertResponse != nil || err != nil {
return upsertResponse, err
}
}
Expand Down Expand Up @@ -563,18 +563,18 @@ func (ah *ApplicationHandler) triggerPipelineBuildOrBuildDeploy(ctx context.Cont
return jobSummary, nil
}

func (ah *ApplicationHandler) getRegistrationInsertWarnings(radixRegistration *v1.RadixRegistration) ([]string, error) {
return radixvalidators.GetRadixRegistrationBeInsertedWarnings(ah.getServiceAccount().RadixClient, radixRegistration)
func (ah *ApplicationHandler) getRegistrationInsertWarnings(ctx context.Context, radixRegistration *v1.RadixRegistration) ([]string, error) {
return radixvalidators.GetRadixRegistrationBeInsertedWarnings(ctx, ah.getServiceAccount().RadixClient, radixRegistration)
}

func (ah *ApplicationHandler) getRegistrationUpdateWarnings(radixRegistration *v1.RadixRegistration) ([]string, error) {
return radixvalidators.GetRadixRegistrationBeUpdatedWarnings(ah.getServiceAccount().RadixClient, radixRegistration)
func (ah *ApplicationHandler) getRegistrationUpdateWarnings(ctx context.Context, radixRegistration *v1.RadixRegistration) ([]string, error) {
return radixvalidators.GetRadixRegistrationBeUpdatedWarnings(ctx, ah.getServiceAccount().RadixClient, radixRegistration)
}

func (ah *ApplicationHandler) isValidRegistrationInsert(radixRegistration *v1.RadixRegistration) error {
func (ah *ApplicationHandler) isValidRegistrationInsert(ctx context.Context, radixRegistration *v1.RadixRegistration) error {
// Need to use in-cluster client of the API server, because the user might not have enough privileges
// to run a full validation
return radixvalidators.CanRadixRegistrationBeInserted(ah.getServiceAccount().RadixClient, radixRegistration, ah.getAdditionalRadixRegistrationInsertValidators()...)
return radixvalidators.CanRadixRegistrationBeInserted(ctx, ah.getServiceAccount().RadixClient, radixRegistration, ah.getAdditionalRadixRegistrationInsertValidators()...)
}

func (ah *ApplicationHandler) isValidRegistrationUpdate(updatedRegistration, currentRegistration *v1.RadixRegistration) error {
Expand Down
22 changes: 12 additions & 10 deletions api/buildsecrets/buildsecrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

environmentModels "github.com/equinor/radix-api/api/secrets/models"
kedafake "github.com/kedacore/keda/v2/pkg/generated/clientset/versioned/fake"
"github.com/stretchr/testify/require"
secretproviderfake "sigs.k8s.io/secrets-store-csi-driver/pkg/client/clientset/versioned/fake"

Expand All @@ -27,21 +28,22 @@ const (
subscriptionId = "12347718-c8f8-4995-bfbb-02655ff1f89c"
)

func setupTest(t *testing.T) (*commontest.Utils, *controllertest.Utils, *kubefake.Clientset, *radixfake.Clientset) {
func setupTest(t *testing.T) (*commontest.Utils, *controllertest.Utils, *kubefake.Clientset, *radixfake.Clientset, *kedafake.Clientset) {
// Setup
kubeclient := kubefake.NewSimpleClientset()
radixclient := radixfake.NewSimpleClientset()
kedaClient := kedafake.NewSimpleClientset()
secretproviderclient := secretproviderfake.NewSimpleClientset()
certClient := certclientfake.NewSimpleClientset()

// commonTestUtils is used for creating CRDs
commonTestUtils := commontest.NewTestUtils(kubeclient, radixclient, secretproviderclient)
commonTestUtils := commontest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient)
err := commonTestUtils.CreateClusterPrerequisites(clusterName, egressIps, subscriptionId)
require.NoError(t, err)
// controllerTestUtils is used for issuing HTTP request and processing responses
controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, secretproviderclient, certClient, NewBuildSecretsController())
controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient, certClient, NewBuildSecretsController())

return &commonTestUtils, &controllerTestUtils, kubeclient, radixclient
return &commonTestUtils, &controllerTestUtils, kubeclient, radixclient, kedaClient
}

func TestGetBuildSecrets_ListsAll(t *testing.T) {
Expand All @@ -50,9 +52,9 @@ func TestGetBuildSecrets_ListsAll(t *testing.T) {
anyBuildSecret3 := "secret3"

// Setup
commonTestUtils, controllerTestUtils, client, radixclient := setupTest(t)
commonTestUtils, controllerTestUtils, client, radixclient, kedaClient := setupTest(t)

err := utils.ApplyApplicationWithSync(client, radixclient, commonTestUtils,
err := utils.ApplyApplicationWithSync(client, radixclient, kedaClient, commonTestUtils,
builders.ARadixApplication().
WithAppName(anyAppName).
WithBuildSecrets(anyBuildSecret1, anyBuildSecret2))
Expand All @@ -69,7 +71,7 @@ func TestGetBuildSecrets_ListsAll(t *testing.T) {
assert.Equal(t, anyBuildSecret1, buildSecrets[0].Name)
assert.Equal(t, anyBuildSecret2, buildSecrets[1].Name)

err = utils.ApplyApplicationWithSync(client, radixclient, commonTestUtils,
err = utils.ApplyApplicationWithSync(client, radixclient, kedaClient, commonTestUtils,
builders.ARadixApplication().
WithAppName(anyAppName).
WithBuildSecrets(anyBuildSecret1, anyBuildSecret2, anyBuildSecret3))
Expand All @@ -86,7 +88,7 @@ func TestGetBuildSecrets_ListsAll(t *testing.T) {
assert.Equal(t, anyBuildSecret2, buildSecrets[1].Name)
assert.Equal(t, anyBuildSecret3, buildSecrets[2].Name)

err = utils.ApplyApplicationWithSync(client, radixclient, commonTestUtils,
err = utils.ApplyApplicationWithSync(client, radixclient, kedaClient, commonTestUtils,
builders.ARadixApplication().
WithAppName(anyAppName).
WithBuildSecrets(anyBuildSecret1, anyBuildSecret3))
Expand All @@ -107,9 +109,9 @@ func TestUpdateBuildSecret_UpdatedOk(t *testing.T) {
anyBuildSecret1 := "secret1"

// Setup
commonTestUtils, controllerTestUtils, client, radixclient := setupTest(t)
commonTestUtils, controllerTestUtils, client, radixclient, kedaClient := setupTest(t)

err := utils.ApplyApplicationWithSync(client, radixclient, commonTestUtils,
err := utils.ApplyApplicationWithSync(client, radixclient, kedaClient, commonTestUtils,
builders.ARadixApplication().
WithAppName(anyAppName).
WithBuildSecrets(anyBuildSecret1))
Expand Down
20 changes: 11 additions & 9 deletions api/buildstatus/build_status_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

kedafake "github.com/kedacore/keda/v2/pkg/generated/clientset/versioned/fake"
"github.com/stretchr/testify/require"
secretproviderfake "sigs.k8s.io/secrets-store-csi-driver/pkg/client/clientset/versioned/fake"

Expand All @@ -29,24 +30,25 @@ const (
subscriptionId = "12347718-c8f8-4995-bfbb-02655ff1f89c"
)

func setupTest(t *testing.T) (*commontest.Utils, *kubefake.Clientset, *radixfake.Clientset, *secretproviderfake.Clientset, *certclientfake.Clientset) {
func setupTest(t *testing.T) (*commontest.Utils, *kubefake.Clientset, *radixfake.Clientset, *kedafake.Clientset, *secretproviderfake.Clientset, *certclientfake.Clientset) {
// Setup
kubeclient := kubefake.NewSimpleClientset()
radixclient := radixfake.NewSimpleClientset()
kedaClient := kedafake.NewSimpleClientset()
secretproviderclient := secretproviderfake.NewSimpleClientset()
certClient := certclientfake.NewSimpleClientset()

// commonTestUtils is used for creating CRDs
commonTestUtils := commontest.NewTestUtils(kubeclient, radixclient, secretproviderclient)
commonTestUtils := commontest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient)
err := commonTestUtils.CreateClusterPrerequisites(clusterName, egressIps, subscriptionId)
require.NoError(t, err)
_ = os.Setenv(defaults.ActiveClusternameEnvironmentVariable, clusterName)

return &commonTestUtils, kubeclient, radixclient, secretproviderclient, certClient
return &commonTestUtils, kubeclient, radixclient, kedaClient, secretproviderclient, certClient
}

func TestGetBuildStatus(t *testing.T) {
commonTestUtils, kubeclient, radixclient, secretproviderclient, certClient := setupTest(t)
commonTestUtils, kubeclient, radixclient, kedaClient, secretproviderclient, certClient := setupTest(t)

jobStartReferenceTime := time.Date(2020, 1, 10, 0, 0, 0, 0, time.UTC)
_, err := commonTestUtils.ApplyRegistration(builders.ARadixRegistration())
Expand Down Expand Up @@ -103,7 +105,7 @@ func TestGetBuildStatus(t *testing.T) {
Return(expected, nil).
Times(1)

controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))
controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))

responseChannel := controllerTestUtils.ExecuteUnAuthorizedRequest("GET", "/api/v1/applications/my-app/environments/test/buildstatus")
response := <-responseChannel
Expand Down Expand Up @@ -132,7 +134,7 @@ func TestGetBuildStatus(t *testing.T) {
return nil, nil
})

controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))
controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))

responseChannel := controllerTestUtils.ExecuteUnAuthorizedRequest("GET", "/api/v1/applications/my-app/environments/test/buildstatus")
response := <-responseChannel
Expand Down Expand Up @@ -160,7 +162,7 @@ func TestGetBuildStatus(t *testing.T) {
return nil, nil
})

controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))
controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))

responseChannel := controllerTestUtils.ExecuteUnAuthorizedRequest("GET", "/api/v1/applications/my-app/environments/test/buildstatus?pipeline=deploy")
response := <-responseChannel
Expand Down Expand Up @@ -188,7 +190,7 @@ func TestGetBuildStatus(t *testing.T) {
return nil, nil
})

controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))
controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))

responseChannel := controllerTestUtils.ExecuteUnAuthorizedRequest("GET", "/api/v1/applications/my-app/environments/test/buildstatus?pipeline=promote")
response := <-responseChannel
Expand All @@ -210,7 +212,7 @@ func TestGetBuildStatus(t *testing.T) {
Return(nil, errors.New("error")).
Times(1)

controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))
controllerTestUtils := controllertest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient, certClient, NewBuildStatusController(fakeBuildStatus))

responseChannel := controllerTestUtils.ExecuteUnAuthorizedRequest("GET", "/api/v1/applications/my-app/environments/test/buildstatus")
response := <-responseChannel
Expand Down
Loading

0 comments on commit 52e034f

Please sign in to comment.