Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach a byomachine to multiple byohost in some condition by mistake #185

Merged
merged 7 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions agent/reconciler/host_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,15 @@ func (r *HostReconciler) hostCleanUp(ctx context.Context, byoHost *infrastructur
// Remove host reservation
byoHost.Status.MachineRef = nil

// Remove BootstrapSecret
byoHost.Spec.BootstrapSecret = nil

// Remove cluster-name label
delete(byoHost.Labels, clusterv1.ClusterLabelName)

// Remove Byomachine-name label
delete(byoHost.Labels, infrastructurev1beta1.AttachedByoMachineName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
delete(byoHost.Labels, infrastructurev1beta1.AttachedByoMachineName)
delete(byoHost.Labels, infrastructurev1beta1.ByoMachineName)


// Remove the EndPointIP annotation
delete(byoHost.Annotations, infrastructurev1beta1.EndPointIPAnnotation)

Expand Down
7 changes: 4 additions & 3 deletions apis/infrastructure/v1beta1/byohost_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
)

const (
HostCleanupAnnotation = "byoh.infrastructure.cluster.x-k8s.io/unregistering"
EndPointIPAnnotation = "byoh.infrastructure.cluster.x-k8s.io/endpointip"
K8sVersionAnnotation = "byoh.infrastructure.cluster.x-k8s.io/k8sversion"
HostCleanupAnnotation = "byoh.infrastructure.cluster.x-k8s.io/unregistering"
EndPointIPAnnotation = "byoh.infrastructure.cluster.x-k8s.io/endpointip"
K8sVersionAnnotation = "byoh.infrastructure.cluster.x-k8s.io/k8sversion"
AttachedByoMachineLabel = "byoh.infrastructure.cluster.x-k8s.io/byomachine-name"
dharmjit marked this conversation as resolved.
Show resolved Hide resolved
)

// ByoHostSpec defines the desired state of ByoHost
Expand Down
36 changes: 27 additions & 9 deletions controllers/infrastructure/byomachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,11 @@ func (r *ByoMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}()

// Fetch the BYOHost which is referencing this machine, if any
hostsList := &infrav1.ByoHostList{}
err = r.Client.List(
ctx,
hostsList,
client.MatchingFields{hostMachineRefIndex: fmt.Sprintf("%s/%s", byoMachine.Namespace, byoMachine.Name)},
huchen2021 marked this conversation as resolved.
Show resolved Hide resolved
)
refByoHost, err := r.FetchAttachedByoHost(ctx, byoMachine.Name, byoMachine.Namespace)
if err != nil {
return ctrl.Result{}, err
}
var refByoHost *infrav1.ByoHost
if len(hostsList.Items) == 1 {
refByoHost = &hostsList.Items[0]
if refByoHost != nil {
logger = logger.WithValues("BYOHost", refByoHost.Name)
}

Expand Down Expand Up @@ -170,6 +163,30 @@ func (r *ByoMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return r.reconcileNormal(ctx, machineScope)
}

func (r *ByoMachineReconciler) FetchAttachedByoHost(ctx context.Context, byomachineName, byomachineNamespace string) (*infrav1.ByoHost, error) {
logger := log.FromContext(ctx)
logger.Info("Fetching an attached ByoHost")

selector := labels.NewSelector()
byohostLabels, _ := labels.NewRequirement(infrav1.AttachedByoMachineName, selection.Equals, []string{byomachineNamespace + "." + byomachineName})
selector = selector.Add(*byohostLabels)
hostsList := &infrav1.ByoHostList{}
err := r.Client.List(
ctx,
hostsList,
&client.ListOptions{LabelSelector: selector},
)
if err != nil {
return nil, err
}
var refByoHost *infrav1.ByoHost = nil
if len(hostsList.Items) == 1 {
huchen2021 marked this conversation as resolved.
Show resolved Hide resolved
refByoHost = &hostsList.Items[0]
logger.Info("Successfully to fetch an attached Byohost", "byohost", refByoHost.Name)
huchen2021 marked this conversation as resolved.
Show resolved Hide resolved
}
return refByoHost, nil
}

func (r *ByoMachineReconciler) reconcileDelete(ctx context.Context, machineScope *byoMachineScope) (reconcile.Result, error) {
logger := log.FromContext(ctx).WithValues("cluster", machineScope.Cluster.Name)
logger.Info("Deleting ByoMachine")
Expand Down Expand Up @@ -427,6 +444,7 @@ func (r *ByoMachineReconciler) attachByoHost(ctx context.Context, machineScope *
hostLabels = make(map[string]string)
}
hostLabels[clusterv1.ClusterLabelName] = machineScope.ByoMachine.Labels[clusterv1.ClusterLabelName]
hostLabels[infrav1.AttachedByoMachineName] = machineScope.ByoMachine.Namespace + "." + machineScope.ByoMachine.Name
host.Labels = hostLabels

host.Spec.BootstrapSecret = &corev1.ObjectReference{
Expand Down
5 changes: 4 additions & 1 deletion controllers/infrastructure/byomachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
infrastructurev1beta1 "github.com/vmware-tanzu/cluster-api-provider-byoh/apis/infrastructure/v1beta1"

"github.com/vmware-tanzu/cluster-api-provider-byoh/test/builder"
eventutils "github.com/vmware-tanzu/cluster-api-provider-byoh/test/utils/events"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -248,6 +247,10 @@ var _ = Describe("Controllers/ByomachineController", func() {
UID: byoMachine.UID,
APIVersion: byoHost.APIVersion,
}
if byoHost.Labels == nil {
byoHost.Labels = make(map[string]string)
}
byoHost.Labels[infrastructurev1beta1.AttachedByoMachineName] = byoMachine.Namespace + "." + byoMachine.Name
Expect(ph.Patch(ctx, byoHost, patch.WithStatusObservedGeneration{})).Should(Succeed())

WaitForObjectToBeUpdatedInCache(byoHost, func(object client.Object) bool {
Expand Down