-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic E2E test for machine controller
- Loading branch information
Richard Kovacs
committed
Jan 30, 2024
1 parent
f0b7c2b
commit 0234fb6
Showing
20 changed files
with
222 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/google/uuid" | ||
"github.com/linode/cluster-api-provider-linode/cloud/scope" | ||
"github.com/linode/cluster-api-provider-linode/util" | ||
"github.com/linode/cluster-api-provider-linode/util/reconciler" | ||
|
@@ -93,6 +94,8 @@ type LinodeMachineReconciler struct { | |
// | ||
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
// | ||
//nolint:gocyclo,cyclop // As simple as possible. | ||
func (r *LinodeMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultedLoopTimeout(r.ReconcileTimeout)) | ||
defer cancel() | ||
|
@@ -101,17 +104,21 @@ func (r *LinodeMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques | |
|
||
linodeMachine := &infrav1.LinodeMachine{} | ||
if err := r.Client.Get(ctx, req.NamespacedName, linodeMachine); err != nil { | ||
log.Error(err, "Failed to fetch Linode machine") | ||
if err = client.IgnoreNotFound(err); err != nil { | ||
log.Error(err, "Failed to fetch Linode machine") | ||
} | ||
|
||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
return ctrl.Result{}, err | ||
} | ||
|
||
machine, err := kutil.GetOwnerMachine(ctx, r.Client, linodeMachine.ObjectMeta) | ||
switch { | ||
case err != nil: | ||
log.Error(err, "Failed to fetch owner machine") | ||
if err = client.IgnoreNotFound(err); err != nil { | ||
log.Error(err, "Failed to fetch owner machine") | ||
} | ||
|
||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
return ctrl.Result{}, err | ||
case machine == nil: | ||
log.Info("Machine Controller has not yet set OwnerRef, skipping reconciliation") | ||
|
||
|
@@ -138,14 +145,25 @@ func (r *LinodeMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques | |
log = log.WithValues("Linode machine: ", machine.Name) | ||
|
||
cluster, err := kutil.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta) | ||
if err != nil { | ||
log.Info("Failed to fetch cluster by label") | ||
switch { | ||
case err != nil: | ||
if err = client.IgnoreNotFound(err); err != nil { | ||
log.Error(err, "Failed to fetch cluster by label") | ||
} | ||
|
||
return ctrl.Result{}, err | ||
case cluster == nil: | ||
err = errors.New("missing cluster") | ||
|
||
log.Error(err, "Missing cluster") | ||
|
||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} else if cluster == nil { | ||
log.Info("Failed to find cluster by label") | ||
return ctrl.Result{}, err | ||
case cluster.Spec.InfrastructureRef == nil: | ||
err = errors.New("missing infrastructure reference") | ||
|
||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
log.Error(err, "Missing infrastructure reference") | ||
|
||
return ctrl.Result{}, err | ||
} | ||
|
||
linodeCluster := &infrav1.LinodeCluster{} | ||
|
@@ -155,9 +173,11 @@ func (r *LinodeMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques | |
} | ||
|
||
if err = r.Client.Get(ctx, linodeClusterKey, linodeCluster); err != nil { | ||
log.Error(err, "Failed to fetch Linode cluster") | ||
if err = client.IgnoreNotFound(err); err != nil { | ||
log.Error(err, "Failed to fetch Linode cluster") | ||
} | ||
|
||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
return ctrl.Result{}, err | ||
} | ||
|
||
machineScope, err := scope.NewMachineScope( | ||
|
@@ -202,7 +222,7 @@ func (r *LinodeMachineReconciler) reconcile( | |
r.Recorder.Event(machineScope.LinodeMachine, corev1.EventTypeWarning, string(failureReason), err.Error()) | ||
} | ||
|
||
if patchErr := machineScope.PatchHelper.Patch(ctx, machineScope.LinodeMachine); patchErr != nil && client.IgnoreNotFound(patchErr) != nil { | ||
if patchErr := machineScope.PatchHelper.Patch(ctx, machineScope.LinodeMachine); patchErr != nil && util.IgnoreKubeNotFound(patchErr) != nil { | ||
logger.Error(patchErr, "failed to patch LinodeMachine") | ||
|
||
err = errors.Join(err, patchErr) | ||
|
@@ -274,12 +294,24 @@ func (r *LinodeMachineReconciler) reconcileCreate(ctx context.Context, machineSc | |
|
||
return nil, err | ||
} | ||
createConfig.Tags = tags | ||
|
||
if createConfig.Tags == nil { | ||
createConfig.Tags = []string{} | ||
} | ||
createConfig.Tags = append(createConfig.Tags, tags...) | ||
|
||
if createConfig.Label == "" { | ||
createConfig.Label = util.RenderObjectLabel(machineScope.LinodeMachine.UID) | ||
} | ||
|
||
if createConfig.Image == "" { | ||
createConfig.Image = reconciler.DefaultMachineControllerLinodeImage | ||
} | ||
|
||
if createConfig.RootPass == "" { | ||
createConfig.RootPass = uuid.NewString() | ||
} | ||
|
||
if machineScope.LinodeCluster.Spec.VPCRef != nil { | ||
iface, err := r.getVPCInterfaceConfig(ctx, machineScope, createConfig.Interfaces, logger) | ||
if err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*-generated.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
MAKEFLAGS += -s | ||
|
||
runTestCase: | ||
@T="$$(kuttl test --skip-delete --namespace $(NAMESPACE) $$(make _renderTestCase))" ;\ | ||
echo "$$T" | grep -s '0-step' ;\ | ||
echo "$$T" | grep -e '^PASS' | ||
|
||
_renderTestCase: | ||
@D=$$(mktemp -d) ;\ | ||
mkdir -p $$D/step ;\ | ||
envsubst -i $(TPL) -o $$D/step/00-step.yaml ;\ | ||
echo -n $$D | ||
|
||
getKubeUid: | ||
@kubectl get -o jsonpath='{.metadata.uid}' -n $(NAMESPACE) $R | ||
|
||
callLinodeApiGet: | ||
@curl -s -H "Authorization: Bearer $(LINODE_TOKEN)" -H "X-Filter: $(F)" "https://api.linode.com/v4beta/$(U)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: capi-controller-manager | ||
namespace: capi-system | ||
status: | ||
availableReplicas: 1 | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: cluster-api-provider-linode-controller-manager | ||
namespace: cluster-api-provider-linode-system | ||
status: | ||
availableReplicas: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: capi-controller-manager | ||
namespace: capi-system | ||
status: | ||
availableReplicas: 1 | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: cluster-api-provider-linode-controller-manager | ||
namespace: cluster-api-provider-linode-system | ||
status: | ||
availableReplicas: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Cluster | ||
metadata: | ||
annotations: | ||
cluster.x-k8s.io/paused: "true" | ||
name: cluster-sample | ||
spec: | ||
paused: true | ||
--- | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Machine | ||
metadata: | ||
annotations: | ||
cluster.x-k8s.io/paused: "true" | ||
name: machine-sample | ||
spec: | ||
clusterName: cluster-sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 | ||
kind: LinodeCluster | ||
metadata: | ||
annotations: | ||
cluster.x-k8s.io/paused: "true" | ||
name: linodecluster-sample | ||
--- | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Cluster | ||
metadata: | ||
annotations: | ||
cluster.x-k8s.io/paused: "true" | ||
name: cluster-sample | ||
spec: | ||
paused: true | ||
infrastructureRef: | ||
name: linodecluster-sample | ||
--- | ||
apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Machine | ||
metadata: | ||
annotations: | ||
cluster.x-k8s.io/paused: "true" | ||
name: machine-sample | ||
spec: | ||
clusterName: cluster-sample | ||
bootstrap: | ||
configRef: | ||
apiVersion: v1 | ||
kind: "ConfigMap" | ||
name: "boostrap-sample" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 | ||
kind: LinodeMachine | ||
metadata: | ||
name: linodemachine-sample | ||
spec: | ||
region: us-southeast | ||
type: g5-nanode-1 | ||
status: | ||
ready: true | ||
instanceState: running |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 | ||
kind: LinodeMachine | ||
metadata: | ||
ownerReferences: | ||
- apiVersion: cluster.x-k8s.io/v1beta1 | ||
kind: Machine | ||
name: machine-sample | ||
uid: ${MACHINE_UID} | ||
name: linodemachine-sample | ||
namespace: ${NAMESPACE} | ||
spec: | ||
region: us-southeast | ||
type: g5-nanode-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
apiVersion: kuttl.dev/v1beta1 | ||
kind: TestStep | ||
commands: | ||
- script: |- | ||
NAMESPACE=$NAMESPACE \ | ||
MACHINE_UID=$(R=machines/machine-sample make -C ../.. getKubeUid) \ | ||
TPL=$PWD/02-create-linodemachine.tpl.yml \ | ||
make -C ../.. runTestCase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: kuttl.dev/v1beta1 | ||
kind: TestStep | ||
delete: | ||
- apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 | ||
kind: LinodeMachine | ||
name: linodemachine-sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1 | ||
kind: LinodeMachine | ||
metadata: | ||
name: linodemachine-sample |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apiVersion: kuttl.dev/v1beta1 | ||
kind: TestStep | ||
commands: | ||
- script: |- | ||
U="linode/instances" F="{\\\"tags\\\":\\\"$(R=linodemachines/linodemachine-sample make -C ../.. getKubeUid)\\\"}" make -C ../.. callLinodeApiGet | grep 'results": 0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: capi-controller-manager | ||
namespace: capi-system | ||
status: | ||
availableReplicas: 1 | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: cluster-api-provider-linode-controller-manager | ||
namespace: cluster-api-provider-linode-system | ||
status: | ||
availableReplicas: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters