Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit d2cf858

Browse files
committed
make watch cache sizes configuratable of kube-apiserver
1 parent c09b67f commit d2cf858

File tree

23 files changed

+143
-19
lines changed

23 files changed

+143
-19
lines changed

cmd/kube-apiserver/app/options/options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type APIServer struct {
8080
ServiceNodePortRange utilnet.PortRange
8181
StorageVersions string
8282
TokenAuthFile string
83+
WatchCacheSizes []string
8384
}
8485

8586
// NewAPIServer creates a new APIServer object with default parameters
@@ -208,4 +209,5 @@ func (s *APIServer) AddFlags(fs *pflag.FlagSet) {
208209
fs.IntVar(&s.KubernetesServiceNodePort, "kubernetes-service-node-port", 0, "If non-zero, the Kubernetes master service (which apiserver creates/maintains) will be of type NodePort, using this as the value of the port. If zero, the Kubernetes master service will be of type ClusterIP.")
209210
// TODO: delete this flag as soon as we identify and fix all clients that send malformed updates, like #14126.
210211
fs.BoolVar(&validation.RepairMalformedUpdates, "repair-malformed-updates", true, "If true, server will do its best to fix the update request to pass the validation, e.g., setting empty UID in update request to its existing value. This flag can be turned off after we fix all the clients that send malformed updates.")
212+
fs.StringSliceVar(&s.WatchCacheSizes, "watch-cache-sizes", s.WatchCacheSizes, "List of watch cache sizes for every resource (pods, nodes, etc.), comma separated. The individual override format: resource#size, where size is a number. It takes effect when watch-cache is enabled.")
211213
}

cmd/kube-apiserver/app/server.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"k8s.io/kubernetes/pkg/genericapiserver"
5050
kubeletclient "k8s.io/kubernetes/pkg/kubelet/client"
5151
"k8s.io/kubernetes/pkg/master"
52+
"k8s.io/kubernetes/pkg/registry/cachesize"
5253
"k8s.io/kubernetes/pkg/runtime"
5354
"k8s.io/kubernetes/pkg/serviceaccount"
5455
"k8s.io/kubernetes/pkg/storage"
@@ -401,10 +402,16 @@ func Run(s *options.APIServer) error {
401402

402403
Tunneler: tunneler,
403404
}
405+
406+
if s.EnableWatchCache {
407+
cachesize.SetWatchCacheSizes(s.WatchCacheSizes)
408+
}
409+
404410
m, err := master.New(config)
405411
if err != nil {
406412
return err
407413
}
414+
408415
m.Run(s.ServerRunOptions)
409416
return nil
410417
}

