Skip to content

Commit b9ac53a

Browse files
authored
Merge pull request #12060 from chrischdi/pr-cc-name-clusters-in-deletion-hook
🌱 clusterclass: improve webhook output to include the names of the clusters blocking a deletion
2 parents 59db2a7 + aafb69c commit b9ac53a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

internal/webhooks/clusterclass.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/util/sets"
3030
"k8s.io/apimachinery/pkg/util/validation"
3131
"k8s.io/apimachinery/pkg/util/validation/field"
32+
"k8s.io/klog/v2"
3233
ctrl "sigs.k8s.io/controller-runtime"
3334
"sigs.k8s.io/controller-runtime/pkg/client"
3435
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -40,6 +41,7 @@ import (
4041
"sigs.k8s.io/cluster-api/internal/topology/check"
4142
topologynames "sigs.k8s.io/cluster-api/internal/topology/names"
4243
"sigs.k8s.io/cluster-api/internal/topology/variables"
44+
clog "sigs.k8s.io/cluster-api/util/log"
4345
)
4446

4547
func (webhook *ClusterClass) SetupWebhookWithManager(mgr ctrl.Manager) error {
@@ -137,9 +139,11 @@ func (webhook *ClusterClass) ValidateDelete(ctx context.Context, obj runtime.Obj
137139
}
138140

139141
if len(clusters) > 0 {
140-
// TODO(killianmuldoon): Improve error here to include the names of some clusters using the clusterClass.
142+
clustersList := clog.ListToString(clusters, func(cluster clusterv1.Cluster) string {
143+
return klog.KObj(&cluster).String()
144+
}, 3)
141145
return nil, apierrors.NewForbidden(clusterv1.GroupVersion.WithResource("ClusterClass").GroupResource(), clusterClass.Name,
142-
fmt.Errorf("ClusterClass cannot be deleted because it is used by %d Cluster(s)", len(clusters)))
146+
fmt.Errorf("ClusterClass cannot be deleted because it is used by Cluster(s): %s", clustersList))
143147
}
144148
return nil, nil
145149
}

0 commit comments

Comments
 (0)