Skip to content

Commit

Permalink
Extend vcsim controller: create default namespaces, add more Node
Browse files Browse the repository at this point in the history
conditions

Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Nov 6, 2024
1 parent fb16df1 commit ec9e03e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{}
Expand Down
21 changes: 18 additions & 3 deletions test/infrastructure/vcsim/controllers/vmbootstrap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
Expand Down
26 changes: 25 additions & 1 deletion test/infrastructure/vcsim/controllers/vspherevm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ec9e03e

Please sign in to comment.