You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright 2021 The KubeEdge 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 globalmanager
  14. import (
  15. "context"
  16. "fmt"
  17. "math"
  18. "strings"
  19. "time"
  20. v1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/client-go/kubernetes"
  23. "k8s.io/client-go/util/workqueue"
  24. )
  25. const (
  26. // DefaultBackOff is the default backoff period
  27. DefaultBackOff = 10 * time.Second
  28. // MaxBackOff is the max backoff period
  29. MaxBackOff = 360 * time.Second
  30. statusUpdateRetries = 3
  31. bigModelPort int32 = 5000
  32. )
  33. // GetNodeIPByName get node ip by node name
  34. func GetNodeIPByName(kubeClient kubernetes.Interface, name string) (string, error) {
  35. n, err := kubeClient.CoreV1().Nodes().Get(context.Background(), name, metav1.GetOptions{})
  36. if err != nil {
  37. return "", err
  38. }
  39. typeToAddress := make(map[v1.NodeAddressType]string)
  40. for _, addr := range n.Status.Addresses {
  41. typeToAddress[addr.Type] = addr.Address
  42. }
  43. address, found := typeToAddress[v1.NodeExternalIP]
  44. if found {
  45. return address, nil
  46. }
  47. address, found = typeToAddress[v1.NodeInternalIP]
  48. if found {
  49. return address, nil
  50. }
  51. return "", fmt.Errorf("can't found node ip for node %s", name)
  52. }
  53. // getBackoff calc the next wait time for the key
  54. func getBackoff(queue workqueue.RateLimitingInterface, key interface{}) time.Duration {
  55. exp := queue.NumRequeues(key)
  56. if exp <= 0 {
  57. return time.Duration(0)
  58. }
  59. // The backoff is capped such that 'calculated' value never overflows.
  60. backoff := float64(DefaultBackOff.Nanoseconds()) * math.Pow(2, float64(exp-1))
  61. if backoff > math.MaxInt64 {
  62. return MaxBackOff
  63. }
  64. calculated := time.Duration(backoff)
  65. if calculated > MaxBackOff {
  66. return MaxBackOff
  67. }
  68. return calculated
  69. }
  70. func calcActivePodCount(pods []*v1.Pod) int32 {
  71. var result int32 = 0
  72. for _, p := range pods {
  73. if v1.PodSucceeded != p.Status.Phase &&
  74. v1.PodFailed != p.Status.Phase &&
  75. p.DeletionTimestamp == nil {
  76. result++
  77. }
  78. }
  79. return result
  80. }
  81. // ConvertK8SValidName converts to the k8s valid name
  82. func ConvertK8SValidName(name string) string {
  83. // the name(e.g. pod/volume name) should be a lowercase RFC 1123 label:
  84. // [a-z0-9]([-a-z0-9]*[a-z0-9])?
  85. // and no more than 63 characters
  86. limitCount := 63
  87. var fixName []byte
  88. for _, c := range []byte(strings.ToLower(name)) {
  89. if ('a' <= c && c <= 'z') ||
  90. ('0' <= c && c <= '9') ||
  91. c == '-' {
  92. fixName = append(fixName, c)
  93. continue
  94. }
  95. // the first char not '-'
  96. // and no two consecutive '-'
  97. if len(fixName) > 0 && fixName[len(fixName)-1] != '-' {
  98. fixName = append(fixName, '-')
  99. }
  100. }
  101. // fix limitCount
  102. if len(fixName) > limitCount {
  103. fixName = fixName[:limitCount]
  104. }
  105. // fix the end character
  106. if len(fixName) > 0 && fixName[len(fixName)-1] == '-' {
  107. fixName[len(fixName)-1] = 'z'
  108. }
  109. return string(fixName)
  110. }