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

Updated discovery webhook logging for validation #240

Merged
merged 3 commits into from
Apr 25, 2024
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
34 changes: 15 additions & 19 deletions api/v1/discovery_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.
package v1

import (
"errors"
"fmt"

admissionregistration "k8s.io/api/admissionregistration/v1"
Expand All @@ -35,8 +34,6 @@ import (
var (
discoveredclusterLog = logf.Log.WithName("discoveredcluster-resource")
Client cl.Client

ErrInvalidImportStrategy = errors.New("invalid import-strategy")
)

// ValidatingWebhook returns the ValidatingWebhookConfiguration used for the discoveredcluster
Expand Down Expand Up @@ -107,42 +104,41 @@ var _ webhook.Validator = &DiscoveredCluster{}

// 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)
discoveredclusterLog.Info("validate create", "Name", r.Name, "Type", r.Spec.Type)

// Validate resource
if r.Spec.Type != "ROSA" && r.Spec.ImportAsManagedCluster {
return nil, fmt.Errorf(
err := fmt.Errorf(
"cannot create DiscoveredCluster '%s': importAsManagedCluster is not allowed for clusters of type '%s'. "+
"Only ROSA type clusters support auto import",
r.Spec.Type, r.Name,
)
"Only ROSA type clusters support auto import", r.Name, r.Spec.Type)

discoveredclusterLog.Error(err, "validation failed")
return nil, err
}

return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *DiscoveredCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
discoveredclusterLog.Info("validate update", "Name", r.Name)

if r.Annotations == nil {
r.Annotations = make(map[string]string)
}
discoveredclusterLog.Info("validate update", "Name", r.Name, "Type", r.Spec.Type)

// Validate resource
oldDiscoveredCluster := old.(*DiscoveredCluster)
if oldDiscoveredCluster.Spec.Type != "ROSA" && r.Spec.ImportAsManagedCluster {
return nil, fmt.Errorf(
"cannot update DiscoveredCluster '%s': importAsManagedCluster is not allowed for clusters of type '%s'."+
"Only ROSA type clusters support auto import",
r.Spec.Type, r.Name,
)
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)

discoveredclusterLog.Error(err, "validation failed")
return nil, err
}

return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *DiscoveredCluster) ValidateDelete() (admission.Warnings, error) {
discoveredclusterLog.Info("validate delete", "Name", r.Name)
discoveredclusterLog.Info("validate delete", "Name", r.Name, "Type", r.Spec.Type)
return nil, nil
}
Loading