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

[main] Upgrade to latest dependencies #76

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ require (
k8s.io/api v0.22.5
k8s.io/apimachinery v0.22.5
k8s.io/client-go v0.22.5
knative.dev/eventing v0.28.1-0.20220113075012-61d97d366c26
knative.dev/eventing v0.28.1-0.20220116182528-68d441013ca8
knative.dev/hack v0.0.0-20220111151514-59b0cf17578e
knative.dev/pkg v0.0.0-20220113045912-c0e1594c2fb1
knative.dev/pkg v0.0.0-20220114141842-0a429cba1c73
)

replace github.com/prometheus/client_golang => github.com/prometheus/client_golang v0.9.2
12 changes: 5 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1142,17 +1142,15 @@ k8s.io/kube-openapi v0.0.0-20211109043538-20434351676c h1:jvamsI1tn9V0S8jicyX82q
k8s.io/kube-openapi v0.0.0-20211109043538-20434351676c/go.mod h1:vHXdDvt9+2spS2Rx9ql3I8tycm3H9FDfdUoIuKCefvw=
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a h1:8dYfu/Fc9Gz2rNJKB9IQRGgQOh2clmRzNIPPY1xLY5g=
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
knative.dev/eventing v0.28.1-0.20220113075012-61d97d366c26 h1:/pErSO92QlxwT+df28PsF+UW43HbOvrHA+vd8zJvyd0=
knative.dev/eventing v0.28.1-0.20220113075012-61d97d366c26/go.mod h1:UTpytBINNwxN50DbajsmW1OLvfAWLryVEr/zRQuWj5c=
knative.dev/hack v0.0.0-20211203062838-e11ac125e707/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
knative.dev/eventing v0.28.1-0.20220116182528-68d441013ca8 h1:O+qLyfDNKBv4K5UDHXE9go9SKfRdjI6g/SRqpUeQJTg=
knative.dev/eventing v0.28.1-0.20220116182528-68d441013ca8/go.mod h1:SKbTN3yj9f5ywji6CYOUy80cj8kvTPxiQ43r+gdEf9A=
knative.dev/hack v0.0.0-20220111151514-59b0cf17578e h1:tESsFI1iMD2Al+6FZueFN9CddxJ8Fa2cd9KmyNINe3w=
knative.dev/hack v0.0.0-20220111151514-59b0cf17578e/go.mod h1:PHt8x8yX5Z9pPquBEfIj0X66f8iWkWfR0S/sarACJrI=
knative.dev/hack/schema v0.0.0-20220111151514-59b0cf17578e/go.mod h1:ffjwmdcrH5vN3mPhO8RrF2KfNnbHeCE2C60A+2cv3U0=
knative.dev/pkg v0.0.0-20220105211333-96f18522d78d/go.mod h1:189cvGP0mwpqwZGFrLk5WuERIsNI/J6HuQ1CIX7SXxY=
knative.dev/pkg v0.0.0-20220112074250-c568527ffc5b/go.mod h1:X5B0/SR3tzOioS2WQtS9uJTO71M52qcrDkWQ5wtyLVc=
knative.dev/pkg v0.0.0-20220113045912-c0e1594c2fb1 h1:vingyaoZbiMRLha4JCVxPG0ghcpjKVN9VGikAHwpqqs=
knative.dev/pkg v0.0.0-20220113045912-c0e1594c2fb1/go.mod h1:X5B0/SR3tzOioS2WQtS9uJTO71M52qcrDkWQ5wtyLVc=
knative.dev/reconciler-test v0.0.0-20220111195214-587a13ddffc7/go.mod h1:q2kbfoIYHrjFCJtPNaG0MITcXYq0ClBk3MG3l5CuX24=
knative.dev/pkg v0.0.0-20220114141842-0a429cba1c73 h1:O2rdhNu49InR/IciU06z+vtTa/ygtFFBGHBdZPcGMdE=
knative.dev/pkg v0.0.0-20220114141842-0a429cba1c73/go.mod h1:X5B0/SR3tzOioS2WQtS9uJTO71M52qcrDkWQ5wtyLVc=
knative.dev/reconciler-test v0.0.0-20220113155112-af7be729d5e7/go.mod h1:zhcMJ0CfqI7de0Ir0yXTTB6BgShWvLEx+BqQdvg+1pU=
pgregory.net/rapid v0.3.3/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/cache"

