Skip to content

Commit

Permalink
Extensions matcher used plus some fixes for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
akgalwas committed Aug 12, 2024
1 parent 67ba2e0 commit 1c84bff
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 119 deletions.
22 changes: 11 additions & 11 deletions api/v1/runtime_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,17 @@ type RuntimeStatus struct {
}

type RuntimeShoot struct {
Name string `json:"name"`
Purpose gardener.ShootPurpose `json:"purpose"`
PlatformRegion string `json:"platformRegion"`
Region string `json:"region"`
LicenceType *string `json:"licenceType,omitempty"`
SecretBindingName string `json:"secretBindingName"`
EnforceSeedLocation *bool `json:"enforceSeedLocation,omitempty"`
Kubernetes Kubernetes `json:"kubernetes,omitempty"`
Provider Provider `json:"provider"`
Networking Networking `json:"networking"`
ControlPlane gardener.ControlPlane `json:"controlPlane"`
Name string `json:"name"`
Purpose gardener.ShootPurpose `json:"purpose"`
PlatformRegion string `json:"platformRegion"`
Region string `json:"region"`
LicenceType *string `json:"licenceType,omitempty"`
SecretBindingName string `json:"secretBindingName"`
EnforceSeedLocation *bool `json:"enforceSeedLocation,omitempty"`
Kubernetes Kubernetes `json:"kubernetes,omitempty"`
Provider Provider `json:"provider"`
Networking Networking `json:"networking"`
ControlPlane *gardener.ControlPlane `json:"controlPlane"`
}

