2014-06-20 01:03:48 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2014-06-20 01:03:48 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-09-09 14:18:17 +00:00
|
|
|
package node
|
2014-06-20 01:03:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-10-17 21:21:53 +00:00
|
|
|
"net"
|
2015-03-23 18:42:39 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2014-06-20 01:03:48 +00:00
|
|
|
|
2017-01-13 17:48:50 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-19 14:50:16 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
utilnet "k8s.io/apimachinery/pkg/util/net"
|
|
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
2017-01-17 10:38:25 +00:00
|
|
|
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
2017-02-02 09:25:56 +00:00
|
|
|
"k8s.io/apiserver/pkg/registry/generic"
|
2017-01-31 16:47:19 +00:00
|
|
|
pkgstorage "k8s.io/apiserver/pkg/storage"
|
2017-01-13 19:56:52 +00:00
|
|
|
"k8s.io/apiserver/pkg/storage/names"
|
2017-08-18 20:18:53 +00:00
|
|
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/validation"
|
2017-08-18 20:18:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/features"
|
2015-10-27 13:18:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/client"
|
2014-06-20 01:03:48 +00:00
|
|
|
)
|
|
|
|
|
2015-03-13 14:49:38 +00:00
|
|
|
// nodeStrategy implements behavior for nodes
|
|
|
|
type nodeStrategy struct {
|
|
|
|
runtime.ObjectTyper
|
2017-01-13 19:56:52 +00:00
|
|
|
names.NameGenerator
|
2014-06-20 01:03:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 14:49:38 +00:00
|
|
|
// Nodes is the default logic that applies when creating and updating Node
|
|
|
|
// objects.
|
2017-01-13 19:56:52 +00:00
|
|
|
var Strategy = nodeStrategy{api.Scheme, names.SimpleNameGenerator}
|
2014-08-12 19:14:00 +00:00
|
|
|
|
2015-03-13 14:49:38 +00:00
|
|
|
// NamespaceScoped is false for nodes.
|
|
|
|
func (nodeStrategy) NamespaceScoped() bool {
|
|
|
|
return false
|
2014-06-20 01:03:48 +00:00
|
|
|
}
|
2014-08-11 07:34:59 +00:00
|
|
|
|
2015-03-13 14:49:38 +00:00
|
|
|
// AllowCreateOnUpdate is false for nodes.
|
|
|
|
func (nodeStrategy) AllowCreateOnUpdate() bool {
|
|
|
|
return false
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 21:45:07 +00:00
|
|
|
// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
|
2017-01-02 14:07:36 +00:00
|
|
|
func (nodeStrategy) PrepareForCreate(ctx genericapirequest.Context, obj runtime.Object) {
|
2017-08-18 20:18:53 +00:00
|
|
|
node := obj.(*api.Node)
|
2015-04-08 09:32:47 +00:00
|
|
|
// Nodes allow *all* fields, including status, to be set on create.
|
2017-08-18 20:18:53 +00:00
|
|
|
|
|
|
|
if !utilfeature.DefaultFeatureGate.Enabled(features.DynamicKubeletConfig) {
|
|
|
|
node.Spec.ConfigSource = nil
|
|
|
|
}
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 00:03:30 +00:00
|
|
|
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
|
2017-01-02 14:07:36 +00:00
|
|
|
func (nodeStrategy) PrepareForUpdate(ctx genericapirequest.Context, obj, old runtime.Object) {
|
2015-04-08 09:32:47 +00:00
|
|
|
newNode := obj.(*api.Node)
|
|
|
|
oldNode := old.(*api.Node)
|
|
|
|
newNode.Status = oldNode.Status
|
2017-08-18 20:18:53 +00:00
|
|
|
|
|
|
|
if !utilfeature.DefaultFeatureGate.Enabled(features.DynamicKubeletConfig) {
|
|
|
|
newNode.Spec.ConfigSource = nil
|
|
|
|
oldNode.Spec.ConfigSource = nil
|
|
|
|
}
|
2015-03-26 00:03:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 14:49:38 +00:00
|
|
|
// Validate validates a new node.
|
2017-01-02 14:07:36 +00:00
|
|
|
func (nodeStrategy) Validate(ctx genericapirequest.Context, obj runtime.Object) field.ErrorList {
|
2015-03-13 14:49:38 +00:00
|
|
|
node := obj.(*api.Node)
|
2015-04-22 17:55:05 +00:00
|
|
|
return validation.ValidateNode(node)
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 05:13:16 +00:00
|
|
|
// Canonicalize normalizes the object after validation.
|
|
|
|
func (nodeStrategy) Canonicalize(obj runtime.Object) {
|
|
|
|
}
|
|
|
|
|
2015-03-13 14:49:38 +00:00
|
|
|
// ValidateUpdate is the default update validation for an end user.
|
2017-01-02 14:07:36 +00:00
|
|
|
func (nodeStrategy) ValidateUpdate(ctx genericapirequest.Context, obj, old runtime.Object) field.ErrorList {
|
2015-04-22 17:55:05 +00:00
|
|
|
errorList := validation.ValidateNode(obj.(*api.Node))
|
2015-11-04 07:47:11 +00:00
|
|
|
return append(errorList, validation.ValidateNodeUpdate(obj.(*api.Node), old.(*api.Node))...)
|
2015-01-12 05:33:25 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 00:42:01 +00:00
|
|
|
func (nodeStrategy) AllowUnconditionalUpdate() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (ns nodeStrategy) Export(ctx genericapirequest.Context, obj runtime.Object, exact bool) error {
|
2015-12-01 23:45:29 +00:00
|
|
|
n, ok := obj.(*api.Node)
|
|
|
|
if !ok {
|
|
|
|
// unexpected programmer error
|
|
|
|
return fmt.Errorf("unexpected object: %v", obj)
|
|
|
|
}
|
2016-08-08 20:15:33 +00:00
|
|
|
ns.PrepareForCreate(ctx, obj)
|
2015-12-01 23:45:29 +00:00
|
|
|
if exact {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Nodes are the only resources that allow direct status edits, therefore
|
|
|
|
// we clear that without exact so that the node value can be reused.
|
|
|
|
n.Status = api.NodeStatus{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-08 09:32:47 +00:00
|
|
|
type nodeStatusStrategy struct {
|
|
|
|
nodeStrategy
|
|
|
|
}
|
|
|
|
|
|
|
|
var StatusStrategy = nodeStatusStrategy{Strategy}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (nodeStatusStrategy) PrepareForCreate(ctx genericapirequest.Context, obj runtime.Object) {
|
2015-04-08 09:32:47 +00:00
|
|
|
_ = obj.(*api.Node)
|
|
|
|
// Nodes allow *all* fields, including status, to be set on create.
|
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (nodeStatusStrategy) PrepareForUpdate(ctx genericapirequest.Context, obj, old runtime.Object) {
|
2015-04-08 09:32:47 +00:00
|
|
|
newNode := obj.(*api.Node)
|
|
|
|
oldNode := old.(*api.Node)
|
|
|
|
newNode.Spec = oldNode.Spec
|
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
func (nodeStatusStrategy) ValidateUpdate(ctx genericapirequest.Context, obj, old runtime.Object) field.ErrorList {
|
2015-11-04 07:47:11 +00:00
|
|
|
return validation.ValidateNodeUpdate(obj.(*api.Node), old.(*api.Node))
|
2015-04-08 09:32:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 05:13:16 +00:00
|
|
|
// Canonicalize normalizes the object after validation.
|
|
|
|
func (nodeStatusStrategy) Canonicalize(obj runtime.Object) {
|
|
|
|
}
|
|
|
|
|
2015-03-13 14:49:38 +00:00
|
|
|
// ResourceGetter is an interface for retrieving resources by ResourceLocation.
|
|
|
|
type ResourceGetter interface {
|
2017-01-02 14:07:36 +00:00
|
|
|
Get(genericapirequest.Context, string, *metav1.GetOptions) (runtime.Object, error)
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 08:19:20 +00:00
|
|
|
// NodeToSelectableFields returns a field set that represents the object.
|
2015-04-02 18:11:00 +00:00
|
|
|
func NodeToSelectableFields(node *api.Node) fields.Set {
|
2016-08-23 14:34:02 +00:00
|
|
|
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&node.ObjectMeta, false)
|
2015-10-31 02:48:24 +00:00
|
|
|
specificFieldsSet := fields.Set{
|
2015-04-15 12:01:36 +00:00
|
|
|
"spec.unschedulable": fmt.Sprint(node.Spec.Unschedulable),
|
2015-04-02 08:57:28 +00:00
|
|
|
}
|
2015-10-31 02:48:24 +00:00
|
|
|
return generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
|
2015-04-02 08:57:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:19:55 +00:00
|
|
|
// GetAttrs returns labels and fields of a given object for filtering purposes.
|
2017-05-26 22:43:42 +00:00
|
|
|
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, bool, error) {
|
2016-11-16 09:19:55 +00:00
|
|
|
nodeObj, ok := obj.(*api.Node)
|
|
|
|
if !ok {
|
2017-05-26 22:43:42 +00:00
|
|
|
return nil, nil, false, fmt.Errorf("not a node")
|
2016-11-16 09:19:55 +00:00
|
|
|
}
|
2017-05-26 22:43:42 +00:00
|
|
|
return labels.Set(nodeObj.ObjectMeta.Labels), NodeToSelectableFields(nodeObj), nodeObj.Initializers != nil, nil
|
2016-11-16 09:19:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 14:49:38 +00:00
|
|
|
// MatchNode returns a generic matcher for a given label and field selector.
|
2016-08-23 03:41:21 +00:00
|
|
|
func MatchNode(label labels.Selector, field fields.Selector) pkgstorage.SelectionPredicate {
|
|
|
|
return pkgstorage.SelectionPredicate{
|
2016-11-16 09:19:55 +00:00
|
|
|
Label: label,
|
|
|
|
Field: field,
|
|
|
|
GetAttrs: GetAttrs,
|
2016-06-03 13:23:26 +00:00
|
|
|
IndexFields: []string{"metadata.name"},
|
2015-04-02 18:11:00 +00:00
|
|
|
}
|
2014-08-11 07:34:59 +00:00
|
|
|
}
|
2014-10-17 21:21:53 +00:00
|
|
|
|
2016-06-03 13:23:26 +00:00
|
|
|
func NodeNameTriggerFunc(obj runtime.Object) []pkgstorage.MatchValue {
|
|
|
|
node := obj.(*api.Node)
|
|
|
|
result := pkgstorage.MatchValue{IndexName: "metadata.name", Value: node.ObjectMeta.Name}
|
|
|
|
return []pkgstorage.MatchValue{result}
|
|
|
|
}
|
|
|
|
|
2015-06-04 05:13:59 +00:00
|
|
|
// ResourceLocation returns an URL and transport which one can use to send traffic for the specified node.
|
2017-01-02 14:07:36 +00:00
|
|
|
func ResourceLocation(getter ResourceGetter, connection client.ConnectionInfoGetter, proxyTransport http.RoundTripper, ctx genericapirequest.Context, id string) (*url.URL, http.RoundTripper, error) {
|
2016-01-06 15:56:41 +00:00
|
|
|
schemeReq, name, portReq, valid := utilnet.SplitSchemeNamePort(id)
|
2015-04-20 21:31:44 +00:00
|
|
|
if !valid {
|
|
|
|
return nil, nil, errors.NewBadRequest(fmt.Sprintf("invalid node request %q", id))
|
|
|
|
}
|
|
|
|
|
2017-01-02 14:07:36 +00:00
|
|
|
info, err := connection.GetConnectionInfo(types.NodeName(name))
|
2014-10-17 21:21:53 +00:00
|
|
|
if err != nil {
|
2015-03-23 18:42:39 +00:00
|
|
|
return nil, nil, err
|
2014-10-17 21:21:53 +00:00
|
|
|
}
|
2015-04-20 21:31:44 +00:00
|
|
|
|
2015-10-27 13:18:45 +00:00
|
|
|
// We check if we want to get a default Kubelet's transport. It happens if either:
|
2016-10-07 19:30:45 +00:00
|
|
|
// - no port is specified in request (Kubelet's port is default)
|
|
|
|
// - the requested port matches the kubelet port for this node
|
|
|
|
if portReq == "" || portReq == info.Port {
|
2015-06-05 14:33:34 +00:00
|
|
|
return &url.URL{
|
2016-10-07 19:30:45 +00:00
|
|
|
Scheme: info.Scheme,
|
|
|
|
Host: net.JoinHostPort(info.Hostname, info.Port),
|
2015-06-05 14:33:34 +00:00
|
|
|
},
|
2016-10-07 19:30:45 +00:00
|
|
|
info.Transport,
|
2015-06-05 14:33:34 +00:00
|
|
|
nil
|
2015-06-04 05:13:59 +00:00
|
|
|
}
|
2016-10-07 19:30:45 +00:00
|
|
|
|
|
|
|
// Otherwise, return the requested scheme and port, and the proxy transport
|
|
|
|
return &url.URL{Scheme: schemeReq, Host: net.JoinHostPort(info.Hostname, portReq)}, proxyTransport, nil
|
2014-10-17 21:21:53 +00:00
|
|
|
}
|