Skip to content

Commit fc11ba9

Browse files
committed
Extend vcsim controller: create default namespaces, add more Node
conditions Signed-off-by: Stefan Büringer [email protected]
1 parent fb16df1 commit fc11ba9

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

test/infrastructure/vcsim/controllers/virtualmachine_controller.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/pkg/errors"
2525
vmoprv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2"
26+
corev1 "k8s.io/api/core/v1"
2627
apierrors "k8s.io/apimachinery/pkg/api/errors"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
kerrors "k8s.io/apimachinery/pkg/util/errors"
@@ -138,6 +139,30 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
138139
resourceGroup := klog.KObj(cluster).String()
139140
r.InMemoryManager.AddResourceGroup(resourceGroup)
140141

142+
inmemoryClient := r.InMemoryManager.GetResourceGroup(resourceGroup).GetClient()
143+
144+
// Create default Namespaces.
145+
for _, nsName := range []string{metav1.NamespaceDefault, metav1.NamespacePublic, metav1.NamespaceSystem} {
146+
ns := &corev1.Namespace{
147+
ObjectMeta: metav1.ObjectMeta{
148+
Name: nsName,
149+
Labels: map[string]string{
150+
"kubernetes.io/metadata.name": nsName,
151+
},
152+
},
153+
}
154+
155+
if err := inmemoryClient.Get(ctx, client.ObjectKeyFromObject(ns), ns); err != nil {
156+
if !apierrors.IsNotFound(err) {
157+
return ctrl.Result{}, errors.Wrapf(err, "failed to get %s Namespace", nsName)
158+
}
159+
160+
if err := inmemoryClient.Create(ctx, ns); err != nil && !apierrors.IsAlreadyExists(err) {
161+
return ctrl.Result{}, errors.Wrapf(err, "failed to create %s Namespace", nsName)
162+
}
163+
}
164+
}
165+
141166
if _, err := r.APIServerMux.WorkloadClusterByResourceGroup(resourceGroup); err != nil {
142167
l := &vcsimv1.ControlPlaneEndpointList{}
143168
if err := r.Client.List(ctx, l); err != nil {
@@ -168,7 +193,6 @@ func (r *VirtualMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reque
168193
// The conditionsTracker is an object stored in memory with the scope of storing conditions used for keeping
169194
// track of the provisioning process of the fake node, etcd, api server, etc for this specific virtualMachine.
170195
// (the process managed by this controller).
171-
inmemoryClient := r.InMemoryManager.GetResourceGroup(resourceGroup).GetClient()
172196
// NOTE: The type of the in memory conditionsTracker object doesn't matter as soon as it implements Cluster API's conditions interfaces.
173197
// Unfortunately vmoprv1.VirtualMachine isn't a condition getter, so we fallback on using a infrav1.VSphereVM.
174198
conditionsTracker := &infrav1.VSphereVM{}

test/infrastructure/vcsim/controllers/vmbootstrap_controller.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,25 @@ func (r *vmBootstrapReconciler) reconcileBoostrapNode(ctx context.Context, clust
237237
LastTransitionTime: metav1.Now(),
238238
Type: corev1.NodeReady,
239239
Status: corev1.ConditionTrue,
240+
Reason: "KubeletReady",
241+
},
242+
{
243+
LastTransitionTime: metav1.Now(),
244+
Type: corev1.NodeMemoryPressure,
245+
Status: corev1.ConditionFalse,
246+
Reason: "KubeletHasSufficientMemory",
247+
},
248+
{
249+
LastTransitionTime: metav1.Now(),
250+
Type: corev1.NodeDiskPressure,
251+
Status: corev1.ConditionFalse,
252+
Reason: "KubeletHasNoDiskPressure",
253+
},
254+
{
255+
LastTransitionTime: metav1.Now(),
256+
Type: corev1.NodePIDPressure,
257+
Status: corev1.ConditionFalse,
258+
Reason: "KubeletHasSufficientPID",
240259
},
241260
},
242261
},

test/infrastructure/vcsim/controllers/vspherevm_controller.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path"
2323

2424
"github.com/pkg/errors"
25+
corev1 "k8s.io/api/core/v1"
2526
apierrors "k8s.io/apimachinery/pkg/api/errors"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
kerrors "k8s.io/apimachinery/pkg/util/errors"
@@ -141,6 +142,30 @@ func (r *VSphereVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
141142
resourceGroup := klog.KObj(cluster).String()
142143
r.InMemoryManager.AddResourceGroup(resourceGroup)
143144

145+
inmemoryClient := r.InMemoryManager.GetResourceGroup(resourceGroup).GetClient()
146+
147+
// Create default Namespaces.
148+
for _, nsName := range []string{metav1.NamespaceDefault, metav1.NamespacePublic, metav1.NamespaceSystem} {
149+
ns := &corev1.Namespace{
150+
ObjectMeta: metav1.ObjectMeta{
151+
Name: nsName,
152+
Labels: map[string]string{
153+
"kubernetes.io/metadata.name": nsName,
154+
},
155+
},
156+
}
157+
158+
if err := inmemoryClient.Get(ctx, client.ObjectKeyFromObject(ns), ns); err != nil {
159+
if !apierrors.IsNotFound(err) {
160+
return ctrl.Result{}, errors.Wrapf(err, "failed to get %s Namespace", nsName)
161+
}
162+
163+
if err := inmemoryClient.Create(ctx, ns); err != nil && !apierrors.IsAlreadyExists(err) {
164+
return ctrl.Result{}, errors.Wrapf(err, "failed to create %s Namespace", nsName)
165+
}
166+
}
167+
}
168+
144169
if _, err := r.APIServerMux.WorkloadClusterByResourceGroup(resourceGroup); err != nil {
145170
l := &vcsimv1.ControlPlaneEndpointList{}
146171
if err := r.Client.List(ctx, l); err != nil {
@@ -171,7 +196,6 @@ func (r *VSphereVMReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
171196
// The conditionsTracker is an object stored in memory with the scope of storing conditions used for keeping
172197
// track of the provisioning process of the fake node, etcd, api server, etc for this specific vSphereVM.
173198
// (the process managed by this controller).
174-
inmemoryClient := r.InMemoryManager.GetResourceGroup(resourceGroup).GetClient()
175199
// NOTE: The type of the in memory conditionsTracker object doesn't matter as soon as it implements Cluster API's conditions interfaces.
176200
conditionsTracker := &infrav1.VSphereVM{}
177201
if err := inmemoryClient.Get(ctx, client.ObjectKeyFromObject(vSphereVM), conditionsTracker); err != nil {

0 commit comments

Comments
 (0)