helpers.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. package api
  14. import (
  15. "crypto/md5"
  16. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. "strings"
  20. "time"
  21. "k8s.io/kubernetes/pkg/api/resource"
  22. "k8s.io/kubernetes/pkg/api/unversioned"
  23. "k8s.io/kubernetes/pkg/conversion"
  24. "k8s.io/kubernetes/pkg/fields"
  25. "k8s.io/kubernetes/pkg/labels"
  26. "k8s.io/kubernetes/pkg/runtime"
  27. "k8s.io/kubernetes/pkg/selection"
  28. "k8s.io/kubernetes/pkg/types"
  29. "k8s.io/kubernetes/pkg/util/sets"
  30. "github.com/davecgh/go-spew/spew"
  31. )
  32. // Conversion error conveniently packages up errors in conversions.
  33. type ConversionError struct {
  34. In, Out interface{}
  35. Message string
  36. }
  37. // Return a helpful string about the error
  38. func (c *ConversionError) Error() string {
  39. return spew.Sprintf(
  40. "Conversion error: %s. (in: %v(%+v) out: %v)",
  41. c.Message, reflect.TypeOf(c.In), c.In, reflect.TypeOf(c.Out),
  42. )
  43. }
  44. // Semantic can do semantic deep equality checks for api objects.
  45. // Example: api.Semantic.DeepEqual(aPod, aPodWithNonNilButEmptyMaps) == true
  46. var Semantic = conversion.EqualitiesOrDie(
  47. func(a, b resource.Quantity) bool {
  48. // Ignore formatting, only care that numeric value stayed the same.
  49. // TODO: if we decide it's important, it should be safe to start comparing the format.
  50. //
  51. // Uninitialized quantities are equivalent to 0 quantities.
  52. return a.Cmp(b) == 0
  53. },
  54. func(a, b unversioned.Time) bool {
  55. return a.UTC() == b.UTC()
  56. },
  57. func(a, b labels.Selector) bool {
  58. return a.String() == b.String()
  59. },
  60. func(a, b fields.Selector) bool {
  61. return a.String() == b.String()
  62. },
  63. )
  64. var standardResourceQuotaScopes = sets.NewString(
  65. string(ResourceQuotaScopeTerminating),
  66. string(ResourceQuotaScopeNotTerminating),
  67. string(ResourceQuotaScopeBestEffort),
  68. string(ResourceQuotaScopeNotBestEffort),
  69. )
  70. // IsStandardResourceQuotaScope returns true if the scope is a standard value
  71. func IsStandardResourceQuotaScope(str string) bool {
  72. return standardResourceQuotaScopes.Has(str)
  73. }
  74. var podObjectCountQuotaResources = sets.NewString(
  75. string(ResourcePods),
  76. )
  77. var podComputeQuotaResources = sets.NewString(
  78. string(ResourceCPU),
  79. string(ResourceMemory),
  80. string(ResourceLimitsCPU),
  81. string(ResourceLimitsMemory),
  82. string(ResourceRequestsCPU),
  83. string(ResourceRequestsMemory),
  84. )
  85. // IsResourceQuotaScopeValidForResource returns true if the resource applies to the specified scope
  86. func IsResourceQuotaScopeValidForResource(scope ResourceQuotaScope, resource string) bool {
  87. switch scope {
  88. case ResourceQuotaScopeTerminating, ResourceQuotaScopeNotTerminating, ResourceQuotaScopeNotBestEffort:
  89. return podObjectCountQuotaResources.Has(resource) || podComputeQuotaResources.Has(resource)
  90. case ResourceQuotaScopeBestEffort:
  91. return podObjectCountQuotaResources.Has(resource)
  92. default:
  93. return true
  94. }
  95. }
  96. var standardContainerResources = sets.NewString(
  97. string(ResourceCPU),
  98. string(ResourceMemory),
  99. )
  100. // IsStandardContainerResourceName returns true if the container can make a resource request
  101. // for the specified resource
  102. func IsStandardContainerResourceName(str string) bool {
  103. return standardContainerResources.Has(str)
  104. }
  105. var standardLimitRangeTypes = sets.NewString(
  106. string(LimitTypePod),
  107. string(LimitTypeContainer),
  108. )
  109. // IsStandardLimitRangeType returns true if the type is Pod or Container
  110. func IsStandardLimitRangeType(str string) bool {
  111. return standardLimitRangeTypes.Has(str)
  112. }
  113. var standardQuotaResources = sets.NewString(
  114. string(ResourceCPU),
  115. string(ResourceMemory),
  116. string(ResourceRequestsCPU),
  117. string(ResourceRequestsMemory),
  118. string(ResourceRequestsStorage),
  119. string(ResourceLimitsCPU),
  120. string(ResourceLimitsMemory),
  121. string(ResourcePods),
  122. string(ResourceQuotas),
  123. string(ResourceServices),
  124. string(ResourceReplicationControllers),
  125. string(ResourceSecrets),
  126. string(ResourcePersistentVolumeClaims),
  127. string(ResourceConfigMaps),
  128. string(ResourceServicesNodePorts),
  129. string(ResourceServicesLoadBalancers),
  130. )
  131. // IsStandardQuotaResourceName returns true if the resource is known to
  132. // the quota tracking system
  133. func IsStandardQuotaResourceName(str string) bool {
  134. return standardQuotaResources.Has(str)
  135. }
  136. var standardResources = sets.NewString(
  137. string(ResourceCPU),
  138. string(ResourceMemory),
  139. string(ResourceRequestsCPU),
  140. string(ResourceRequestsMemory),
  141. string(ResourceLimitsCPU),
  142. string(ResourceLimitsMemory),
  143. string(ResourcePods),
  144. string(ResourceQuotas),
  145. string(ResourceServices),
  146. string(ResourceReplicationControllers),
  147. string(ResourceSecrets),
  148. string(ResourceConfigMaps),
  149. string(ResourcePersistentVolumeClaims),
  150. string(ResourceStorage),
  151. )
  152. // IsStandardResourceName returns true if the resource is known to the system
  153. func IsStandardResourceName(str string) bool {
  154. return standardResources.Has(str)
  155. }
  156. var integerResources = sets.NewString(
  157. string(ResourcePods),
  158. string(ResourceQuotas),
  159. string(ResourceServices),
  160. string(ResourceReplicationControllers),
  161. string(ResourceSecrets),
  162. string(ResourceConfigMaps),
  163. string(ResourcePersistentVolumeClaims),
  164. string(ResourceServicesNodePorts),
  165. string(ResourceServicesLoadBalancers),
  166. )
  167. // IsIntegerResourceName returns true if the resource is measured in integer values
  168. func IsIntegerResourceName(str string) bool {
  169. return integerResources.Has(str)
  170. }
  171. // NewDeleteOptions returns a DeleteOptions indicating the resource should
  172. // be deleted within the specified grace period. Use zero to indicate
  173. // immediate deletion. If you would prefer to use the default grace period,
  174. // use &api.DeleteOptions{} directly.
  175. func NewDeleteOptions(grace int64) *DeleteOptions {
  176. return &DeleteOptions{GracePeriodSeconds: &grace}
  177. }
  178. // NewPreconditionDeleteOptions returns a DeleteOptions with a UID precondition set.
  179. func NewPreconditionDeleteOptions(uid string) *DeleteOptions {
  180. u := types.UID(uid)
  181. p := Preconditions{UID: &u}
  182. return &DeleteOptions{Preconditions: &p}
  183. }
  184. // NewUIDPreconditions returns a Preconditions with UID set.
  185. func NewUIDPreconditions(uid string) *Preconditions {
  186. u := types.UID(uid)
  187. return &Preconditions{UID: &u}
  188. }
  189. // this function aims to check if the service's ClusterIP is set or not
  190. // the objective is not to perform validation here
  191. func IsServiceIPSet(service *Service) bool {
  192. return service.Spec.ClusterIP != ClusterIPNone && service.Spec.ClusterIP != ""
  193. }
  194. // this function aims to check if the service's cluster IP is requested or not
  195. func IsServiceIPRequested(service *Service) bool {
  196. // ExternalName services are CNAME aliases to external ones. Ignore the IP.
  197. if service.Spec.Type == ServiceTypeExternalName {
  198. return false
  199. }
  200. return service.Spec.ClusterIP == ""
  201. }
  202. var standardFinalizers = sets.NewString(
  203. string(FinalizerKubernetes),
  204. FinalizerOrphan,
  205. )
  206. func IsStandardFinalizerName(str string) bool {
  207. return standardFinalizers.Has(str)
  208. }
  209. // SingleObject returns a ListOptions for watching a single object.
  210. func SingleObject(meta ObjectMeta) ListOptions {
  211. return ListOptions{
  212. FieldSelector: fields.OneTermEqualSelector("metadata.name", meta.Name),
  213. ResourceVersion: meta.ResourceVersion,
  214. }
  215. }
  216. // AddToNodeAddresses appends the NodeAddresses to the passed-by-pointer slice,
  217. // only if they do not already exist
  218. func AddToNodeAddresses(addresses *[]NodeAddress, addAddresses ...NodeAddress) {
  219. for _, add := range addAddresses {
  220. exists := false
  221. for _, existing := range *addresses {
  222. if existing.Address == add.Address && existing.Type == add.Type {
  223. exists = true
  224. break
  225. }
  226. }
  227. if !exists {
  228. *addresses = append(*addresses, add)
  229. }
  230. }
  231. }
  232. func HashObject(obj runtime.Object, codec runtime.Codec) (string, error) {
  233. data, err := runtime.Encode(codec, obj)
  234. if err != nil {
  235. return "", err
  236. }
  237. return fmt.Sprintf("%x", md5.Sum(data)), nil
  238. }
  239. // TODO: make method on LoadBalancerStatus?
  240. func LoadBalancerStatusEqual(l, r *LoadBalancerStatus) bool {
  241. return ingressSliceEqual(l.Ingress, r.Ingress)
  242. }
  243. func ingressSliceEqual(lhs, rhs []LoadBalancerIngress) bool {
  244. if len(lhs) != len(rhs) {
  245. return false
  246. }
  247. for i := range lhs {
  248. if !ingressEqual(&lhs[i], &rhs[i]) {
  249. return false
  250. }
  251. }
  252. return true
  253. }
  254. func ingressEqual(lhs, rhs *LoadBalancerIngress) bool {
  255. if lhs.IP != rhs.IP {
  256. return false
  257. }
  258. if lhs.Hostname != rhs.Hostname {
  259. return false
  260. }
  261. return true
  262. }
  263. // TODO: make method on LoadBalancerStatus?
  264. func LoadBalancerStatusDeepCopy(lb *LoadBalancerStatus) *LoadBalancerStatus {
  265. c := &LoadBalancerStatus{}
  266. c.Ingress = make([]LoadBalancerIngress, len(lb.Ingress))
  267. for i := range lb.Ingress {
  268. c.Ingress[i] = lb.Ingress[i]
  269. }
  270. return c
  271. }
  272. // GetAccessModesAsString returns a string representation of an array of access modes.
  273. // modes, when present, are always in the same order: RWO,ROX,RWX.
  274. func GetAccessModesAsString(modes []PersistentVolumeAccessMode) string {
  275. modes = removeDuplicateAccessModes(modes)
  276. modesStr := []string{}
  277. if containsAccessMode(modes, ReadWriteOnce) {
  278. modesStr = append(modesStr, "RWO")
  279. }
  280. if containsAccessMode(modes, ReadOnlyMany) {
  281. modesStr = append(modesStr, "ROX")
  282. }
  283. if containsAccessMode(modes, ReadWriteMany) {
  284. modesStr = append(modesStr, "RWX")
  285. }
  286. return strings.Join(modesStr, ",")
  287. }
  288. // GetAccessModesAsString returns an array of AccessModes from a string created by GetAccessModesAsString
  289. func GetAccessModesFromString(modes string) []PersistentVolumeAccessMode {
  290. strmodes := strings.Split(modes, ",")
  291. accessModes := []PersistentVolumeAccessMode{}
  292. for _, s := range strmodes {
  293. s = strings.Trim(s, " ")
  294. switch {
  295. case s == "RWO":
  296. accessModes = append(accessModes, ReadWriteOnce)
  297. case s == "ROX":
  298. accessModes = append(accessModes, ReadOnlyMany)
  299. case s == "RWX":
  300. accessModes = append(accessModes, ReadWriteMany)
  301. }
  302. }
  303. return accessModes
  304. }
  305. // removeDuplicateAccessModes returns an array of access modes without any duplicates
  306. func removeDuplicateAccessModes(modes []PersistentVolumeAccessMode) []PersistentVolumeAccessMode {
  307. accessModes := []PersistentVolumeAccessMode{}
  308. for _, m := range modes {
  309. if !containsAccessMode(accessModes, m) {
  310. accessModes = append(accessModes, m)
  311. }
  312. }
  313. return accessModes
  314. }
  315. func containsAccessMode(modes []PersistentVolumeAccessMode, mode PersistentVolumeAccessMode) bool {
  316. for _, m := range modes {
  317. if m == mode {
  318. return true
  319. }
  320. }
  321. return false
  322. }
  323. // ParseRFC3339 parses an RFC3339 date in either RFC3339Nano or RFC3339 format.
  324. func ParseRFC3339(s string, nowFn func() unversioned.Time) (unversioned.Time, error) {
  325. if t, timeErr := time.Parse(time.RFC3339Nano, s); timeErr == nil {
  326. return unversioned.Time{Time: t}, nil
  327. }
  328. t, err := time.Parse(time.RFC3339, s)
  329. if err != nil {
  330. return unversioned.Time{}, err
  331. }
  332. return unversioned.Time{Time: t}, nil
  333. }
  334. // NodeSelectorRequirementsAsSelector converts the []NodeSelectorRequirement api type into a struct that implements
  335. // labels.Selector.
  336. func NodeSelectorRequirementsAsSelector(nsm []NodeSelectorRequirement) (labels.Selector, error) {
  337. if len(nsm) == 0 {
  338. return labels.Nothing(), nil
  339. }
  340. selector := labels.NewSelector()
  341. for _, expr := range nsm {
  342. var op selection.Operator
  343. switch expr.Operator {
  344. case NodeSelectorOpIn:
  345. op = selection.In
  346. case NodeSelectorOpNotIn:
  347. op = selection.NotIn
  348. case NodeSelectorOpExists:
  349. op = selection.Exists
  350. case NodeSelectorOpDoesNotExist:
  351. op = selection.DoesNotExist
  352. case NodeSelectorOpGt:
  353. op = selection.GreaterThan
  354. case NodeSelectorOpLt:
  355. op = selection.LessThan
  356. default:
  357. return nil, fmt.Errorf("%q is not a valid node selector operator", expr.Operator)
  358. }
  359. r, err := labels.NewRequirement(expr.Key, op, sets.NewString(expr.Values...))
  360. if err != nil {
  361. return nil, err
  362. }
  363. selector = selector.Add(*r)
  364. }
  365. return selector, nil
  366. }
  367. const (
  368. // AffinityAnnotationKey represents the key of affinity data (json serialized)
  369. // in the Annotations of a Pod.
  370. AffinityAnnotationKey string = "scheduler.alpha.kubernetes.io/affinity"
  371. // TolerationsAnnotationKey represents the key of tolerations data (json serialized)
  372. // in the Annotations of a Pod.
  373. TolerationsAnnotationKey string = "scheduler.alpha.kubernetes.io/tolerations"
  374. // TaintsAnnotationKey represents the key of taints data (json serialized)
  375. // in the Annotations of a Node.
  376. TaintsAnnotationKey string = "scheduler.alpha.kubernetes.io/taints"
  377. // SeccompPodAnnotationKey represents the key of a seccomp profile applied
  378. // to all containers of a pod.
  379. SeccompPodAnnotationKey string = "seccomp.security.alpha.kubernetes.io/pod"
  380. // SeccompContainerAnnotationKeyPrefix represents the key of a seccomp profile applied
  381. // to one container of a pod.
  382. SeccompContainerAnnotationKeyPrefix string = "container.seccomp.security.alpha.kubernetes.io/"
  383. // CreatedByAnnotation represents the key used to store the spec(json)
  384. // used to create the resource.
  385. CreatedByAnnotation = "kubernetes.io/created-by"
  386. // PreferAvoidPodsAnnotationKey represents the key of preferAvoidPods data (json serialized)
  387. // in the Annotations of a Node.
  388. PreferAvoidPodsAnnotationKey string = "scheduler.alpha.kubernetes.io/preferAvoidPods"
  389. // SysctlsPodAnnotationKey represents the key of sysctls which are set for the infrastructure
  390. // container of a pod. The annotation value is a comma separated list of sysctl_name=value
  391. // key-value pairs. Only a limited set of whitelisted and isolated sysctls is supported by
  392. // the kubelet. Pods with other sysctls will fail to launch.
  393. SysctlsPodAnnotationKey string = "security.alpha.kubernetes.io/sysctls"
  394. // UnsafeSysctlsPodAnnotationKey represents the key of sysctls which are set for the infrastructure
  395. // container of a pod. The annotation value is a comma separated list of sysctl_name=value
  396. // key-value pairs. Unsafe sysctls must be explicitly enabled for a kubelet. They are properly
  397. // namespaced to a pod or a container, but their isolation is usually unclear or weak. Their use
  398. // is at-your-own-risk. Pods that attempt to set an unsafe sysctl that is not enabled for a kubelet
  399. // will fail to launch.
  400. UnsafeSysctlsPodAnnotationKey string = "security.alpha.kubernetes.io/unsafe-sysctls"
  401. )
  402. // GetAffinityFromPod gets the json serialized affinity data from Pod.Annotations
  403. // and converts it to the Affinity type in api.
  404. func GetAffinityFromPodAnnotations(annotations map[string]string) (*Affinity, error) {
  405. if len(annotations) > 0 && annotations[AffinityAnnotationKey] != "" {
  406. var affinity Affinity
  407. err := json.Unmarshal([]byte(annotations[AffinityAnnotationKey]), &affinity)
  408. if err != nil {
  409. return nil, err
  410. }
  411. return &affinity, nil
  412. }
  413. return nil, nil
  414. }
  415. // GetTolerationsFromPodAnnotations gets the json serialized tolerations data from Pod.Annotations
  416. // and converts it to the []Toleration type in api.
  417. func GetTolerationsFromPodAnnotations(annotations map[string]string) ([]Toleration, error) {
  418. var tolerations []Toleration
  419. if len(annotations) > 0 && annotations[TolerationsAnnotationKey] != "" {
  420. err := json.Unmarshal([]byte(annotations[TolerationsAnnotationKey]), &tolerations)
  421. if err != nil {
  422. return tolerations, err
  423. }
  424. }
  425. return tolerations, nil
  426. }
  427. // GetTaintsFromNodeAnnotations gets the json serialized taints data from Pod.Annotations
  428. // and converts it to the []Taint type in api.
  429. func GetTaintsFromNodeAnnotations(annotations map[string]string) ([]Taint, error) {
  430. var taints []Taint
  431. if len(annotations) > 0 && annotations[TaintsAnnotationKey] != "" {
  432. err := json.Unmarshal([]byte(annotations[TaintsAnnotationKey]), &taints)
  433. if err != nil {
  434. return []Taint{}, err
  435. }
  436. }
  437. return taints, nil
  438. }
  439. // TolerationToleratesTaint checks if the toleration tolerates the taint.
  440. func TolerationToleratesTaint(toleration *Toleration, taint *Taint) bool {
  441. if len(toleration.Effect) != 0 && toleration.Effect != taint.Effect {
  442. return false
  443. }
  444. if toleration.Key != taint.Key {
  445. return false
  446. }
  447. // TODO: Use proper defaulting when Toleration becomes a field of PodSpec
  448. if (len(toleration.Operator) == 0 || toleration.Operator == TolerationOpEqual) && toleration.Value == taint.Value {
  449. return true
  450. }
  451. if toleration.Operator == TolerationOpExists {
  452. return true
  453. }
  454. return false
  455. }
  456. // TaintToleratedByTolerations checks if taint is tolerated by any of the tolerations.
  457. func TaintToleratedByTolerations(taint *Taint, tolerations []Toleration) bool {
  458. tolerated := false
  459. for i := range tolerations {
  460. if TolerationToleratesTaint(&tolerations[i], taint) {
  461. tolerated = true
  462. break
  463. }
  464. }
  465. return tolerated
  466. }
  467. func GetAvoidPodsFromNodeAnnotations(annotations map[string]string) (AvoidPods, error) {
  468. var avoidPods AvoidPods
  469. if len(annotations) > 0 && annotations[PreferAvoidPodsAnnotationKey] != "" {
  470. err := json.Unmarshal([]byte(annotations[PreferAvoidPodsAnnotationKey]), &avoidPods)
  471. if err != nil {
  472. return avoidPods, err
  473. }
  474. }
  475. return avoidPods, nil
  476. }
  477. // SysctlsFromPodAnnotations parses the sysctl annotations into a slice of safe Sysctls
  478. // and a slice of unsafe Sysctls. This is only a convenience wrapper around
  479. // SysctlsFromPodAnnotation.
  480. func SysctlsFromPodAnnotations(a map[string]string) ([]Sysctl, []Sysctl, error) {
  481. safe, err := SysctlsFromPodAnnotation(a[SysctlsPodAnnotationKey])
  482. if err != nil {
  483. return nil, nil, err
  484. }
  485. unsafe, err := SysctlsFromPodAnnotation(a[UnsafeSysctlsPodAnnotationKey])
  486. if err != nil {
  487. return nil, nil, err
  488. }
  489. return safe, unsafe, nil
  490. }
  491. // SysctlsFromPodAnnotation parses an annotation value into a slice of Sysctls.
  492. func SysctlsFromPodAnnotation(annotation string) ([]Sysctl, error) {
  493. if len(annotation) == 0 {
  494. return nil, nil
  495. }
  496. kvs := strings.Split(annotation, ",")
  497. sysctls := make([]Sysctl, len(kvs))
  498. for i, kv := range kvs {
  499. cs := strings.Split(kv, "=")
  500. if len(cs) != 2 {
  501. return nil, fmt.Errorf("sysctl %q not of the format sysctl_name=value", kv)
  502. }
  503. sysctls[i].Name = cs[0]
  504. sysctls[i].Value = cs[1]
  505. }
  506. return sysctls, nil
  507. }
  508. // PodAnnotationsFromSysctls creates an annotation value for a slice of Sysctls.
  509. func PodAnnotationsFromSysctls(sysctls []Sysctl) string {
  510. if len(sysctls) == 0 {
  511. return ""
  512. }
  513. kvs := make([]string, len(sysctls))
  514. for i := range sysctls {
  515. kvs[i] = fmt.Sprintf("%s=%s", sysctls[i].Name, sysctls[i].Value)
  516. }
  517. return strings.Join(kvs, ",")
  518. }