Skip to content

Commit

Permalink
First phase of fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mvshao committed Aug 23, 2024
1 parent c923e98 commit b348fda
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 49 deletions.
16 changes: 8 additions & 8 deletions internal/auditlogging/auditlogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (

//go:generate mockery --name=AuditLogging
type AuditLogging interface {
Enable(ctx context.Context, shoot *gardener.Shoot) error
Enable(ctx context.Context, shoot *gardener.Shoot) (bool, error)
}

//go:generate mockery --name=auditLogConfigurator
Expand Down Expand Up @@ -102,41 +102,41 @@ func (a *auditLogConfig) getSeedObj(ctx context.Context, seedKey types.Namespace
return seed, nil
}

func (al *AuditLog) Enable(ctx context.Context, shoot *gardener.Shoot) error {
func (al *AuditLog) Enable(ctx context.Context, shoot *gardener.Shoot) (bool, error) {
log := al.getLogInstance()
seedName := getSeedName(*shoot)

if !al.canEnableAuditLogsForShoot(seedName) {
log.Info("Seed name or Tenant config path is empty while configuring Audit Logs on shoot: " + shoot.Name)
return nil
return false, nil
}

auditConfigFromFile, err := al.getConfigFromFile()
if err != nil {
return errors.Wrap(err, "Cannot get Audit Log config from file")
return false, errors.Wrap(err, "Cannot get Audit Log config from file")
}

configureAuditPolicy(shoot, al.getPolicyConfigMapName())

seedKey := types.NamespacedName{Name: seedName, Namespace: ""}
seed, err := al.getSeedObj(ctx, seedKey)
if err != nil {
return errors.Wrap(err, "Cannot get Gardener Seed object")
return false, errors.Wrap(err, "Cannot get Gardener Seed object")
}

annotated, err := enableAuditLogs(shoot, auditConfigFromFile, seed.Spec.Provider.Type)

if err != nil {
return errors.Wrap(err, "Error during enabling Audit Logs on shoot: "+shoot.Name)
return false, errors.Wrap(err, "Error during enabling Audit Logs on shoot: "+shoot.Name)
}

if annotated {
if err = al.updateShoot(ctx, shoot); err != nil {
return errors.Wrap(err, "Cannot update shoot")
return false, errors.Wrap(err, "Cannot update shoot")
}
}

return nil
return true, nil
}

func enableAuditLogs(shoot *gardener.Shoot, auditConfigFromFile map[string]map[string]AuditLogData, providerType string) (bool, error) {
Expand Down
21 changes: 12 additions & 9 deletions internal/controller/runtime/fsm/runtime_fsm_configure_auditlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
func sFnConfigureAuditLog(ctx context.Context, m *fsm, s *systemState) (stateFn, *ctrl.Result, error) {
m.log.Info("Configure Audit Log state")

err := m.AuditLogging.Enable(ctx, s.shoot)
if err != nil {
wasAuditLogEnabled, err := m.AuditLogging.Enable(ctx, s.shoot)
if err != nil && !wasAuditLogEnabled {
m.log.Error(err, "Failed to configure Audit Log")
s.instance.UpdateStatePending(
imv1.ConditionTypeRuntimeConfigured,
Expand All @@ -22,12 +22,15 @@ func sFnConfigureAuditLog(ctx context.Context, m *fsm, s *systemState) (stateFn,
return updateStatusAndRequeueAfter(gardenerRequeueDuration)
}

m.log.Info("Audit Log configured for shoot: " + s.shoot.Name)
s.instance.UpdateStateReady(
imv1.ConditionTypeRuntimeConfigured,
imv1.ConditionReasonConfigurationCompleted,
"Audit Log configured",
)
if wasAuditLogEnabled {
m.log.Info("Audit Log configured for shoot: " + s.shoot.Name)
s.instance.UpdateStateReady(
imv1.ConditionTypeRuntimeConfigured,
imv1.ConditionReasonConfigurationCompleted,
"Audit Log configured",
)

return updateStatusAndStop()
return updateStatusAndStop()
}
return requeue()
}
5 changes: 3 additions & 2 deletions internal/controller/runtime/runtime_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package runtime

import (
"context"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"time"

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
. "github.com/onsi/ginkgo/v2" //nolint:revive
. "github.com/onsi/gomega" //nolint:revive
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -34,7 +34,6 @@ var _ = Describe("Runtime Controller", func() {

Context("When reconciling a resource", func() {
const ResourceName = "test-resource"

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Expand All @@ -44,6 +43,7 @@ var _ = Describe("Runtime Controller", func() {

It("Should successfully create new Shoot from provided Runtime and set Ready status on CR", func() {
setupGardenerTestClientForProvisioning()
Expect(setupSeedObjectOnCluster(gardenerTestClient)).To(Succeed())

By("Create Runtime CR")
runtimeStub := CreateRuntimeStub(ResourceName)
Expand Down Expand Up @@ -256,6 +256,7 @@ func CreateRuntimeStub(resourceName string) *imv1.Runtime {
},
},
},
Region: "eu-central-1",
},
Security: imv1.Security{
Administrators: []string{
Expand Down
85 changes: 71 additions & 14 deletions internal/controller/runtime/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package runtime

import (
"context"
v12 "k8s.io/api/core/v1"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -100,10 +101,10 @@ var _ = BeforeSuite(func() {

// tracker will be updated with different shoot sequence for each test case
tracker := clienttesting.NewObjectTracker(clientScheme, serializer.NewCodecFactory(clientScheme).UniversalDecoder())
customTracker = NewCustomTracker(tracker, []*gardener_api.Shoot{})
customTracker = NewCustomTracker(tracker, []*gardener_api.Shoot{}, []*gardener_api.Seed{})
gardenerTestClient = fake.NewClientBuilder().WithScheme(clientScheme).WithObjectTracker(customTracker).Build()

runtimeReconciler = NewRuntimeReconciler(mgr, gardenerTestClient, logger, fsm.RCCfg{Finalizer: infrastructuremanagerv1.Finalizer})
runtimeReconciler = NewRuntimeReconciler(mgr, gardenerTestClient, logger, fsm.RCCfg{Finalizer: infrastructuremanagerv1.Finalizer, ConverterConfig: fixConverterConfigForTests()})
Expect(runtimeReconciler).NotTo(BeNil())
err = runtimeReconciler.SetupWithManager(mgr)
Expect(err).To(BeNil())
Expand Down Expand Up @@ -140,27 +141,30 @@ var _ = AfterSuite(func() {
func setupGardenerTestClientForProvisioning() {
baseShoot := getBaseShootForTestingSequence()
shoots := fixShootsSequenceForProvisioning(&baseShoot)
setupShootClientWithSequence(shoots)
seeds := fixSeedsSequence()
setupGardenerClientWithSequence(shoots, seeds)
}

func setupGardenerTestClientForUpdate() {
baseShoot := getBaseShootForTestingSequence()
shoots := fixShootsSequenceForUpdate(&baseShoot)
setupShootClientWithSequence(shoots)
seeds := fixSeedsSequence()
setupGardenerClientWithSequence(shoots, seeds)
}

func setupGardenerTestClientForDelete() {
baseShoot := getBaseShootForTestingSequence()
shoots := fixShootsSequenceForDelete(&baseShoot)
setupShootClientWithSequence(shoots)
seeds := fixSeedsSequence()
setupGardenerClientWithSequence(shoots, seeds)
}

func setupShootClientWithSequence(shoots []*gardener_api.Shoot) {
func setupGardenerClientWithSequence(shoots []*gardener_api.Shoot, seeds []*gardener_api.Seed) {
clientScheme := runtime.NewScheme()
_ = gardener_api.AddToScheme(clientScheme)

tracker := clienttesting.NewObjectTracker(clientScheme, serializer.NewCodecFactory(clientScheme).UniversalDecoder())
customTracker = NewCustomTracker(tracker, shoots)
customTracker = NewCustomTracker(tracker, shoots, seeds)
gardenerTestClient = fake.NewClientBuilder().WithScheme(clientScheme).WithObjectTracker(customTracker).Build()
runtimeReconciler.UpdateShootClient(gardenerTestClient)
}
Expand Down Expand Up @@ -195,8 +199,16 @@ func fixShootsSequenceForProvisioning(shoot *gardener_api.Shoot) []*gardener_api
},
}

processingShoot := pendingShoot.DeepCopy()
pendingShoot.Spec.SeedName = ptr.To("test-seed")

auditLogShoot := pendingShoot.DeepCopy()
auditLogShoot.Spec.Kubernetes.KubeAPIServer.AuditConfig = &gardener_api.AuditConfig{
AuditPolicy: &gardener_api.AuditPolicy{
ConfigMapRef: &v12.ObjectReference{Name: "policy-config-map"},
},
}

processingShoot := auditLogShoot.DeepCopy()
processingShoot.Status.LastOperation.State = gardener_api.LastOperationStateProcessing

readyShoot := processingShoot.DeepCopy()
Expand All @@ -205,7 +217,7 @@ func fixShootsSequenceForProvisioning(shoot *gardener_api.Shoot) []*gardener_api

// processedShoot := processingShoot.DeepCopy() // will add specific data later

return []*gardener_api.Shoot{missingShoot, missingShoot, missingShoot, initialisedShoot, dnsShoot, pendingShoot, processingShoot, readyShoot, readyShoot, readyShoot, readyShoot}
return []*gardener_api.Shoot{missingShoot, missingShoot, missingShoot, initialisedShoot, dnsShoot, auditLogShoot, auditLogShoot, processingShoot, readyShoot, readyShoot, readyShoot, readyShoot}
}

func fixShootsSequenceForUpdate(shoot *gardener_api.Shoot) []*gardener_api.Shoot {
Expand All @@ -215,14 +227,23 @@ func fixShootsSequenceForUpdate(shoot *gardener_api.Shoot) []*gardener_api.Shoot
Domain: ptr.To("test.domain"),
}

pendingShoot.Spec.SeedName = ptr.To("test-seed")

pendingShoot.Status = gardener_api.ShootStatus{
LastOperation: &gardener_api.LastOperation{
Type: gardener_api.LastOperationTypeReconcile,
State: gardener_api.LastOperationStatePending,
},
}

processingShoot := pendingShoot.DeepCopy()
auditLogShoot := pendingShoot.DeepCopy()
auditLogShoot.Spec.Kubernetes.KubeAPIServer.AuditConfig = &gardener_api.AuditConfig{
AuditPolicy: &gardener_api.AuditPolicy{
ConfigMapRef: &v12.ObjectReference{Name: "policy-config-map"},
},
}

processingShoot := auditLogShoot.DeepCopy()

processingShoot.Status.LastOperation.State = gardener_api.LastOperationStateProcessing

Expand All @@ -232,7 +253,7 @@ func fixShootsSequenceForUpdate(shoot *gardener_api.Shoot) []*gardener_api.Shoot

// processedShoot := processingShoot.DeepCopy() // will add specific data later

return []*gardener_api.Shoot{pendingShoot, processingShoot, readyShoot, readyShoot}
return []*gardener_api.Shoot{pendingShoot, pendingShoot, auditLogShoot, processingShoot, readyShoot, readyShoot}
}

func fixShootsSequenceForDelete(shoot *gardener_api.Shoot) []*gardener_api.Shoot {
Expand All @@ -242,6 +263,8 @@ func fixShootsSequenceForDelete(shoot *gardener_api.Shoot) []*gardener_api.Shoot
Domain: ptr.To("test.domain"),
}

currentShoot.Spec.SeedName = ptr.To("test-seed")

// To workaround limitation that apply patches are not supported in the fake client.
// We need to set the annotation manually. https://github.com/kubernetes/kubernetes/issues/115598
currentShoot.Annotations = map[string]string{
Expand All @@ -264,10 +287,40 @@ func fixShootsSequenceForDelete(shoot *gardener_api.Shoot) []*gardener_api.Shoot
return []*gardener_api.Shoot{currentShoot, currentShoot, currentShoot, pendingDeleteShoot, nil}
}

func fixSeedsSequence() []*gardener_api.Seed {
seed := &gardener_api.Seed{
ObjectMeta: metav1.ObjectMeta{
Name: "test-seed",
},
Spec: gardener_api.SeedSpec{
Provider: gardener_api.SeedProvider{
Type: "aws",
},
},
}

return []*gardener_api.Seed{seed}
}

func setupSeedObjectOnCluster(client client.Client) error {
seed := &gardener_api.Seed{
ObjectMeta: metav1.ObjectMeta{
Name: "test-seed",
},
Spec: gardener_api.SeedSpec{
Provider: gardener_api.SeedProvider{
Type: "aws",
},
},
}

return client.Create(context.Background(), seed)
}

func fixConverterConfigForTests() gardener_shoot.ConverterConfig {
return gardener_shoot.ConverterConfig{
Kubernetes: gardener_shoot.KubernetesConfig{
DefaultVersion: "1.29", //nolint:godox TODO: Should be parametrised
DefaultVersion: "1.29",
},

DNS: gardener_shoot.DNSConfig{
Expand All @@ -277,11 +330,15 @@ func fixConverterConfigForTests() gardener_shoot.ConverterConfig {
},
Provider: gardener_shoot.ProviderConfig{
AWS: gardener_shoot.AWSConfig{
EnableIMDSv2: true, //nolint:godox TODO: Should be parametrised
EnableIMDSv2: true,
},
},
Gardener: gardener_shoot.GardenerConfig{
ProjectName: "kyma-dev", //nolint:godox TODO: should be parametrised
ProjectName: "kyma-dev",
},
AuditLog: gardener_shoot.AuditLogConfig{
PolicyConfigMapName: "policy-config-map",
TenantConfigPath: filepath.Join("testdata", "auditConfig.json"),
},
}
}
Loading

0 comments on commit b348fda

Please sign in to comment.