2016-02-05 07:47:27 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2016-02-05 07:47:27 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//use for --watch-cache-sizes param of kube-apiserver
|
|
|
|
//make watch cache size of resources configurable
|
|
|
|
package cachesize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Resource string
|
|
|
|
|
|
|
|
const (
|
2016-04-14 01:25:33 +00:00
|
|
|
CertificateSigningRequests Resource = "certificatesigningrequests"
|
|
|
|
ClusterRoles Resource = "clusterroles"
|
|
|
|
ClusterRoleBindings Resource = "clusterrolebindings"
|
2016-08-03 09:44:10 +00:00
|
|
|
ConfigMaps Resource = "configmaps"
|
2016-04-14 01:25:33 +00:00
|
|
|
Controllers Resource = "controllers"
|
|
|
|
Daemonsets Resource = "daemonsets"
|
|
|
|
Deployments Resource = "deployments"
|
|
|
|
Endpoints Resource = "endpoints"
|
|
|
|
HorizontalPodAutoscalers Resource = "horizontalpodautoscalers"
|
|
|
|
Ingress Resource = "ingress"
|
|
|
|
PodDisruptionBudget Resource = "poddisruptionbudgets"
|
2016-10-26 20:44:07 +00:00
|
|
|
StatefulSet Resource = "statefulset"
|
2016-04-14 01:25:33 +00:00
|
|
|
Jobs Resource = "jobs"
|
|
|
|
LimitRanges Resource = "limitranges"
|
|
|
|
Namespaces Resource = "namespaces"
|
|
|
|
NetworkPolicys Resource = "networkpolicies"
|
|
|
|
Nodes Resource = "nodes"
|
|
|
|
PersistentVolumes Resource = "persistentvolumes"
|
|
|
|
PersistentVolumeClaims Resource = "persistentvolumeclaims"
|
|
|
|
Pods Resource = "pods"
|
2016-08-03 09:44:10 +00:00
|
|
|
PodSecurityPolicies Resource = "podsecuritypolicies"
|
2016-04-14 01:25:33 +00:00
|
|
|
PodTemplates Resource = "podtemplates"
|
|
|
|
Replicasets Resource = "replicasets"
|
|
|
|
ResourceQuotas Resource = "resourcequotas"
|
2016-11-01 22:46:23 +00:00
|
|
|
CronJobs Resource = "cronjobs"
|
2016-04-14 01:25:33 +00:00
|
|
|
Roles Resource = "roles"
|
|
|
|
RoleBindings Resource = "rolebindings"
|
|
|
|
Secrets Resource = "secrets"
|
|
|
|
ServiceAccounts Resource = "serviceaccounts"
|
|
|
|
Services Resource = "services"
|
2016-08-03 09:44:10 +00:00
|
|
|
StorageClasses Resource = "storageclasses"
|
|
|
|
|
|
|
|
// Default value of watch cache size for a resource if not specified.
|
|
|
|
defaultWatchCacheSize = 100
|
2016-02-05 07:47:27 +00:00
|
|
|
)
|
|
|
|
|
2016-08-03 09:44:10 +00:00
|
|
|
// TODO: This shouldn't be a global variable.
|
2016-02-05 07:47:27 +00:00
|
|
|
var watchCacheSizes map[Resource]int
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
watchCacheSizes = make(map[Resource]int)
|
2016-08-03 09:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func InitializeWatchCacheSizes(expectedRAMCapacityMB int) {
|
|
|
|
// This is the heuristics that from memory capacity is trying to infer
|
|
|
|
// the maximum number of nodes in the cluster and set cache sizes based
|
|
|
|
// on that value.
|
|
|
|
// From our documentation, we officially recomment 120GB machines for
|
|
|
|
// 2000 nodes, and we scale from that point. Thus we assume ~60MB of
|
|
|
|
// capacity per node.
|
|
|
|
// TODO: Revisit this heuristics
|
|
|
|
clusterSize := expectedRAMCapacityMB / 60
|
|
|
|
|
|
|
|
// We should specify cache size for a given resource only if it
|
|
|
|
// is supposed to have non-default value.
|
|
|
|
//
|
|
|
|
// TODO: Figure out which resource we should have non-default value.
|
2016-08-29 07:28:44 +00:00
|
|
|
watchCacheSizes[Controllers] = maxInt(5*clusterSize, 100)
|
2016-08-03 09:44:10 +00:00
|
|
|
watchCacheSizes[Endpoints] = maxInt(10*clusterSize, 1000)
|
2016-10-27 08:10:43 +00:00
|
|
|
watchCacheSizes[Nodes] = maxInt(5*clusterSize, 1000)
|
|
|
|
watchCacheSizes[Pods] = maxInt(50*clusterSize, 1000)
|
2016-09-01 11:32:27 +00:00
|
|
|
watchCacheSizes[Services] = maxInt(5*clusterSize, 1000)
|
2016-02-05 07:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetWatchCacheSizes(cacheSizes []string) {
|
|
|
|
for _, c := range cacheSizes {
|
|
|
|
tokens := strings.Split(c, "#")
|
|
|
|
if len(tokens) != 2 {
|
|
|
|
glog.Errorf("invalid value of watch cache capabilities: %s", c)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := strconv.Atoi(tokens[1])
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("invalid size of watch cache capabilities: %s", c)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
watchCacheSizes[Resource(strings.ToLower(tokens[0]))] = size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetWatchCacheSizeByResource(resource Resource) int {
|
2016-08-03 09:44:10 +00:00
|
|
|
if value, found := watchCacheSizes[resource]; found {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return defaultWatchCacheSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func maxInt(a, b int) int {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
2016-02-05 07:47:27 +00:00
|
|
|
}
|