well_known_labels.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. )
  31. var kubeletLabels = sets.NewString(
  32. v1.LabelHostname,
  33. v1.LabelZoneFailureDomainStable,
  34. v1.LabelZoneRegionStable,
  35. v1.LabelZoneFailureDomain,
  36. v1.LabelZoneRegion,
  37. v1.LabelInstanceType,
  38. v1.LabelInstanceTypeStable,
  39. v1.LabelOSStable,
  40. v1.LabelArchStable,
  41. LabelOS,
  42. LabelArch,
  43. )
  44. var kubeletLabelNamespaces = sets.NewString(
  45. v1.LabelNamespaceSuffixKubelet,
  46. v1.LabelNamespaceSuffixNode,
  47. )
  48. // KubeletLabels returns the list of label keys kubelets are allowed to set on their own Node objects
  49. func KubeletLabels() []string {
  50. return kubeletLabels.List()
  51. }
  52. // KubeletLabelNamespaces returns the list of label key namespaces kubelets are allowed to set on their own Node objects
  53. func KubeletLabelNamespaces() []string {
  54. return kubeletLabelNamespaces.List()
  55. }
  56. // IsKubeletLabel returns true if the label key is one that kubelets are allowed to set on their own Node object.
  57. // This checks if the key is in the KubeletLabels() list, or has a namespace in the KubeletLabelNamespaces() list.
  58. func IsKubeletLabel(key string) bool {
  59. if kubeletLabels.Has(key) {
  60. return true
  61. }
  62. namespace := getLabelNamespace(key)
  63. for allowedNamespace := range kubeletLabelNamespaces {
  64. if namespace == allowedNamespace || strings.HasSuffix(namespace, "."+allowedNamespace) {
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. func getLabelNamespace(key string) string {
  71. if parts := strings.SplitN(key, "/", 2); len(parts) == 2 {
  72. return parts[0]
  73. }
  74. return ""
  75. }