-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds validating and mutating webhooks for MirrorPeer. Signed-off-by: Vineet Badrinath <vineetbnath@gmail.com>
- Loading branch information
Showing
20 changed files
with
419 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
Copyright 2021 Red Hat OpenShift Data Foundation. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"regexp" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
) | ||
|
||
// log is for logging in this package. | ||
var mirrorpeerlog = logf.Log.WithName("mirrorpeer-webhook") | ||
|
||
const ( | ||
WebhookCertDir = "/apiserver.local.config/certificates" | ||
WebhookCertName = "apiserver.crt" | ||
WebhookKeyName = "apiserver.key" | ||
) | ||
|
||
func (r *MirrorPeer) SetupWebhookWithManager(mgr ctrl.Manager) error { | ||
bldr := ctrl.NewWebhookManagedBy(mgr). | ||
For(r) | ||
|
||
srv := mgr.GetWebhookServer() | ||
srv.CertDir = WebhookCertDir | ||
srv.CertName = WebhookCertName | ||
srv.KeyName = WebhookKeyName | ||
|
||
return bldr.Complete() | ||
} | ||
|
||
//+kubebuilder:webhook:path=/mutate-multicluster-odf-openshift-io-v1alpha1-mirrorpeer,mutating=true,failurePolicy=fail,sideEffects=None,groups=multicluster.odf.openshift.io,resources=mirrorpeers,verbs=create;update,versions=v1alpha1,name=mmirrorpeer.kb.io,admissionReviewVersions=v1;v1beta1 | ||
|
||
var _ webhook.Defaulter = &MirrorPeer{} | ||
|
||
// Default implements webhook.Defaulter so a webhook will be registered for the type | ||
func (r *MirrorPeer) Default() { | ||
if len(r.Spec.SchedulingIntervals) == 0 { | ||
mirrorpeerlog.Info("No values set for schedulingIntervals, defaulting to 5m", "MirrorPeer", r.ObjectMeta.Name) | ||
r.Spec.SchedulingIntervals = []string{"5m"} | ||
} | ||
} | ||
|
||
//+kubebuilder:webhook:path=/validate-multicluster-odf-openshift-io-v1alpha1-mirrorpeer,mutating=false,failurePolicy=fail,sideEffects=None,groups=multicluster.odf.openshift.io,resources=mirrorpeers,verbs=create;update;delete,versions=v1alpha1,name=vmirrorpeer.kb.io,admissionReviewVersions=v1;v1beta1 | ||
|
||
var _ webhook.Validator = &MirrorPeer{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *MirrorPeer) ValidateCreate() error { | ||
mirrorpeerlog.Info("validate create", "name", r.ObjectMeta.Name) | ||
|
||
return validateMirrorPeer(r) | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *MirrorPeer) ValidateUpdate(old runtime.Object) error { | ||
mirrorpeerlog.Info("validate update", "name", r.ObjectMeta.Name) | ||
oldMirrorPeer, ok := old.(*MirrorPeer) | ||
if !ok { | ||
return fmt.Errorf("error casting old object to MirrorPeer") | ||
} | ||
|
||
if len(r.Spec.Items) != len(oldMirrorPeer.Spec.Items) { | ||
return fmt.Errorf("error updating MirrorPeer, new and old spec.items have different lengths") | ||
} | ||
|
||
refs := make(map[string]int) | ||
for idx, pr := range oldMirrorPeer.Spec.Items { | ||
// Creating a make-shift set of references to check for duplicates | ||
refs[fmt.Sprintf("%s-%s-%s", pr.ClusterName, pr.StorageClusterRef.Namespace, pr.StorageClusterRef.Name)] = idx | ||
} | ||
|
||
for _, pr := range r.Spec.Items { | ||
key := fmt.Sprintf("%s-%s-%s", pr.ClusterName, pr.StorageClusterRef.Namespace, pr.StorageClusterRef.Name) | ||
if _, ok := refs[key]; !ok { | ||
return fmt.Errorf("error validating update: new MirrorPeer %s references a StorageCluster %s/%s that is not in the old MirrorPeer", r.ObjectMeta.Name, pr.StorageClusterRef.Namespace, pr.StorageClusterRef.Name) | ||
} | ||
} | ||
return validateMirrorPeer(r) | ||
} | ||
|
||
/* | ||
validateSchedulingInterval checks if the scheduling interval is valid and ending with a 'm' or 'h' or 'd' | ||
else it will return error. | ||
*/ | ||
func validateSchedulingInterval(interval string) error { | ||
re := regexp.MustCompile(`^\d+[mhd]$`) | ||
if re.MatchString(interval) { | ||
return nil | ||
} | ||
return fmt.Errorf("invalid scheduling interval %q. interval should have suffix 'm|h|d' ", interval) | ||
} | ||
|
||
// validateMirrorPeer validates the MirrorPeer | ||
func validateMirrorPeer(instance *MirrorPeer) error { | ||
|
||
if instance.Spec.Items == nil { | ||
return fmt.Errorf("Spec.Items can not be nil") | ||
} | ||
|
||
if instance.Spec.SchedulingIntervals == nil || len(instance.Spec.SchedulingIntervals) == 0 { | ||
return fmt.Errorf("Spec.SchedulingIntervals can not be nil or empty") | ||
} | ||
|
||
for _, interval := range instance.Spec.SchedulingIntervals { | ||
if err := validateSchedulingInterval(interval); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type | ||
func (r *MirrorPeer) ValidateDelete() error { | ||
mirrorpeerlog.Info("validate delete", "name", r.ObjectMeta.Name) | ||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
creationTimestamp: null | ||
labels: | ||
control-plane: odfmo-controller-manager | ||
name: odfmo-webhook-service | ||
spec: | ||
ports: | ||
- port: 443 | ||
protocol: TCP | ||
targetPort: 9443 | ||
selector: | ||
control-plane: odfmo-controller-manager | ||
status: | ||
loadBalancer: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# The following manifests contain a self-signed issuer CR and a certificate CR. | ||
# More document can be found at https://docs.cert-manager.io | ||
# WARNING: Targets CertManager v1.0. Check https://cert-manager.io/docs/installation/upgrading/ for breaking changes. | ||
apiVersion: cert-manager.io/v1 | ||
kind: Issuer | ||
metadata: | ||
name: selfsigned-issuer | ||
namespace: system | ||
spec: | ||
selfSigned: {} | ||
--- | ||
apiVersion: cert-manager.io/v1 | ||
kind: Certificate | ||
metadata: | ||
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml | ||
namespace: system | ||
spec: | ||
# $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize | ||
dnsNames: | ||
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc | ||
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local | ||
issuerRef: | ||
kind: Issuer | ||
name: selfsigned-issuer | ||
secretName: webhook-server-cert # this secret will not be prefixed, since it's not managed by kustomize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
resources: | ||
- certificate.yaml | ||
|
||
configurations: | ||
- kustomizeconfig.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# This configuration is for teaching kustomize how to update name ref and var substitution | ||
nameReference: | ||
- kind: Issuer | ||
group: cert-manager.io | ||
fieldSpecs: | ||
- kind: Certificate | ||
group: cert-manager.io | ||
path: spec/issuerRef/name | ||
|
||
varReference: | ||
- kind: Certificate | ||
group: cert-manager.io | ||
path: spec/commonName | ||
- kind: Certificate | ||
group: cert-manager.io | ||
path: spec/dnsNames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: controller-manager | ||
namespace: system | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: manager | ||
ports: | ||
- containerPort: 9443 | ||
name: webhook-server | ||
protocol: TCP | ||
volumeMounts: | ||
- mountPath: /tmp/k8s-webhook-server/serving-certs | ||
name: cert | ||
readOnly: true | ||
volumes: | ||
- name: cert | ||
secret: | ||
defaultMode: 420 | ||
secretName: webhook-server-cert |
Oops, something went wrong.