node.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 node
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "net"
  18. "os"
  19. "strings"
  20. "time"
  21. "github.com/golang/glog"
  22. "k8s.io/kubernetes/pkg/api"
  23. "k8s.io/kubernetes/pkg/api/unversioned"
  24. clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
  25. )
  26. func GetHostname(hostnameOverride string) string {
  27. var hostname string = hostnameOverride
  28. if hostname == "" {
  29. nodename, err := os.Hostname()
  30. if err != nil {
  31. glog.Fatalf("Couldn't determine hostname: %v", err)
  32. }
  33. hostname = nodename
  34. }
  35. return strings.ToLower(strings.TrimSpace(hostname))
  36. }
  37. // GetNodeHostIP returns the provided node's IP, based on the priority:
  38. // 1. NodeInternalIP
  39. // 2. NodeExternalIP
  40. // 3. NodeLegacyHostIP
  41. func GetNodeHostIP(node *api.Node) (net.IP, error) {
  42. addresses := node.Status.Addresses
  43. addressMap := make(map[api.NodeAddressType][]api.NodeAddress)
  44. for i := range addresses {
  45. addressMap[addresses[i].Type] = append(addressMap[addresses[i].Type], addresses[i])
  46. }
  47. if addresses, ok := addressMap[api.NodeInternalIP]; ok {
  48. return net.ParseIP(addresses[0].Address), nil
  49. }
  50. if addresses, ok := addressMap[api.NodeExternalIP]; ok {
  51. return net.ParseIP(addresses[0].Address), nil
  52. }
  53. if addresses, ok := addressMap[api.NodeLegacyHostIP]; ok {
  54. return net.ParseIP(addresses[0].Address), nil
  55. }
  56. return nil, fmt.Errorf("host IP unknown; known addresses: %v", addresses)
  57. }
  58. // Helper function that builds a string identifier that is unique per failure-zone
  59. // Returns empty-string for no zone
  60. func GetZoneKey(node *api.Node) string {
  61. labels := node.Labels
  62. if labels == nil {
  63. return ""
  64. }
  65. region, _ := labels[unversioned.LabelZoneRegion]
  66. failureDomain, _ := labels[unversioned.LabelZoneFailureDomain]
  67. if region == "" && failureDomain == "" {
  68. return ""
  69. }
  70. // We include the null character just in case region or failureDomain has a colon
  71. // (We do assume there's no null characters in a region or failureDomain)
  72. // As a nice side-benefit, the null character is not printed by fmt.Print or glog
  73. return region + ":\x00:" + failureDomain
  74. }
  75. // SetNodeCondition updates specific node condition with patch operation.
  76. func SetNodeCondition(c clientset.Interface, node string, condition api.NodeCondition) error {
  77. generatePatch := func(condition api.NodeCondition) ([]byte, error) {
  78. raw, err := json.Marshal(&[]api.NodeCondition{condition})
  79. if err != nil {
  80. return nil, err
  81. }
  82. return []byte(fmt.Sprintf(`{"status":{"conditions":%s}}`, raw)), nil
  83. }
  84. condition.LastHeartbeatTime = unversioned.NewTime(time.Now())
  85. patch, err := generatePatch(condition)
  86. if err != nil {
  87. return nil
  88. }
  89. _, err = c.Core().Nodes().PatchStatus(node, patch)
  90. return err
  91. }