Skip to content

Commit

Permalink
Implement SingularNameProvider for REST (#79)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Jan 4, 2024
1 parent d80da24 commit 28c3d11
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ REPO := $(notdir $(shell pwd))
BIN := webhook-runtime

# https://github.com/appscodelabs/gengo-builder
CODE_GENERATOR_IMAGE ?= ghcr.io/appscode/gengo:release-1.25
CODE_GENERATOR_IMAGE ?= ghcr.io/appscode/gengo:release-1.29

# This version-strategy uses git tags to set the version string
git_branch := $(shell git rev-parse --abbrev-ref HEAD)
Expand Down Expand Up @@ -191,7 +191,7 @@ unit-tests: $(BUILD_DIRS)
./hack/test.sh $(SRC_DIRS) \
"

ADDTL_LINTERS := goconst,gofmt,goimports,unparam
ADDTL_LINTERS := gofmt,goimports,unparam

.PHONY: lint
lint: $(BUILD_DIRS)
Expand Down
4 changes: 2 additions & 2 deletions admission/v1/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type GenericWebhook struct {
plural schema.GroupVersionResource
singular string

srcGroups sets.String
srcGroups sets.Set[string]
target schema.GroupVersionKind
factory api.GetterFactory
get api.GetFunc
Expand All @@ -65,7 +65,7 @@ func NewGenericWebhook(
return &GenericWebhook{
plural: plural,
singular: singular,
srcGroups: sets.NewString(srcGroups...),
srcGroups: sets.New[string](srcGroups...),
target: target,
factory: factory,
handler: handler,
Expand Down
4 changes: 2 additions & 2 deletions admission/v1/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type WorkloadWebhook struct {
singular string
kind string

srcGroups sets.String
srcGroups sets.Set[string]
factory api.GetterFactory
get api.GetFunc
handler lib.ResourceHandler
Expand All @@ -71,7 +71,7 @@ func NewWorkloadWebhook(
plural: plural,
singular: singular,
kind: kind,
srcGroups: sets.NewString(core.GroupName, appsv1.GroupName, extensions.GroupName, batchv1.GroupName),
srcGroups: sets.New[string](core.GroupName, appsv1.GroupName, extensions.GroupName, batchv1.GroupName),
factory: factory,
handler: handler,
}
Expand Down
4 changes: 2 additions & 2 deletions admission/v1beta1/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type GenericWebhook struct {
plural schema.GroupVersionResource
singular string

srcGroups sets.String
srcGroups sets.Set[string]
target schema.GroupVersionKind
factory api.GetterFactory
get api.GetFunc
Expand All @@ -65,7 +65,7 @@ func NewGenericWebhook(
return &GenericWebhook{
plural: plural,
singular: singular,
srcGroups: sets.NewString(srcGroups...),
srcGroups: sets.New[string](srcGroups...),
target: target,
factory: factory,
handler: handler,
Expand Down
4 changes: 2 additions & 2 deletions admission/v1beta1/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type WorkloadWebhook struct {
singular string
kind string

srcGroups sets.String
srcGroups sets.Set[string]
factory api.GetterFactory
get api.GetFunc
handler admission.ResourceHandler
Expand All @@ -71,7 +71,7 @@ func NewWorkloadWebhook(
plural: plural,
singular: singular,
kind: kind,
srcGroups: sets.NewString(core.GroupName, appsv1.GroupName, extensions.GroupName, batchv1.GroupName),
srcGroups: sets.New[string](core.GroupName, appsv1.GroupName, extensions.GroupName, batchv1.GroupName),
factory: factory,
handler: handler,
}
Expand Down
22 changes: 7 additions & 15 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,36 @@ func (blder *WebhookBuilder) Complete() (hooks.AdmissionHook, hooks.AdmissionHoo
}
blder.gk = gvk.GroupKind()

mutator, err := blder.registerDefaultingWebhook()
if err != nil {
return nil, nil, err
}
validator, err := blder.registerValidatingWebhook()
if err != nil {
return nil, nil, err
}
return mutator, validator, nil
return blder.registerDefaultingWebhook(), blder.registerValidatingWebhook(), nil
}

// registerDefaultingWebhook registers a defaulting webhook if th.
func (blder *WebhookBuilder) registerDefaultingWebhook() (hooks.AdmissionHook, error) {
func (blder *WebhookBuilder) registerDefaultingWebhook() hooks.AdmissionHook {
defaulter, isDefaulter := blder.apiType.(admission.Defaulter)
if !isDefaulter {
log.Info("skip registering a mutating webhook, admission.Defaulter interface is not implemented", "GK", blder.gk)
return nil, nil
return nil
}

mwh := admission.DefaultingWebhookFor(blder.scheme, defaulter)
return &webhook{
prefix: MutatorGroupPrefix,
gk: blder.gk,
w: mwh,
}, nil
}
}

func (blder *WebhookBuilder) registerValidatingWebhook() (hooks.AdmissionHook, error) {
func (blder *WebhookBuilder) registerValidatingWebhook() hooks.AdmissionHook {
checker, isValidator := blder.apiType.(admission.Validator)
if !isValidator {
log.Info("skip registering a validating webhook, admission.Validator interface is not implemented", "GK", blder.gk)
return nil, nil
return nil
}

vwh := admission.ValidatingWebhookFor(blder.scheme, checker)
return &webhook{
prefix: ValidatorGroupPrefix,
gk: blder.gk,
w: vwh,
}, nil
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ require (
k8s.io/client-go v0.29.0
k8s.io/klog/v2 v2.110.1
k8s.io/kubernetes v0.0.0-00010101000000-000000000000
kmodules.xyz/client-go v0.29.1
kmodules.xyz/client-go v0.29.5
kmodules.xyz/openshift v0.29.0
sigs.k8s.io/controller-runtime v0.16.1-0.20231215020716-1b80b9629af8
sigs.k8s.io/controller-runtime v0.16.3
)

require (
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ=
github.com/go-logr/zapr v1.3.0/go.mod h1:YKepepNBd1u/oyhd/yQmtjVXmm9uML4IXUgMOwR8/Gg=
github.com/go-logr/zapr v1.2.4 h1:QHVo+6stLbfJmYGkQ7uGHUCu5hnAFAj6mDe6Ea0SeOo=
github.com/go-logr/zapr v1.2.4/go.mod h1:FyHWQIzQORZ0QVE1BtVHv3cKtNLuXsbNLtpuhNapBOA=
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE=
Expand Down Expand Up @@ -95,8 +95,8 @@ github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs=
github.com/onsi/ginkgo/v2 v2.13.2/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
Expand Down Expand Up @@ -180,8 +180,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -233,12 +233,12 @@ k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSn
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
kmodules.xyz/apiversion v0.2.0 h1:vAQYqZFm4xu4pbB1cAdHbFEPES6EQkcR4wc06xdTOWk=
kmodules.xyz/apiversion v0.2.0/go.mod h1:oPX8g8LvlPdPX3Yc5YvCzJHQnw3YF/X4/jdW0b1am80=
kmodules.xyz/client-go v0.29.1 h1:XC7/LybFnyKnPmjur/F+1wRk9c9QNu7yICiehnKNzf4=
kmodules.xyz/client-go v0.29.1/go.mod h1:xWlS/1zWkx1sIKCAkzULy9570mHZYi2exDECEoP1ek4=
kmodules.xyz/client-go v0.29.5 h1:iRl4MoV+96TM1csInOCWjn5xSOXzuYlil6CO40vXLHU=
kmodules.xyz/client-go v0.29.5/go.mod h1:pHuzpwzEcDUIGjVVvwz9N8lY+6A7HXwvs2d7NtK7Hho=
kmodules.xyz/openshift v0.29.0 h1:8PXjeQ+usUGkLnYUMSbZrMg+i1OIYBe9UWOeBf2FRzU=
kmodules.xyz/openshift v0.29.0/go.mod h1:neEinR+BcvP4vKKCkh9HpRK9e3c+upSst9E7rskZVuQ=
sigs.k8s.io/controller-runtime v0.16.1-0.20231215020716-1b80b9629af8 h1:2uFstfRgF3VNnIepR+DADX2twekWrW4oIF/DJGtq/1I=
sigs.k8s.io/controller-runtime v0.16.1-0.20231215020716-1b80b9629af8/go.mod h1:R//DPbq8lk8vvdJ931v3FxDSXkICZIssoGQ1vOSnaFg=
sigs.k8s.io/controller-runtime v0.16.3 h1:2TuvuokmfXvDUamSx1SuAOO3eTyye+47mJCigwG62c4=
sigs.k8s.io/controller-runtime v0.16.3/go.mod h1:j7bialYoSn142nv9sCOJmQgDXQXxnroFU4VnX/brVJ0=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=
Expand Down
6 changes: 6 additions & 0 deletions registry/admissionreview/v1/admission_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1

import (
"context"
"strings"

admissionv1 "k8s.io/api/admission/v1"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
Expand All @@ -40,6 +41,7 @@ var (
_ rest.Scoper = &REST{}
_ rest.GroupVersionKindProvider = &REST{}
_ rest.Storage = &REST{}
_ rest.SingularNameProvider = &REST{}
)

func NewREST(hookFn AdmissionHookFunc) *REST {
Expand All @@ -56,6 +58,10 @@ func (r *REST) GroupVersionKind(_ schema.GroupVersion) schema.GroupVersionKind {
return admissionv1beta1.SchemeGroupVersion.WithKind("AdmissionReview")
}

func (r *REST) GetSingularName() string {
return strings.ToLower("AdmissionReview")
}

func (r *REST) NamespaceScoped() bool {
return false
}
Expand Down
6 changes: 6 additions & 0 deletions registry/admissionreview/v1beta1/admission_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1beta1

import (
"context"
"strings"

admission "k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -39,6 +40,7 @@ var (
_ rest.Scoper = &REST{}
_ rest.GroupVersionKindProvider = &REST{}
_ rest.Storage = &REST{}
_ rest.SingularNameProvider = &REST{}
)

func NewREST(hookFn AdmissionHookFunc) *REST {
Expand All @@ -55,6 +57,10 @@ func (r *REST) GroupVersionKind(_ schema.GroupVersion) schema.GroupVersionKind {
return admission.SchemeGroupVersion.WithKind("AdmissionReview")
}

func (r *REST) GetSingularName() string {
return strings.ToLower("AdmissionReview")
}

func (r *REST) NamespaceScoped() bool {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/kmodules.xyz/client-go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ REPO := $(notdir $(shell pwd))
BIN := client-go

# https://github.com/appscodelabs/gengo-builder
CODE_GENERATOR_IMAGE ?= ghcr.io/appscode/gengo:release-1.25
CODE_GENERATOR_IMAGE ?= ghcr.io/appscode/gengo:release-1.29

# This version-strategy uses git tags to set the version string
git_branch := $(shell git rev-parse --abbrev-ref HEAD)
Expand Down
6 changes: 3 additions & 3 deletions vendor/kmodules.xyz/client-go/meta/preconditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

type PreConditionSet struct {
sets.String
sets.Set[string]
}

func (s PreConditionSet) PreconditionFunc() []mergepatch.PreconditionFunc {
Expand All @@ -36,7 +36,7 @@ func (s PreConditionSet) PreconditionFunc() []mergepatch.PreconditionFunc {
mergepatch.RequireMetadataKeyUnchanged("namespace"),
}

for _, field := range s.List() {
for _, field := range sets.List[string](s.Set) {
preconditions = append(preconditions,
RequireChainKeyUnchanged(field),
)
Expand All @@ -45,7 +45,7 @@ func (s PreConditionSet) PreconditionFunc() []mergepatch.PreconditionFunc {
}

func (s PreConditionSet) Error() error {
strList := strings.Join(s.List(), "\n\t")
strList := strings.Join(sets.List[string](s.Set), "\n\t")
return fmt.Errorf(strings.Join([]string{`At least one of the following was changed:
apiVersion
kind
Expand Down
6 changes: 3 additions & 3 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ k8s.io/utils/strings/slices
# kmodules.xyz/apiversion v0.2.0
## explicit; go 1.14
kmodules.xyz/apiversion
# kmodules.xyz/client-go v0.29.1
# kmodules.xyz/client-go v0.29.5
## explicit; go 1.21.5
kmodules.xyz/client-go
kmodules.xyz/client-go/api/v1
Expand All @@ -579,8 +579,8 @@ kmodules.xyz/openshift/client/clientset/versioned
kmodules.xyz/openshift/client/clientset/versioned/scheme
kmodules.xyz/openshift/client/clientset/versioned/typed/apps/v1
kmodules.xyz/openshift/client/clientset/versioned/typed/security/v1
# sigs.k8s.io/controller-runtime v0.16.1-0.20231215020716-1b80b9629af8
## explicit; go 1.21
# sigs.k8s.io/controller-runtime v0.16.3
## explicit; go 1.20
sigs.k8s.io/controller-runtime/pkg/client
sigs.k8s.io/controller-runtime/pkg/client/apiutil
sigs.k8s.io/controller-runtime/pkg/log
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions vendor/sigs.k8s.io/controller-runtime/pkg/client/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 28c3d11

Please sign in to comment.