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

Adding the admistrationregistration api group #52

Conversation

shawn-hurley
Copy link

Add ability to handle validating and mutating webhooks for logical cluster
Attempted to keep the changes localized to admission plugin package

What type of PR is this?

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?


Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@@ -222,6 +222,7 @@ func (cm *ClientManager) HookClient(cc ClientConfig) (*rest.RESTClient, error) {
cfg := rest.CopyConfig(restConfig)
cfg.Host = u.Scheme + "://" + u.Host
cfg.APIPath = u.Path
cfg.TLSClientConfig.Insecure = true
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove, this was to just help me testing a webhook via URL without having to spend too much time on certs.


name, err := genericapirequest.ClusterNameFrom(ctx)

fmt.Printf("\n\nCluster: %v\nerr:%v\n\n", name, err)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove, used for testing

@shawn-hurley shawn-hurley force-pushed the add-validation-admission-lcluster-aware branch from d14bc9e to ef2bdb0 Compare March 11, 2022 19:21
@@ -114,7 +113,7 @@ func NewServerRunOptions() *ServerRunOptions {
s.Etcd.EnableWatchCache = false

// TODO: turn off the admission webhooks for now
s.Admission.DefaultOffPlugins.Insert(validating.PluginName, mutating.PluginName)
s.Admission.DefaultOffPlugins.Insert(mutating.PluginName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why leaving mutating disabled?

@@ -118,7 +118,7 @@ func mergeValidatingWebhookConfigurations(configurations []*v1.ValidatingWebhook
n := c.Webhooks[i].Name
uid := fmt.Sprintf("%s/%s/%d", c.Name, n, names[n])
names[n]++
accessors = append(accessors, webhook.NewValidatingWebhookAccessor(uid, c.Name, &c.Webhooks[i]))
accessors = append(accessors, webhook.NewValidatingWebhookAccessor(uid, c.Name, c.ObjectMeta.ClusterName, &c.Webhooks[i]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs a rebase to latest kube branch, with logicalcluster.LogicalCluster

@@ -21,7 +21,7 @@ import (
"sort"
"sync/atomic"

"k8s.io/api/admissionregistration/v1"
v1 "k8s.io/api/admissionregistration/v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop this change

@@ -31,6 +31,8 @@ type WebhookAccessor interface {
// GetUID gets a string that uniquely identifies the webhook.
GetUID() string

GetCluster() string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetLogicalCluster() logicalcluster.LogicalCluster

@@ -93,6 +95,10 @@ type mutatingWebhookAccessor struct {
clientErr error
}

func (m *mutatingWebhookAccessor) GetCluster() string {
return ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why empty?

}

type validatingWebhookAccessor struct {
*v1.ValidatingWebhook
uid string
configurationName string
cluster string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logicalcluster.LogicalCluster

@@ -23,7 +23,7 @@ import (

admissionv1 "k8s.io/api/admission/v1"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
"k8s.io/api/admissionregistration/v1"
v1 "k8s.io/api/admissionregistration/v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop the change

func (a *Webhook) ShouldCallHook(h webhook.WebhookAccessor, attr admission.Attributes, o admission.ObjectInterfaces, clusterName string) (*WebhookInvocation, *apierrors.StatusError) {
if h.GetCluster() != clusterName {
return nil, nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks fine for CRDs, but probably not enough for APIBindings. Before merging this PR I would like to see a kcp "proof" PR showing in e2e that admission works for APIBindings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discussed with Shawn yesterday. I think we might want to split this into

  1. Just getting webhooks working within a single workspace
  2. Support for webhooks for APIBindings, after we implement identity

WDYT?

Add ability to handle validating and mutating webhooks for logical cluster
Attempted to keep the changes localized to admission plugin package
@shawn-hurley shawn-hurley force-pushed the add-validation-admission-lcluster-aware branch from 5d3d7d5 to 7610335 Compare April 6, 2022 17:49
go.mod Outdated
@@ -98,7 +98,7 @@ require (
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.0.0
k8s.io/apiextensions-apiserver v0.0.0
k8s.io/apimachinery v0.0.0
k8s.io/apimachinery v0.23.5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the thing I was saying a rebase should fix, and needs to be reverted. Let me know if you can't get it undone after a go mod tidy.

@shawn-hurley shawn-hurley force-pushed the add-validation-admission-lcluster-aware branch from 7610335 to 0513df7 Compare April 6, 2022 17:50
@@ -71,14 +75,15 @@ type WebhookAccessor interface {
}

// NewMutatingWebhookAccessor creates an accessor for a MutatingWebhook.
func NewMutatingWebhookAccessor(uid, configurationName string, h *v1.MutatingWebhook) WebhookAccessor {
return &mutatingWebhookAccessor{uid: uid, configurationName: configurationName, MutatingWebhook: h}
func NewMutatingWebhookAccessor(uid, configurationName string, cluster logicalcluster.LogicalCluster, h *v1.MutatingWebhook) WebhookAccessor {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can undo this and just get the cluster from h inside the function (rather than passing in cluster)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ncdc h has no ObjectMeta

@@ -171,14 +180,15 @@ func (m *mutatingWebhookAccessor) GetValidatingWebhook() (*v1.ValidatingWebhook,
}

// NewValidatingWebhookAccessor creates an accessor for a ValidatingWebhook.
func NewValidatingWebhookAccessor(uid, configurationName string, h *v1.ValidatingWebhook) WebhookAccessor {
return &validatingWebhookAccessor{uid: uid, configurationName: configurationName, ValidatingWebhook: h}
func NewValidatingWebhookAccessor(uid, configurationName string, cluster logicalcluster.LogicalCluster, h *v1.ValidatingWebhook) WebhookAccessor {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto re undo

@shawn-hurley shawn-hurley force-pushed the add-validation-admission-lcluster-aware branch from 0513df7 to 1cbba47 Compare April 6, 2022 17:59
@@ -37,7 +38,7 @@ func TestMutatingWebhookAccessor(t *testing.T) {
orig.ReinvocationPolicy = nil

uid := fmt.Sprintf("test.configuration.admission/%s/0", orig.Name)
accessor := NewMutatingWebhookAccessor(uid, "test.configuration.admission", orig)
accessor := NewMutatingWebhookAccessor(uid, "test.configuration.admission", logicalcluster.New(""), orig)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(General comment, not line specific)

Would like to ensure the unit test accounts for the new GetLogicalCluster function. I see 2 options:

  1. We modify this file
  2. We create a new kcp_accessors_test.go file where the only thing we test is our changes

1 keeps everything together. 2 makes rebases easier. I think I maybe am voting for 2.

@sttts WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

external file if possible

// Overwrite the default for storage data format.
s.Etcd.DefaultStorageMediaType = "application/vnd.kubernetes.protobuf"

s.Admission.DefaultOffPlugins.Insert(validating.PluginName, mutating.PluginName)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this move? Note that every carry patch costs. We shouldn't do them if they are not strictly necessary.

"k8s.io/api/admissionregistration/v1"
logicalcluster "github.com/kcp-dev/apimachinery/pkg/logicalcluster"

v1 "k8s.io/api/admissionregistration/v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduce changes. The alias is not necessary.

@@ -22,7 +22,8 @@ import (
"testing"

fuzz "github.com/google/gofuzz"
"k8s.io/api/admissionregistration/v1"
"github.com/kcp-dev/apimachinery/pkg/logicalcluster"
v1 "k8s.io/api/admissionregistration/v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minimize changes

return v.cluster

}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put into patch_accessor.go file

@@ -0,0 +1,10 @@
package mutating
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patch_dispatcher.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants