-
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
Feb 6, 2024
1 parent
e1fccfc
commit c0c809a
Showing
27 changed files
with
378 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ jobs: | |
- name: Docker cache | ||
uses: ScribeMD/[email protected] | ||
with: | ||
key: docker-${{ runner.os }}-${{ hashFiles('Makefile') }}} | ||
key: docker-${{ runner.os }}-${{ hashFiles('go.sum') }}} | ||
|
||
- name: Lint | ||
run: make lint | ||
|
@@ -100,6 +100,7 @@ jobs: | |
disable-sudo: true | ||
egress-policy: block | ||
allowed-endpoints: > | ||
api.linode.com:443 | ||
api.github.com:443 | ||
github.com:443 | ||
gcr.io:443 | ||
|
@@ -130,7 +131,7 @@ jobs: | |
- name: Docker cache | ||
uses: ScribeMD/[email protected] | ||
with: | ||
key: docker-${{ runner.os }}-${{ hashFiles('Makefile') }}} | ||
key: docker-${{ runner.os }}-${{ hashFiles('go.sum') }}} | ||
|
||
- name: E2E test | ||
run: make e2etest | ||
|
@@ -173,7 +174,7 @@ jobs: | |
- name: Docker cache | ||
uses: ScribeMD/[email protected] | ||
with: | ||
key: docker-${{ runner.os }}-${{ hashFiles('Makefile') }} | ||
key: docker-${{ runner.os }}-${{ hashFiles('go.sum') }} | ||
|
||
- name: Build the Docker image | ||
run: make docker-build |
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 |
---|---|---|
|
@@ -18,7 +18,6 @@ package controller | |
|
||
import ( | ||
"context" | ||
b64 "encoding/base64" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
@@ -31,6 +30,7 @@ import ( | |
"github.com/linode/linodego" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
"k8s.io/client-go/tools/record" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
cerrs "sigs.k8s.io/cluster-api/errors" | ||
|
@@ -44,7 +44,7 @@ import ( | |
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
|
||
infrav1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1" | ||
infrav1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1" | ||
) | ||
|
||
var skippedMachinePhases = map[string]bool{ | ||
|
@@ -94,25 +94,31 @@ 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() | ||
|
||
log := ctrl.LoggerFrom(ctx).WithName("LinodeMachineReconciler").WithValues("name", req.NamespacedName.String()) | ||
|
||
linodeMachine := &infrav1.LinodeMachine{} | ||
linodeMachine := &infrav1alpha1.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,26 +144,39 @@ 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{}, err | ||
case cluster.Spec.InfrastructureRef == nil: | ||
err = errors.New("missing infrastructure reference") | ||
|
||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} else if cluster == nil { | ||
log.Info("Failed to find cluster by label") | ||
log.Error(err, "Missing infrastructure reference") | ||
|
||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
return ctrl.Result{}, err | ||
} | ||
|
||
linodeCluster := &infrav1.LinodeCluster{} | ||
linodeCluster := &infrav1alpha1.LinodeCluster{} | ||
linodeClusterKey := client.ObjectKey{ | ||
Namespace: linodeMachine.Namespace, | ||
Name: cluster.Spec.InfrastructureRef.Name, | ||
} | ||
|
||
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( | ||
|
@@ -203,7 +222,7 @@ func (r *LinodeMachineReconciler) reconcile( | |
} | ||
|
||
// Always close the scope when exiting this function so we can persist any LinodeMachine changes. | ||
if patchErr := machineScope.Close(ctx); patchErr != nil && client.IgnoreNotFound(patchErr) != nil { | ||
if patchErr := machineScope.Close(ctx); patchErr != nil && utilerrors.FilterOut(patchErr) != nil { | ||
logger.Error(patchErr, "failed to patch LinodeMachine") | ||
|
||
err = errors.Join(err, patchErr) | ||
|
@@ -278,31 +297,13 @@ func (r *LinodeMachineReconciler) reconcileCreate(ctx context.Context, machineSc | |
|
||
linodeInstance = &linodeInstances[0] | ||
case 0: | ||
createConfig := linodeMachineSpecToInstanceCreateConfig(machineScope.LinodeMachine.Spec) | ||
if createConfig == nil { | ||
err = errors.New("failed to convert machine spec to create instance config") | ||
|
||
logger.Error(err, "Panic! Struct of LinodeMachineSpec is different than InstanceCreateOptions") | ||
|
||
return nil, err | ||
} | ||
createConfig.Tags = tags | ||
createConfig.SwapSize = util.Pointer(0) | ||
|
||
// get the bootstrap data for the Linode instance and set it for create config | ||
bootstrapData, err := machineScope.GetBootstrapData(ctx) | ||
createConfig, err := r.newCreateConfig(ctx, machineScope, tags, logger) | ||
if err != nil { | ||
logger.Info("Failed to get bootstrap data", "error", err.Error()) | ||
logger.Error(err, "Failed to create Linode machine create config") | ||
|
||
return nil, err | ||
} | ||
createConfig.Metadata = &linodego.InstanceMetadataOptions{ | ||
UserData: b64.StdEncoding.EncodeToString(bootstrapData), | ||
} | ||
|
||
if createConfig.Label == "" { | ||
createConfig.Label = util.RenderObjectLabel(machineScope.LinodeMachine.UID) | ||
} | ||
|
||
if machineScope.LinodeCluster.Spec.VPCRef != nil { | ||
iface, err := r.getVPCInterfaceConfig(ctx, machineScope, createConfig.Interfaces, logger) | ||
|
@@ -428,21 +429,21 @@ func (r *LinodeMachineReconciler) reconcileDelete(ctx context.Context, logger lo | |
|
||
machineScope.LinodeMachine.Spec.ProviderID = nil | ||
machineScope.LinodeMachine.Spec.InstanceID = nil | ||
controllerutil.RemoveFinalizer(machineScope.LinodeMachine, infrav1.GroupVersion.String()) | ||
controllerutil.RemoveFinalizer(machineScope.LinodeMachine, infrav1alpha1.GroupVersion.String()) | ||
|
||
return nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *LinodeMachineReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
controller, err := ctrl.NewControllerManagedBy(mgr). | ||
For(&infrav1.LinodeMachine{}). | ||
For(&infrav1alpha1.LinodeMachine{}). | ||
Watches( | ||
&clusterv1.Machine{}, | ||
handler.EnqueueRequestsFromMapFunc(kutil.MachineToInfrastructureMapFunc(infrav1.GroupVersion.WithKind("LinodeMachine"))), | ||
handler.EnqueueRequestsFromMapFunc(kutil.MachineToInfrastructureMapFunc(infrav1alpha1.GroupVersion.WithKind("LinodeMachine"))), | ||
). | ||
Watches( | ||
&infrav1.LinodeCluster{}, | ||
&infrav1alpha1.LinodeCluster{}, | ||
handler.EnqueueRequestsFromMapFunc(r.linodeClusterToLinodeMachines(mgr.GetLogger())), | ||
). | ||
WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(mgr.GetLogger(), r.WatchFilterValue)). | ||
|
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
Oops, something went wrong.