This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
feat(crdvalidator): adding webhook to ensure safety of crd create/upgrades #213
Merged
tylerslaton
merged 1 commit into
operator-framework:main
from
tylerslaton:crd-admission-webhook
Apr 14, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,75 @@ | ||
/* | ||
Copyright 2022. | ||
|
||
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 handlers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/operator-framework/rukpak/internal/crd" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// +kubebuilder:webhook:path=/validate-crd,mutating=false,failurePolicy=fail,groups="",resources=customresourcedefinitions,verbs=create;update,versions=v1,name=crd-validation-webhook.io | ||
|
||
// CrdValidator houses a client, decoder and Handle function for ensuring | ||
// that a CRD create/update request is safe | ||
type CrdValidator struct { | ||
log logr.Logger | ||
client client.Client | ||
decoder *admission.Decoder | ||
} | ||
|
||
func NewCrdValidator(log logr.Logger, client client.Client) CrdValidator { | ||
return CrdValidator{ | ||
log: log.V(1).WithName("crdhandler"), // Default to non-verbose logs | ||
client: client, | ||
} | ||
} | ||
|
||
// Handle takes an incoming CRD create/update request and confirms that it is | ||
// a safe upgrade based on the crd.Validate() function call | ||
func (cv *CrdValidator) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
incomingCrd := &apiextensionsv1.CustomResourceDefinition{} | ||
|
||
err := cv.decoder.Decode(req, incomingCrd) | ||
if err != nil { | ||
message := fmt.Sprintf("failed to decode CRD %q", req.Name) | ||
cv.log.V(0).Error(err, message) | ||
return admission.Errored(http.StatusBadRequest, fmt.Errorf("%s: %w", message, err)) | ||
} | ||
|
||
err = crd.Validate(ctx, cv.client, incomingCrd) | ||
if err != nil { | ||
message := fmt.Sprintf("failed to validate safety of %s for CRD %q: %s", req.Operation, req.Name, err) | ||
cv.log.V(0).Info(message) | ||
return admission.Denied(message) | ||
} | ||
|
||
cv.log.Info("admission allowed for %s of CRD %q", req.Name, req.Operation) | ||
return admission.Allowed("") | ||
} | ||
|
||
// InjectDecoder injects a decoder for the CrdValidator. | ||
func (cv *CrdValidator) InjectDecoder(d *admission.Decoder) error { | ||
cv.decoder = d | ||
return nil | ||
} |
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,75 @@ | ||
/* | ||
Copyright 2022. | ||
|
||
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 main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/operator-framework/rukpak/cmd/crdvalidator/handlers" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
) | ||
|
||
var ( | ||
scheme = runtime.NewScheme() | ||
entryLog = log.Log.WithName("crdvalidator") | ||
) | ||
|
||
const defaultCertDir = "/etc/admission-webhook/tls" | ||
|
||
func init() { | ||
if err := apiextensionsv1.AddToScheme(scheme); err != nil { | ||
entryLog.Error(err, "unable to set up crd scheme") | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func main() { | ||
// Setup a Manager | ||
entryLog.Info("setting up manager") | ||
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{Scheme: scheme}) | ||
if err != nil { | ||
entryLog.Error(err, "unable to set up overall controller manager") | ||
os.Exit(1) | ||
} | ||
|
||
entryLog.Info("setting up webhook server") | ||
hookServer := mgr.GetWebhookServer() | ||
|
||
// Point to where cert-mgr is placing the cert | ||
hookServer.CertDir = defaultCertDir | ||
timflannagan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Register CRD validation handler | ||
entryLog.Info("registering webhooks to the webhook server") | ||
crdValidatorHandler := handlers.NewCrdValidator(entryLog, mgr.GetClient()) | ||
hookServer.Register("/validate-crd", &webhook.Admission{ | ||
Handler: &crdValidatorHandler, | ||
}) | ||
|
||
entryLog.Info("starting manager") | ||
if err := mgr.Start(signals.SetupSignalHandler()); err != nil { | ||
entryLog.Error(err, "unable to run manager") | ||
os.Exit(1) | ||
} | ||
} |
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,7 @@ | ||
package util | ||
|
||
import "k8s.io/apimachinery/pkg/util/rand" | ||
|
||
func GenName(namePrefix string) string { | ||
return namePrefix + rand.String(5) | ||
} |
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,4 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: crdvalidator-system |
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,11 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: crd-validation-webhook | ||
rules: | ||
- apiGroups: ["apiextensions.k8s.io"] | ||
resources: ["customresourcedefinitions"] | ||
verbs: ["get", "watch", "list"] | ||
- apiGroups: ["*"] | ||
resources: ["*"] | ||
verbs: ["list"] |
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 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
namespace: crdvalidator-system | ||
name: crd-validation-webhook |
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,12 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: crd-validation-webhook | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: crd-validation-webhook | ||
subjects: | ||
- kind: ServiceAccount | ||
name: crd-validation-webhook | ||
namespace: crdvalidator-system |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
install-apis
already callscert-mgr
so we end up installing cert-manager twice on deploy. I'm not sure a good way around this -- is there a way to only run a target if it hasn't already been run?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was also thinking on this. I haven't come up with a great solution other than having all the cert-mgr dependent installs in a single target.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds reasonable, we can have something like
install-webhooks
which callscert-mgr
under the hood. I suppose we can address this is in a follow-upThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally, but I agree with you. Its an easy addition that I can just tack onto the follow-ups.