Skip to content

Commit ee2cdfd

Browse files
committed
tmp
1 parent 3294a57 commit ee2cdfd

File tree

5 files changed

+132
-10
lines changed

5 files changed

+132
-10
lines changed

exp/api/v1beta2/rosamachinepool_webhook.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package v1beta2
22

33
import (
4+
"fmt"
5+
46
"github.com/blang/semver"
57
"github.com/google/go-cmp/cmp"
68
"github.com/pkg/errors"
@@ -31,6 +33,7 @@ var _ webhook.Validator = &ROSAMachinePool{}
3133
// ValidateCreate implements admission.Validator.
3234
func (r *ROSAMachinePool) ValidateCreate() (warnings admission.Warnings, err error) {
3335
var allErrs field.ErrorList
36+
fmt.Println("VALIDTAE CREATE")
3437

3538
if err := r.validateVersion(); err != nil {
3639
allErrs = append(allErrs, err)
@@ -43,6 +46,8 @@ func (r *ROSAMachinePool) ValidateCreate() (warnings admission.Warnings, err err
4346
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
4447

4548
if len(allErrs) == 0 {
49+
fmt.Println("VALIDTAE CREATE FINE")
50+
4651
return nil, nil
4752
}
4853

@@ -55,6 +60,7 @@ func (r *ROSAMachinePool) ValidateCreate() (warnings admission.Warnings, err err
5560

5661
// ValidateUpdate implements admission.Validator.
5762
func (r *ROSAMachinePool) ValidateUpdate(old runtime.Object) (warnings admission.Warnings, err error) {
63+
fmt.Println("VALIDTAE UPDATE")
5864
oldPool, ok := old.(*ROSAMachinePool)
5965
if !ok {
6066
return nil, apierrors.NewInvalid(GroupVersion.WithKind("ROSAMachinePool").GroupKind(), r.Name, field.ErrorList{

exp/controllers/rosamachinepool_controller.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func (r *ROSAMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Requ
135135
return ctrl.Result{}, err
136136
}
137137

138+
fmt.Println("CLIENT:", &r.Client)
138139
machinePoolScope, err := scope.NewRosaMachinePoolScope(scope.RosaMachinePoolScopeParams{
139140
Client: r.Client,
140141
ControllerName: "rosamachinepool",
@@ -168,11 +169,36 @@ func (r *ROSAMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Requ
168169
}
169170

170171
defer func() {
172+
fmt.Println("DEFER")
171173
conditions.SetSummary(machinePoolScope.RosaMachinePool, conditions.WithConditions(expinfrav1.RosaMachinePoolReadyCondition), conditions.WithStepCounter())
172-
173-
if err := machinePoolScope.Close(); err != nil && reterr == nil {
174-
reterr = err
174+
fmt.Println("DEFER")
175+
176+
// patchHelper, err := patch.NewHelper(machinePoolScope.RosaMachinePool, r.Client)
177+
// if err != nil {
178+
// fmt.Println("ERROR when creatin helper")
179+
// }
180+
181+
failureMessage := "fail blabla"
182+
machinePoolScope.RosaMachinePool.Status.FailureMessage = &failureMessage
183+
// err = patchHelper.Patch(
184+
// ctx,
185+
// // context.TODO(),
186+
// machinePoolScope.RosaMachinePool,
187+
// patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
188+
// expinfrav1.RosaMachinePoolReadyCondition,
189+
// }})
190+
// r.Client.Ge
191+
192+
err = r.Client.Status().Patch(ctx, machinePoolScope.RosaMachinePool, client.MergeFrom(machinePoolScope.RosaMachinePool))
193+
if err != nil {
194+
fmt.Println("ERR WHEN PATCHING", err)
175195
}
196+
197+
// if err := machinePoolScope.Close(); err != nil && reterr == nil {
198+
// fmt.Println("DEFER", err)
199+
200+
// reterr = err
201+
// }
176202
}()
177203

178204
if !rosaMachinePool.ObjectMeta.DeletionTimestamp.IsZero() {
@@ -201,14 +227,34 @@ func (r *ROSAMachinePoolReconciler) reconcileNormal(ctx context.Context,
201227
}
202228

203229
failureMessage, err := validateMachinePoolSpec(machinePoolScope)
230+
fmt.Println("VALIDATION", failureMessage, err)
231+
204232
if err != nil {
205233
return ctrl.Result{}, fmt.Errorf("failed to validate ROSAMachinePool.spec: %w", err)
206234
}
207235
if failureMessage != nil {
208-
machinePoolScope.RosaMachinePool.Status.FailureMessage = failureMessage
236+
fmt.Println("FAIL", machinePoolScope.RosaMachinePool.Name)
237+
238+
// machinePoolScope.RosaMachinePool.Status.FailureMessage = failureMessage
239+
// conditions.MarkFalse(machinePoolScope.RosaMachinePool,
240+
// expinfrav1.RosaMachinePoolReadyCondition,
241+
// expinfrav1.RosaMachinePoolReconciliationFailedReason,
242+
// clusterv1.ConditionSeverityError,
243+
// "failed to create ROSAMachinePool: %s", *failureMessage)
244+
// machinePoolScope.Close()
245+
// annotations.AddAnnotations(machinePoolScope.RosaMachinePool, map[string]string{
246+
// clusterv1.ReplicasManagedByAnnotation: "rosa",
247+
// })
248+
// if err := machinePoolScope.PatchRosaMachinePoolObject(ctx); err != nil {
249+
// fmt.Println("ERRRRR PatchRosaMachinePoolObject", err.Error())
250+
// return ctrl.Result{}, err
251+
// }
252+
// machinePoolScope.Patch(ctx, machinePoolScope.RosaMachinePool)
253+
// r.Status().Update(ctx, machinePoolScope.RosaMachinePool)
209254
// dont' requeue because input is invalid and manual intervention is needed.
210255
return ctrl.Result{}, nil
211256
}
257+
fmt.Println("CCCCC")
212258
machinePoolScope.RosaMachinePool.Status.FailureMessage = nil
213259

214260
rosaMachinePool := machinePoolScope.RosaMachinePool
@@ -225,11 +271,14 @@ func (r *ROSAMachinePoolReconciler) reconcileNormal(ctx context.Context,
225271
}
226272

227273
nodePool, found, err := ocmClient.GetNodePool(machinePoolScope.ControlPlane.Status.ID, rosaMachinePool.Spec.NodePoolName)
274+
fmt.Println("DDDDD", nodePool, found, err)
275+
228276
if err != nil {
229277
return ctrl.Result{}, err
230278
}
231279

232280
if found {
281+
fmt.Println("FOUND")
233282
if rosaMachinePool.Spec.AvailabilityZone == "" {
234283
// reflect the current AvailabilityZone in the spec if not set.
235284
rosaMachinePool.Spec.AvailabilityZone = nodePool.AvailabilityZone()
@@ -280,6 +329,8 @@ func (r *ROSAMachinePoolReconciler) reconcileNormal(ctx context.Context,
280329
return ctrl.Result{RequeueAfter: time.Second * 60}, nil
281330
}
282331

332+
fmt.Println("EEEEEE ")
333+
283334
npBuilder := nodePoolBuilder(rosaMachinePool.Spec, machinePool.Spec)
284335
nodePoolSpec, err := npBuilder.Build()
285336
if err != nil {
@@ -297,6 +348,7 @@ func (r *ROSAMachinePoolReconciler) reconcileNormal(ctx context.Context,
297348
}
298349

299350
machinePoolScope.RosaMachinePool.Status.ID = nodePool.ID()
351+
fmt.Println("BBBB")
300352
return ctrl.Result{}, nil
301353
}
302354

exp/controllers/rosamachinepool_controller_test.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67
"time"
78

@@ -31,6 +32,7 @@ import (
3132
"sigs.k8s.io/cluster-api-provider-aws/v2/test/mocks"
3233
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3334
expclusterv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
35+
"sigs.k8s.io/cluster-api/util/patch"
3436
)
3537

3638
func TestNodePoolToRosaMachinePoolSpec(t *testing.T) {
@@ -104,6 +106,7 @@ func TestRosaMachinePoolReconcile(t *testing.T) {
104106
recorder = record.NewFakeRecorder(10)
105107
ctx = context.TODO()
106108
scheme = runtime.NewScheme()
109+
// scheme = testEnv.Scheme()
107110
ns, err = testEnv.CreateNamespace(ctx, "test-namespace")
108111
g.Expect(err).To(BeNil())
109112

@@ -174,12 +177,18 @@ func TestRosaMachinePoolReconcile(t *testing.T) {
174177
ObjectMeta: metav1.ObjectMeta{
175178
Name: "rosa-machinepool",
176179
Namespace: ns.Name,
180+
UID: "rosa-machinepool-1",
177181
},
178182
TypeMeta: metav1.TypeMeta{
179183
Kind: "ROSAMachinePool",
180184
APIVersion: expinfrav1.GroupVersion.String(),
181185
},
182-
Spec: expinfrav1.RosaMachinePoolSpec{},
186+
Spec: expinfrav1.RosaMachinePoolSpec{
187+
NodePoolName: "test-nodepool",
188+
Version: "4.14.5",
189+
Subnet: "subnet-id",
190+
InstanceType: "m5.large",
191+
},
183192
}
184193

185194
ownerMachinePool = &expclusterv1.MachinePool{
@@ -220,7 +229,7 @@ func TestRosaMachinePoolReconcile(t *testing.T) {
220229
},
221230
}
222231

223-
objects = []client.Object{secret, ownerCluster, ownerMachinePool}
232+
objects = []client.Object{secret, ownerCluster, ownerMachinePool, rosaMachinePool}
224233

225234
for _, obj := range objects {
226235
createObject(g, obj, ns.Name)
@@ -235,6 +244,7 @@ func TestRosaMachinePoolReconcile(t *testing.T) {
235244
}
236245

237246
t.Run("Reconcile create node pool", func(t *testing.T) {
247+
fmt.Println("START test")
238248
setup(t)
239249
defer teardown()
240250
ocmMock = mocks.NewMockOCMClient(mockCtrl)
@@ -243,12 +253,40 @@ func TestRosaMachinePoolReconcile(t *testing.T) {
243253
return nil, false, nil
244254
}).Times(1)
245255
m.CreateNodePool(gomock.Any(), gomock.Any()).DoAndReturn(func(clusterId string, nodePool *cmv1.NodePool) (*cmv1.NodePool, error) {
256+
fmt.Println("NODE POOL", nodePool.ID())
246257
return nodePool, nil
247258
}).Times(1)
248259
}
249260
expect(ocmMock.EXPECT())
250261

262+
fmt.Println("REC", scheme.Recognizes(rosaMachinePool.GroupVersionKind()))
263+
fmt.Println("REC", scheme.Recognizes(ownerCluster.GroupVersionKind()))
264+
251265
c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(rosaMachinePool, ownerCluster, ownerMachinePool, rosaControlPlane, secret).Build()
266+
267+
// failureMessage := "fail blabla"
268+
// rosaMachinePool.Status.FailureMessage = &failureMessage
269+
270+
// err = c.Status().Patch(ctx, rosaMachinePool, client.MergeFrom(rosaMachinePool))
271+
// err = c.Status().Patch(ctx, ownerMachinePool, client.MergeFrom(ownerMachinePool))
272+
g.Expect(err).NotTo(HaveOccurred())
273+
mpPh, err := patch.NewHelper(rosaMachinePool, testEnv)
274+
275+
rosaMachinePool.Status.Ready = true
276+
l := map[string]string{"key": "value"}
277+
rosaMachinePool.SetLabels(l)
278+
279+
err = mpPh.Patch(ctx, rosaMachinePool, patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
280+
expinfrav1.RosaMachinePoolReadyCondition,
281+
}})
282+
283+
m := &expinfrav1.ROSAMachinePool{}
284+
key := client.ObjectKey{Name: rosaMachinePool.Name, Namespace: ns.Name}
285+
c.Get(ctx, key, m)
286+
fmt.Println("ROSAMACHINEPOOL:", m.Name, m.Status, m.Namespace, m.Labels)
287+
288+
g.Expect(err).NotTo(HaveOccurred())
289+
252290
stsMock := mock_stsiface.NewMockSTSAPI(mockCtrl)
253291
stsMock.EXPECT().GetCallerIdentity(gomock.Any()).Times(1)
254292

@@ -266,10 +304,15 @@ func TestRosaMachinePoolReconcile(t *testing.T) {
266304
req := ctrl.Request{}
267305
req.NamespacedName = types.NamespacedName{Name: "rosa-machinepool", Namespace: ns.Name}
268306

269-
result, err := r.Reconcile(ctx, req)
307+
g.Expect(r.Endpoints).To(Equal("a"))
308+
// result, err := r.Reconcile(ctx, req)
309+
// key := client.ObjectKey{Name: ownerMachinePool.Name, Namespace: ns.Name}
310+
311+
g.Expect(m.Status).To(Equal(map[string]string{"cluster.x-k8s.io/replicas-managed-by": "rosa"}))
270312

271313
g.Expect(err).ToNot(HaveOccurred())
272-
g.Expect(result).To(Equal(ctrl.Result{}))
314+
315+
// g.Expect(result).To(Equal(ctrl.Result{}))
273316
})
274317

275318
// t.Run("Reconcile delete", func(t *testing.T) {

exp/controllers/suite_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import (
2121
"path"
2222
"testing"
2323

24+
corev1 "k8s.io/api/core/v1"
2425
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2526
"k8s.io/client-go/kubernetes/scheme"
2627
ctrl "sigs.k8s.io/controller-runtime"
2728

2829
// +kubebuilder:scaffold:imports
2930
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
31+
rosacontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/rosa/api/v1beta2"
3032
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
3133
"sigs.k8s.io/cluster-api-provider-aws/v2/test/helpers"
3234
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -52,6 +54,8 @@ func setup() {
5254
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
5355
utilruntime.Must(expinfrav1.AddToScheme(scheme.Scheme))
5456
utilruntime.Must(expclusterv1.AddToScheme(scheme.Scheme))
57+
utilruntime.Must(corev1.AddToScheme(scheme.Scheme))
58+
utilruntime.Must(rosacontrolplanev1.AddToScheme(scheme.Scheme))
5559
testEnvConfig := helpers.NewTestEnvironmentConfiguration([]string{
5660
path.Join("config", "crd", "bases"),
5761
},
@@ -76,6 +80,12 @@ func setup() {
7680
if err := (&expinfrav1.AWSManagedMachinePool{}).SetupWebhookWithManager(testEnv); err != nil {
7781
panic(fmt.Sprintf("Unable to setup AWSManagedMachinePool webhook: %v", err))
7882
}
83+
if err := (&expinfrav1.ROSAMachinePool{}).SetupWebhookWithManager(testEnv); err != nil {
84+
panic(fmt.Sprintf("Unable to setup ROSAMachinePool webhook: %v", err))
85+
}
86+
if err := (&rosacontrolplanev1.ROSAControlPlane{}).SetupWebhookWithManager(testEnv); err != nil {
87+
panic(fmt.Sprintf("Unable to setup ROSAMachinePool webhook: %v", err))
88+
}
7989
go func() {
8090
fmt.Println("Starting the manager")
8191
if err := testEnv.StartManager(ctx); err != nil {

pkg/cloud/scope/rosamachinepool.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package scope
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
awsclient "github.com/aws/aws-sdk-go/aws/client"
2324
"github.com/pkg/errors"
@@ -66,6 +67,8 @@ func NewRosaMachinePoolScope(params RosaMachinePoolScopeParams) (*RosaMachinePoo
6667
params.Logger = logger.NewLogger(log)
6768
}
6869

70+
fmt.Println("CLIENT:", &params.Client)
71+
6972
ammpHelper, err := patch.NewHelper(params.RosaMachinePool, params.Client)
7073
if err != nil {
7174
return nil, errors.Wrap(err, "failed to init RosaMachinePool patch helper")
@@ -76,8 +79,9 @@ func NewRosaMachinePoolScope(params RosaMachinePoolScopeParams) (*RosaMachinePoo
7679
}
7780

7881
scope := &RosaMachinePoolScope{
79-
Logger: *params.Logger,
80-
Client: params.Client,
82+
Logger: *params.Logger,
83+
Client: params.Client,
84+
// issue here for tests?
8185
patchHelper: ammpHelper,
8286
capiMachinePoolPatchHelper: mpHelper,
8387

@@ -227,6 +231,13 @@ func (s *RosaMachinePoolScope) PatchCAPIMachinePoolObject(ctx context.Context) e
227231
)
228232
}
229233

234+
func (s *RosaMachinePoolScope) PatchRosaMachinePoolObject(ctx context.Context) error {
235+
return s.patchHelper.Patch(
236+
ctx,
237+
s.RosaMachinePool,
238+
)
239+
}
240+
230241
// Close closes the current scope persisting the control plane configuration and status.
231242
func (s *RosaMachinePoolScope) Close() error {
232243
return s.PatchObject()

0 commit comments

Comments
 (0)