cachesize.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. //use for --watch-cache-sizes param of kube-apiserver
  14. //make watch cache size of resources configurable
  15. package cachesize
  16. import (
  17. "strconv"
  18. "strings"
  19. "github.com/golang/glog"
  20. )
  21. type Resource string
  22. const (
  23. CertificateSigningRequests Resource = "certificatesigningrequests"
  24. ClusterRoles Resource = "clusterroles"
  25. ClusterRoleBindings Resource = "clusterrolebindings"
  26. ConfigMaps Resource = "configmaps"
  27. Controllers Resource = "controllers"
  28. Daemonsets Resource = "daemonsets"
  29. Deployments Resource = "deployments"
  30. Endpoints Resource = "endpoints"
  31. HorizontalPodAutoscalers Resource = "horizontalpodautoscalers"
  32. Ingress Resource = "ingress"
  33. PodDisruptionBudget Resource = "poddisruptionbudgets"
  34. PetSet Resource = "petset"
  35. Jobs Resource = "jobs"
  36. LimitRanges Resource = "limitranges"
  37. Namespaces Resource = "namespaces"
  38. NetworkPolicys Resource = "networkpolicies"
  39. Nodes Resource = "nodes"
  40. PersistentVolumes Resource = "persistentvolumes"
  41. PersistentVolumeClaims Resource = "persistentvolumeclaims"
  42. Pods Resource = "pods"
  43. PodSecurityPolicies Resource = "podsecuritypolicies"
  44. PodTemplates Resource = "podtemplates"
  45. Replicasets Resource = "replicasets"
  46. ResourceQuotas Resource = "resourcequotas"
  47. ScheduledJobs Resource = "scheduledjobs"
  48. Roles Resource = "roles"
  49. RoleBindings Resource = "rolebindings"
  50. Secrets Resource = "secrets"
  51. ServiceAccounts Resource = "serviceaccounts"
  52. Services Resource = "services"
  53. StorageClasses Resource = "storageclasses"
  54. // Default value of watch cache size for a resource if not specified.
  55. defaultWatchCacheSize = 100
  56. )
  57. // TODO: This shouldn't be a global variable.
  58. var watchCacheSizes map[Resource]int
  59. func init() {
  60. watchCacheSizes = make(map[Resource]int)
  61. }
  62. func InitializeWatchCacheSizes(expectedRAMCapacityMB int) {
  63. // This is the heuristics that from memory capacity is trying to infer
  64. // the maximum number of nodes in the cluster and set cache sizes based
  65. // on that value.
  66. // From our documentation, we officially recomment 120GB machines for
  67. // 2000 nodes, and we scale from that point. Thus we assume ~60MB of
  68. // capacity per node.
  69. // TODO: Revisit this heuristics
  70. clusterSize := expectedRAMCapacityMB / 60
  71. // We should specify cache size for a given resource only if it
  72. // is supposed to have non-default value.
  73. //
  74. // TODO: Figure out which resource we should have non-default value.
  75. watchCacheSizes[Controllers] = maxInt(5*clusterSize, 100)
  76. watchCacheSizes[Endpoints] = maxInt(10*clusterSize, 1000)
  77. watchCacheSizes[Nodes] = maxInt(3*clusterSize, 1000)
  78. watchCacheSizes[Pods] = maxInt(10*clusterSize, 1000)
  79. watchCacheSizes[Services] = maxInt(5*clusterSize, 1000)
  80. }
  81. func SetWatchCacheSizes(cacheSizes []string) {
  82. for _, c := range cacheSizes {
  83. tokens := strings.Split(c, "#")
  84. if len(tokens) != 2 {
  85. glog.Errorf("invalid value of watch cache capabilities: %s", c)
  86. continue
  87. }
  88. size, err := strconv.Atoi(tokens[1])
  89. if err != nil {
  90. glog.Errorf("invalid size of watch cache capabilities: %s", c)
  91. continue
  92. }
  93. watchCacheSizes[Resource(strings.ToLower(tokens[0]))] = size
  94. }
  95. }
  96. func GetWatchCacheSizeByResource(resource Resource) int {
  97. if value, found := watchCacheSizes[resource]; found {
  98. return value
  99. }
  100. return defaultWatchCacheSize
  101. }
  102. func maxInt(a, b int) int {
  103. if a > b {
  104. return a
  105. }
  106. return b
  107. }