Skip to content

Commit

Permalink
fix: add owner reference to cluster registration request
Browse files Browse the repository at this point in the history
  • Loading branch information
pratikjagrut committed Jul 4, 2024
1 parent 6d7cb2a commit ef89452
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,7 @@ func clusterNamespace(clusterNamespace, clusterName string) string {
}

func (h *handler) OnClusterChanged(cluster *fleet.Cluster, status fleet.ClusterStatus) (fleet.ClusterStatus, error) {
logrus.Debugf("OnClusterChanged for cluster status %s, checking cluster registration, updating namespace in status", cluster.Name)
if cluster.DeletionTimestamp != nil {
// cluster is being deleted, clean up the cluster registrations
clusterRegistrations, err := h.clusterRegistrations.List(cluster.Namespace, metav1.ListOptions{})
if err != nil {
return status, err
}
for _, clusterRegistration := range clusterRegistrations.Items {
if clusterRegistration.Status.ClusterName == cluster.Name {
err := h.clusterRegistrations.Delete(clusterRegistration.Namespace, clusterRegistration.Name, &metav1.DeleteOptions{})
if err == nil {
logrus.Debugf("deleted leftover ClusterRegistration (%s) for cluster: %s", clusterRegistration.Name, cluster.Name)
} else if !apierrors.IsNotFound(err) {
return status, err
}
}
}
return status, nil
}

logrus.Debugf("OnClusterChanged for cluster status %s, updating namespace in status", cluster.Name)
if status.Namespace == "" {
status.Namespace = clusterNamespace(cluster.Namespace, cluster.Name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@ func (h *handler) OnChange(request *fleet.ClusterRegistration, status fleet.Clus
return nil, status, nil
}

// set the Cluster as owner of the cluster registration request
// ownerFound is used to avoid calling update on request whenever OnChange is called
ownerFound := false
for _, owner := range request.OwnerReferences {
if owner.Kind == "Cluster" && owner.Name == cluster.Name && owner.UID == cluster.UID {
ownerFound = true
break
}
}
if !ownerFound {
request.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: fleet.SchemeGroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
},
})
request, err = h.clusterRegistration.Update(request)
if err != nil {
return nil, status, err
}
}

saName := name.SafeConcatName(request.Name, string(request.UID))
sa, err := h.serviceAccountCache.Get(cluster.Status.Namespace, saName)
if err != nil && apierrors.IsNotFound(err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ var _ = Describe("ClusterRegistration OnChange", func() {
BeforeEach(func() {
cluster.Status = fleet.ClusterStatus{Namespace: "fleet-default"}
saCache.EXPECT().Get(gomock.Any(), gomock.Any()).Return(nil, notFound)
clusterRegistrationController.EXPECT().Update(gomock.Any()).Return(&fleet.ClusterRegistration{}, nil)
})

It("creates a new service account", func() {
Expand All @@ -178,6 +179,7 @@ var _ = Describe("ClusterRegistration OnChange", func() {
// post k8s 1.24 service account without sa.Secrets list
sa = &corev1.ServiceAccount{}
saCache.EXPECT().Get(gomock.Any(), gomock.Any()).Return(sa, nil)
clusterRegistrationController.EXPECT().Update(gomock.Any()).Return(&fleet.ClusterRegistration{}, nil)
})

Context("cannot create secret", func() {
Expand Down Expand Up @@ -226,6 +228,8 @@ var _ = Describe("ClusterRegistration OnChange", func() {
secretController.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(secret, nil)

clusterRegistrationController.EXPECT().List(gomock.Any(), gomock.Any()).Return(&fleet.ClusterRegistrationList{}, nil)

clusterRegistrationController.EXPECT().Update(gomock.Any()).Return(&fleet.ClusterRegistration{}, nil)
})

Context("grants registration, cleans up and creates objects", func() {
Expand Down

0 comments on commit ef89452

Please sign in to comment.