Skip to content

Commit

Permalink
feat: Functionality to create MicrovmDeployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Callisto13 committed Dec 7, 2022
1 parent 4c3aac3 commit 8515d6b
Show file tree
Hide file tree
Showing 9 changed files with 662 additions and 5 deletions.
9 changes: 9 additions & 0 deletions api/v1alpha1/condition_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ const (

// MicrovmReplicaSetUpdatingReason indicates the microvm is in a pending state.
MicrovmReplicaSetUpdatingReason = "MicrovmReplicaSetUpdating"

// MicrovmDeploymentReadyCondition indicates that the microvmreplicaset is in a complete state.
MicrovmDeploymentReadyCondition clusterv1.ConditionType = "MicrovmDeploymentReady"

// MicrovmDeploymentIncompleteReason indicates the microvmreplicaset does not have all replicas yet.
MicrovmDeploymentIncompleteReason = "MicrovmDeploymentIncomplete"

// MicrovmDeploymentProvisionFailedReason indicates that the microvm deployment failed to provision.
MicrovmDeploymentProvisionFailedReason = "MicrovmDeploymentProvisionFailed"
)
10 changes: 9 additions & 1 deletion api/v1alpha1/microvmdeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

const (
// MvDeploymentSFinalizer allows ReconcileMicrovmDeployment to clean up resources associated with the Deployment
// before removing it from the apiserver.
MvmDeploymentFinalizer = "microvmdeployment.infrastructure.microvm.x-k8s.io"
)

type HostMap map[string]struct{}

