Created
June 27, 2018 21:30
-
-
Save dshcherb/1ba9b9cb62f8753de210bc459115c07a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// kubernetes tree: c005b9d0ab6cd963abf66a9a12fb8ad5e48121ad | |
enforcedNodeAddresses = append(enforcedNodeAddresses, v1.NodeAddress{Type: v1.NodeHostName, Address: kl.GetHostname()}) | |
node.Status.Addresses = enforcedNodeAddresses | |
return nil | |
} | |
// Only add a NodeHostName address if the cloudprovider did not specify one | |
// (we assume the cloudprovider knows best) | |
var addressNodeHostName *v1.NodeAddress | |
for i := range nodeAddresses { | |
if nodeAddresses[i].Type == v1.NodeHostName { | |
addressNodeHostName = &nodeAddresses[i] | |
break | |
} | |
} | |
func (kl *Kubelet) tryRegisterWithAPIServer(node *v1.Node) bool { | |
_, err := kl.kubeClient.CoreV1().Nodes().Create(node) | |
pkg/client/clientset_generated/internalclientset/typed/core/internalversion/node.go | |
// Create takes the representation of a node and creates it. Returns the server's representation of the node, and an error, if there is any. | |
func (c *nodes) Create(node *core.Node) (result *core.Node, err error) { | |
result = &core.Node{} | |
err = c.client.Post(). | |
Resource("nodes"). | |
Body(node). | |
Do(). | |
Into(result) | |
return | |
} | |
pkg/apis/core/validation/validation.go | |
// NOTE: nameFn -> ValidateNodeName | |
// ValidateNodeName can be used to check whether the given node name is valid. | |
// Prefix indicates this name will be used as part of generation, in which case | |
// trailing dashes are allowed. | |
var ValidateNodeName = NameIsDNSSubdomain | |
// NOTE: see NameIsDNSSubdomain below | |
// ValidateNode tests if required fields in the node are set. | |
func ValidateNode(node *core.Node) field.ErrorList { | |
fldPath := field.NewPath("metadata") | |
allErrs := ValidateObjectMeta(&node.ObjectMeta, false, ValidateNodeName, fldPath) | |
allErrs = append(allErrs, ValidateNodeSpecificAnnotations(node.ObjectMeta.Annotations, fldPath.Child("annotations"))...) | |
if len(node.Spec.Taints) > 0 { | |
allErrs = append(allErrs, validateNodeTaints(node.Spec.Taints, fldPath.Child("taints"))...) | |
} | |
// ValidateObjectMeta validates an object's metadata on creation. It expects that name generation has already | |
// been performed. | |
// It doesn't return an error for rootscoped resources with namespace, because namespace should already be cleared before. | |
// TODO: Remove calls to this method scattered in validations of specific resources, e.g., ValidatePodUpdate. | |
func ValidateObjectMeta(meta *metav1.ObjectMeta, requiresNamespace bool, nameFn ValidateNameFunc, fldPath *field.Path) field.ErrorList { | |
allErrs := apimachineryvalidation.ValidateObjectMeta(meta, requiresNamespace, apimachineryvalidation.ValidateNameFunc(nameFn), fldPath) | |
// run additional checks for the finalizer name | |
for i := range meta.Finalizers { | |
allErrs = append(allErrs, validateKubeFinalizerName(string(meta.Finalizers[i]), fldPath.Child("finalizers").Index(i))...) | |
} | |
return allErrs | |
} | |
// ValidateObjectMeta validates an object's metadata on creation. It expects that name generation has already | |
// been performed. | |
// It doesn't return an error for rootscoped resources with namespace, because namespace should already be cleared before. | |
func ValidateObjectMeta(objMeta *metav1.ObjectMeta, requiresNamespace bool, nameFn ValidateNameFunc, fldPath *field.Path) field.ErrorList { | |
metadata, err := meta.Accessor(objMeta) | |
if err != nil { | |
allErrs := field.ErrorList{} | |
allErrs = append(allErrs, field.Invalid(fldPath, objMeta, err.Error())) | |
return allErrs | |
} | |
return ValidateObjectMetaAccessor(metadata, requiresNamespace, nameFn, fldPath) | |
} | |
// ValidateObjectMeta validates an object's metadata on creation. It expects that name generation has already | |
// been performed. | |
// It doesn't return an error for rootscoped resources with namespace, because namespace should already be cleared before. | |
func ValidateObjectMetaAccessor(meta metav1.Object, requiresNamespace bool, nameFn ValidateNameFunc, fldPath *field.Path) field.ErrorList { | |
allErrs := field.ErrorList{} | |
if len(meta.GetGenerateName()) != 0 { | |
for _, msg := range nameFn(meta.GetGenerateName(), true) { | |
allErrs = append(allErrs, field.Invalid(fldPath.Child("generateName"), meta.GetGenerateName(), msg)) | |
} | |
} | |
// If the generated name validates, but the calculated value does not, it's a problem with generation, and we | |
// report it here. This may confuse users, but indicates a programming bug and still must be validated. | |
// If there are multiple fields out of which one is required then add an or as a separator | |
if len(meta.GetName()) == 0 { | |
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "name or generateName is required")) | |
} else { | |
for _, msg := range nameFn(meta.GetName(), false) { | |
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), meta.GetName(), msg)) | |
} | |
} | |
if requiresNamespace { | |
if len(meta.GetNamespace()) == 0 { | |
allErrs = append(allErrs, field.Required(fldPath.Child("namespace"), "")) | |
} else { | |
for _, msg := range ValidateNamespaceName(meta.GetNamespace(), false) { | |
allErrs = append(allErrs, field.Invalid(fldPath.Child("namespace"), meta.GetNamespace(), msg)) | |
} | |
} | |
} else { | |
if len(meta.GetNamespace()) != 0 { | |
allErrs = append(allErrs, field.Forbidden(fldPath.Child("namespace"), "not allowed on this type")) | |
} | |
} | |
if len(meta.GetClusterName()) != 0 { | |
for _, msg := range ValidateClusterName(meta.GetClusterName(), false) { | |
allErrs = append(allErrs, field.Invalid(fldPath.Child("clusterName"), meta.GetClusterName(), msg)) | |
} | |
} | |
allErrs = append(allErrs, ValidateNonnegativeField(meta.GetGeneration(), fldPath.Child("generation"))...) | |
allErrs = append(allErrs, v1validation.ValidateLabels(meta.GetLabels(), fldPath.Child("labels"))...) | |
allErrs = append(allErrs, ValidateAnnotations(meta.GetAnnotations(), fldPath.Child("annotations"))...) | |
allErrs = append(allErrs, ValidateOwnerReferences(meta.GetOwnerReferences(), fldPath.Child("ownerReferences"))...) | |
allErrs = append(allErrs, ValidateInitializers(meta.GetInitializers(), fldPath.Child("initializers"))...) | |
allErrs = append(allErrs, ValidateFinalizers(meta.GetFinalizers(), fldPath.Child("finalizers"))...) | |
return allErrs | |
} | |
// pkg/apis/core/validation/validation.go | |
// TODO update all references to these functions to point to the apimachineryvalidation ones | |
// NameIsDNSSubdomain is a ValidateNameFunc for names that must be a DNS subdomain. | |
func NameIsDNSSubdomain(name string, prefix bool) []string { | |
return apimachineryvalidation.NameIsDNSSubdomain(name, prefix) | |
} | |
// NameIsDNS1035Label is a ValidateNameFunc for names that must be a DNS 952 label. | |
func NameIsDNS1035Label(name string, prefix bool) []string { | |
return apimachineryvalidation.NameIsDNS1035Label(name, prefix) | |
} | |
// NameIsDNS1035Label is a ValidateNameFunc for names that must be a DNS 952 label. | |
func NameIsDNS1035Label(name string, prefix bool) []string { | |
if prefix { | |
name = maskTrailingDash(name) | |
} | |
return validation.IsDNS1035Label(name) | |
} | |
// staging/src/k8s.io/apimachinery/pkg/api/validation/generic.go | |
// NameIsDNSSubdomain is a ValidateNameFunc for names that must be a DNS subdomain. | |
func NameIsDNSSubdomain(name string, prefix bool) []string { | |
if prefix { | |
name = maskTrailingDash(name) | |
} | |
return validation.IsDNS1123Subdomain(name) | |
} | |
// NameIsDNS1035Label is a ValidateNameFunc for names that must be a DNS 952 label. | |
func NameIsDNS1035Label(name string, prefix bool) []string { | |
if prefix { | |
name = maskTrailingDash(name) | |
} | |
return validation.IsDNS1035Label(name) | |
} | |
const dns1035LabelFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?" | |
const dns1035LabelErrMsg string = "a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character" | |
const DNS1035LabelMaxLength int = 63 | |
var dns1035LabelRegexp = regexp.MustCompile("^" + dns1035LabelFmt + "$") | |
// IsDNS1035Label tests for a string that conforms to the definition of a label in | |
// DNS (RFC 1035). | |
func IsDNS1035Label(value string) []string { | |
var errs []string | |
if len(value) > DNS1035LabelMaxLength { | |
errs = append(errs, MaxLenError(DNS1035LabelMaxLength)) | |
} | |
if !dns1035LabelRegexp.MatchString(value) { | |
errs = append(errs, RegexError(dns1035LabelErrMsg, dns1035LabelFmt, "my-name", "abc-123")) | |
} | |
return errs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment