strategy.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 node
  14. import (
  15. "fmt"
  16. "net"
  17. "net/http"
  18. "net/url"
  19. "strconv"
  20. "k8s.io/kubernetes/pkg/api"
  21. "k8s.io/kubernetes/pkg/api/errors"
  22. "k8s.io/kubernetes/pkg/api/validation"
  23. "k8s.io/kubernetes/pkg/fields"
  24. "k8s.io/kubernetes/pkg/kubelet/client"
  25. "k8s.io/kubernetes/pkg/labels"
  26. "k8s.io/kubernetes/pkg/master/ports"
  27. "k8s.io/kubernetes/pkg/registry/generic"
  28. "k8s.io/kubernetes/pkg/runtime"
  29. pkgstorage "k8s.io/kubernetes/pkg/storage"
  30. utilnet "k8s.io/kubernetes/pkg/util/net"
  31. nodeutil "k8s.io/kubernetes/pkg/util/node"
  32. "k8s.io/kubernetes/pkg/util/validation/field"
  33. )
  34. // nodeStrategy implements behavior for nodes
  35. type nodeStrategy struct {
  36. runtime.ObjectTyper
  37. api.NameGenerator
  38. }
  39. // Nodes is the default logic that applies when creating and updating Node
  40. // objects.
  41. var Strategy = nodeStrategy{api.Scheme, api.SimpleNameGenerator}
  42. // NamespaceScoped is false for nodes.
  43. func (nodeStrategy) NamespaceScoped() bool {
  44. return false
  45. }
  46. // AllowCreateOnUpdate is false for nodes.
  47. func (nodeStrategy) AllowCreateOnUpdate() bool {
  48. return false
  49. }
  50. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  51. func (nodeStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
  52. _ = obj.(*api.Node)
  53. // Nodes allow *all* fields, including status, to be set on create.
  54. }
  55. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  56. func (nodeStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
  57. newNode := obj.(*api.Node)
  58. oldNode := old.(*api.Node)
  59. newNode.Status = oldNode.Status
  60. }
  61. // Validate validates a new node.
  62. func (nodeStrategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
  63. node := obj.(*api.Node)
  64. return validation.ValidateNode(node)
  65. }
  66. // Canonicalize normalizes the object after validation.
  67. func (nodeStrategy) Canonicalize(obj runtime.Object) {
  68. }
  69. // ValidateUpdate is the default update validation for an end user.
  70. func (nodeStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  71. errorList := validation.ValidateNode(obj.(*api.Node))
  72. return append(errorList, validation.ValidateNodeUpdate(obj.(*api.Node), old.(*api.Node))...)
  73. }
  74. func (nodeStrategy) AllowUnconditionalUpdate() bool {
  75. return true
  76. }
  77. func (ns nodeStrategy) Export(ctx api.Context, obj runtime.Object, exact bool) error {
  78. n, ok := obj.(*api.Node)
  79. if !ok {
  80. // unexpected programmer error
  81. return fmt.Errorf("unexpected object: %v", obj)
  82. }
  83. ns.PrepareForCreate(ctx, obj)
  84. if exact {
  85. return nil
  86. }
  87. // Nodes are the only resources that allow direct status edits, therefore
  88. // we clear that without exact so that the node value can be reused.
  89. n.Status = api.NodeStatus{}
  90. return nil
  91. }
  92. type nodeStatusStrategy struct {
  93. nodeStrategy
  94. }
  95. var StatusStrategy = nodeStatusStrategy{Strategy}
  96. func (nodeStatusStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
  97. _ = obj.(*api.Node)
  98. // Nodes allow *all* fields, including status, to be set on create.
  99. }
  100. func (nodeStatusStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
  101. newNode := obj.(*api.Node)
  102. oldNode := old.(*api.Node)
  103. newNode.Spec = oldNode.Spec
  104. }
  105. func (nodeStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  106. return validation.ValidateNodeUpdate(obj.(*api.Node), old.(*api.Node))
  107. }
  108. // Canonicalize normalizes the object after validation.
  109. func (nodeStatusStrategy) Canonicalize(obj runtime.Object) {
  110. }
  111. // ResourceGetter is an interface for retrieving resources by ResourceLocation.
  112. type ResourceGetter interface {
  113. Get(api.Context, string) (runtime.Object, error)
  114. }
  115. // NodeToSelectableFields returns a field set that represents the object.
  116. func NodeToSelectableFields(node *api.Node) fields.Set {
  117. objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&node.ObjectMeta, false)
  118. specificFieldsSet := fields.Set{
  119. "spec.unschedulable": fmt.Sprint(node.Spec.Unschedulable),
  120. }
  121. return generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
  122. }
  123. // MatchNode returns a generic matcher for a given label and field selector.
  124. func MatchNode(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
  125. return &generic.SelectionPredicate{
  126. Label: label,
  127. Field: field,
  128. GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
  129. nodeObj, ok := obj.(*api.Node)
  130. if !ok {
  131. return nil, nil, fmt.Errorf("not a node")
  132. }
  133. return labels.Set(nodeObj.ObjectMeta.Labels), NodeToSelectableFields(nodeObj), nil
  134. },
  135. IndexFields: []string{"metadata.name"},
  136. }
  137. }
  138. func NodeNameTriggerFunc(obj runtime.Object) []pkgstorage.MatchValue {
  139. node := obj.(*api.Node)
  140. result := pkgstorage.MatchValue{IndexName: "metadata.name", Value: node.ObjectMeta.Name}
  141. return []pkgstorage.MatchValue{result}
  142. }
  143. // ResourceLocation returns an URL and transport which one can use to send traffic for the specified node.
  144. func ResourceLocation(getter ResourceGetter, connection client.ConnectionInfoGetter, proxyTransport http.RoundTripper, ctx api.Context, id string) (*url.URL, http.RoundTripper, error) {
  145. schemeReq, name, portReq, valid := utilnet.SplitSchemeNamePort(id)
  146. if !valid {
  147. return nil, nil, errors.NewBadRequest(fmt.Sprintf("invalid node request %q", id))
  148. }
  149. nodeObj, err := getter.Get(ctx, name)
  150. if err != nil {
  151. return nil, nil, err
  152. }
  153. node := nodeObj.(*api.Node)
  154. hostIP, err := nodeutil.GetNodeHostIP(node)
  155. if err != nil {
  156. return nil, nil, err
  157. }
  158. host := hostIP.String()
  159. // We check if we want to get a default Kubelet's transport. It happens if either:
  160. // - no port is specified in request (Kubelet's port is default),
  161. // - we're using Port stored as a DaemonEndpoint and requested port is a Kubelet's port stored in the DaemonEndpoint,
  162. // - there's no information in the API about DaemonEnpoint (legacy cluster) and requested port is equal to ports.KubeletPort (cluster-wide config)
  163. kubeletPort := node.Status.DaemonEndpoints.KubeletEndpoint.Port
  164. if kubeletPort == 0 {
  165. kubeletPort = ports.KubeletPort
  166. }
  167. if portReq == "" || strconv.Itoa(int(kubeletPort)) == portReq {
  168. scheme, port, kubeletTransport, err := connection.GetConnectionInfo(ctx, node.Name)
  169. if err != nil {
  170. return nil, nil, err
  171. }
  172. return &url.URL{
  173. Scheme: scheme,
  174. Host: net.JoinHostPort(
  175. host,
  176. strconv.FormatUint(uint64(port), 10),
  177. ),
  178. },
  179. kubeletTransport,
  180. nil
  181. }
  182. return &url.URL{Scheme: schemeReq, Host: net.JoinHostPort(host, portReq)}, proxyTransport, nil
  183. }