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

fix(deps): update module sigs.k8s.io/controller-runtime to v0.16.1 #265

Merged
merged 9 commits into from
Sep 18, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion contents/kubernetes-operator/client-go/deltafifo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func main() {

// Informer handleDeltas Pop()
// https://github.com/kubernetes/client-go/blob/ee1a5aaf793a9ace9c433f5fb26a19058ed5f37c/tools/cache/controller.go#L182-L195
// cannot use process (value of type func(obj interface{}) error) as "k8s.io/client-go/tools/cache".PopProcessFunc value in argument to fifo.Pop
fifo.Pop(process)
}

Expand Down Expand Up @@ -67,7 +68,8 @@ func newPodWithContainer() *corev1.Pod {
}
}

func process(obj interface{}) error { // type PopProcessFunc func(interface{}) error
// https://github.com/kubernetes/client-go/blame/08f892964c345b3d94d78992b4e924cf9fa7f98a/tools/cache/fifo.go#L28
func process(obj interface{}, isInInitialList bool) error { // type PopProcessFunc func(obj interface{}, isInInitialList bool) error
if deltas, ok := obj.(cache.Deltas); ok {
return processDeltas(deltas)
}
Expand Down
29 changes: 28 additions & 1 deletion contents/kubernetes-operator/controller-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,35 @@ List of components:
1. [handler](handler)

## Examples

1. [example-controller](example-controller)
1. envtest

## Memo
- [v0.11.0](https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.11.0): Allow Specification of the Log Timestamp Format. -> Default EpochTimeEncoder

1 [v0.11.0](https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.11.0): Allow Specification of the Log Timestamp Format. -> Default EpochTimeEncoder

1. [v0.15.0](https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0)
1. [⚠️ Refactor source/handler/predicate packages to remove dep injection #2120](https://github.com/kubernetes-sigs/controller-runtime/pull/2120)

```diff
- kindWithCacheMysqlUser := source.NewKindWithCache(mysqluser, cache)
- kindWithCacheMysql := source.NewKindWithCache(mysql, cache)
- kindWithCachesecret := source.NewKindWithCache(secret, cache)
+ kindWithCacheMysqlUser := source.Kind(cache, mysqluser)
+ kindWithCacheMysql := source.Kind(cache, mysql)
+ kindWithCachesecret := source.Kind(cache, secret)
```

1. [v0.16.0](https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.16.0)
1. [⚠ Introduce Metrics Options struct & secure metrics serving #2407](https://github.com/kubernetes-sigs/controller-runtime/pull/2407)

```diff
import (
+ metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)
- MetricsBindAddress: metricsAddr
+ Metrics: metricsserver.Options{BindAddress: metricsAddr},
```

1. [⚠ Remove deprecated manager, webhook and cluster options #2422](https://github.com/kubernetes-sigs/controller-runtime/pull/2422)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
}

mapper, err := func(c *rest.Config) (meta.RESTMapper, error) {
return apiutil.NewDynamicRESTMapper(c)
return apiutil.NewDynamicRESTMapper(c, nil)
}(cfg)
if err != nil {
log.Fatal(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type cluster struct {
|name|value|where to use|
|---|---|---|
|Scheme|scheme.Scheme||
|MapperProvider|`func(c *rest.Config) (meta.RESTMapper, error) {return apiutil.NewDynamicRESTMapper(c)}`||
|MapperProvider|`func(c *rest.Config) (meta.RESTMapper, error) {return apiutil.NewDynamicRESTMapper(c, nil)}`||
|NewClient|DefaultNewClient|
|NewCache|cache.New|
|newRecorderProvider|intrec.NewProvider|
Expand All @@ -143,7 +143,7 @@ type cluster struct {
1. MapperProvider
```go
options.MapperProvider = func(c *rest.Config) (meta.RESTMapper, error) {
return apiutil.NewDynamicRESTMapper(c)
return apiutil.NewDynamicRESTMapper(c, nil)
}
```
1. `options.NewClient = DefaultNewClient`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type SyncingSource interface {

## Implementation: [kindWithCache](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L77-L79), [Kind](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L91-L102), [Channel](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L207-L226), [Informer](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L338-L341)

1. [kindWithCache](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L77-L79): Just a wrapper of `Kind` without `InjectCache`. NewKindWithCache creates a Source without `InjectCache`, so that it is **assured that the given cache is used and not overwritten**.
1. ~~[kindWithCache](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L77-L79): Just a wrapper of `Kind` without `InjectCache`. NewKindWithCache creates a Source without `InjectCache`, so that it is **assured that the given cache is used and not overwritten**.~~ -> Already removed in [Refactor source/handler/predicate packages to remove dep injection](https://github.com/kubernetes-sigs/controller-runtime/pull/2120) (from [v0.15.0](https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0))
```go
type kindWithCache struct {
kind Kind
Expand Down Expand Up @@ -170,9 +170,9 @@ if you want to check events of specific resource you can set by the following.
}
}()
```
1. Create `kindWithCache` for the target resource.
1. Create `Kind` for the target resource.
```go
kindWithCache := source.NewKindWithCache(mysqluser, cache)
kindWithCache := source.Kind(cache, mysqluser)
```
1. Prepare `workqueue` and `eventHandler`.
```go
Expand Down
12 changes: 6 additions & 6 deletions contents/kubernetes-operator/controller-runtime/source/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {

// Set a mapper
mapper, err := func(c *rest.Config) (meta.RESTMapper, error) {
return apiutil.NewDynamicRESTMapper(c)
return apiutil.NewDynamicRESTMapper(c, nil)
}(cfg)
if err != nil {
log.Error(err, "")
Expand Down Expand Up @@ -82,22 +82,22 @@ func main() {
}()
log.Info("cache is started")

kindWithCacheMysqlUser := source.NewKindWithCache(mysqluser, cache)
kindWithCachePod := source.NewKindWithCache(pod, cache)
kindWithCacheMysqlUser := source.Kind(cache, mysqluser)
kindWithCachePod := source.Kind(cache, pod)

// Prepare queue and eventHandler
queue := workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test")

eventHandler := handler.Funcs{
CreateFunc: func(e event.CreateEvent, q workqueue.RateLimitingInterface) {
CreateFunc: func(ctx context.Context, e event.CreateEvent, q workqueue.RateLimitingInterface) {
log.Info("CreateFunc is called", "object", e.Object.GetName())
queue.Add(WorkQueueItem{Event: "Create", Name: e.Object.GetName()})
},
UpdateFunc: func(e event.UpdateEvent, q workqueue.RateLimitingInterface) {
UpdateFunc: func(ctx context.Context, e event.UpdateEvent, q workqueue.RateLimitingInterface) {
log.Info("UpdateFunc is called", "objectNew", e.ObjectNew.GetName(), "objectOld", e.ObjectOld.GetName())
queue.Add(WorkQueueItem{Event: "Update", Name: e.ObjectNew.GetName()})
},
DeleteFunc: func(e event.DeleteEvent, q workqueue.RateLimitingInterface) {
DeleteFunc: func(ctx context.Context, e event.DeleteEvent, q workqueue.RateLimitingInterface) {
log.Info("DeleteFunc is called", "object", e.Object.GetName())
queue.Add(WorkQueueItem{Event: "Delete", Name: e.Object.GetName()})
},
Expand Down
57 changes: 42 additions & 15 deletions contents/kubernetes-operator/controller-runtime/webhook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
> Package webhook provides methods to build and bootstrap a webhook server.

```go
type Webhook struct {
// Handler actually processes an admission request returning whether it was allowed or denied,
// and potentially patches to apply to the handler.
Handler Handler

// RecoverPanic indicates whether the panic caused by webhook should be recovered.
RecoverPanic bool

// WithContextFunc will allow you to take the http.Request.Context() and
// add any additional information such as passing the request path or
// headers thus allowing you to read them from within the handler
WithContextFunc func(context.Context, *http.Request) context.Context
// contains filtered or unexported fields
type Server interface {
// NeedLeaderElection implements the LeaderElectionRunnable interface, which indicates
// the webhook server doesn't need leader election.
NeedLeaderElection() bool

// Register marks the given webhook as being served at the given path.
// It panics if two hooks are registered on the same path.
Register(path string, hook http.Handler)

// Start runs the server.
// It will install the webhook related resources depend on the server configuration.
Start(ctx context.Context) error

// StartedChecker returns an healthz.Checker which is healthy after the
// server has been started.
StartedChecker() healthz.Checker

// WebhookMux returns the servers WebhookMux
WebhookMux() *http.ServeMux
}
```

Expand Down Expand Up @@ -46,9 +52,9 @@ if err != nil {
}

// Create a webhook server.
hookServer := &Server{
hookServer := NewServer(Options{
Port: 8443,
}
})
if err := mgr.Add(hookServer); err != nil {
panic(err)
}
Expand All @@ -64,3 +70,24 @@ if err != nil {
panic(err)
}
```

## Run

```
go run contents/kubernetes-operator/controller-runtime/webhook/main.go
panic: open /var/folders/c2/hjlk2kcn63s4kds9k2_ctdhc0000gp/T/k8s-webhook-server/serving-certs/tls.crt: no such file or directory

goroutine 1 [running]:
main.main()
/Users/m.naka/repos/nakamasato/kubernetes-training/contents/kubernetes-operator/controller-runtime/webhook/main.go:56 +0x1c4
exit status 2
```

## Changes

1. [v0.15.0](https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.15.0)
1. [Allow passing a custom webhook server controller-runtime#2293](https://github.com/kubernetes-sigs/controller-runtime/pull/2293) `webhook.Server` `struct` was changed to `interface`.
```diff
- hookServer := &Server{Port: 8443}
+ hookServer := NewServer(Options{Port: 8443})
```
58 changes: 58 additions & 0 deletions contents/kubernetes-operator/controller-runtime/webhook/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"

ctrl "sigs.k8s.io/controller-runtime"
. "sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

var (
// Build webhooks used for the various server
// configuration options
//
// These handlers could be also be implementations
// of the AdmissionHandler interface for more complex
// implementations.
mutatingHook = &Admission{
Handler: admission.HandlerFunc(func(ctx context.Context, req AdmissionRequest) AdmissionResponse {
return Patched("some changes",
JSONPatchOp{Operation: "add", Path: "/metadata/annotations/access", Value: "granted"},
JSONPatchOp{Operation: "add", Path: "/metadata/annotations/reason", Value: "not so secret"},
)
}),
}

validatingHook = &Admission{
Handler: admission.HandlerFunc(func(ctx context.Context, req AdmissionRequest) AdmissionResponse {
return Denied("none shall pass!")
}),
}
)

func main() {
// Create a manager
// Note: GetConfigOrDie will os.Exit(1) w/o any message if no kube-config can be found
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
if err != nil {
panic(err)
}

// Create a webhook server.
hookServer := NewServer(Options{Port: 8443})
if err := mgr.Add(hookServer); err != nil {
panic(err)
}

// Register the webhooks in the server.
hookServer.Register("/mutating", mutatingHook)
hookServer.Register("/validating", validatingHook)

// Start the server by starting a previously-set-up manager
err = mgr.Start(ctrl.SetupSignalHandler())
if err != nil {
// handle error
panic(err)
}
}
61 changes: 32 additions & 29 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,66 @@ require (
go.uber.org/zap v1.25.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.26.3
k8s.io/apimachinery v0.26.3
k8s.io/client-go v0.26.3
k8s.io/api v0.28.1
k8s.io/apimachinery v0.28.1
k8s.io/client-go v0.28.1
k8s.io/klog/v2 v2.100.1
sigs.k8s.io/controller-runtime v0.14.6
sigs.k8s.io/controller-runtime v0.16.1
)

require (
github.com/GoogleContainerTools/skaffold v1.39.18 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/zapr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.15.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
k8s.io/apiextensions-apiserver v0.26.1 // indirect
k8s.io/component-base v0.26.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/apiextensions-apiserver v0.28.0 // indirect
k8s.io/component-base v0.28.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading