From ec9e03e267dc169c9338f426fdbb27de795bd884 Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Wed, 6 Nov 2024 13:31:39 +0100 Subject: [PATCH] Extend vcsim controller: create default namespaces, add more Node conditions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Büringer buringerst@vmware.com --- .../controllers/virtualmachine_controller.go | 26 ++++++++++++++++++- .../controllers/vmbootstrap_controller.go | 21 ++++++++++++--- .../vcsim/controllers/vspherevm_controller.go | 26 ++++++++++++++++++- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/test/infrastructure/vcsim/controllers/virtualmachine_controller.go b/test/infrastructure/vcsim/controllers/virtualmachine_controller.go index 3c65de530c..6b9d5184d1 100644 --- a/test/infrastructure/vcsim/controllers/virtualmachine_controller.go +++ b/test/infrastructure/vcsim/controllers/virtualmachine_controller.go @@ -23,6 +23,7 @@ import ( "github.com/pkg/errors" vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kerrors "k8s.io/apimachinery/pkg/util/errors" @@ -138,6 +139,30 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque resourceGroup := klog.KObj(cluster).String() r.InMemoryManager.AddResourceGroup(resourceGroup) + inmemoryClient := r.InMemoryManager.GetResourceGroup(resourceGroup).GetClient() + + // Create default Namespaces. + for _, nsName := range []string{metav1.NamespaceDefault, metav1.NamespacePublic, metav1.NamespaceSystem} { + ns := &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: nsName, + Labels: map[string]string{ + "kubernetes.io/metadata.name": nsName, + }, + }, + } + + if err := inmemoryClient.Get(ctx, client.ObjectKeyFromObject(ns), ns); err != nil { + if !apierrors.IsNotFound(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to get %s Namespace", nsName) + } + + if err := inmemoryClient.Create(ctx, ns); err != nil && !apierrors.IsAlreadyExists(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to create %s Namespace", nsName) + } + } + } + if _, err := r.APIServerMux.WorkloadClusterByResourceGroup(resourceGroup); err != nil { l := &vcsimv1.ControlPlaneEndpointList{} if err := r.Client.List(ctx, l); err != nil { @@ -168,7 +193,6 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque // The conditionsTracker is an object stored in memory with the scope of storing conditions used for keeping // track of the provisioning process of the fake node, etcd, api server, etc for this specific virtualMachine. // (the process managed by this controller). - inmemoryClient := r.InMemoryManager.GetResourceGroup(resourceGroup).GetClient() // NOTE: The type of the in memory conditionsTracker object doesn't matter as soon as it implements Cluster API's conditions interfaces. // Unfortunately vmoprv1.VirtualMachine isn't a condition getter, so we fallback on using a infrav1.VSphereVM. conditionsTracker := &infrav1.VSphereVM{} diff --git a/test/infrastructure/vcsim/controllers/vmbootstrap_controller.go b/test/infrastructure/vcsim/controllers/vmbootstrap_controller.go index 5d690aefd9..7f4e311c7c 100644 --- a/test/infrastructure/vcsim/controllers/vmbootstrap_controller.go +++ b/test/infrastructure/vcsim/controllers/vmbootstrap_controller.go @@ -234,9 +234,24 @@ func (r *vmBootstrapReconciler) reconcileBoostrapNode(ctx context.Context, clust Status: corev1.NodeStatus{ Conditions: []corev1.NodeCondition{ { - LastTransitionTime: metav1.Now(), - Type: corev1.NodeReady, - Status: corev1.ConditionTrue, + Type: corev1.NodeReady, + Status: corev1.ConditionTrue, + Reason: "KubeletReady", + }, + { + Type: corev1.NodeMemoryPressure, + Status: corev1.ConditionFalse, + Reason: "KubeletHasSufficientMemory", + }, + { + Type: corev1.NodeDiskPressure, + Status: corev1.ConditionFalse, + Reason: "KubeletHasNoDiskPressure", + }, + { + Type: corev1.NodePIDPressure, + Status: corev1.ConditionFalse, + Reason: "KubeletHasSufficientPID", }, }, }, diff --git a/test/infrastructure/vcsim/controllers/vspherevm_controller.go b/test/infrastructure/vcsim/controllers/vspherevm_controller.go index 511f93aed1..b2e9cfed19 100644 --- a/test/infrastructure/vcsim/controllers/vspherevm_controller.go +++ b/test/infrastructure/vcsim/controllers/vspherevm_controller.go @@ -22,6 +22,7 @@ import ( "path" "github.com/pkg/errors" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kerrors "k8s.io/apimachinery/pkg/util/errors" @@ -141,6 +142,30 @@ func (r *VSphereVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( resourceGroup := klog.KObj(cluster).String() r.InMemoryManager.AddResourceGroup(resourceGroup) + inmemoryClient := r.InMemoryManager.GetResourceGroup(resourceGroup).GetClient() + + // Create default Namespaces. + for _, nsName := range []string{metav1.NamespaceDefault, metav1.NamespacePublic, metav1.NamespaceSystem} { + ns := &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: nsName, + Labels: map[string]string{ + "kubernetes.io/metadata.name": nsName, + }, + }, + } + + if err := inmemoryClient.Get(ctx, client.ObjectKeyFromObject(ns), ns); err != nil { + if !apierrors.IsNotFound(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to get %s Namespace", nsName) + } + + if err := inmemoryClient.Create(ctx, ns); err != nil && !apierrors.IsAlreadyExists(err) { + return ctrl.Result{}, errors.Wrapf(err, "failed to create %s Namespace", nsName) + } + } + } + if _, err := r.APIServerMux.WorkloadClusterByResourceGroup(resourceGroup); err != nil { l := &vcsimv1.ControlPlaneEndpointList{} if err := r.Client.List(ctx, l); err != nil { @@ -171,7 +196,6 @@ func (r *VSphereVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( // The conditionsTracker is an object stored in memory with the scope of storing conditions used for keeping // track of the provisioning process of the fake node, etcd, api server, etc for this specific vSphereVM. // (the process managed by this controller). - inmemoryClient := r.InMemoryManager.GetResourceGroup(resourceGroup).GetClient() // NOTE: The type of the in memory conditionsTracker object doesn't matter as soon as it implements Cluster API's conditions interfaces. conditionsTracker := &infrav1.VSphereVM{} if err := inmemoryClient.Get(ctx, client.ObjectKeyFromObject(vSphereVM), conditionsTracker); err != nil {