// MicrovmDeploymentSpec defines the desired state of MicrovmDeployment
type MicrovmDeploymentSpec struct {
// Replicas is the number of Microvms to create on the given Host with the given
Expand Down Expand Up @@ -49,7 +57,7 @@ type MicrovmDeploymentStatus struct {
// +optional
Replicas int32 `json:"replicas"`

// ReadyReplicas is the number of pods targeted by this ReplicaSet with a Ready Condition.
// ReadyReplicas is the number of microvms controlled by this Deployment with a Ready Condition.
// +optional
ReadyReplicas int32 `json:"readyReplicas,omitempty"`

Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/microvmreplicaset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type MicrovmReplicaSetStatus struct {
// +optional
Replicas int32 `json:"replicas"`

// ReadyReplicas is the number of pods targeted by this ReplicaSet with a Ready Condition.
// ReadyReplicas is the number of microvms targeted by this ReplicaSet with a Ready Condition.
// +optional
ReadyReplicas int32 `json:"readyReplicas,omitempty"`

Expand Down
101 changes: 101 additions & 0 deletions controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
testNamespace = "ns1"
testMicrovmName = "mvm1"
testMicrovmReplicaSetName = "rs1"
testMicrovmDeploymentName = "d1"
testMicrovmUID = "ABCDEF123456"
testBootstrapData = "somesamplebootstrapsdata"
)
Expand Down Expand Up @@ -97,6 +98,22 @@ func reconcileMicrovmReplicaSetNTimes(g *WithT, client client.Client, count int3
return nil
}

func reconcileMicrovmDeployment(client client.Client) (ctrl.Result, error) {
mvmDepController := &controllers.MicrovmDeploymentReconciler{
Client: client,
Scheme: client.Scheme(),
}

request := ctrl.Request{
NamespacedName: types.NamespacedName{
Name: testMicrovmDeploymentName,
Namespace: testNamespace,
},
}

return mvmDepController.Reconcile(context.TODO(), request)
}

func getMicrovm(c client.Client, name, namespace string) (*infrav1.Microvm, error) {
key := client.ObjectKey{
Name: name,
Expand All @@ -114,6 +131,12 @@ func listMicrovm(c client.Client) (*infrav1.MicrovmList, error) {
return mvm, err
}

func listMicrovmReplicaSet(c client.Client) (*infrav1.MicrovmReplicaSetList, error) {
mvmRS := &infrav1.MicrovmReplicaSetList{}
err := c.List(context.TODO(), mvmRS)
return mvmRS, err
}

func getMicrovmReplicaSet(c client.Client, name, namespace string) (*infrav1.MicrovmReplicaSet, error) {
key := client.ObjectKey{
Name: name,
Expand All @@ -125,6 +148,17 @@ func getMicrovmReplicaSet(c client.Client, name, namespace string) (*infrav1.Mic
return mvmRS, err
}

func getMicrovmDeployment(c client.Client, name, namespace string) (*infrav1.MicrovmDeployment, error) {
key := client.ObjectKey{
Name: name,
Namespace: namespace,
}

mvmD := &infrav1.MicrovmDeployment{}
err := c.Get(context.TODO(), key, mvmD)
return mvmD, err
}

func createFakeClient(g *WithT, objects []runtime.Object) client.Client {
scheme := runtime.NewScheme()

Expand Down Expand Up @@ -194,6 +228,33 @@ func createMicrovmReplicaSet(reps int32) *infrav1.MicrovmReplicaSet {
}
}

func createMicrovmDeployment(reps int32, hostCount int) *infrav1.MicrovmDeployment {
mvm := createMicrovm()
mvm.Spec.Host = microvm.Host{}

var hosts []microvm.Host

for i := 0; i < hostCount; i++ {
hosts = append(hosts, microvm.Host{
Endpoint: fmt.Sprintf("1.2.3.4:909%d", i),
})
}

return &infrav1.MicrovmDeployment{
ObjectMeta: metav1.ObjectMeta{
Name: testMicrovmDeploymentName,
Namespace: testNamespace,
},
Spec: infrav1.MicrovmDeploymentSpec{
Hosts: hosts,
Replicas: pointer.Int32(reps),
Template: infrav1.MicrovmTemplateSpec{
Spec: mvm.Spec,
},
},
}
}

func withExistingMicrovm(fc *fakes.FakeClient, mvmState flintlocktypes.MicroVMStatus_MicroVMState) {
fc.GetMicroVMReturns(&flintlockv1.GetMicroVMResponse{
Microvm: &flintlocktypes.MicroVM{
Expand Down Expand Up @@ -252,12 +313,34 @@ func assertMicrovmReconciled(g *WithT, reconciled *infrav1.Microvm) {
g.Expect(reconciled.Status.Ready).To(BeTrue(), "The Ready property must be true when the mvm has been reconciled")
}

func assertOneSetPerHost(g *WithT, reconciled *infrav1.MicrovmDeployment, c client.Client) {
hosts := reconciled.Spec.Hosts
sets, err := listMicrovmReplicaSet(c)
g.Expect(err).NotTo(HaveOccurred())

g.Expect(len(hosts)).To(Equal(len(sets.Items)))

seen := map[string]struct{}{}

for _, rs := range sets.Items {
seen[rs.Spec.Host.Endpoint] = struct{}{}
}

g.Expect(seen).To(HaveLen(len(hosts)))
}

func microvmsCreated(g *WithT, c client.Client) int32 {
mvmList, err := listMicrovm(c)
g.Expect(err).NotTo(HaveOccurred())
return int32(len(mvmList.Items))
}

func microvmReplicaSetsCreated(g *WithT, c client.Client) int {
mvmList, err := listMicrovmReplicaSet(c)
g.Expect(err).NotTo(HaveOccurred())
return len(mvmList.Items)
}

func ensureMicrovmState(g *WithT, c client.Client) {
// update the microvms so they report as ready to move the replicaset reconciliation along
mvmList, err := listMicrovm(c)
Expand All @@ -269,6 +352,19 @@ func ensureMicrovmState(g *WithT, c client.Client) {
}
}

func ensureMicrovmReplicaSetState(g *WithT, c client.Client, r, rr int32) {
// update the microvmreplicasets so they report as ready to move the deployment reconciliation along
mvmList, err := listMicrovmReplicaSet(c)
g.Expect(err).NotTo(HaveOccurred())

for _, mvm := range mvmList.Items {
mvm.Status.Ready = true
mvm.Status.ReadyReplicas = rr
mvm.Status.Replicas = r
g.Expect(c.Update(context.TODO(), &mvm)).To(Succeed())
}
}

func assertFinalizer(g *WithT, reconciled *infrav1.Microvm) {
g.Expect(reconciled.ObjectMeta.Finalizers).NotTo(BeEmpty(), "Expected at least one finalizer to be set")
g.Expect(hasMicrovmFinalizer(&reconciled.ObjectMeta, infrav1.MvmFinalizer)).To(BeTrue(), "Expect the mvm finalizer")
Expand All @@ -279,6 +375,11 @@ func assertMRSFinalizer(g *WithT, reconciled *infrav1.MicrovmReplicaSet) {
g.Expect(hasMicrovmFinalizer(&reconciled.ObjectMeta, infrav1.MvmRSFinalizer)).To(BeTrue(), "Expect the mvmrs finalizer")
}

func assertMDFinalizer(g *WithT, reconciled *infrav1.MicrovmDeployment) {
g.Expect(reconciled.ObjectMeta.Finalizers).NotTo(BeEmpty(), "Expected at least one finalizer to be set")
g.Expect(hasMicrovmFinalizer(&reconciled.ObjectMeta, infrav1.MvmDeploymentFinalizer)).To(BeTrue(), "Expect the mvmd finalizer")
}

func hasMicrovmFinalizer(meta *metav1.ObjectMeta, finalizer string) bool {
if len(meta.Finalizers) == 0 {
return false
Expand Down
Loading

0 comments on commit 8515d6b

Please sign in to comment.