type Kubernetes struct {
Expand Down
6 changes: 5 additions & 1 deletion api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions hack/runtime-migrator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ func getShootList(ctx context.Context, cfg migrator.Config, gardenerNamespace st
return list
}

func getControlPlane(shoot v1beta1.Shoot) v1beta1.ControlPlane {
func getControlPlane(shoot v1beta1.Shoot) *v1beta1.ControlPlane {
if shoot.Spec.ControlPlane != nil {
if shoot.Spec.ControlPlane.HighAvailability != nil {
return v1beta1.ControlPlane{HighAvailability: &v1beta1.HighAvailability{
return &v1beta1.ControlPlane{HighAvailability: &v1beta1.HighAvailability{
FailureTolerance: v1beta1.FailureTolerance{
Type: shoot.Spec.ControlPlane.HighAvailability.FailureTolerance.Type,
},
Expand All @@ -286,7 +286,7 @@ func getControlPlane(shoot v1beta1.Shoot) v1beta1.ControlPlane {
}
}

return v1beta1.ControlPlane{}
return nil
}

func getAdministratorsList(ctx context.Context, provider kubeconfig.Provider, shootName string) []string {
Expand Down
74 changes: 0 additions & 74 deletions hack/shoot-comparator/pkg/shoot/etensionmatcher_test.go

This file was deleted.

56 changes: 28 additions & 28 deletions hack/shoot-comparator/pkg/shoot/extensionmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/onsi/gomega"
"github.com/onsi/gomega/types"
"github.com/pkg/errors"
"reflect"
"strings"
)
Expand All @@ -23,53 +22,58 @@ func NewExtensionMatcher(i interface{}) types.GomegaMatcher {

func (m *ExtensionMatcher) Match(actual interface{}) (success bool, err error) {

aExtensions, err := getExtension(actual)
if err != nil {
return false, err
}

if actual == nil && m.toMatch != nil {
return false, errors.New("actual is nil")
eExtensions, err := getExtension(m.toMatch)
if err != nil {
return false, err
}

if actual != nil && m.toMatch == nil {
return false, errors.New("expected is nil")
if len(aExtensions) != len(eExtensions) {
m.fails = append(m.fails, "Extensions count mismatch")
return false, nil
}

if actual == nil && m.toMatch == nil {
if len(aExtensions) == 0 && len(eExtensions) == 0 {
return true, nil
}

aExtensions, err := getExtension(actual)
findExtension := func(extensions []v1beta1.Extension, extensionToFind string) v1beta1.Extension {
for _, extension := range extensions {
if extension.Type == extensionToFind {
return extension
findExtension := func(name string, extensions []v1beta1.Extension) v1beta1.Extension {
for _, e := range extensions {
if e.Type == name {
return e
}
}

return v1beta1.Extension{}
}

eExtensions, err := getExtension(m.toMatch)
if err != nil {
return false, err
}
differenceFound := false

for _, e := range eExtensions {
a := findExtension(e.Type, aExtensions)
if a.Type == "" {
m.fails = append(m.fails, fmt.Sprintf("Extension %s not found in both expected and actual", e.Type))
return false, nil
}

for i, eExtension := range eExtensions {
aExtension := findExtension(aExtensions, eExtension.Type)
matcher := gomega.BeComparableTo(eExtension.Type)
ok, err := matcher.Match(aExtension.Type)
matcher := gomega.BeComparableTo(e)

ok, err := matcher.Match(a)
if err != nil {
return false, err
}

if !ok {
msg := fmt.Sprintf("spec/Extensions[%d]: %s", i, matcher.FailureMessage(aExtension))
m.fails = append(m.fails, msg)
differenceFound = true
m.fails = append(m.fails, matcher.FailureMessage(a))
}

}

return false, nil
return !differenceFound, nil
}

func (m *ExtensionMatcher) NegatedFailureMessage(_ interface{}) string {
Expand All @@ -80,11 +84,7 @@ func (m *ExtensionMatcher) FailureMessage(_ interface{}) string {
return strings.Join(m.fails, "\n")
}

func getExtension(i interface{}) (shoot []v1beta1.Extension, err error) {
if i == nil {
return []v1beta1.Extension{}, fmt.Errorf("invalid value nil")
}

func getExtension(i interface{}) ([]v1beta1.Extension, error) {
switch v := i.(type) {
case []v1beta1.Extension:
return v, nil
Expand Down
106 changes: 106 additions & 0 deletions hack/shoot-comparator/pkg/shoot/extensionmatcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package shoot

import (
"fmt"
"github.com/gardener/gardener/pkg/apis/core/v1beta1"
. "github.com/onsi/ginkgo/v2" //nolint:revive
. "github.com/onsi/gomega" //nolint:revive
"k8s.io/apimachinery/pkg/runtime"
)

var _ = Describe(":: extension matcher :: ", func() {
emptyExt := make([]v1beta1.Extension, 0)

DescribeTable(
"checking invalid args :: ",
testExtensionInvalidArgs,
Entry("when actual is not of Extension type", v1beta1.Shoot{}, emptyExt),
Entry("when expected is not of Extension type", emptyExt, v1beta1.Shoot{}),
Entry("when actual is nil", nil, emptyExt),
Entry("when expected is nil", emptyExt, nil),
)

dnsExtEnabled := getDNSExtension(false, true)
dnsExtDisabled := getDNSExtension(true, false)
networkingExtDisabled := getNetworkingExtension(true)
certificateExtEnabled := getCertificateExtension(false, true)

DescribeTable(
"checking results :: ",
testExtensionResults,
Entry(
"should match empty values",
emptyExt,
emptyExt,
true,
),
Entry(
"should match identical tables",
[]v1beta1.Extension{dnsExtEnabled, networkingExtDisabled, certificateExtEnabled},
[]v1beta1.Extension{networkingExtDisabled, certificateExtEnabled, dnsExtEnabled},
true,
),
Entry(
"should detect extension differs",
[]v1beta1.Extension{dnsExtEnabled, networkingExtDisabled, certificateExtEnabled},
[]v1beta1.Extension{dnsExtDisabled, networkingExtDisabled, certificateExtEnabled},
false,
),
Entry(
"should detect missing extension",
[]v1beta1.Extension{dnsExtEnabled, networkingExtDisabled, certificateExtEnabled},
[]v1beta1.Extension{networkingExtDisabled, certificateExtEnabled},
false,
),
Entry(
"should detect redundant extension",
[]v1beta1.Extension{networkingExtDisabled, certificateExtEnabled},
[]v1beta1.Extension{dnsExtEnabled, networkingExtDisabled, certificateExtEnabled},
false,
),
)
})

func testExtensionInvalidArgs(actual, expected interface{}) {
matcher := NewExtensionMatcher(expected)
_, err := matcher.Match(actual)
Expect(err).To(HaveOccurred())
}

func testExtensionResults(actual, expected interface{}, expectedMatch bool) {
matcher := NewExtensionMatcher(expected)
actualMatch, err := matcher.Match(actual)
Expect(err).ShouldNot(HaveOccurred())
Expect(actualMatch).Should(Equal(expectedMatch), matcher.FailureMessage(actual))
}

func getDNSExtension(disabled bool, replicationEnabled bool) v1beta1.Extension {
json := fmt.Sprintf(`{apiVersion: "service.dns.extensions.gardener.cloud/v1alpha1", kind: "DNSConfig", dnsProviderReplication: {enabled: %v}}`, replicationEnabled)

return v1beta1.Extension{
Type: "shoot-dns-service",
Disabled: &disabled,
ProviderConfig: &runtime.RawExtension{
Raw: []byte(json),
},
}
}

func getNetworkingExtension(disabled bool) v1beta1.Extension {
return v1beta1.Extension{
Type: "shoot-networking-service",
Disabled: &disabled,
}
}

func getCertificateExtension(disabled bool, shootIssuersEnabled bool) v1beta1.Extension {
json := fmt.Sprintf(`{apiVersion: "service.cert.extensions.gardener.cloud/v1alpha1", kind: "CertConfig", shootIssuers: {enabled: %v}}`, shootIssuersEnabled)

return v1beta1.Extension{
Type: "shoot-cert-service",
Disabled: &disabled,
ProviderConfig: &runtime.RawExtension{
Raw: []byte(json),
},
}
}
2 changes: 1 addition & 1 deletion hack/shoot-comparator/pkg/shoot/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (m *Matcher) Match(actual interface{}) (success bool, err error) {
path: "spec/DNS",
},
{
GomegaMatcher: gomega.BeComparableTo(eShoot.Spec.Extensions),
GomegaMatcher: NewExtensionMatcher(eShoot.Spec.Extensions),
expected: aShoot.Spec.Extensions,
path: "spec/Extensions",
},
Expand Down
2 changes: 1 addition & 1 deletion internal/gardener/shoot/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c Converter) ToShoot(runtime imv1.Runtime) (gardener.Shoot, error) {
Pods: &runtime.Spec.Shoot.Networking.Pods,
Services: &runtime.Spec.Shoot.Networking.Services,
},
ControlPlane: &runtime.Spec.Shoot.ControlPlane,
ControlPlane: runtime.Spec.Shoot.ControlPlane,
},
}

Expand Down

0 comments on commit 1c84bff

Please sign in to comment.