152 lines
6.0 KiB
Go
152 lines
6.0 KiB
Go
/*
|
|
Copyright 2014 The Kubernetes Authors.
|
|
|
|
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"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
"k8s.io/client-go/dynamic"
|
|
"k8s.io/client-go/kubernetes"
|
|
restclient "k8s.io/client-go/rest"
|
|
scaleclient "k8s.io/client-go/scale"
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
|
"k8s.io/kubernetes/pkg/kubectl"
|
|
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
|
"k8s.io/kubernetes/pkg/kubectl/validation"
|
|
)
|
|
|
|
// Factory provides abstractions that allow the Kubectl command to be extended across multiple types
|
|
// of resources and different API sets.
|
|
// 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 in an earlier ring, but cannot depend
|
|
// upon peer methods in its own ring.
|
|
// 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).
|
|
type Factory interface {
|
|
ClientAccessFactory
|
|
ObjectMappingFactory
|
|
BuilderFactory
|
|
}
|
|
|
|
// 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 {
|
|
genericclioptions.RESTClientGetter
|
|
|
|
// ClientSet gives you back an internal, generated clientset
|
|
ClientSet() (internalclientset.Interface, error)
|
|
|
|
// DynamicClient returns a dynamic client ready for use
|
|
DynamicClient() (dynamic.Interface, error)
|
|
|
|
// KubernetesClientSet gives you back an external clientset
|
|
KubernetesClientSet() (*kubernetes.Clientset, error)
|
|
|
|
// Returns a RESTClient for accessing Kubernetes resources or an error.
|
|
RESTClient() (*restclient.RESTClient, error)
|
|
|
|
// 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.
|
|
NewBuilder() *resource.Builder
|
|
|
|
// SuggestedPodTemplateResources returns a list of resource types that declare a pod template
|
|
SuggestedPodTemplateResources() []schema.GroupResource
|
|
|
|
// Returns the default namespace to use in cases where no
|
|
// other namespace is specified and whether the namespace was
|
|
// overridden.
|
|
DefaultNamespace() (string, bool, error)
|
|
// Generators returns the generators for the provided command
|
|
Generators(cmdName string) map[string]kubectl.Generator
|
|
}
|
|
|
|
// 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 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 schema that can validate objects stored on disk.
|
|
Validator(validate bool) (validation.Schema, error)
|
|
// OpenAPISchema returns the schema openapi schema definition
|
|
OpenAPISchema() (openapi.Resources, error)
|
|
}
|
|
|
|
// BuilderFactory holds the third level of factory methods. These functions depend upon ObjectMappingFactory and ClientAccessFactory methods.
|
|
// Generally they depend upon client mapper functions
|
|
type BuilderFactory interface {
|
|
// ScaleClient gives you back scale getter
|
|
ScaleClient() (scaleclient.ScalesGetter, error)
|
|
}
|
|
|
|
type factory struct {
|
|
ClientAccessFactory
|
|
ObjectMappingFactory
|
|
BuilderFactory
|
|
}
|
|
|
|
// NewFactory creates a factory with the default Kubernetes resources defined
|
|
// Receives a clientGetter capable of providing a discovery client and a REST client configuration.
|
|
func NewFactory(clientGetter genericclioptions.RESTClientGetter) Factory {
|
|
clientAccessFactory := NewClientAccessFactory(clientGetter)
|
|
objectMappingFactory := NewObjectMappingFactory(clientAccessFactory)
|
|
builderFactory := NewBuilderFactory(clientAccessFactory, objectMappingFactory)
|
|
|
|
return &factory{
|
|
ClientAccessFactory: clientAccessFactory,
|
|
ObjectMappingFactory: objectMappingFactory,
|
|
BuilderFactory: builderFactory,
|
|
}
|
|
}
|
|
|
|
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, ",")
|
|
}
|
|
|
|
// 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
|
|
}
|