2015-04-07 18:21:25 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-04-07 18:21:25 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-29 17:01:54 +00:00
|
|
|
"sort"
|
2015-04-07 18:21:25 +00:00
|
|
|
"strconv"
|
2016-01-22 18:33:23 +00:00
|
|
|
"strings"
|
2015-10-22 17:34:19 +00:00
|
|
|
"time"
|
2015-04-07 18:21:25 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
2017-06-22 17:25:57 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
2017-01-22 03:36:02 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2017-01-25 19:00:30 +00:00
|
|
|
"k8s.io/client-go/discovery"
|
2017-08-13 17:53:55 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2017-01-19 18:27:59 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
2018-02-26 20:23:33 +00:00
|
|
|
scaleclient "k8s.io/client-go/scale"
|
2017-01-20 18:06:17 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2017-11-08 22:34:54 +00:00
|
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
|
|
|
apiv1 "k8s.io/kubernetes/pkg/apis/core/v1"
|
2016-09-06 18:23:54 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
2016-10-21 22:24:05 +00:00
|
|
|
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
2017-11-09 02:11:54 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/categories"
|
2017-09-06 18:34:28 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
2016-11-25 17:37:02 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/plugins"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/resource"
|
2017-05-23 17:29:19 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/validation"
|
2017-02-19 22:39:43 +00:00
|
|
|
"k8s.io/kubernetes/pkg/printers"
|
2015-04-07 18:21:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FlagMatchBinaryVersion = "match-server-version"
|
|
|
|
)
|
|
|
|
|
2017-08-22 21:10:58 +00:00
|
|
|
var (
|
|
|
|
FlagHTTPCacheDir = "cache-dir"
|
|
|
|
)
|
|
|
|
|
2015-04-07 18:21:25 +00:00
|
|
|
// Factory provides abstractions that allow the Kubectl command to be extended across multiple types
|
|
|
|
// of resources and different API sets.
|
2017-09-12 03:17:50 +00:00
|
|
|
// The rings are here for a reason. In order for composers to be able to provide alternative factory implementations
|
2016-12-15 18:10:33 +00:00
|
|
|
// they need to provide low level pieces of *certain* functions so that when the factory calls back into itself
|
2017-09-12 03:17:50 +00:00
|
|
|
// it uses the custom version of the function. Rather than try to enumerate everything that someone would want to override
|
|
|
|
// we split the factory into rings, where each ring can depend on methods in an earlier ring, but cannot depend
|
2016-12-15 18:10:33 +00:00
|
|
|
// upon peer methods in its own ring.
|
2015-04-07 18:21:25 +00:00
|
|
|
// TODO: make the functions interfaces
|
|
|
|
// TODO: pass the various interfaces on the factory directly into the command constructors (so the
|
|
|
|
// commands are decoupled from the factory).
|
2016-10-13 00:09:26 +00:00
|
|
|
type Factory interface {
|
2016-12-15 18:10:33 +00:00
|
|
|
ClientAccessFactory
|
|
|
|
ObjectMappingFactory
|
|
|
|
BuilderFactory
|
|
|
|
}
|
2015-04-07 18:21:25 +00:00
|
|
|
|
2016-12-15 18:10:33 +00:00
|
|
|
type DiscoveryClientFactory interface {
|
2016-10-24 10:58:47 +00:00
|
|
|
// Returns a discovery client
|
|
|
|
DiscoveryClient() (discovery.CachedDiscoveryInterface, error)
|
2017-08-22 21:10:58 +00:00
|
|
|
|
|
|
|
// BindFlags adds any discovery flags that are common to all kubectl sub commands.
|
|
|
|
BindFlags(flags *pflag.FlagSet)
|
2016-12-15 18:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientAccessFactory holds the first level of factory methods.
|
|
|
|
// Generally provides discovery, negotiation, and no-dep calls.
|
|
|
|
// TODO The polymorphic calls probably deserve their own interface.
|
|
|
|
type ClientAccessFactory interface {
|
2017-08-22 21:10:58 +00:00
|
|
|
// Returns a discovery client
|
|
|
|
DiscoveryClient() (discovery.CachedDiscoveryInterface, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
|
2016-09-06 18:23:54 +00:00
|
|
|
// ClientSet gives you back an internal, generated clientset
|
2017-02-16 11:46:15 +00:00
|
|
|
ClientSet() (internalclientset.Interface, error)
|
2017-08-13 17:53:55 +00:00
|
|
|
|
2017-08-17 18:48:18 +00:00
|
|
|
// KubernetesClientSet gives you back an external clientset
|
2017-08-13 17:53:55 +00:00
|
|
|
KubernetesClientSet() (*kubernetes.Clientset, error)
|
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
// Returns a RESTClient for accessing Kubernetes resources or an error.
|
2016-10-13 00:09:26 +00:00
|
|
|
RESTClient() (*restclient.RESTClient, error)
|
2015-04-07 18:21:25 +00:00
|
|
|
// Returns a client.Config for accessing the Kubernetes server.
|
2016-10-13 00:09:26 +00:00
|
|
|
ClientConfig() (*restclient.Config, error)
|
2016-12-16 21:08:46 +00:00
|
|
|
// BareClientConfig returns a client.Config that has NOT been negotiated. It's
|
|
|
|
// just directions to the server. People use this to build RESTMappers on top of
|
|
|
|
BareClientConfig() (*restclient.Config, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
|
|
|
|
// UpdatePodSpecForObject will call the provided function on the pod spec this object supports,
|
|
|
|
// return false if no pod spec is supported, or return an error.
|
2017-11-10 18:36:12 +00:00
|
|
|
UpdatePodSpecForObject(obj runtime.Object, fn func(*v1.PodSpec) error) (bool, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
|
2016-02-06 02:43:02 +00:00
|
|
|
// MapBasedSelectorForObject returns the map-based selector associated with the provided object. If a
|
|
|
|
// new set-based selector is provided, an error is returned if the selector cannot be converted to a
|
|
|
|
// map-based selector
|
2016-10-13 00:09:26 +00:00
|
|
|
MapBasedSelectorForObject(object runtime.Object) (string, error)
|
2015-05-07 15:30:28 +00:00
|
|
|
// PortsForObject returns the ports associated with the provided object
|
2016-10-13 00:09:26 +00:00
|
|
|
PortsForObject(object runtime.Object) ([]string, error)
|
2016-05-12 04:07:07 +00:00
|
|
|
// ProtocolsForObject returns the <port, protocol> mapping associated with the provided object
|
2016-10-13 00:09:26 +00:00
|
|
|
ProtocolsForObject(object runtime.Object) (map[string]string, error)
|
2015-05-13 08:52:25 +00:00
|
|
|
// LabelsForObject returns the labels associated with the provided object
|
2016-10-13 00:09:26 +00:00
|
|
|
LabelsForObject(object runtime.Object) (map[string]string, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
|
|
|
|
// Command will stringify and return all environment arguments ie. a command run by a client
|
|
|
|
// using the factory.
|
2017-02-25 15:40:50 +00:00
|
|
|
Command(cmd *cobra.Command, showSecrets bool) string
|
2016-12-15 18:10:33 +00:00
|
|
|
// BindFlags adds any flags that are common to all kubectl sub commands.
|
|
|
|
BindFlags(flags *pflag.FlagSet)
|
|
|
|
// BindExternalFlags adds any flags defined by external projects (not part of pflags)
|
|
|
|
BindExternalFlags(flags *pflag.FlagSet)
|
|
|
|
|
|
|
|
// SuggestedPodTemplateResources returns a list of resource types that declare a pod template
|
|
|
|
SuggestedPodTemplateResources() []schema.GroupResource
|
|
|
|
|
2016-11-14 11:53:27 +00:00
|
|
|
// Pauser marks the object in the info as paused. Currently supported only for Deployments.
|
2018-02-09 06:53:53 +00:00
|
|
|
// Returns the patched object in bytes and any error that occurred during the encoding or
|
2016-11-14 11:53:27 +00:00
|
|
|
// in case the object is already paused.
|
|
|
|
Pauser(info *resource.Info) ([]byte, error)
|
|
|
|
// Resumer resumes a paused object inside the info. Currently supported only for Deployments.
|
2018-02-09 06:53:53 +00:00
|
|
|
// Returns the patched object in bytes and any error that occurred during the encoding or
|
2016-11-14 11:53:27 +00:00
|
|
|
// in case the object is already resumed.
|
|
|
|
Resumer(info *resource.Info) ([]byte, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
|
2016-09-20 12:19:01 +00:00
|
|
|
// ResolveImage resolves the image names. For kubernetes this function is just
|
|
|
|
// passthrough but it allows to perform more sophisticated image name resolving for
|
|
|
|
// third-party vendors.
|
|
|
|
ResolveImage(imageName string) (string, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
|
2015-06-26 20:49:34 +00:00
|
|
|
// Returns the default namespace to use in cases where no
|
|
|
|
// other namespace is specified and whether the namespace was
|
2016-05-11 07:14:23 +00:00
|
|
|
// overridden.
|
2016-10-13 00:09:26 +00:00
|
|
|
DefaultNamespace() (string, bool, error)
|
2015-11-19 18:14:10 +00:00
|
|
|
// Generators returns the generators for the provided command
|
2016-10-13 00:09:26 +00:00
|
|
|
Generators(cmdName string) map[string]kubectl.Generator
|
2015-08-26 08:06:40 +00:00
|
|
|
// Check whether the kind of resources could be exposed
|
2016-11-21 02:55:31 +00:00
|
|
|
CanBeExposed(kind schema.GroupKind) error
|
2015-10-15 22:15:13 +00:00
|
|
|
// Check whether the kind of resources could be autoscaled
|
2016-11-21 02:55:31 +00:00
|
|
|
CanBeAutoscaled(kind schema.GroupKind) error
|
2016-12-15 18:10:33 +00:00
|
|
|
|
2015-11-11 11:29:52 +00:00
|
|
|
// EditorEnvs returns a group of environment variables that the edit command
|
|
|
|
// can range over in order to determine if the user has specified an editor
|
|
|
|
// of their choice.
|
2016-10-13 00:09:26 +00:00
|
|
|
EditorEnvs() []string
|
2016-12-15 18:10:33 +00:00
|
|
|
}
|
2016-10-13 00:09:26 +00:00
|
|
|
|
2017-09-12 03:17:50 +00:00
|
|
|
// ObjectMappingFactory holds the second level of factory methods. These functions depend upon ClientAccessFactory methods.
|
2016-12-15 18:10:33 +00:00
|
|
|
// Generally they provide object typing and functions that build requests based on the negotiated clients.
|
|
|
|
type ObjectMappingFactory interface {
|
|
|
|
// Returns interfaces for dealing with arbitrary runtime.Objects.
|
|
|
|
Object() (meta.RESTMapper, runtime.ObjectTyper)
|
2017-03-24 12:02:10 +00:00
|
|
|
// Returns interface for expanding categories like `all`.
|
2017-11-09 02:11:54 +00:00
|
|
|
CategoryExpander() categories.CategoryExpander
|
2016-12-15 18:10:33 +00:00
|
|
|
// Returns a RESTClient for working with the specified RESTMapping or an error. This is intended
|
|
|
|
// for working with arbitrary resources and is not guaranteed to point to a Kubernetes APIServer.
|
|
|
|
ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
|
|
|
// Returns a RESTClient for working with Unstructured objects.
|
|
|
|
UnstructuredClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
|
|
|
// Returns a Describer for displaying the specified RESTMapping type or an error.
|
2017-02-19 22:39:43 +00:00
|
|
|
Describer(mapping *meta.RESTMapping) (printers.Describer, error)
|
2016-10-13 00:09:26 +00:00
|
|
|
|
2016-12-15 18:10:33 +00:00
|
|
|
// LogsForObject returns a request for the logs associated with the provided object
|
2017-02-21 16:19:13 +00:00
|
|
|
LogsForObject(object, options runtime.Object, timeout time.Duration) (*restclient.Request, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
// Returns a HistoryViewer for viewing change history
|
|
|
|
HistoryViewer(mapping *meta.RESTMapping) (kubectl.HistoryViewer, error)
|
|
|
|
// Returns a Rollbacker for changing the rollback version of the specified RESTMapping type or an error
|
|
|
|
Rollbacker(mapping *meta.RESTMapping) (kubectl.Rollbacker, error)
|
|
|
|
// Returns a StatusViewer for printing rollout status.
|
|
|
|
StatusViewer(mapping *meta.RESTMapping) (kubectl.StatusViewer, error)
|
|
|
|
|
|
|
|
// AttachablePodForObject returns the pod to which to attach given an object.
|
2017-02-21 16:19:13 +00:00
|
|
|
AttachablePodForObject(object runtime.Object, timeout time.Duration) (*api.Pod, error)
|
2016-10-13 00:09:26 +00:00
|
|
|
|
2017-10-09 18:57:23 +00:00
|
|
|
// ApproximatePodTemplateForObject returns a pod template object for the provided source.
|
|
|
|
// It may return both an error and a object. It attempt to return the best possible template
|
|
|
|
// available at the current time.
|
|
|
|
ApproximatePodTemplateForObject(runtime.Object) (*api.PodTemplateSpec, error)
|
|
|
|
|
2016-12-15 18:10:33 +00:00
|
|
|
// Returns a schema that can validate objects stored on disk.
|
2017-09-28 22:39:17 +00:00
|
|
|
Validator(validate bool) (validation.Schema, error)
|
2018-02-09 06:53:53 +00:00
|
|
|
// OpenAPISchema returns the schema openapi schema definition
|
2017-08-08 22:00:23 +00:00
|
|
|
OpenAPISchema() (openapi.Resources, error)
|
2015-04-07 18:21:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 03:17:50 +00:00
|
|
|
// BuilderFactory holds the third level of factory methods. These functions depend upon ObjectMappingFactory and ClientAccessFactory methods.
|
2016-12-15 18:10:33 +00:00
|
|
|
// Generally they depend upon client mapper functions
|
|
|
|
type BuilderFactory interface {
|
2017-11-14 03:43:58 +00:00
|
|
|
// NewBuilder returns an object that assists in loading objects from both disk and the server
|
|
|
|
// and which implements the common patterns for CLI interactions with generic resources.
|
2017-08-02 20:23:07 +00:00
|
|
|
NewBuilder() *resource.Builder
|
2016-11-25 17:37:02 +00:00
|
|
|
// PluginLoader provides the implementation to be used to load cli plugins.
|
|
|
|
PluginLoader() plugins.PluginLoader
|
|
|
|
// PluginRunner provides the implementation to be used to run cli plugins.
|
|
|
|
PluginRunner() plugins.PluginRunner
|
2018-02-26 20:23:33 +00:00
|
|
|
// Returns a Scaler for changing the size of the specified RESTMapping type or an error
|
2018-03-29 20:16:03 +00:00
|
|
|
Scaler() (kubectl.Scaler, error)
|
2018-02-26 20:23:33 +00:00
|
|
|
// ScaleClient gives you back scale getter
|
|
|
|
ScaleClient() (scaleclient.ScalesGetter, error)
|
|
|
|
// Returns a Reaper for gracefully shutting down resources.
|
|
|
|
Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error)
|
2015-10-21 14:41:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 00:09:26 +00:00
|
|
|
type factory struct {
|
2016-12-15 18:10:33 +00:00
|
|
|
ClientAccessFactory
|
|
|
|
ObjectMappingFactory
|
|
|
|
BuilderFactory
|
2016-10-13 00:09:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 18:21:25 +00:00
|
|
|
// NewFactory creates a factory with the default Kubernetes resources defined
|
|
|
|
// if optionalClientConfig is nil, then flags will be bound to a new clientcmd.ClientConfig.
|
|
|
|
// if optionalClientConfig is not nil, then this factory will make use of it.
|
2016-10-13 00:09:26 +00:00
|
|
|
func NewFactory(optionalClientConfig clientcmd.ClientConfig) Factory {
|
2016-12-15 18:10:33 +00:00
|
|
|
clientAccessFactory := NewClientAccessFactory(optionalClientConfig)
|
|
|
|
objectMappingFactory := NewObjectMappingFactory(clientAccessFactory)
|
|
|
|
builderFactory := NewBuilderFactory(clientAccessFactory, objectMappingFactory)
|
2015-08-10 20:08:34 +00:00
|
|
|
|
2016-12-15 18:10:33 +00:00
|
|
|
return &factory{
|
|
|
|
ClientAccessFactory: clientAccessFactory,
|
|
|
|
ObjectMappingFactory: objectMappingFactory,
|
|
|
|
BuilderFactory: builderFactory,
|
2015-04-07 18:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 17:01:54 +00:00
|
|
|
// GetFirstPod returns a pod matching the namespace and label selector
|
|
|
|
// and the number of all pods that match the label selector.
|
2017-09-25 06:32:52 +00:00
|
|
|
func GetFirstPod(client coreclient.PodsGetter, namespace string, selector string, timeout time.Duration, sortBy func([]*v1.Pod) sort.Interface) (*api.Pod, int, error) {
|
|
|
|
options := metav1.ListOptions{LabelSelector: selector}
|
2016-03-29 17:01:54 +00:00
|
|
|
|
|
|
|
podList, err := client.Pods(namespace).List(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2016-11-18 20:58:22 +00:00
|
|
|
pods := []*v1.Pod{}
|
2016-03-29 17:01:54 +00:00
|
|
|
for i := range podList.Items {
|
|
|
|
pod := podList.Items[i]
|
2016-11-18 20:58:22 +00:00
|
|
|
externalPod := &v1.Pod{}
|
2017-10-09 17:13:46 +00:00
|
|
|
apiv1.Convert_core_Pod_To_v1_Pod(&pod, externalPod, nil)
|
2016-11-18 20:58:22 +00:00
|
|
|
pods = append(pods, externalPod)
|
2016-03-29 17:01:54 +00:00
|
|
|
}
|
|
|
|
if len(pods) > 0 {
|
|
|
|
sort.Sort(sortBy(pods))
|
2016-11-18 20:58:22 +00:00
|
|
|
internalPod := &api.Pod{}
|
2017-10-09 17:13:46 +00:00
|
|
|
apiv1.Convert_v1_Pod_To_core_Pod(pods[0], internalPod, nil)
|
2016-11-18 20:58:22 +00:00
|
|
|
return internalPod, len(podList.Items), nil
|
2016-03-29 17:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Watch until we observe a pod
|
|
|
|
options.ResourceVersion = podList.ResourceVersion
|
|
|
|
w, err := client.Pods(namespace).Watch(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
defer w.Stop()
|
|
|
|
|
|
|
|
condition := func(event watch.Event) (bool, error) {
|
|
|
|
return event.Type == watch.Added || event.Type == watch.Modified, nil
|
|
|
|
}
|
|
|
|
event, err := watch.Until(timeout, w, condition)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
pod, ok := event.Object.(*api.Pod)
|
|
|
|
if !ok {
|
|
|
|
return nil, 0, fmt.Errorf("%#v is not a pod event", event)
|
2015-11-13 01:07:21 +00:00
|
|
|
}
|
2016-03-29 17:01:54 +00:00
|
|
|
return pod, 1, nil
|
2015-11-13 01:07:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 12:56:12 +00:00
|
|
|
func makePortsString(ports []api.ServicePort, useNodePort bool) string {
|
|
|
|
pieces := make([]string, len(ports))
|
|
|
|
for ix := range ports {
|
|
|
|
var port int32
|
|
|
|
if useNodePort {
|
|
|
|
port = ports[ix].NodePort
|
|
|
|
} else {
|
|
|
|
port = ports[ix].Port
|
|
|
|
}
|
|
|
|
pieces[ix] = fmt.Sprintf("%s:%d", strings.ToLower(string(ports[ix].Protocol)), port)
|
|
|
|
}
|
|
|
|
return strings.Join(pieces, ",")
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:21:25 +00:00
|
|
|
func getPorts(spec api.PodSpec) []string {
|
|
|
|
result := []string{}
|
|
|
|
for _, container := range spec.Containers {
|
|
|
|
for _, port := range container.Ports {
|
2016-04-27 04:35:14 +00:00
|
|
|
result = append(result, strconv.Itoa(int(port.ContainerPort)))
|
2015-04-07 18:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-05-12 04:07:07 +00:00
|
|
|
func getProtocols(spec api.PodSpec) map[string]string {
|
|
|
|
result := make(map[string]string)
|
|
|
|
for _, container := range spec.Containers {
|
|
|
|
for _, port := range container.Ports {
|
|
|
|
result[strconv.Itoa(int(port.ContainerPort))] = string(port.Protocol)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-07-17 01:02:36 +00:00
|
|
|
// Extracts the ports exposed by a service from the given service spec.
|
|
|
|
func getServicePorts(spec api.ServiceSpec) []string {
|
|
|
|
result := []string{}
|
|
|
|
for _, servicePort := range spec.Ports {
|
2016-04-27 04:35:14 +00:00
|
|
|
result = append(result, strconv.Itoa(int(servicePort.Port)))
|
2015-07-17 01:02:36 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-05-12 04:07:07 +00:00
|
|
|
// Extracts the protocols exposed by a service from the given service spec.
|
|
|
|
func getServiceProtocols(spec api.ServiceSpec) map[string]string {
|
|
|
|
result := make(map[string]string)
|
|
|
|
for _, servicePort := range spec.Ports {
|
|
|
|
result[strconv.Itoa(int(servicePort.Port))] = string(servicePort.Protocol)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|