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 (
|
2015-09-11 03:54:22 +00:00
|
|
|
"bytes"
|
2015-04-07 18:21:25 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-09-10 21:58:09 +00:00
|
|
|
"io/ioutil"
|
2015-04-07 18:21:25 +00:00
|
|
|
"os"
|
2015-09-16 23:31:45 +00:00
|
|
|
"os/user"
|
2015-09-10 21:58:09 +00:00
|
|
|
"path"
|
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
|
|
|
|
2017-05-23 17:29:19 +00:00
|
|
|
swagger "github.com/emicklei/go-restful-swagger12"
|
2017-04-14 01:01:38 +00:00
|
|
|
"github.com/golang/glog"
|
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/labels"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer/json"
|
|
|
|
"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"
|
2017-01-20 18:06:17 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2017-06-08 06:02:38 +00:00
|
|
|
fedclientset "k8s.io/kubernetes/federation/client/clientset_generated/federation_clientset"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2017-07-29 08:47:14 +00:00
|
|
|
apiv1 "k8s.io/kubernetes/pkg/api/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-04-14 01:01:38 +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.
|
2016-12-15 18:10:33 +00:00
|
|
|
// The rings are here for a reason. In order for composers to be able to provide alternative factory implementations
|
|
|
|
// they need to provide low level pieces of *certain* functions so that when the factory calls back into itself
|
|
|
|
// 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 an earlier ring, but cannot depend
|
|
|
|
// 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
|
|
|
|
|
|
|
// TODO this should probably be removed and collapsed into whatever we want to use long term
|
|
|
|
// probably returning a restclient for a version and leaving contruction up to someone else
|
|
|
|
FederationClientSetForVersion(version *schema.GroupVersion) (fedclientset.Interface, error)
|
|
|
|
// TODO remove this should be rolled into restclient with the right version
|
|
|
|
FederationClientForVersion(version *schema.GroupVersion) (*restclient.RESTClient, error)
|
|
|
|
// TODO remove. This should be rolled into `ClientSet`
|
2017-02-16 11:46:15 +00:00
|
|
|
ClientSetForVersion(requiredVersion *schema.GroupVersion) (internalclientset.Interface, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
// TODO remove. This should be rolled into `ClientConfig`
|
|
|
|
ClientConfigForVersion(requiredVersion *schema.GroupVersion) (*restclient.Config, error)
|
|
|
|
|
|
|
|
// Returns interfaces for decoding objects - if toInternal is set, decoded objects will be converted
|
|
|
|
// into their internal form (if possible). Eventually the internal form will be removed as an option,
|
|
|
|
// and only versioned objects will be returned.
|
|
|
|
Decoder(toInternal bool) runtime.Decoder
|
|
|
|
// Returns an encoder capable of encoding a provided object into JSON in the default desired version.
|
|
|
|
JSONEncoder() runtime.Encoder
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
UpdatePodSpecForObject(obj runtime.Object, fn func(*api.PodSpec) error) (bool, error)
|
|
|
|
|
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
|
|
|
|
|
|
|
// Returns internal flagset
|
|
|
|
FlagSet() *pflag.FlagSet
|
|
|
|
// 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)
|
|
|
|
|
2017-02-19 22:39:43 +00:00
|
|
|
// TODO: Break the dependency on cmd here.
|
|
|
|
DefaultResourceFilterOptions(cmd *cobra.Command, withNamespace bool) *printers.PrintOptions
|
2016-12-15 18:10:33 +00:00
|
|
|
// DefaultResourceFilterFunc returns a collection of FilterFuncs suitable for filtering specific resource types.
|
|
|
|
DefaultResourceFilterFunc() kubectl.Filters
|
|
|
|
|
|
|
|
// SuggestedPodTemplateResources returns a list of resource types that declare a pod template
|
|
|
|
SuggestedPodTemplateResources() []schema.GroupResource
|
|
|
|
|
|
|
|
// Returns a Printer for formatting objects of the given type or an error.
|
2017-02-19 22:39:43 +00:00
|
|
|
Printer(mapping *meta.RESTMapping, options printers.PrintOptions) (printers.ResourcePrinter, error)
|
2016-11-14 11:53:27 +00:00
|
|
|
// Pauser marks the object in the info as paused. Currently supported only for Deployments.
|
|
|
|
// Returns the patched object in bytes and any error that occured during the encoding or
|
|
|
|
// 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.
|
|
|
|
// Returns the patched object in bytes and any error that occured during the encoding or
|
|
|
|
// 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-05-24 12:56:12 +00:00
|
|
|
// PrintObjectSpecificMessage prints object-specific messages on the provided writer
|
2016-10-13 00:09:26 +00:00
|
|
|
PrintObjectSpecificMessage(obj runtime.Object, out io.Writer)
|
2016-12-15 18:10:33 +00:00
|
|
|
}
|
2016-10-13 00:09:26 +00:00
|
|
|
|
2016-12-15 18:10:33 +00:00
|
|
|
// ObjectMappingFactory holds the second level of factory methods. These functions depend upon ClientAccessFactory methods.
|
|
|
|
// 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)
|
|
|
|
// Returns interfaces for dealing with arbitrary
|
|
|
|
// runtime.Unstructured. This performs API calls to discover types.
|
|
|
|
UnstructuredObject() (meta.RESTMapper, runtime.ObjectTyper, error)
|
2017-03-24 12:02:10 +00:00
|
|
|
// Returns interface for expanding categories like `all`.
|
|
|
|
CategoryExpander() resource.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 Scaler for changing the size of the specified RESTMapping type or an error
|
|
|
|
Scaler(mapping *meta.RESTMapping) (kubectl.Scaler, error)
|
|
|
|
// Returns a Reaper for gracefully shutting down resources.
|
|
|
|
Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error)
|
|
|
|
// 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
|
|
|
|
2016-12-15 18:10:33 +00:00
|
|
|
// Returns a schema that can validate objects stored on disk.
|
2017-08-11 16:38:59 +00:00
|
|
|
Validator(validate bool, openapi bool, cacheDir string) (validation.Schema, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
// SwaggerSchema returns the schema declaration for the provided group version kind.
|
|
|
|
SwaggerSchema(schema.GroupVersionKind) (*swagger.ApiDeclaration, error)
|
2017-04-14 01:01:38 +00:00
|
|
|
// OpenAPISchema returns the schema openapi schema definiton
|
2017-08-08 22:00:23 +00:00
|
|
|
OpenAPISchema() (openapi.Resources, error)
|
2015-04-07 18:21:25 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 18:10:33 +00:00
|
|
|
// BuilderFactory holds the second level of factory methods. These functions depend upon ObjectMappingFactory and ClientAccessFactory methods.
|
|
|
|
// Generally they depend upon client mapper functions
|
|
|
|
type BuilderFactory interface {
|
2017-02-19 22:39:43 +00:00
|
|
|
// PrinterForCommand returns the default printer for the command. It requires that certain options
|
2017-05-23 02:47:20 +00:00
|
|
|
// are declared on the command (see AddPrinterFlags). Returns a printer, or an error if a printer
|
|
|
|
// could not be found.
|
2017-02-19 22:39:43 +00:00
|
|
|
// TODO: Break the dependency on cmd here.
|
2017-05-16 21:52:51 +00:00
|
|
|
PrinterForCommand(cmd *cobra.Command, isLocal bool, outputOpts *printers.OutputOptions, options printers.PrintOptions) (printers.ResourcePrinter, error)
|
2017-02-19 22:39:43 +00:00
|
|
|
// PrinterForMapping returns a printer suitable for displaying the provided resource type.
|
|
|
|
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
|
2017-05-19 21:24:39 +00:00
|
|
|
// Returns a printer, true if the printer is generic (is not internal), or
|
|
|
|
// an error if a printer could not be found.
|
2017-05-16 21:52:51 +00:00
|
|
|
PrinterForMapping(cmd *cobra.Command, isLocal bool, outputOpts *printers.OutputOptions, mapping *meta.RESTMapping, withNamespace bool) (printers.ResourcePrinter, error)
|
2016-12-15 18:10:33 +00:00
|
|
|
// PrintObject prints an api object given command line flags to modify the output format
|
2017-05-16 21:52:51 +00:00
|
|
|
PrintObject(cmd *cobra.Command, isLocal bool, mapper meta.RESTMapper, obj runtime.Object, out io.Writer) error
|
2016-12-15 18:10:33 +00:00
|
|
|
// One stop shopping for a Builder
|
2017-05-16 21:52:51 +00:00
|
|
|
NewBuilder(allowRemoteCalls bool) *resource.Builder
|
|
|
|
// Resource builder for working with unstructured objects
|
|
|
|
NewUnstructuredBuilder(allowRemoteCalls bool) (*resource.Builder, error)
|
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
|
2015-10-21 14:41:42 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 02:55:31 +00:00
|
|
|
func getGroupVersionKinds(gvks []schema.GroupVersionKind, group string) []schema.GroupVersionKind {
|
|
|
|
result := []schema.GroupVersionKind{}
|
2016-03-10 01:27:19 +00:00
|
|
|
for ix := range gvks {
|
|
|
|
if gvks[ix].Group == group {
|
|
|
|
result = append(result, gvks[ix])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
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.
|
2016-11-18 20:58:22 +00:00
|
|
|
func GetFirstPod(client coreclient.PodsGetter, namespace string, selector labels.Selector, timeout time.Duration, sortBy func([]*v1.Pod) sort.Interface) (*api.Pod, int, error) {
|
2017-01-22 03:36:02 +00:00
|
|
|
options := metav1.ListOptions{LabelSelector: selector.String()}
|
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-07-29 08:47:14 +00:00
|
|
|
apiv1.Convert_api_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-07-29 08:47:14 +00:00
|
|
|
apiv1.Convert_v1_Pod_To_api_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
|
|
|
|
}
|
|
|
|
|
2015-04-07 18:21:25 +00:00
|
|
|
type clientSwaggerSchema struct {
|
2016-12-07 18:18:28 +00:00
|
|
|
c restclient.Interface
|
2015-09-10 21:58:09 +00:00
|
|
|
cacheDir string
|
2015-04-07 18:21:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 21:58:09 +00:00
|
|
|
const schemaFileName = "schema.json"
|
|
|
|
|
|
|
|
type schemaClient interface {
|
2016-02-12 18:58:43 +00:00
|
|
|
Get() *restclient.Request
|
2015-09-10 21:58:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 23:31:45 +00:00
|
|
|
func recursiveSplit(dir string) []string {
|
|
|
|
parent, file := path.Split(dir)
|
|
|
|
if len(parent) == 0 {
|
|
|
|
return []string{file}
|
|
|
|
}
|
|
|
|
return append(recursiveSplit(parent[:len(parent)-1]), file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func substituteUserHome(dir string) (string, error) {
|
|
|
|
if len(dir) == 0 || dir[0] != '~' {
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
parts := recursiveSplit(dir)
|
|
|
|
if len(parts[0]) == 1 {
|
|
|
|
parts[0] = os.Getenv("HOME")
|
|
|
|
} else {
|
|
|
|
usr, err := user.Lookup(parts[0][1:])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
parts[0] = usr.HomeDir
|
|
|
|
}
|
|
|
|
return path.Join(parts...), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeSchemaFile(schemaData []byte, cacheDir, cacheFile, prefix, groupVersion string) error {
|
|
|
|
if err := os.MkdirAll(path.Join(cacheDir, prefix, groupVersion), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tmpFile, err := ioutil.TempFile(cacheDir, "schema")
|
|
|
|
if err != nil {
|
|
|
|
// If we can't write, keep going.
|
|
|
|
if os.IsPermission(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(tmpFile, bytes.NewBuffer(schemaData)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-14 01:01:38 +00:00
|
|
|
glog.V(4).Infof("Writing swagger cache (dir %v) file %v (from %v)", cacheDir, cacheFile, tmpFile.Name())
|
2015-09-16 23:31:45 +00:00
|
|
|
if err := os.Link(tmpFile.Name(), cacheFile); err != nil {
|
|
|
|
// If we can't write due to file existing, or permission problems, keep going.
|
|
|
|
if os.IsExist(err) || os.IsPermission(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-04 22:52:19 +00:00
|
|
|
func getSchemaAndValidate(c schemaClient, data []byte, prefix, groupVersion, cacheDir string, delegate validation.Schema) (err error) {
|
2015-09-10 21:58:09 +00:00
|
|
|
var schemaData []byte
|
2016-03-04 23:59:40 +00:00
|
|
|
var firstSeen bool
|
2015-09-16 23:31:45 +00:00
|
|
|
fullDir, err := substituteUserHome(cacheDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cacheFile := path.Join(fullDir, prefix, groupVersion, schemaFileName)
|
2015-09-10 21:58:09 +00:00
|
|
|
|
|
|
|
if len(cacheDir) != 0 {
|
|
|
|
if schemaData, err = ioutil.ReadFile(cacheFile); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if schemaData == nil {
|
2016-03-04 23:59:40 +00:00
|
|
|
firstSeen = true
|
|
|
|
schemaData, err = downloadSchemaAndStore(c, cacheDir, fullDir, cacheFile, prefix, groupVersion)
|
2015-09-10 21:58:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-07 18:21:25 +00:00
|
|
|
}
|
2016-05-04 22:52:19 +00:00
|
|
|
schema, err := validation.NewSwaggerSchemaFromBytes(schemaData, delegate)
|
2015-04-07 18:21:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-04 23:59:40 +00:00
|
|
|
err = schema.ValidateBytes(data)
|
|
|
|
if _, ok := err.(validation.TypeNotFoundError); ok && !firstSeen {
|
2016-08-02 16:51:51 +00:00
|
|
|
// As a temporary hack, kubectl would re-get the schema if validation
|
2016-03-04 23:59:40 +00:00
|
|
|
// fails for type not found reason.
|
|
|
|
// TODO: runtime-config settings needs to make into the file's name
|
|
|
|
schemaData, err = downloadSchemaAndStore(c, cacheDir, fullDir, cacheFile, prefix, groupVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-04 22:52:19 +00:00
|
|
|
schema, err := validation.NewSwaggerSchemaFromBytes(schemaData, delegate)
|
2016-03-04 23:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return schema.ValidateBytes(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download swagger schema from apiserver and store it to file.
|
|
|
|
func downloadSchemaAndStore(c schemaClient, cacheDir, fullDir, cacheFile, prefix, groupVersion string) (schemaData []byte, err error) {
|
|
|
|
schemaData, err = c.Get().
|
|
|
|
AbsPath("/swaggerapi", prefix, groupVersion).
|
|
|
|
Do().
|
|
|
|
Raw()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(cacheDir) != 0 {
|
|
|
|
if err = writeSchemaFile(schemaData, fullDir, cacheFile, prefix, groupVersion); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2015-04-07 18:21:25 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 20:08:34 +00:00
|
|
|
func (c *clientSwaggerSchema) ValidateBytes(data []byte) error {
|
2015-12-21 05:37:49 +00:00
|
|
|
gvk, err := json.DefaultMetaFactory.Interpret(data)
|
2015-08-10 20:08:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-12 18:17:43 +00:00
|
|
|
if ok := api.Registry.IsEnabledVersion(gvk.GroupVersion()); !ok {
|
2016-11-16 15:32:27 +00:00
|
|
|
// if we don't have this in our scheme, just skip validation because its an object we don't recognize
|
2016-03-10 01:27:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-08 15:50:53 +00:00
|
|
|
|
2016-08-18 11:23:10 +00:00
|
|
|
switch gvk.Group {
|
2016-09-08 15:50:53 +00:00
|
|
|
case api.GroupName:
|
|
|
|
return getSchemaAndValidate(c.c, data, "api", gvk.GroupVersion().String(), c.cacheDir, c)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return getSchemaAndValidate(c.c, data, "apis/", gvk.GroupVersion().String(), c.cacheDir, c)
|
2016-05-25 21:47:14 +00:00
|
|
|
}
|
2015-08-10 20:08:34 +00:00
|
|
|
}
|