-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reconcile logic for MD Upgrade controller
- Loading branch information
Showing
9 changed files
with
316 additions
and
25 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
115 changes: 115 additions & 0 deletions
115
controllers/machinedeploymentupgrade_controller_test.go
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,115 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"github.com/aws/eks-anywhere/controllers" | ||
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1" | ||
) | ||
|
||
func TestMDUpgradeReconcile(t *testing.T) { | ||
g := NewWithT(t) | ||
ctx := context.Background() | ||
|
||
cluster, machine, node, mdUpgrade, nodeUpgrade := getObjectsForMDUpgradeTest() | ||
nodeUpgrade.Name = fmt.Sprintf("%s-node-upgrade", machine.Name) | ||
nodeUpgrade.Status = anywherev1.NodeUpgradeStatus{ | ||
Completed: true, | ||
} | ||
client := fake.NewClientBuilder().WithRuntimeObjects(cluster, machine, node, mdUpgrade, nodeUpgrade).Build() | ||
|
||
r := controllers.NewMachineDeploymentUpgradeReconciler(client) | ||
req := mdUpgradeRequest(mdUpgrade) | ||
_, err := r.Reconcile(ctx, req) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
mdu := &anywherev1.MachineDeploymentUpgrade{} | ||
err = client.Get(ctx, types.NamespacedName{Name: mdUpgrade.Name, Namespace: "eksa-system"}, mdu) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
} | ||
|
||
func TestMDUpgradeReconcileDelete(t *testing.T) { | ||
g := NewWithT(t) | ||
ctx := context.Background() | ||
|
||
cluster, machine, node, mdUpgrade, nodeUpgrade := getObjectsForMDUpgradeTest() | ||
nodeUpgrade.Name = fmt.Sprintf("%s-node-upgrade", machine.Name) | ||
nodeUpgrade.Status = anywherev1.NodeUpgradeStatus{ | ||
Completed: true, | ||
} | ||
client := fake.NewClientBuilder().WithRuntimeObjects(cluster, machine, node, mdUpgrade, nodeUpgrade).Build() | ||
|
||
r := controllers.NewMachineDeploymentUpgradeReconciler(client) | ||
req := mdUpgradeRequest(mdUpgrade) | ||
_, err := r.Reconcile(ctx, req) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
mdu := &anywherev1.MachineDeploymentUpgrade{} | ||
err = client.Get(ctx, types.NamespacedName{Name: mdUpgrade.Name, Namespace: "eksa-system"}, mdu) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
err = client.Delete(ctx, mdUpgrade) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
_, err = r.Reconcile(ctx, req) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
mdu = &anywherev1.MachineDeploymentUpgrade{} | ||
err = client.Get(ctx, types.NamespacedName{Name: mdUpgrade.Name, Namespace: "eksa-system"}, mdu) | ||
g.Expect(err).To(MatchError("machinedeploymentupgrades.anywhere.eks.amazonaws.com \"md-upgrade-request\" not found")) | ||
} | ||
|
||
func getObjectsForMDUpgradeTest() (*clusterv1.Cluster, *clusterv1.Machine, *corev1.Node, *anywherev1.MachineDeploymentUpgrade, *anywherev1.NodeUpgrade) { | ||
cluster := generateCluster() | ||
node := generateNode() | ||
machine := generateMachine(cluster, node) | ||
nodeUpgrade := generateNodeUpgrade(machine) | ||
mdUpgrade := generateMDUpgrade(machine, cluster) | ||
return cluster, machine, node, mdUpgrade, nodeUpgrade | ||
} | ||
|
||
func mdUpgradeRequest(mdUpgrade *anywherev1.MachineDeploymentUpgrade) reconcile.Request { | ||
return reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: mdUpgrade.Name, | ||
Namespace: mdUpgrade.Namespace, | ||
}, | ||
} | ||
} | ||
|
||
func generateMDUpgrade(machine *clusterv1.Machine, cluster *clusterv1.Cluster) *anywherev1.MachineDeploymentUpgrade { | ||
return &anywherev1.MachineDeploymentUpgrade{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "md-upgrade-request", | ||
Namespace: "eksa-system", | ||
}, | ||
Spec: anywherev1.MachineDeploymentUpgradeSpec{ | ||
Cluster: anywherev1.Ref{ | ||
Name: cluster.Name, | ||
Kind: "Cluster", | ||
}, | ||
MachineDeployment: anywherev1.Ref{ | ||
Name: "my-md", | ||
Kind: "MachineDeployment", | ||
}, | ||
MachinesRequireUpgrade: []anywherev1.Ref{ | ||
{ | ||
Name: machine.Name, | ||
Kind: "Machine", | ||
}, | ||
}, | ||
KubernetesVersion: "v1.28.1", | ||
KubeletVersion: "v1.28.1", | ||
KubeadmClusterConfig: "", | ||
}, | ||
} | ||
} |
Oops, something went wrong.