docs/admin/kube-apiserver.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ kube-apiserver
106106
--tls-private-key-file="": File containing x509 private key matching --tls-cert-file.
107107
--token-auth-file="": If set, the file that will be used to secure the secure port of the API server via token authentication.
108108
--watch-cache[=true]: Enable watch caching in the apiserver
109+
--watch-cache-sizes=[]: List of watch cache sizes for every resource (pods, nodes, etc.), comma separated. The individual override format: resource#size, where size is a number. It takes effect when watch-cache is enabled.
109110
```
110111

111-
###### Auto generated by spf13/cobra on 26-Jan-2016
112+
###### Auto generated by spf13/cobra on 5-Feb-2016
112113

113114

114115
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->

hack/verify-flags/known-flags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,4 @@ leader-elect
372372
leader-elect-lease-duration
373373
leader-elect-renew-deadline
374374
leader-elect-retry-period
375+
watch-cache-sizes
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Copyright 2014 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
//use for --watch-cache-sizes param of kube-apiserver
18+
//make watch cache size of resources configurable
19+
package cachesize
20+
21+
import (
22+
"strconv"
23+
"strings"
24+
25+
"github.com/golang/glog"
26+
)
27+
28+
type Resource string
29+
30+
const (
31+
Controllers Resource = "controllers"
32+
Daemonsets Resource = "daemonsets"
33+
Deployments Resource = "deployments"
34+
Endpoints Resource = "endpoints"
35+
HorizontalPodAutoscalers Resource = "horizontalpodautoscalers"
36+
Ingress Resource = "ingress"
37+
Jobs Resource = "jobs"
38+
LimitRanges Resource = "limitranges"
39+
Namespaces Resource = "namespaces"
40+
Nodes Resource = "nodes"
41+
PersistentVolumes Resource = "persistentvolumes"
42+
PersistentVolumeClaims Resource = "persistentvolumeclaims"
43+
Pods Resource = "pods"
44+
PodTemplates Resource = "podtemplates"
45+
ResourceQuotas Resource = "resourcequotas"
46+
Secrets Resource = "secrets"
47+
ServiceAccounts Resource = "serviceaccounts"
48+
Services Resource = "services"
49+
)
50+
51+
var watchCacheSizes map[Resource]int
52+
53+
func init() {
54+
watchCacheSizes = make(map[Resource]int)
55+
watchCacheSizes[Controllers] = 100
56+
watchCacheSizes[Daemonsets] = 100
57+
watchCacheSizes[Deployments] = 100
58+
watchCacheSizes[Endpoints] = 1000
59+
watchCacheSizes[HorizontalPodAutoscalers] = 100
60+
watchCacheSizes[Ingress] = 100
61+
watchCacheSizes[Jobs] = 100
62+
watchCacheSizes[LimitRanges] = 100
63+
watchCacheSizes[Namespaces] = 100
64+
watchCacheSizes[Nodes] = 1000
65+
watchCacheSizes[PersistentVolumes] = 100
66+
watchCacheSizes[PersistentVolumeClaims] = 100
67+
watchCacheSizes[Pods] = 1000
68+
watchCacheSizes[PodTemplates] = 100
69+
watchCacheSizes[ResourceQuotas] = 100
70+
watchCacheSizes[Secrets] = 100
71+
watchCacheSizes[ServiceAccounts] = 100
72+
watchCacheSizes[Services] = 100
73+
}
74+
75+
func SetWatchCacheSizes(cacheSizes []string) {
76+
for _, c := range cacheSizes {
77+
tokens := strings.Split(c, "#")
78+
if len(tokens) != 2 {
79+
glog.Errorf("invalid value of watch cache capabilities: %s", c)
80+
continue
81+
}
82+
83+
size, err := strconv.Atoi(tokens[1])
84+
if err != nil {
85+
glog.Errorf("invalid size of watch cache capabilities: %s", c)
86+
continue
87+
}
88+
89+
watchCacheSizes[Resource(strings.ToLower(tokens[0]))] = size
90+
}
91+
}
92+
93+
func GetWatchCacheSizeByResource(resource Resource) int {
94+
return watchCacheSizes[resource]
95+
}

pkg/registry/controller/etcd/etcd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"k8s.io/kubernetes/pkg/api"
2121
"k8s.io/kubernetes/pkg/fields"
2222
"k8s.io/kubernetes/pkg/labels"
23+
"k8s.io/kubernetes/pkg/registry/cachesize"
2324
"k8s.io/kubernetes/pkg/registry/controller"
2425
"k8s.io/kubernetes/pkg/registry/generic"
2526
etcdgeneric "k8s.io/kubernetes/pkg/registry/generic/etcd"
@@ -37,7 +38,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
3738

3839
newListFunc := func() runtime.Object { return &api.ReplicationControllerList{} }
3940
storageInterface := storageDecorator(
40-
s, 100, &api.ReplicationController{}, prefix, controller.Strategy, newListFunc)
41+
s, cachesize.GetWatchCacheSizeByResource(cachesize.Controllers), &api.ReplicationController{}, prefix, controller.Strategy, newListFunc)
4142

4243
store := &etcdgeneric.Etcd{
4344
NewFunc: func() runtime.Object { return &api.ReplicationController{} },

pkg/registry/daemonset/etcd/etcd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"k8s.io/kubernetes/pkg/apis/extensions"
2222
"k8s.io/kubernetes/pkg/fields"
2323
"k8s.io/kubernetes/pkg/labels"
24+
"k8s.io/kubernetes/pkg/registry/cachesize"
2425
"k8s.io/kubernetes/pkg/registry/daemonset"
2526
"k8s.io/kubernetes/pkg/registry/generic"
2627
etcdgeneric "k8s.io/kubernetes/pkg/registry/generic/etcd"
@@ -39,7 +40,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
3940

4041
newListFunc := func() runtime.Object { return &extensions.DaemonSetList{} }
4142
storageInterface := storageDecorator(
42-
s, 100, &extensions.DaemonSet{}, prefix, daemonset.Strategy, newListFunc)
43+
s, cachesize.GetWatchCacheSizeByResource(cachesize.Daemonsets), &extensions.DaemonSet{}, prefix, daemonset.Strategy, newListFunc)
4344

4445
store := &etcdgeneric.Etcd{
4546
NewFunc: func() runtime.Object { return &extensions.DaemonSet{} },

pkg/registry/deployment/etcd/etcd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
extvalidation "k8s.io/kubernetes/pkg/apis/extensions/validation"
2828
"k8s.io/kubernetes/pkg/fields"
2929
"k8s.io/kubernetes/pkg/labels"
30+
"k8s.io/kubernetes/pkg/registry/cachesize"
3031
"k8s.io/kubernetes/pkg/registry/deployment"
3132
"k8s.io/kubernetes/pkg/registry/generic"
3233
etcdgeneric "k8s.io/kubernetes/pkg/registry/generic/etcd"
@@ -64,7 +65,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
6465

6566
newListFunc := func() runtime.Object { return &extensions.DeploymentList{} }
6667
storageInterface := storageDecorator(
67-
s, 100, &extensions.Deployment{}, prefix, deployment.Strategy, newListFunc)
68+
s, cachesize.GetWatchCacheSizeByResource(cachesize.Deployments), &extensions.Deployment{}, prefix, deployment.Strategy, newListFunc)
6869

6970
store := &etcdgeneric.Etcd{
7071
NewFunc: func() runtime.Object { return &extensions.Deployment{} },

pkg/registry/endpoint/etcd/etcd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"k8s.io/kubernetes/pkg/api"
2121
"k8s.io/kubernetes/pkg/fields"
2222
"k8s.io/kubernetes/pkg/labels"
23+
"k8s.io/kubernetes/pkg/registry/cachesize"
2324
"k8s.io/kubernetes/pkg/registry/endpoint"
2425
"k8s.io/kubernetes/pkg/registry/generic"
2526
etcdgeneric "k8s.io/kubernetes/pkg/registry/generic/etcd"
@@ -37,7 +38,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *RE
3738

3839
newListFunc := func() runtime.Object { return &api.EndpointsList{} }
3940
storageInterface := storageDecorator(
40-
s, 1000, &api.Endpoints{}, prefix, endpoint.Strategy, newListFunc)
41+
s, cachesize.GetWatchCacheSizeByResource(cachesize.Endpoints), &api.Endpoints{}, prefix, endpoint.Strategy, newListFunc)
4142

4243
store := &etcdgeneric.Etcd{
4344
NewFunc: func() runtime.Object { return &api.Endpoints{} },

pkg/registry/horizontalpodautoscaler/etcd/etcd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"k8s.io/kubernetes/pkg/apis/extensions"
2222
"k8s.io/kubernetes/pkg/fields"
2323
"k8s.io/kubernetes/pkg/labels"
24+
"k8s.io/kubernetes/pkg/registry/cachesize"
2425
"k8s.io/kubernetes/pkg/registry/generic"
2526
etcdgeneric "k8s.io/kubernetes/pkg/registry/generic/etcd"
2627
"k8s.io/kubernetes/pkg/registry/horizontalpodautoscaler"
@@ -38,7 +39,7 @@ func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) (*R
3839

3940
newListFunc := func() runtime.Object { return &extensions.HorizontalPodAutoscalerList{} }
4041
storageInterface := storageDecorator(
41-
s, 100, &extensions.HorizontalPodAutoscaler{}, prefix, horizontalpodautoscaler.Strategy, newListFunc)
42+
s, cachesize.GetWatchCacheSizeByResource(cachesize.HorizontalPodAutoscalers), &extensions.HorizontalPodAutoscaler{}, prefix, horizontalpodautoscaler.Strategy, newListFunc)
4243

4344
store := &etcdgeneric.Etcd{
4445
NewFunc: func() runtime.Object { return &extensions.HorizontalPodAutoscaler{} },

0 commit comments

Comments
 (0)