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

chore: update controller-runtime source #293

Merged
merged 1 commit 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
25 changes: 11 additions & 14 deletions contents/kubernetes-operator/controller-runtime/source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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)
## Implementations

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
Expand All @@ -32,7 +32,7 @@ type SyncingSource interface {
}
```
`Kind` has `InjectCache` while `kindWithCache` doesn't.
1. [Kind](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L91-L102): Kind is used to provide a source of **events originating inside the cluster** from Watches (e.g. Pod Create).
1. ~~[Kind](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L91-L102)~~ -> [Kind](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.16.0/pkg/internal/source/kind.go#L20-L31) (Moved to `pkg/internal/source/kind`): Kind is used to provide a source of **events originating inside the cluster** from Watches (e.g. Pod Create).
```go
type Kind struct {
Type client.Object
Expand All @@ -41,19 +41,16 @@ type SyncingSource interface {
startCancel func()
}
```
1. `Kind` has `InjectCache` while `kindWithCache` doesn't.
1. This is used by default if you build a controller with [builder](../builder/README.md#-convert-client.Object-to-source).
1. ~~`Kind` has `InjectCache` while `kindWithCache` doesn't.~~
1. ~~This is used by default if you build a controller with [builder](../builder/README.md#-convert-client.Object-to-source).~~
```go
src := &source.Kind{Type: typeForSrc}
```
1. The cache is injected in [controller.Watch](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/internal/controller/controller.go#L129-L130) by [inject](../inject) feature.
1. ~~The cache is injected in [controller.Watch](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/internal/controller/controller.go#L129-L130) by [inject](../inject) feature~~ -> Changed to provide cache explicitly with the initialization method [`func Kind(cache cache.Cache, object client.Object) SyncingSource`](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.16.0/pkg/source/source.go#L61).
```go
// Inject Cache into arguments
if err := c.SetFields(src); err != nil {
return err
}
source.Kind(mgr.GetCache(), &corev1.Pod{})
```
1. [Channel](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L207-L226): Channel is used to provide a source of **events originating outside the cluster** (e.g. GitHub Webhook callback). **Channel requires the user to wire the external source** (eh.g. http handler) to write GenericEvents to the underlying channel.
1. [Channel](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.16.0/pkg/source/source.go#L70-L86): Channel is used to provide a source of **events originating outside the cluster** (e.g. GitHub Webhook callback). **Channel requires the user to wire the external source** (eh.g. http handler) to write GenericEvents to the underlying channel.
1. [Informer](https://github.com/kubernetes-sigs/controller-runtime/blob/v0.13.0/pkg/source/source.go#L338-L341): Informer is used to provide a source of **events originating inside the cluster** from Watches (e.g. Pod Create).
```go
type Informer struct {
Expand Down Expand Up @@ -172,7 +169,7 @@ if you want to check events of specific resource you can set by the following.
```
1. Create `Kind` for the target resource.
```go
kindWithCache := source.Kind(cache, mysqluser)
kind := source.Kind(cache, mysqluser)
```
1. Prepare `workqueue` and `eventHandler`.
```go
Expand All @@ -192,9 +189,9 @@ if you want to check events of specific resource you can set by the following.
},
}
```
1. Start `kindWithCache` with the prepared `eventHandler` and `queue`.
1. Start `kind` with the prepared `eventHandler` and `queue`.
```go
if err := kindWithCache.Start(ctx, eventHandler, queue); err != nil { // Get informer and set eventHandler
if err := kind.Start(ctx, eventHandler, queue); err != nil { // Get informer and set eventHandler
log.Error(err, "")
}
```
Expand All @@ -205,7 +202,7 @@ You can run:
```
kubectl apply -f https://raw.githubusercontent.com/nakamasato/mysql-operator/main/config/crd/bases/mysql.nakamasato.com_mysqlusers.yaml
```
1. Run the `kindWithCache`
1. Run the `kind`
```
go run main.go
```
Expand Down
14 changes: 7 additions & 7 deletions contents/kubernetes-operator/controller-runtime/source/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func main() {
}()
log.Info("cache is started")

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

// Prepare queue and eventHandler
queue := workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "test")
Expand All @@ -104,21 +104,21 @@ func main() {
}

// Start Source
if err := kindWithCacheMysqlUser.Start(ctx, eventHandler, queue); err != nil { // Get informer and set eventHandler
if err := kindMysqlUser.Start(ctx, eventHandler, queue); err != nil { // Get informer and set eventHandler
log.Error(err, "")
}
if err := kindWithCachePod.Start(ctx, eventHandler, queue); err != nil { // Get informer and set eventHandler
if err := kindPod.Start(ctx, eventHandler, queue); err != nil { // Get informer and set eventHandler
log.Error(err, "")
}

// Wait for cache
if err := kindWithCacheMysqlUser.WaitForSync(ctx); err != nil {
if err := kindMysqlUser.WaitForSync(ctx); err != nil {
log.Error(err, "")
}
if err := kindWithCachePod.WaitForSync(ctx); err != nil {
if err := kindPod.WaitForSync(ctx); err != nil {
log.Error(err, "")
}
log.Info("kindWithCache is ready")
log.Info("kind is ready")

for {
item, shutdown := queue.Get()
Expand Down