Skip to content

Commit

Permalink
Patch infrastruture status
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuckal777 committed Jan 29, 2025
1 parent f33e92c commit f86c881
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 7 deletions.
7 changes: 7 additions & 0 deletions pkg/controller/infrastructure/actuator_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand All @@ -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)
}

Expand Down
15 changes: 9 additions & 6 deletions pkg/controller/infrastructure/actuator_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package infrastructure

import (
"context"
"encoding/json"

extensionscontroller "github.com/gardener/gardener/extensions/pkg/controller"
Expand All @@ -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"
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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())

Expand Down
83 changes: 82 additions & 1 deletion pkg/controller/infrastructure/infrastructure_suite_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
4 changes: 4 additions & 0 deletions pkg/controller/worker/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,7 @@ func mapToString(m map[string]interface{}) (string, error) {
}
return string(yamlData), nil
}

var _ = AfterSuite(func() {
Expect(testEnv.Stop()).To(Succeed())
})

0 comments on commit f86c881

Please sign in to comment.