Skip to content
Merged
Changes from all 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
46 changes: 34 additions & 12 deletions hypershift-operator/controllers/hostedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ import (
)

const (
finalizer = "hypershift.openshift.io/finalizer"
pullSecretName = "pull-secret"
sshKeySecretName = "ssh-key"
providerCredsSecretName = "provider-creds"
finalizer = "hypershift.openshift.io/finalizer"
pullSecretName = "pull-secret"
sshKeySecretName = "ssh-key"
providerCredsSecretName = "provider-creds"
clusterNameIndexFieldName = "clusterName"
)

// HostedClusterReconciler reconciles a HostedCluster object
Expand Down Expand Up @@ -286,19 +287,30 @@ func (r *HostedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, nil
}

func (r *HostedClusterReconciler) listNodePools(key, value string) ([]hyperv1.NodePool, error) {
nodePoolList := &hyperv1.NodePoolList{}
if err := r.Client.List(
context.TODO(),
nodePoolList,
client.MatchingFields{key: value},
); err != nil {
return nil, fmt.Errorf("failed getting nodePool list: %v", err)
}
return nodePoolList.Items, nil
}

func (r *HostedClusterReconciler) delete(ctx context.Context, req ctrl.Request) error {
targetNamespace := req.Name

r.Log.Info("Deleting default nodePool", "name", req.Name)
defaultNodePool := &hyperv1.NodePool{
ObjectMeta: metav1.ObjectMeta{
Name: req.Name,
Namespace: req.Namespace,
},
nodePools, err := r.listNodePools(clusterNameIndexFieldName, req.Name)
if err != nil {
return fmt.Errorf("failed to get nodePools by cluster name for cluster %q: %w", req.Name, err)
}

if err := r.Delete(ctx, defaultNodePool); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to delete defaultNodePool: %w", err)
for key := range nodePools {
if err := r.Delete(ctx, &nodePools[key]); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("failed to delete defaultNodePool: %w", err)
}
}

r.Log.Info("Deleting cluster", "name", req.Name, "namespace", targetNamespace)
Expand Down Expand Up @@ -330,6 +342,16 @@ func (r *HostedClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
}
r.Infra = &infra

// index nodePool by clusterName

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

mgr.GetCache().IndexField(context.Background(), &hyperv1.NodePool{}, clusterNameIndexFieldName,
func(object client.Object) []string {
if nodePool, ok := object.(*hyperv1.NodePool); ok {
return []string{nodePool.Spec.ClusterName}
}
return nil
},
)

return ctrl.NewControllerManagedBy(mgr).
For(&hyperv1.HostedCluster{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
Expand Down