mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-28 21:55:48 +00:00
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. move scaleClient from factory rebase https://github.com/kubernetes/kubernetes/pull/64287 ```release-note NONE ```
104 lines
4.0 KiB
Go
104 lines
4.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/client-go/dynamic"
|
|
"k8s.io/client-go/kubernetes"
|
|
restclient "k8s.io/client-go/rest"
|
|
api "k8s.io/kubernetes/pkg/apis/core"
|
|
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
|
"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 {
|
|
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
|
|
|
|
// Returns the default namespace to use in cases where no
|
|
// other namespace is specified and whether the namespace was
|
|
// overridden.
|
|
DefaultNamespace() (string, bool, error)
|
|
|
|
// 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)
|
|
}
|
|
|
|
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
|
|
}
|