"knative.dev/pkg/controller"
"knative.dev/pkg/system"
"knative.dev/pkg/webhook"
Expand All @@ -42,6 +43,7 @@ func NewAdmissionController(
handlers map[schema.GroupVersionKind]resourcesemantics.GenericCRD,
wc func(context.Context) context.Context,
disallowUnknownFields bool,
callbacks ...map[schema.GroupVersionKind]Callback,
) *controller.Impl {

client := kubeclient.Get(ctx)
Expand All @@ -51,6 +53,19 @@ func NewAdmissionController(

key := types.NamespacedName{Name: name}

// This not ideal, we are using a variadic argument to effectively make callbacks optional
// This allows this addition to be non-breaking to consumers of /pkg
// TODO: once all sub-repos have adopted this, we might move this back to a traditional param.
var unwrappedCallbacks map[schema.GroupVersionKind]Callback
switch len(callbacks) {
case 0:
unwrappedCallbacks = map[schema.GroupVersionKind]Callback{}
case 1:
unwrappedCallbacks = callbacks[0]
default:
panic("NewAdmissionController may not be called with multiple callback maps")
}

wh := &reconciler{
LeaderAwareFuncs: pkgreconciler.LeaderAwareFuncs{
// Have this reconciler enqueue our singleton whenever it becomes leader.
Expand All @@ -60,9 +75,10 @@ func NewAdmissionController(
},
},

key: key,
path: path,
handlers: handlers,
key: key,
path: path,
handlers: handlers,
callbacks: unwrappedCallbacks,

withContext: wc,
disallowUnknownFields: disallowUnknownFields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ import (

"github.com/gobuffalo/flect"
"go.uber.org/zap"
jsonpatch "gomodules.xyz/jsonpatch/v2"
"gomodules.xyz/jsonpatch/v2"
admissionv1 "k8s.io/api/admission/v1"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
admissionlisters "k8s.io/client-go/listers/admissionregistration/v1"
corelisters "k8s.io/client-go/listers/core/v1"

"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
"knative.dev/pkg/controller"
Expand All @@ -56,9 +58,10 @@ type reconciler struct {
webhook.StatelessAdmissionImpl
pkgreconciler.LeaderAwareFuncs

key types.NamespacedName
path string
handlers map[schema.GroupVersionKind]resourcesemantics.GenericCRD
key types.NamespacedName
path string
handlers map[schema.GroupVersionKind]resourcesemantics.GenericCRD
callbacks map[schema.GroupVersionKind]Callback

withContext func(context.Context) context.Context

Expand All @@ -70,6 +73,37 @@ type reconciler struct {
secretName string
}

// CallbackFunc is the function to be invoked.
type CallbackFunc func(ctx context.Context, unstructured *unstructured.Unstructured) error

// Callback is a generic function to be called by a consumer of defaulting.
type Callback struct {
// function is the callback to be invoked.
function CallbackFunc

// supportedVerbs are the verbs supported for the callback.
// The function will only be called on these actions.
supportedVerbs map[webhook.Operation]struct{}
}

// NewCallback creates a new callback function to be invoked on supported verbs.
func NewCallback(function func(context.Context, *unstructured.Unstructured) error, supportedVerbs ...webhook.Operation) Callback {
if function == nil {
panic("expected function, got nil")
}
m := make(map[webhook.Operation]struct{})
for _, op := range supportedVerbs {
if op == webhook.Delete {
panic("Verb " + webhook.Delete + " not allowed")
}
if _, has := m[op]; has {
panic("duplicate verbs not allowed")
}
m[op] = struct{}{}
}
return Callback{function: function, supportedVerbs: m}
}

var _ controller.Reconciler = (*reconciler)(nil)
var _ pkgreconciler.LeaderAware = (*reconciler)(nil)
var _ webhook.AdmissionController = (*reconciler)(nil)
Expand Down Expand Up @@ -137,7 +171,17 @@ func (ac *reconciler) reconcileMutatingWebhook(ctx context.Context, caCert []byt
logger := logging.FromContext(ctx)

rules := make([]admissionregistrationv1.RuleWithOperations, 0, len(ac.handlers))
gvks := make(map[schema.GroupVersionKind]struct{}, len(ac.handlers)+len(ac.callbacks))
for gvk := range ac.handlers {
gvks[gvk] = struct{}{}
}
for gvk := range ac.callbacks {
if _, ok := gvks[gvk]; !ok {
gvks[gvk] = struct{}{}
}
}

for gvk := range gvks {
plural := strings.ToLower(flect.Pluralize(gvk.Kind))

rules = append(rules, admissionregistrationv1.RuleWithOperations{
Expand Down Expand Up @@ -231,8 +275,18 @@ func (ac *reconciler) mutate(ctx context.Context, req *admissionv1.AdmissionRequ
logger := logging.FromContext(ctx)
handler, ok := ac.handlers[gvk]
if !ok {
logger.Error("Unhandled kind: ", gvk)
return nil, fmt.Errorf("unhandled kind: %v", gvk)
if _, ok := ac.callbacks[gvk]; !ok {
logger.Error("Unhandled kind: ", gvk)
return nil, fmt.Errorf("unhandled kind: %v", gvk)
}
patches, err := ac.callback(ctx, gvk, req, true /* shouldSetUserInfo */, duck.JSONPatch{})
if err != nil {
logger.Errorw("Failed the callback defaulter", zap.Error(err))
// Return the error message as-is to give the defaulter callback
// discretion over (our portion of) the message that the user sees.
return nil, err
}
return json.Marshal(patches)
}

// nil values denote absence of `old` (create) or `new` (delete) objects.
Expand Down Expand Up @@ -302,6 +356,13 @@ func (ac *reconciler) mutate(ctx context.Context, req *admissionv1.AdmissionRequ
return nil, err
}

if patches, err = ac.callback(ctx, gvk, req, false /* shouldSetUserInfo */, patches); err != nil {
logger.Errorw("Failed the callback defaulter", zap.Error(err))
// Return the error message as-is to give the defaulter callback
// discretion over (our portion of) the message that the user sees.
return nil, err
}

// None of the validators will accept a nil value for newObj.
if newObj == nil {
return nil, errMissingNewObject
Expand Down Expand Up @@ -329,6 +390,57 @@ func (ac *reconciler) setUserInfoAnnotations(ctx context.Context, patches duck.J
return append(patches, patch...), nil
}

func (ac *reconciler) callback(ctx context.Context, gvk schema.GroupVersionKind, req *admissionv1.AdmissionRequest, shouldSetUserInfo bool, patches duck.JSONPatch) (duck.JSONPatch, error) {
// Get callback.
callback, ok := ac.callbacks[gvk]
if !ok {
return patches, nil
}

// Check if request operation is a supported webhook operation.
if _, isSupported := callback.supportedVerbs[req.Operation]; !isSupported {
return patches, nil
}

oldBytes := req.OldObject.Raw
newBytes := req.Object.Raw

before := &unstructured.Unstructured{}
after := &unstructured.Unstructured{}

// Get unstructured object.
if err := json.Unmarshal(newBytes, before); err != nil {
return nil, fmt.Errorf("cannot decode object: %w", err)
}
// Copy before in after unstructured objects.
before.DeepCopyInto(after)

// Setup context.
if len(oldBytes) != 0 {
if req.SubResource == "" {
ctx = apis.WithinUpdate(ctx, before)
} else {
ctx = apis.WithinSubResourceUpdate(ctx, before, req.SubResource)
}
} else {
ctx = apis.WithinCreate(ctx)
}
ctx = apis.WithUserInfo(ctx, &req.UserInfo)

// Call callback passing after.
if err := callback.function(ctx, after); err != nil {
return patches, err
}

if shouldSetUserInfo {
setUserInfoAnnotations(adaptUnstructuredHasSpecCtx(ctx, req), unstructuredHasSpec{after}, req.Resource.Group)
}

// Create patches.
patch, err := duck.CreatePatch(before.Object, after.Object)
return append(patches, patch...), err
}

// roundTripPatch generates the JSONPatch that corresponds to round tripping the given bytes through
// the Golang type (JSON -> Golang type -> JSON). Because it is not always true that
// bytes == json.Marshal(json.Unmarshal(bytes)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ package defaulting
import (
"context"

admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"knative.dev/pkg/apis"
)

Expand Down Expand Up @@ -48,5 +51,35 @@ func setUserInfoAnnotations(ctx context.Context, resource apis.HasSpec, groupNam
annotations[groupName+apis.CreatorAnnotationSuffix] = ui.Username
annotations[groupName+apis.UpdaterAnnotationSuffix] = ui.Username
}
objectMetaAccessor.GetObjectMeta().SetAnnotations(annotations)
}
}

type unstructuredHasSpec struct {
*unstructured.Unstructured
}

func (us unstructuredHasSpec) GetObjectMeta() metav1.Object {
return us.Unstructured
}

var _ metav1.ObjectMetaAccessor = unstructuredHasSpec{}

func (us unstructuredHasSpec) GetUntypedSpec() interface{} {
if s, ok := us.Unstructured.Object["spec"]; ok {
return s
}
return nil
}

func adaptUnstructuredHasSpecCtx(ctx context.Context, req *admissionv1.AdmissionRequest) context.Context {
if apis.IsInUpdate(ctx) {
b := apis.GetBaseline(ctx)
if apis.IsInStatusUpdate(ctx) {
ctx = apis.WithinSubResourceUpdate(ctx, unstructuredHasSpec{b.(*unstructured.Unstructured)}, req.SubResource)
} else {
ctx = apis.WithinUpdate(ctx, unstructuredHasSpec{b.(*unstructured.Unstructured)})
}
}
return ctx
}
4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -703,15 +703,15 @@ k8s.io/utils/buffer
k8s.io/utils/integer
k8s.io/utils/pointer
k8s.io/utils/trace
# knative.dev/eventing v0.28.1-0.20220113075012-61d97d366c26
# knative.dev/eventing v0.28.1-0.20220116182528-68d441013ca8
## explicit
knative.dev/eventing/pkg/apis/sources
knative.dev/eventing/pkg/apis/sources/config
knative.dev/eventing/pkg/apis/sources/v1
# knative.dev/hack v0.0.0-20220111151514-59b0cf17578e
## explicit
knative.dev/hack
# knative.dev/pkg v0.0.0-20220113045912-c0e1594c2fb1
# knative.dev/pkg v0.0.0-20220114141842-0a429cba1c73
## explicit
knative.dev/pkg/apis
knative.dev/pkg/apis/duck
Expand Down