Skip to content

Commit 6ae3166

Browse files
authored
Merge pull request #1671 from srm09/automated-cherry-pick-of-#1670-release-1.3
🌱 Automated cherry pick of #1670: Skip creation of cluster modules
2 parents 7a7993c + 67053ca commit 6ae3166

File tree

7 files changed

+152
-12
lines changed

7 files changed

+152
-12
lines changed

controllers/clustermodule_reconciler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ func (r Reconciler) Reconcile(ctx *context.ClusterContext) (reconcile.Result, er
119119
modErrs = append(modErrs, clusterModError{obj.GetName(), err})
120120
continue
121121
}
122+
// module creation was skipped
123+
if moduleUUID == "" {
124+
continue
125+
}
122126
clusterModuleSpecs = append(clusterModuleSpecs, infrav1.ClusterModule{
123127
ControlPlane: obj.IsControlPlane(),
124128
TargetObjectName: obj.GetName(),

controllers/clustermodule_reconciler_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ func TestReconciler_Reconcile(t *testing.T) {
146146
g.Expect(conditions.Get(ctx.VSphereCluster, infrav1.ClusterModulesAvailableCondition).Message).To(gomega.ContainSubstring("kcp"))
147147
},
148148
},
149+
{
150+
name: "when some cluster module creations are skipped",
151+
clusterModules: []infrav1.ClusterModule{},
152+
setupMocks: func(svc *cmodfake.CMService) {
153+
svc.On("Create", mock.Anything, clustermodule.NewWrapper(kcp)).Return(kcpUUID, nil)
154+
// mimics cluster module creation was skipped
155+
svc.On("Create", mock.Anything, clustermodule.NewWrapper(md)).Return("", nil)
156+
},
157+
customAssert: func(g *gomega.WithT, ctx *context.ClusterContext) {
158+
g.Expect(ctx.VSphereCluster.Spec.ClusterModules).To(gomega.HaveLen(1))
159+
g.Expect(ctx.VSphereCluster.Spec.ClusterModules[0].TargetObjectName).To(gomega.Equal("kcp"))
160+
g.Expect(ctx.VSphereCluster.Spec.ClusterModules[0].ModuleUUID).To(gomega.Equal(kcpUUID))
161+
g.Expect(ctx.VSphereCluster.Spec.ClusterModules[0].ControlPlane).To(gomega.BeTrue())
162+
},
163+
},
149164
{
150165
name: "when machine deployment is being deleted",
151166
beforeFn: func(object client.Object) {

pkg/clustermodule/service.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package clustermodule
1919
import (
2020
goctx "context"
2121

22+
"github.com/pkg/errors"
2223
"github.com/vmware/govmomi/find"
2324
"github.com/vmware/govmomi/vim25/types"
2425

@@ -27,19 +28,37 @@ import (
2728
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/session"
2829
)
2930

31+
const validMachineTemplate = "VSphereMachineTemplate"
32+
3033
type service struct{}
3134

3235
func NewService() Service {
3336
return service{}
3437
}
3538

3639
func (s service) Create(ctx *context.ClusterContext, wrapper Wrapper) (string, error) {
37-
logger := ctx.Logger.WithValues("object", wrapper.GetName())
38-
template, err := fetchMachineTemplate(ctx, wrapper)
40+
logger := ctx.Logger.WithValues("object", wrapper.GetName(), "namespace", wrapper.GetNamespace())
41+
42+
templateRef, err := fetchTemplateRef(ctx, ctx.Client, wrapper)
43+
if err != nil {
44+
logger.V(4).Error(err, "error fetching template for object")
45+
return "", errors.Wrapf(err, "error fetching machine template for object %s/%s", wrapper.GetNamespace(), wrapper.GetName())
46+
}
47+
if templateRef.Kind != validMachineTemplate {
48+
// since this is a heterogeneous cluster, we should skip cluster module creation for non VSphereMachine objects
49+
logger.V(4).Info("skipping module creation for object")
50+
return "", nil
51+
}
52+
53+
template, err := fetchMachineTemplate(ctx, wrapper, templateRef.Name)
3954
if err != nil {
4055
logger.V(4).Error(err, "error fetching template")
4156
return "", err
4257
}
58+
if server := template.Spec.Template.Spec.Server; server != ctx.VSphereCluster.Spec.Server {
59+
logger.V(4).Info("skipping module creation for object since template uses a different server", "server", server)
60+
return "", nil
61+
}
4362

4463
vCenterSession, err := fetchSessionForObject(ctx, template)
4564
if err != nil {
@@ -67,7 +86,14 @@ func (s service) Create(ctx *context.ClusterContext, wrapper Wrapper) (string, e
6786

6887
func (s service) DoesExist(ctx *context.ClusterContext, wrapper Wrapper, moduleUUID string) (bool, error) {
6988
logger := ctx.Logger.WithValues("object", wrapper.GetName())
70-
template, err := fetchMachineTemplate(ctx, wrapper)
89+
90+
templateRef, err := fetchTemplateRef(ctx, ctx.Client, wrapper)
91+
if err != nil {
92+
logger.V(4).Error(err, "error fetching template for object")
93+
return false, errors.Wrapf(err, "error fetching infrastructure machine template for object %s/%s", wrapper.GetNamespace(), wrapper.GetName())
94+
}
95+
96+
template, err := fetchMachineTemplate(ctx, wrapper, templateRef.Name)
7197
if err != nil {
7298
logger.V(4).Error(err, "error fetching template")
7399
return false, err

pkg/clustermodule/service_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package clustermodule
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
27+
28+
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
29+
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context/fake"
30+
)
31+
32+
func TestService_Create(t *testing.T) {
33+
svc := NewService()
34+
35+
t.Run("creation is skipped", func(t *testing.T) {
36+
t.Run("when wrapper points to template != VSphereMachineTemplate", func(t *testing.T) {
37+
md := machineDeployment("md", fake.Namespace, fake.Clusterv1a2Name)
38+
md.Spec.Template.Spec.InfrastructureRef = corev1.ObjectReference{
39+
Kind: "NonVSphereMachineTemplate",
40+
Namespace: fake.Namespace,
41+
Name: "blah",
42+
}
43+
44+
g := gomega.NewWithT(t)
45+
controllerCtx := fake.NewControllerContext(fake.NewControllerManagerContext(md))
46+
ctx := fake.NewClusterContext(controllerCtx)
47+
48+
moduleUUID, err := svc.Create(ctx, mdWrapper{md})
49+
g.Expect(err).ToNot(gomega.HaveOccurred())
50+
g.Expect(moduleUUID).To(gomega.BeEmpty())
51+
})
52+
53+
t.Run("when template uses a different vCenter URL", func(t *testing.T) {
54+
md := machineDeployment("md", fake.Namespace, fake.Clusterv1a2Name)
55+
md.Spec.Template.Spec.InfrastructureRef = corev1.ObjectReference{
56+
Kind: "VSphereMachineTemplate",
57+
Namespace: fake.Namespace,
58+
Name: "blah-template",
59+
}
60+
61+
machineTemplate := &infrav1.VSphereMachineTemplate{
62+
TypeMeta: metav1.TypeMeta{Kind: "VSphereMachineTemplate"},
63+
ObjectMeta: metav1.ObjectMeta{
64+
Name: "blah-template",
65+
Namespace: fake.Namespace,
66+
},
67+
Spec: infrav1.VSphereMachineTemplateSpec{
68+
Template: infrav1.VSphereMachineTemplateResource{Spec: infrav1.VSphereMachineSpec{
69+
VirtualMachineCloneSpec: infrav1.VirtualMachineCloneSpec{Server: fmt.Sprintf("not.%s", fake.VCenterURL)},
70+
}},
71+
},
72+
}
73+
74+
g := gomega.NewWithT(t)
75+
controllerCtx := fake.NewControllerContext(fake.NewControllerManagerContext(md, machineTemplate))
76+
ctx := fake.NewClusterContext(controllerCtx)
77+
78+
moduleUUID, err := svc.Create(ctx, mdWrapper{md})
79+
g.Expect(err).ToNot(gomega.HaveOccurred())
80+
g.Expect(moduleUUID).To(gomega.BeEmpty())
81+
})
82+
})
83+
}
84+
85+
func machineDeployment(name, namespace, cluster string) *clusterv1.MachineDeployment {
86+
return &clusterv1.MachineDeployment{
87+
TypeMeta: metav1.TypeMeta{
88+
APIVersion: clusterv1.GroupVersion.String(),
89+
Kind: "MachineDeployment",
90+
},
91+
ObjectMeta: metav1.ObjectMeta{
92+
Name: name,
93+
Namespace: namespace,
94+
Labels: map[string]string{clusterv1.ClusterLabelName: cluster},
95+
},
96+
}
97+
}

pkg/clustermodule/session.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,10 @@ func fetchTemplateRef(ctx goctx.Context, c client.Client, input Wrapper) (*corev
8080
return &objRef, nil
8181
}
8282

83-
func fetchMachineTemplate(ctx *context.ClusterContext, input Wrapper) (*infrav1.VSphereMachineTemplate, error) {
84-
templateRef, err := fetchTemplateRef(ctx, ctx.Client, input)
85-
if err != nil {
86-
return nil, errors.Wrapf(err, "error fetching machine template for object %s/%s", input.GetNamespace(), input.GetName())
87-
}
88-
89-
ctx.Logger.Info("found template", "ref", templateRef.Name)
83+
func fetchMachineTemplate(ctx *context.ClusterContext, input Wrapper, templateName string) (*infrav1.VSphereMachineTemplate, error) {
9084
template := &infrav1.VSphereMachineTemplate{}
9185
if err := ctx.Client.Get(ctx, client.ObjectKey{
92-
Name: templateRef.Name,
86+
Name: templateName,
9387
Namespace: input.GetNamespace(),
9488
}, template); err != nil {
9589
return nil, err

pkg/context/fake/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ const (
7272

7373
// ServiceCIDR is the CIDR for the service network.
7474
ServiceCIDR = "2.0.0.0/16"
75+
76+
VCenterURL = "foo.vcenter.com"
7577
)
7678

7779
var boolTrue = true

pkg/context/fake/fake_cluster_context.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func newVSphereCluster(owner clusterv1.Cluster) infrav1.VSphereCluster {
8585
},
8686
},
8787
},
88-
Spec: infrav1.VSphereClusterSpec{},
88+
Spec: infrav1.VSphereClusterSpec{
89+
Server: VCenterURL,
90+
},
8991
}
9092
}

0 commit comments

Comments
 (0)