...

Source file src/k8s.io/kubernetes/pkg/apis/core/pods/helpers.go

Documentation: k8s.io/kubernetes/pkg/apis/core/pods

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package pods
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/util/validation/field"
    23  	api "k8s.io/kubernetes/pkg/apis/core"
    24  	"k8s.io/kubernetes/pkg/fieldpath"
    25  )
    26  
    27  // ContainerVisitorWithPath is called with each container and the field.Path to that container,
    28  // and returns true if visiting should continue.
    29  type ContainerVisitorWithPath func(container *api.Container, path *field.Path) bool
    30  
    31  // VisitContainersWithPath invokes the visitor function with a pointer to the spec
    32  // of every container in the given pod spec and the field.Path to that container.
    33  // If visitor returns false, visiting is short-circuited. VisitContainersWithPath returns true if visiting completes,
    34  // false if visiting was short-circuited.
    35  func VisitContainersWithPath(podSpec *api.PodSpec, specPath *field.Path, visitor ContainerVisitorWithPath) bool {
    36  	fldPath := specPath.Child("initContainers")
    37  	for i := range podSpec.InitContainers {
    38  		if !visitor(&podSpec.InitContainers[i], fldPath.Index(i)) {
    39  			return false
    40  		}
    41  	}
    42  	fldPath = specPath.Child("containers")
    43  	for i := range podSpec.Containers {
    44  		if !visitor(&podSpec.Containers[i], fldPath.Index(i)) {
    45  			return false
    46  		}
    47  	}
    48  	fldPath = specPath.Child("ephemeralContainers")
    49  	for i := range podSpec.EphemeralContainers {
    50  		if !visitor((*api.Container)(&podSpec.EphemeralContainers[i].EphemeralContainerCommon), fldPath.Index(i)) {
    51  			return false
    52  		}
    53  	}
    54  	return true
    55  }
    56  
    57  // ConvertDownwardAPIFieldLabel converts the specified downward API field label
    58  // and its value in the pod of the specified version to the internal version,
    59  // and returns the converted label and value. This function returns an error if
    60  // the conversion fails.
    61  func ConvertDownwardAPIFieldLabel(version, label, value string) (string, string, error) {
    62  	if version != "v1" {
    63  		return "", "", fmt.Errorf("unsupported pod version: %s", version)
    64  	}
    65  
    66  	if path, _, ok := fieldpath.SplitMaybeSubscriptedPath(label); ok {
    67  		switch path {
    68  		case "metadata.annotations", "metadata.labels":
    69  			return label, value, nil
    70  		default:
    71  			return "", "", fmt.Errorf("field label does not support subscript: %s", label)
    72  		}
    73  	}
    74  
    75  	switch label {
    76  	case "metadata.annotations",
    77  		"metadata.labels",
    78  		"metadata.name",
    79  		"metadata.namespace",
    80  		"metadata.uid",
    81  		"spec.nodeName",
    82  		"spec.restartPolicy",
    83  		"spec.serviceAccountName",
    84  		"spec.schedulerName",
    85  		"status.phase",
    86  		"status.hostIP",
    87  		"status.hostIPs",
    88  		"status.podIP",
    89  		"status.podIPs":
    90  		return label, value, nil
    91  	// This is for backwards compatibility with old v1 clients which send spec.host
    92  	case "spec.host":
    93  		return "spec.nodeName", value, nil
    94  	default:
    95  		return "", "", fmt.Errorf("field label not supported: %s", label)
    96  	}
    97  }
    98  

View as plain text