-
Notifications
You must be signed in to change notification settings - Fork 22
Webhook to check name + namespace exceeds 63 characters #117
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,8 +81,13 @@ jobs: | |
|
||
- name: Verify Deployment Configuration | ||
run: | | ||
make webhook | ||
make build-images | ||
make kind-deploy-controller-dev | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I added |
||
|
||
- name: E2E Tests for Webhook | ||
run: | | ||
make e2e-test-webhook | ||
|
||
- name: Debug | ||
if: ${{ failure() }} | ||
|
@@ -93,3 +98,4 @@ jobs: | |
if: ${{ always() }} | ||
run: | | ||
make kind-delete-cluster | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) 2021 Red Hat, Inc. | ||
// Copyright Contributors to the Open Cluster Management project | ||
|
||
package v1 | ||
|
||
import ( | ||
"errors" | ||
"unicode/utf8" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
var ( | ||
// log is for logging in this package. | ||
policylog = logf.Log.WithName("policy-validating-webhook") | ||
errName = errors.New("the combined length of the policy namespace and name " + | ||
"<namespace>.<name> cannot exceed 63 characters") | ||
) | ||
|
||
func (r *Policy) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(r). | ||
Complete() | ||
} | ||
|
||
//+kubebuilder:webhook:path=/validate-policy-open-cluster-management-io-v1-policy,mutating=false,failurePolicy=Ignore,sideEffects=None,groups=policy.open-cluster-management.io,resources=policies,verbs=create,versions=v1,name=policy.open-cluster-management.io.webhook,admissionReviewVersions=v1 | ||
|
||
var _ webhook.Validator = &Policy{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *Policy) ValidateCreate() (admission.Warnings, error) { | ||
policylog.Info("Validate policy creation request", "name", r.Name) | ||
|
||
return r.validateName() | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *Policy) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) { | ||
mprahl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, nil | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
func (r *Policy) ValidateDelete() (admission.Warnings, error) { | ||
mprahl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, nil | ||
} | ||
|
||
// validate the policy name and namespace length | ||
func (r *Policy) validateName() (admission.Warnings, error) { | ||
policylog.Info("Validating the policy name through a validating webhook") | ||
|
||
// replicated policies don't need pass this validation | ||
if _, ok := r.GetLabels()["policy.open-cluster-management.io/root-policy"]; ok { | ||
return nil, nil | ||
} | ||
|
||
// 1 character for "." | ||
if (utf8.RuneCountInString(r.Name) + utf8.RuneCountInString(r.Namespace)) > 62 { | ||
return nil, errName | ||
} | ||
|
||
return nil, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
apiVersion: v1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that when this is added to ACM, you'll want to use OpenShift's mechanism of generating certificates instead of Cert Manager: No action is needed in this PR though. |
||
kind: Service | ||
metadata: | ||
name: propagator-webhook-service | ||
namespace: open-cluster-management | ||
spec: | ||
ports: | ||
- port: 443 | ||
protocol: TCP | ||
targetPort: 9443 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to use the name (i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why I change to |
||
selector: | ||
webhook-origin: governance-policy-propagator | ||
--- | ||
apiVersion: cert-manager.io/v1 | ||
kind: Certificate | ||
metadata: | ||
name: propagator-webhook-serving-cert | ||
namespace: open-cluster-management | ||
spec: | ||
dnsNames: | ||
- propagator-webhook-service.open-cluster-management.svc | ||
- propagator-webhook-service.open-cluster-management.svc.cluster.local | ||
issuerRef: | ||
kind: Issuer | ||
name: propagator-webhook-selfsigned-issuer | ||
secretName: propagator-webhook-server-cert | ||
--- | ||
apiVersion: cert-manager.io/v1 | ||
kind: Issuer | ||
metadata: | ||
name: propagator-webhook-selfsigned-issuer | ||
namespace: open-cluster-management | ||
spec: | ||
selfSigned: {} | ||
--- | ||
apiVersion: admissionregistration.k8s.io/v1 | ||
kind: ValidatingWebhookConfiguration | ||
metadata: | ||
annotations: | ||
cert-manager.io/inject-ca-from: open-cluster-management/propagator-webhook-serving-cert | ||
name: propagator-webhook-validating-configuration | ||
webhooks: | ||
- admissionReviewVersions: | ||
- v1 | ||
clientConfig: | ||
service: | ||
name: propagator-webhook-service | ||
namespace: open-cluster-management | ||
path: /validate-policy-open-cluster-management-io-v1-policy | ||
failurePolicy: Ignore | ||
name: policy.open-cluster-management.io.webhook | ||
rules: | ||
- apiGroups: | ||
- policy.open-cluster-management.io | ||
apiVersions: | ||
- v1 | ||
operations: | ||
- CREATE | ||
resources: | ||
- policies | ||
sideEffects: None |
Uh oh!
There was an error while loading. Please reload this page.