forked from nmstate/kubernetes-nmstate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The controller-runtime is missing filtering it's cache by labels or fields [1], this means that all the kubernetes-nmstate-handlers will read all the nodes and nodenetworkstates every period, clearly dies does not scale since kubernetes-nmstate-handler runs at as daemonset meaning that there is one handler running at every node so the bigger the cluster the bigger the problem. This change replace the default controller-runtime cache with an implementation that can be configured to use some field selectors depending on the resource, this way we can filter by "metadata.name" using the node name for "node" and "nodenetworkstate" so only one instance of them is feteched. [1] kubernetes-sigs/controller-runtime#244 Signed-off-by: Quique Llorente <ellorent@redhat.com>
- Loading branch information
Showing
12 changed files
with
968 additions
and
17 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
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,79 @@ | ||
/* | ||
Copyright 2018 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 cache | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/nmstate/kubernetes-nmstate/pkg/cache/internal" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/rest" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
crcache "sigs.k8s.io/controller-runtime/pkg/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
) | ||
|
||
var log = ctrl.Log.WithName("object-cache") | ||
|
||
var defaultResyncTime = 10 * time.Hour | ||
|
||
// Options are the optional arguments for creating a new InformersMap object | ||
type Options struct { | ||
crcache.Options | ||
FieldSelectorByResource map[string]string | ||
} | ||
|
||
// New initializes and returns a new Cache. | ||
func New(config *rest.Config, opts Options) (crcache.Cache, error) { | ||
opts, err := defaultOpts(config, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace, opts.FieldSelectorByResource) | ||
return &informerCache{InformersMap: im}, nil | ||
} | ||
|
||
func Builder(opts Options) crcache.NewCacheFunc { | ||
return func(config *rest.Config, cropts crcache.Options) (crcache.Cache, error) { | ||
opts.Options = cropts | ||
return New(config, opts) | ||
} | ||
} | ||
|
||
func defaultOpts(config *rest.Config, opts Options) (Options, error) { | ||
// Use the default Kubernetes Scheme if unset | ||
if opts.Scheme == nil { | ||
opts.Scheme = scheme.Scheme | ||
} | ||
|
||
// Construct a new Mapper if unset | ||
if opts.Mapper == nil { | ||
var err error | ||
opts.Mapper, err = apiutil.NewDiscoveryRESTMapper(config) | ||
if err != nil { | ||
log.WithName("setup").Error(err, "Failed to get API Group-Resources") | ||
return opts, fmt.Errorf("could not create RESTMapper from config") | ||
} | ||
} | ||
|
||
// Default the resync period to 10 hours if unset | ||
if opts.Resync == nil { | ||
opts.Resync = &defaultResyncTime | ||
} | ||
return opts, nil | ||
} |
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,19 @@ | ||
/* | ||
Copyright 2019 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 cache provides object caches that act as caching client.Reader | ||
// instances and help drive Kubernetes-object-based event handlers. | ||
package cache |
Oops, something went wrong.