From f86c881e5ab221b9b6d31a78f90a916ac77f6b87 Mon Sep 17 00:00:00 2001 From: Nuckal777 Date: Wed, 29 Jan 2025 17:37:20 +0100 Subject: [PATCH] Patch infrastruture status --- .../infrastructure/actuator_reconcile.go | 7 ++ .../infrastructure/actuator_reconcile_test.go | 15 ++-- .../infrastructure_suite_test.go | 83 ++++++++++++++++++- pkg/controller/worker/suite_test.go | 4 + 4 files changed, 102 insertions(+), 7 deletions(-) diff --git a/pkg/controller/infrastructure/actuator_reconcile.go b/pkg/controller/infrastructure/actuator_reconcile.go index 8d55ca4..cc8e9a6 100644 --- a/pkg/controller/infrastructure/actuator_reconcile.go +++ b/pkg/controller/infrastructure/actuator_reconcile.go @@ -11,6 +11,7 @@ import ( "github.com/gardener/gardener/extensions/pkg/controller" extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1" "github.com/go-logr/logr" + "sigs.k8s.io/controller-runtime/pkg/client" metalv1alpha1 "github.com/ironcore-dev/gardener-extension-provider-ironcore-metal/pkg/apis/metal/v1alpha1" ) @@ -23,6 +24,8 @@ func (a *actuator) Reconcile(ctx context.Context, log logr.Logger, infra *extens return fmt.Errorf("failed to unmarshal infrastructure config: %w", err) } + originalInfra := infra.DeepCopy() + var newNodes []string if infrastructureConfig.Networks != nil { for _, network := range infrastructureConfig.Networks { @@ -37,6 +40,10 @@ func (a *actuator) Reconcile(ctx context.Context, log logr.Logger, infra *extens infra.Status.Networking.Nodes = newNodes } + if err := a.client.Status().Patch(ctx, infra, client.MergeFrom(originalInfra)); err != nil { + return fmt.Errorf("failed to patch infrastructure status: %w", err) + } + return a.reconcile(ctx, log, infra, cluster) } diff --git a/pkg/controller/infrastructure/actuator_reconcile_test.go b/pkg/controller/infrastructure/actuator_reconcile_test.go index 9ea0caa..7c42126 100644 --- a/pkg/controller/infrastructure/actuator_reconcile_test.go +++ b/pkg/controller/infrastructure/actuator_reconcile_test.go @@ -4,7 +4,6 @@ package infrastructure import ( - "context" "encoding/json" extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller" @@ -13,6 +12,7 @@ import ( "github.com/go-logr/logr" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" metalv1alpha1 "github.com/ironcore-dev/gardener-extension-provider-ironcore-metal/pkg/apis/metal/v1alpha1" @@ -25,15 +25,13 @@ var ( var _ = Describe("Actuator Reconcile", func() { var ( - ctx context.Context log logr.Logger infra *extensionsv1alpha1.Infrastructure cluster *extensionscontroller.Cluster act *actuator ) - BeforeEach(func() { - ctx = context.TODO() + BeforeEach(func(ctx SpecContext) { log = logr.Discard() infrastructureConfig := metalv1alpha1.InfrastructureConfig{ @@ -46,10 +44,15 @@ var _ = Describe("Actuator Reconcile", func() { Expect(err).To(Succeed()) infra = &extensionsv1alpha1.Infrastructure{} + infra.Name = "some-infra" + infra.Namespace = metav1.NamespaceDefault infra.Spec.ProviderConfig = &runtime.RawExtension{ Raw: infrastructureConfigRaw, } + Expect(k8sClient.Create(ctx, infra)).To(Succeed()) + DeferCleanup(k8sClient.Delete, infra) + cluster = &extensionscontroller.Cluster{ Shoot: &gardencorev1beta1.Shoot{ Spec: gardencorev1beta1.ShootSpec{ @@ -60,11 +63,11 @@ var _ = Describe("Actuator Reconcile", func() { }, } - act = &actuator{} + act = &actuator{client: k8sClient} }) Describe("#Reconcile", func() { - It("should update infra.Status.Networking.Nodes", func() { + It("should update infra.Status.Networking.Nodes", func(ctx SpecContext) { err := act.Reconcile(ctx, log, infra, cluster) Expect(err).NotTo(HaveOccurred()) diff --git a/pkg/controller/infrastructure/infrastructure_suite_test.go b/pkg/controller/infrastructure/infrastructure_suite_test.go index de58cd1..11cf042 100644 --- a/pkg/controller/infrastructure/infrastructure_suite_test.go +++ b/pkg/controller/infrastructure/infrastructure_suite_test.go @@ -1,16 +1,97 @@ // SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors // SPDX-License-Identifier: Apache-2.0 -package infrastructure_test +package infrastructure import ( + "fmt" + "path/filepath" + "runtime" "testing" + "time" + gardenerextensionv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1" + machinescheme "github.com/gardener/machine-controller-manager/pkg/client/clientset/versioned/scheme" + "github.com/ironcore-dev/controller-utils/modutils" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "go.uber.org/zap/zapcore" + apiextensionsscheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme" + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/rest" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" + "sigs.k8s.io/controller-runtime/pkg/envtest/komega" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + + apiv1alpha1 "github.com/ironcore-dev/gardener-extension-provider-ironcore-metal/pkg/apis/metal/v1alpha1" +) + +const ( + pollingInterval = 50 * time.Millisecond + eventuallyTimeout = 10 * time.Second + consistentlyDuration = 1 * time.Second ) func TestInfrastructure(t *testing.T) { + SetDefaultConsistentlyPollingInterval(pollingInterval) + SetDefaultEventuallyPollingInterval(pollingInterval) + SetDefaultEventuallyTimeout(eventuallyTimeout) + SetDefaultConsistentlyDuration(consistentlyDuration) + RegisterFailHandler(Fail) RunSpecs(t, "Infrastructure Suite") } + +var ( + testEnv *envtest.Environment + cfg *rest.Config + k8sClient client.Client +) + +var _ = BeforeSuite(func() { + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), zap.Level(zapcore.InfoLevel))) + + By("bootstrapping test environment") + testEnv = &envtest.Environment{ + CRDDirectoryPaths: []string{ + modutils.Dir("github.com/gardener/machine-controller-manager", "kubernetes", "crds", "machine.sapcloud.io_machineclasses.yaml"), + modutils.Dir("github.com/gardener/machine-controller-manager", "kubernetes", "crds", "machine.sapcloud.io_machinedeployments.yaml"), + modutils.Dir("github.com/gardener/machine-controller-manager", "kubernetes", "crds", "machine.sapcloud.io_machines.yaml"), + modutils.Dir("github.com/gardener/machine-controller-manager", "kubernetes", "crds", "machine.sapcloud.io_machinesets.yaml"), + filepath.Join("..", "..", "..", "example", "20-crd-extensions.gardener.cloud_infrastructures.yaml"), + }, + ErrorIfCRDPathMissing: true, + + // The BinaryAssetsDirectory is only required if you want to run the tests directly + // without call the makefile target test. If not informed it will look for the + // default path defined in controller-apiruntime which is /usr/local/kubebuilder/. + // Note that you must have the required binaries setup under the bin directory to perform + // the tests directly. When we run make test it will be setup and used automatically. + BinaryAssetsDirectory: filepath.Join("..", "..", "..", "bin", "k8s", + fmt.Sprintf("1.31.0-%s-%s", runtime.GOOS, runtime.GOARCH)), + } + + var err error + // cfg is defined in this file globally. + cfg, err = testEnv.Start() + Expect(err).NotTo(HaveOccurred()) + Expect(cfg).NotTo(BeNil()) + + Expect(apiextensionsscheme.AddToScheme(scheme.Scheme)).To(Succeed()) + Expect(machinescheme.AddToScheme(scheme.Scheme)).To(Succeed()) + Expect(gardenerextensionv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) + Expect(apiv1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) + + // Init package-level k8sClient + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + Expect(err).NotTo(HaveOccurred()) + Expect(k8sClient).NotTo(BeNil()) + + komega.SetClient(k8sClient) +}) + +var _ = AfterSuite(func() { + Expect(testEnv.Stop()).To(Succeed()) +}) diff --git a/pkg/controller/worker/suite_test.go b/pkg/controller/worker/suite_test.go index d1b0d34..af5f6ab 100644 --- a/pkg/controller/worker/suite_test.go +++ b/pkg/controller/worker/suite_test.go @@ -323,3 +323,7 @@ func mapToString(m map[string]interface{}) (string, error) { } return string(yamlData), nil } + +var _ = AfterSuite(func() { + Expect(testEnv.Stop()).To(Succeed()) +})