Skip to content

Commit

Permalink
updated webhook code
Browse files Browse the repository at this point in the history
Signed-off-by: Disaiah Bennett <[email protected]>
  • Loading branch information
dislbenn committed May 22, 2024
1 parent 3c110a9 commit eaffe7b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions api/v1/discovery_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,22 @@ func (r *DiscoveredCluster) Default() {

var _ webhook.Validator = &DiscoveredCluster{}

// isSupportedClusterType checks if the cluster type is supported for the import
func isSupportedClusterType(clusterType string) bool {
supportedTypes := map[string]bool{
"MultiClusterEngineHCP": true,
"ROSA": true,
}

return supportedTypes[clusterType]
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *DiscoveredCluster) ValidateCreate() (admission.Warnings, error) {
discoveredclusterLog.Info("validate create", "Name", r.Name, "Type", r.Spec.Type)

// Validate resource
if r.Spec.Type != "ROSA" && r.Spec.Type != "MultiClusterEngineHCP" && r.Spec.ImportAsManagedCluster {
if !isSupportedClusterType(r.Spec.Type) && r.Spec.ImportAsManagedCluster {
err := fmt.Errorf(
"cannot create DiscoveredCluster '%s': importAsManagedCluster is not allowed for clusters of type '%s'. "+
"Only ROSA type clusters support auto import", r.Name, r.Spec.Type)
Expand All @@ -125,7 +135,7 @@ func (r *DiscoveredCluster) ValidateUpdate(old runtime.Object) (admission.Warnin

// Validate resource
oldDiscoveredCluster := old.(*DiscoveredCluster)
if oldDiscoveredCluster.Spec.Type != "ROSA" && oldDiscoveredCluster.Spec.Type != "MultiClusterEngineHCP" && r.Spec.ImportAsManagedCluster {
if !isSupportedClusterType(oldDiscoveredCluster.Spec.Type) && r.Spec.ImportAsManagedCluster {
err := fmt.Errorf(
"cannot update DiscoveredCluster '%s': importAsManagedCluster is not allowed for clusters of type '%s'. "+
"Only ROSA type clusters support auto import", r.Name, r.Spec.Type)
Expand Down

0 comments on commit eaffe7b

Please sign in to comment.