...

Source file src/k8s.io/kubectl/pkg/polymorphichelpers/protocolsforobject.go

Documentation: k8s.io/kubectl/pkg/polymorphichelpers

     1  /*
     2  Copyright 2018 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 polymorphichelpers
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  
    23  	appsv1 "k8s.io/api/apps/v1"
    24  	appsv1beta1 "k8s.io/api/apps/v1beta1"
    25  	appsv1beta2 "k8s.io/api/apps/v1beta2"
    26  	corev1 "k8s.io/api/core/v1"
    27  	extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  )
    30  
    31  func protocolsForObject(object runtime.Object) (map[string]string, error) {
    32  	// TODO: replace with a swagger schema based approach (identify pod selector via schema introspection)
    33  	switch t := object.(type) {
    34  	case *corev1.ReplicationController:
    35  		return getProtocols(t.Spec.Template.Spec), nil
    36  
    37  	case *corev1.Pod:
    38  		return getProtocols(t.Spec), nil
    39  
    40  	case *corev1.Service:
    41  		return getServiceProtocols(t.Spec), nil
    42  
    43  	case *extensionsv1beta1.Deployment:
    44  		return getProtocols(t.Spec.Template.Spec), nil
    45  	case *appsv1.Deployment:
    46  		return getProtocols(t.Spec.Template.Spec), nil
    47  	case *appsv1beta2.Deployment:
    48  		return getProtocols(t.Spec.Template.Spec), nil
    49  	case *appsv1beta1.Deployment:
    50  		return getProtocols(t.Spec.Template.Spec), nil
    51  
    52  	case *extensionsv1beta1.ReplicaSet:
    53  		return getProtocols(t.Spec.Template.Spec), nil
    54  	case *appsv1.ReplicaSet:
    55  		return getProtocols(t.Spec.Template.Spec), nil
    56  	case *appsv1beta2.ReplicaSet:
    57  		return getProtocols(t.Spec.Template.Spec), nil
    58  
    59  	default:
    60  		return nil, fmt.Errorf("cannot extract protocols from %T", object)
    61  	}
    62  }
    63  
    64  func getProtocols(spec corev1.PodSpec) map[string]string {
    65  	result := make(map[string]string)
    66  	for _, container := range spec.Containers {
    67  		for _, port := range container.Ports {
    68  			// Empty protocol must be defaulted (TCP)
    69  			if len(port.Protocol) == 0 {
    70  				port.Protocol = corev1.ProtocolTCP
    71  			}
    72  			result[strconv.Itoa(int(port.ContainerPort))] = string(port.Protocol)
    73  		}
    74  	}
    75  	return result
    76  }
    77  
    78  // Extracts the protocols exposed by a service from the given service spec.
    79  func getServiceProtocols(spec corev1.ServiceSpec) map[string]string {
    80  	result := make(map[string]string)
    81  	for _, servicePort := range spec.Ports {
    82  		// Empty protocol must be defaulted (TCP)
    83  		if len(servicePort.Protocol) == 0 {
    84  			servicePort.Protocol = corev1.ProtocolTCP
    85  		}
    86  		result[strconv.Itoa(int(servicePort.Port))] = string(servicePort.Protocol)
    87  	}
    88  	return result
    89  }
    90  

View as plain text