well_known_labels.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright 2015 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. package apis
  14. import (
  15. "strings"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/util/sets"
  18. )
  19. const (
  20. // LabelOS is a label to indicate the operating system of the node.
  21. // The OS labels are promoted to GA in 1.14. kubelet applies both beta
  22. // and GA labels to ensure backward compatibility.
  23. // TODO: stop applying the beta OS labels in Kubernetes 1.18.
  24. LabelOS = "beta.kubernetes.io/os"
  25. // LabelArch is a label to indicate the architecture of the node.
  26. // The Arch labels are promoted to GA in 1.14. kubelet applies both beta
  27. // and GA labels to ensure backward compatibility.
  28. // TODO: stop applying the beta Arch labels in Kubernetes 1.18.
  29. LabelArch = "beta.kubernetes.io/arch"
  30. // GA versions of the legacy beta labels.
  31. // TODO: update kubelet and controllers to set both beta and GA labels, then export these constants
  32. labelZoneFailureDomainGA = "failure-domain.kubernetes.io/zone"
  33. labelZoneRegionGA = "failure-domain.kubernetes.io/region"
  34. labelInstanceTypeGA = "kubernetes.io/instance-type"
  35. )
  36. var kubeletLabels = sets.NewString(
  37. v1.LabelHostname,
  38. v1.LabelZoneFailureDomain,
  39. v1.LabelZoneRegion,
  40. v1.LabelInstanceType,
  41. v1.LabelOSStable,
  42. v1.LabelArchStable,
  43. LabelOS,
  44. LabelArch,
  45. labelZoneFailureDomainGA,
  46. labelZoneRegionGA,
  47. labelInstanceTypeGA,
  48. )
  49. var kubeletLabelNamespaces = sets.NewString(
  50. v1.LabelNamespaceSuffixKubelet,
  51. v1.LabelNamespaceSuffixNode,
  52. )
  53. // KubeletLabels returns the list of label keys kubelets are allowed to set on their own Node objects
  54. func KubeletLabels() []string {
  55. return kubeletLabels.List()
  56. }
  57. // KubeletLabelNamespaces returns the list of label key namespaces kubelets are allowed to set on their own Node objects
  58. func KubeletLabelNamespaces() []string {
  59. return kubeletLabelNamespaces.List()
  60. }
  61. // IsKubeletLabel returns true if the label key is one that kubelets are allowed to set on their own Node object.
  62. // This checks if the key is in the KubeletLabels() list, or has a namespace in the KubeletLabelNamespaces() list.
  63. func IsKubeletLabel(key string) bool {
  64. if kubeletLabels.Has(key) {
  65. return true
  66. }
  67. namespace := getLabelNamespace(key)
  68. for allowedNamespace := range kubeletLabelNamespaces {
  69. if namespace == allowedNamespace || strings.HasSuffix(namespace, "."+allowedNamespace) {
  70. return true
  71. }
  72. }
  73. return false
  74. }
  75. func getLabelNamespace(key string) string {
  76. if parts := strings.SplitN(key, "/", 2); len(parts) == 2 {
  77. return parts[0]
  78. }
  79. return ""
  80. }