-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A long standing issue in controller runtime, the delegating client is the default client that the manager creates. Whenever a user calls Get/List on a typed object, the internal cache spins up an informer and start watching *all* objects for that group-version-kind. This change introduces the ability for the delegating client to take a list of objects that should always hit the live api-server, and bypass the cache. We also offer the ability to build a manager.NewClientFunc with a new builder that exposes the ability to mark which objects we want to be uncached. Signed-off-by: Vince Prignano <vincepri@vmware.com>
- Loading branch information
Showing
5 changed files
with
116 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package manager | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// DefaultNewClient creates the default caching client | ||
var DefaultNewClient = NewClientFuncBuilder().Build() | ||
|
||
// NewClient builder is the interface for the builder for manager.NewClientFunc. | ||
type NewClientBuilder interface { | ||
// WithUncached takes a list of runtime objects (plain or lists) that users don't want to cache | ||
// for this client. This function can be called multiple times, it should append to an internal slice. | ||
WithUncached(objs ...runtime.Object) | ||
|
||
// Build returns the NewClientFunc that can be used when creating a new manager. | ||
Build() NewClientFunc | ||
} | ||
|
||
// NewClientFuncBuilder returns a builder to create a new manager.NewClientFunc to be passed when creating a Manager. | ||
func NewClientFuncBuilder() NewClientBuilder { | ||
return &newClientBuilder{} | ||
} | ||
|
||
type newClientBuilder struct { | ||
uncached []runtime.Object | ||
} | ||
|
||
func (n *newClientBuilder) WithUncached(objs ...runtime.Object) { | ||
n.uncached = append(n.uncached, objs...) | ||
} | ||
|
||
func (n *newClientBuilder) Build() NewClientFunc { | ||
return func(cache cache.Cache, config *rest.Config, options client.Options) (client.Client, error) { | ||
// Create the Client for Write operations. | ||
c, err := client.New(config, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return client.NewDelegatingClient(client.NewDelegatingClientInput{ | ||
CacheReader: cache, | ||
Client: c, | ||
UncachedObjects: n.uncached, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters