Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ExtensionsExtender to generate auditlog extension only if auditlog data exists #562

Merged
8 changes: 3 additions & 5 deletions pkg/gardener/shoot/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ func NewConverterCreate(opts CreateOpts) Converter {
extendersForCreate = append(extendersForCreate,
extender2.NewKubernetesExtender(opts.Kubernetes.DefaultVersion, ""))

var zero auditlogs.AuditLogData
if opts.AuditLogData != zero {
if opts.AuditLogData != (auditlogs.AuditLogData{}) {
extendersForCreate = append(extendersForCreate,
auditlogs.NewAuditlogExtenderForCreate(
opts.AuditLog.PolicyConfigMapName,
Expand Down Expand Up @@ -101,10 +100,9 @@ func NewConverterPatch(opts PatchOpts) Converter {

extendersForPatch = append(extendersForPatch, extender2.NewKubernetesExtender(opts.Kubernetes.DefaultVersion, opts.ShootK8SVersion))

var zero auditlogs.AuditLogData
if opts.AuditLogData != zero {
if opts.AuditLogData != (auditlogs.AuditLogData{}) {
extendersForPatch = append(extendersForPatch,
auditlogs.NewAuditlogExtenderForPatch(opts.AuditLog.PolicyConfigMapName))
auditlogs.NewAuditlogExtenderForPatch(opts.ConverterConfig.AuditLog.PolicyConfigMapName))
}

return newConverter(opts.ConverterConfig, extendersForPatch...)
Expand Down
115 changes: 106 additions & 9 deletions pkg/gardener/shoot/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package shoot

import (
"fmt"
"github.com/kyma-project/infrastructure-manager/pkg/gardener/shoot/extender/auditlogs"
"github.com/kyma-project/infrastructure-manager/pkg/gardener/shoot/extender/extensions"
"io"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
"strings"
"testing"

Expand All @@ -17,11 +21,44 @@ import (
)

func TestConverter(t *testing.T) {
t.Run("Create shoot from Runtime", func(t *testing.T) {
t.Run("Create shoot from Runtime with valid Auditlog Configuration", func(t *testing.T) {
// given
runtime := fixRuntime()
converterConfig := fixConverterConfig()
converter := NewConverterCreate(CreateOpts{ConverterConfig: converterConfig})
auditLogData := auditlogs.AuditLogData{
TenantID: "test-auditlog-tenant",
ServiceURL: "test-auditlog-service-url",
SecretName: "doesnt matter",
}
converter := NewConverterCreate(CreateOpts{
ConverterConfig: converterConfig,
AuditLogData: auditLogData,
})

// when
shoot, err := converter.ToShoot(runtime)

// then
require.NoError(t, err)
assertShootFields(t, runtime, shoot)
assert.Equal(t, "1.28", shoot.Spec.Kubernetes.Version)
assert.Equal(t, "gardenlinux", shoot.Spec.Provider.Workers[0].Machine.Image.Name)
assert.Equal(t, "1591.1.0", *shoot.Spec.Provider.Workers[0].Machine.Image.Version)

extensionLen := len(shoot.Spec.Extensions)
require.Equalf(t, 5, extensionLen, "unexpected number of extensions: %d, expected: 5", extensionLen)
})

t.Run("Create shoot from Runtime with empty Auditlog Configuration", func(t *testing.T) {
// given
runtime := fixRuntime()
converterConfig := fixConverterConfig()
emptyAuditLogData := auditlogs.AuditLogData{}

converter := NewConverterCreate(CreateOpts{
ConverterConfig: converterConfig,
AuditLogData: emptyAuditLogData,
})

// when
shoot, err := converter.ToShoot(runtime)
Expand All @@ -32,13 +69,24 @@ func TestConverter(t *testing.T) {
assert.Equal(t, "1.28", shoot.Spec.Kubernetes.Version)
assert.Equal(t, "gardenlinux", shoot.Spec.Provider.Workers[0].Machine.Image.Name)
assert.Equal(t, "1591.1.0", *shoot.Spec.Provider.Workers[0].Machine.Image.Version)

extensionLen := len(shoot.Spec.Extensions)
require.Equalf(t, 4, extensionLen, "unexpected number of extensions: %d, expected: 4", extensionLen)
})

t.Run("Create shoot with default converter config versions", func(t *testing.T) {
// given
runtime := fixRuntimeWithNoVersionsSpecified()
converterConfig := fixConverterConfig()
converter := NewConverterCreate(CreateOpts{ConverterConfig: converterConfig})
auditLogData := auditlogs.AuditLogData{
TenantID: "test-auditlog-tenant",
ServiceURL: "test-auditlog-service-url",
SecretName: "doesnt matter",
}
converter := NewConverterCreate(CreateOpts{
ConverterConfig: converterConfig,
AuditLogData: auditLogData,
})

// when
shoot, err := converter.ToShoot(runtime)
Expand All @@ -49,18 +97,30 @@ func TestConverter(t *testing.T) {
assert.Equal(t, "1.29", shoot.Spec.Kubernetes.Version)
assert.Equal(t, "gardenlinux", shoot.Spec.Provider.Workers[0].Machine.Image.Name)
assert.Equal(t, "1592.1.0", *shoot.Spec.Provider.Workers[0].Machine.Image.Version)

extensionLen := len(shoot.Spec.Extensions)
require.Equalf(t, 5, extensionLen, "unexpected number of extensions: %d, expected: 5", extensionLen)
})

t.Run("Create shoot from Runtime for existing shoot and keep versions", func(t *testing.T) {
// given
runtime := fixRuntime()
converterConfig := fixConverterConfig()

auditLogData := auditlogs.AuditLogData{
TenantID: "test-auditlog-tenant",
ServiceURL: "test-auditlog-service-url",
SecretName: "doesnt matter",
}

converter := NewConverterPatch(PatchOpts{
ConverterConfig: converterConfig,
Zones: fixReversedZones(),
ShootK8SVersion: "1.30",
ShootImageName: "gardenlinux",
ShootImageVersion: "1592.2.0",
Extensions: fixAllExtensionsOnTheShoot(),
AuditLogData: auditLogData,
})

// when
Expand All @@ -76,30 +136,33 @@ func TestConverter(t *testing.T) {
"eu-central-1a",
}
assert.Equal(t, expectedZonesAreInSameOrder, shoot.Spec.Provider.Workers[0].Zones)
assert.Equal(t, expectedZonesAreInSameOrder, shoot.Spec.Provider.Workers[0].Zones)
assert.Equal(t, "1.30", shoot.Spec.Kubernetes.Version)
assert.Equal(t, "gardenlinux", shoot.Spec.Provider.Workers[0].Machine.Image.Name)
assert.Equal(t, "1592.2.0", *shoot.Spec.Provider.Workers[0].Machine.Image.Version)
assert.Nil(t, shoot.Spec.DNS)

extensionLen := len(shoot.Spec.Extensions)
require.Equalf(t, extensionLen, 2, "unexpected number of extensions: %d, expected: 2", extensionLen)
// consider switchin to NotElementsMatch, whem released https://github.com/Antonboom/testifylint/issues/99
for _, extension := range shoot.Spec.Extensions {
assert.NotEqual(t, "shoot-dns-service", extension.Type, "unexpected immutable field extension: 'shoot-dns-service'")
}
require.Equalf(t, extensionLen, 5, "unexpected number of extensions: %d, expected: 5", extensionLen)
})

t.Run("Create shoot from Runtime for existing shoot and update versions", func(t *testing.T) {
// given
runtime := fixRuntime()
converterConfig := fixConverterConfig()
auditLogData := auditlogs.AuditLogData{
TenantID: "test-auditlog-tenant",
ServiceURL: "test-auditlog-service-url",
SecretName: "doesnt matter",
}

converter := NewConverterPatch(PatchOpts{
ConverterConfig: converterConfig,
Zones: fixReversedZones(),
ShootK8SVersion: "1.27",
ShootImageName: "gardenlinux",
ShootImageVersion: "1591.0.0",
Extensions: fixAllExtensionsOnTheShoot(),
AuditLogData: auditLogData,
})

// when
Expand All @@ -119,6 +182,9 @@ func TestConverter(t *testing.T) {
assert.Equal(t, "1.28", shoot.Spec.Kubernetes.Version)
assert.Equal(t, "gardenlinux", shoot.Spec.Provider.Workers[0].Machine.Image.Name)
assert.Equal(t, "1591.1.0", *shoot.Spec.Provider.Workers[0].Machine.Image.Version)

extensionLen := len(shoot.Spec.Extensions)
require.Equalf(t, extensionLen, 5, "unexpected number of extensions: %d, expected: 5", extensionLen)
})
}

Expand Down Expand Up @@ -166,6 +232,37 @@ func fixConverterConfig() config.ConverterConfig {
}
}

func fixAllExtensionsOnTheShoot() []gardener.Extension {
return []gardener.Extension{
{
Type: extensions.AuditlogExtensionType,
ProviderConfig: &runtime.RawExtension{
Raw: []byte(`{"apiVersion":"service.auditlog.extensions.gardener.cloud/v1alpha1","kind":"AuditlogConfig","type":"standard","tenantID":"test-auditlog-tenant","serviceURL":"test-auditlog-service-url","secretReferenceName":"auditlog-credentials"}`),
},
},
{
Type: extensions.DNSExtensionType,
ProviderConfig: &runtime.RawExtension{
Raw: []byte(`{"apiVersion":"service.dns.extensions.gardener.cloud/v1alpha1","dnsProviderReplication":{"enabled":true},"syncProvidersFromShootSpecDNS":true,"providers":[{"domains":{"include":["test-shoot-name.test-domain"],"exclude":null},"secretName":"test-dns-secret","type":"test-provider"}],"kind":"DNSConfig"}`),
},
},
{
Type: extensions.CertExtensionType,
ProviderConfig: &runtime.RawExtension{
Raw: []byte(`{"apiVersion":"service.cert.extensions.gardener.cloud/v1alpha1","kind":"CertConfig","shootIssuers":{"enabled":true}}`),
},
},
{
Type: extensions.NetworkFilterType,
Disabled: ptr.To(true),
},
{
Type: extensions.OidcExtensionType,
Disabled: ptr.To(false),
},
}
}

func fixRuntime() imv1.Runtime {
kubernetesVersion := "1.28"
clientID := "client-id"
Expand Down
12 changes: 10 additions & 2 deletions pkg/gardener/shoot/extender/extensions/extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func NewExtensionsExtenderForCreate(config config.ConverterConfig, auditLogData
{
Type: AuditlogExtensionType,
Create: func(_ imv1.Runtime, _ gardener.Shoot) (*gardener.Extension, error) {
if auditLogData == (auditlogs.AuditLogData{}) {
return nil, nil
}

return NewAuditLogExtension(auditLogData)
},
},
Expand All @@ -55,8 +59,12 @@ func NewExtensionsExtenderForCreate(config config.ConverterConfig, auditLogData
func NewExtensionsExtenderForPatch(auditLogData auditlogs.AuditLogData, extensionsOnTheShoot []gardener.Extension) func(runtime imv1.Runtime, shoot *gardener.Shoot) error {
return newExtensionsExtender([]Extension{
{
Type: AuditlogExtensionType,
Create: func(_ imv1.Runtime, shoot gardener.Shoot) (*gardener.Extension, error) {
AuditlogExtensionType,
func(_ imv1.Runtime, shoot gardener.Shoot) (*gardener.Extension, error) {
if auditLogData == (auditlogs.AuditLogData{}) {
return nil, nil
}

newAuditLogExtension, err := NewAuditLogExtension(auditLogData)
if err != nil {
return nil, err
Expand Down
Loading
Loading