mirror of https://github.com/k3s-io/k3s
218 lines
8.8 KiB
Go
218 lines
8.8 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"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"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"
|
|
"k8s.io/kubernetes/pkg/printers"
|
|
)
|
|
|
|
// 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
|
|
|
|
// 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
|
|
MapBasedSelectorForObject(object runtime.Object) (string, error)
|
|
// PortsForObject returns the ports associated with the provided object
|
|
PortsForObject(object runtime.Object) ([]string, error)
|
|
// ProtocolsForObject returns the <port, protocol> mapping associated with the provided object
|
|
ProtocolsForObject(object runtime.Object) (map[string]string, error)
|
|
|
|
// Command will stringify and return all environment arguments ie. a command run by a client
|
|
// using the factory.
|
|
Command(cmd *cobra.Command, showSecrets bool) string
|
|
|
|
// SuggestedPodTemplateResources returns a list of resource types that declare a pod template
|
|
SuggestedPodTemplateResources() []schema.GroupResource
|
|
|
|
// Pauser marks the object in the info as paused. Currently supported only for Deployments.
|
|
// Returns the patched object in bytes and any error that occurred 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 occurred during the encoding or
|
|
// in case the object is already resumed.
|
|
Resumer(info *resource.Info) ([]byte, error)
|
|
|
|
// 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
|
|
// Check whether the kind of resources could be exposed
|
|
CanBeExposed(kind schema.GroupKind) error
|
|
// Check whether the kind of resources could be autoscaled
|
|
CanBeAutoscaled(kind schema.GroupKind) error
|
|
}
|
|
|
|
// 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 Describer for displaying the specified RESTMapping type or an error.
|
|
Describer(mapping *meta.RESTMapping) (printers.Describer, 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 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)
|
|
// Returns a Reaper for gracefully shutting down resources.
|
|
Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, 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, ",")
|
|
}
|
|
|
|
func getPorts(spec api.PodSpec) []string {
|
|
result := []string{}
|
|
for _, container := range spec.Containers {
|
|
for _, port := range container.Ports {
|
|
result = append(result, strconv.Itoa(int(port.ContainerPort)))
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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 {
|
|
result = append(result, strconv.Itoa(int(servicePort.Port)))
|
|
}
|
|
return result
|
|
}
|
|
|
|
// 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
|
|
}
|