mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Move some pkg/kubectl into polymorphichelpers
This commit is contained in:
parent
a1f4c2ed21
commit
775b670e8b
@ -143,6 +143,7 @@ pkg/kubectl/describe/versioned
|
||||
pkg/kubectl/generate
|
||||
pkg/kubectl/generate/versioned
|
||||
pkg/kubectl/metricsutil
|
||||
pkg/kubectl/polymorphichelpers
|
||||
pkg/kubelet
|
||||
pkg/kubelet/apis/config
|
||||
pkg/kubelet/apis/config/v1beta1
|
||||
|
@ -9,25 +9,19 @@ load(
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"history_test.go",
|
||||
"rollback_test.go",
|
||||
"rolling_updater_test.go",
|
||||
"rollout_status_test.go",
|
||||
"scale_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/api/apps/v1:go_default_library",
|
||||
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/testapigroup/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
|
||||
@ -48,45 +42,30 @@ go_library(
|
||||
srcs = [
|
||||
"conditions.go",
|
||||
"doc.go",
|
||||
"history.go",
|
||||
"interfaces.go",
|
||||
"rollback.go",
|
||||
"rolling_updater.go",
|
||||
"rollout_status.go",
|
||||
"scale.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl",
|
||||
deps = [
|
||||
"//pkg/kubectl/describe/versioned:go_default_library",
|
||||
"//staging/src/k8s.io/api/apps/v1:go_default_library",
|
||||
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/api/extensions/v1beta1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/intstr:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/typed/apps/v1:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/rest:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/scale:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/util/retry:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/apps:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/scheme:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/deployment:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/podutils:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/slice:go_default_library",
|
||||
"//vendor/k8s.io/utils/integer:go_default_library",
|
||||
"//vendor/k8s.io/utils/pointer:go_default_library",
|
||||
],
|
||||
|
@ -6,8 +6,8 @@ go_library(
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/annotate",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//pkg/kubectl:go_default_library",
|
||||
"//pkg/kubectl/cmd/util:go_default_library",
|
||||
"//pkg/kubectl/polymorphichelpers:go_default_library",
|
||||
"//pkg/kubectl/util/i18n:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
|
@ -36,8 +36,8 @@ import (
|
||||
"k8s.io/cli-runtime/pkg/resource"
|
||||
"k8s.io/kubectl/pkg/scheme"
|
||||
"k8s.io/kubectl/pkg/util/templates"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/kubectl/polymorphichelpers"
|
||||
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
|
||||
)
|
||||
|
||||
@ -334,7 +334,7 @@ func validateNoAnnotationOverwrites(accessor metav1.Object, annotations map[stri
|
||||
var buf bytes.Buffer
|
||||
for key := range annotations {
|
||||
// change-cause annotation can always be overwritten
|
||||
if key == kubectl.ChangeCauseAnnotation {
|
||||
if key == polymorphichelpers.ChangeCauseAnnotation {
|
||||
continue
|
||||
}
|
||||
if value, found := accessor.GetAnnotations()[key]; found {
|
||||
|
@ -297,7 +297,7 @@ func (o *ExposeServiceOptions) RunExpose(cmd *cobra.Command, args []string) erro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params["labels"] = generate.MakeLabels(labels)
|
||||
params["labels"] = polymorphichelpers.MakeLabels(labels)
|
||||
}
|
||||
if err = generate.ValidateParams(names, params); err != nil {
|
||||
return err
|
||||
|
@ -20,7 +20,6 @@ go_library(
|
||||
"//build/visible_to:pkg_kubectl_cmd_rollout_CONSUMERS",
|
||||
],
|
||||
deps = [
|
||||
"//pkg/kubectl:go_default_library",
|
||||
"//pkg/kubectl/cmd/set:go_default_library",
|
||||
"//pkg/kubectl/cmd/util:go_default_library",
|
||||
"//pkg/kubectl/polymorphichelpers:go_default_library",
|
||||
|
@ -38,7 +38,6 @@ import (
|
||||
"k8s.io/kubectl/pkg/scheme"
|
||||
"k8s.io/kubectl/pkg/util/interrupt"
|
||||
"k8s.io/kubectl/pkg/util/templates"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/kubectl/polymorphichelpers"
|
||||
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
|
||||
@ -72,7 +71,7 @@ type RolloutStatusOptions struct {
|
||||
Revision int64
|
||||
Timeout time.Duration
|
||||
|
||||
StatusViewerFn func(*meta.RESTMapping) (kubectl.StatusViewer, error)
|
||||
StatusViewerFn func(*meta.RESTMapping) (polymorphichelpers.StatusViewer, error)
|
||||
Builder func() *resource.Builder
|
||||
DynamicClient dynamic.Interface
|
||||
|
||||
|
@ -174,14 +174,6 @@ func ParseProtocols(protocols interface{}) (map[string]string, error) {
|
||||
return portProtocolMap, nil
|
||||
}
|
||||
|
||||
func MakeLabels(labels map[string]string) string {
|
||||
out := []string{}
|
||||
for key, value := range labels {
|
||||
out = append(out, fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
return strings.Join(out, ",")
|
||||
}
|
||||
|
||||
// ParseLabels turns a string representation of a label set into a map[string]string
|
||||
func ParseLabels(labelSpec interface{}) (map[string]string, error) {
|
||||
labelString, isString := labelSpec.(string)
|
||||
|
@ -17,7 +17,9 @@ limitations under the License.
|
||||
package generate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -265,6 +267,14 @@ func TestGetBool(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func makeLabels(labels map[string]string) string {
|
||||
out := []string{}
|
||||
for key, value := range labels {
|
||||
out = append(out, fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
return strings.Join(out, ",")
|
||||
}
|
||||
|
||||
func TestMakeParseLabels(t *testing.T) {
|
||||
successCases := []struct {
|
||||
name string
|
||||
@ -294,7 +304,7 @@ func TestMakeParseLabels(t *testing.T) {
|
||||
}
|
||||
for _, tt := range successCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
labelString := MakeLabels(tt.labels)
|
||||
labelString := makeLabels(tt.labels)
|
||||
got, err := ParseLabels(labelString)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error :%v", err)
|
||||
|
@ -6,6 +6,7 @@ go_library(
|
||||
"attachablepodforobject.go",
|
||||
"canbeexposed.go",
|
||||
"helpers.go",
|
||||
"history.go",
|
||||
"historyviewer.go",
|
||||
"interface.go",
|
||||
"logsforobject.go",
|
||||
@ -15,15 +16,16 @@ go_library(
|
||||
"objectresumer.go",
|
||||
"portsforobject.go",
|
||||
"protocolsforobject.go",
|
||||
"rollback.go",
|
||||
"rollbacker.go",
|
||||
"rollout_status.go",
|
||||
"statusviewer.go",
|
||||
"updatepodspec.go",
|
||||
],
|
||||
importpath = "k8s.io/kubernetes/pkg/kubectl/polymorphichelpers",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//pkg/kubectl:go_default_library",
|
||||
"//pkg/kubectl/generate:go_default_library",
|
||||
"//pkg/kubectl/describe/versioned:go_default_library",
|
||||
"//staging/src/k8s.io/api/apps/v1:go_default_library",
|
||||
"//staging/src/k8s.io/api/apps/v1beta1:go_default_library",
|
||||
"//staging/src/k8s.io/api/apps/v1beta2:go_default_library",
|
||||
@ -32,19 +34,27 @@ go_library(
|
||||
"//staging/src/k8s.io/api/batch/v2alpha1:go_default_library",
|
||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/api/extensions/v1beta1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/typed/apps/v1:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/rest:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/watch:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/apps:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/scheme:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/deployment:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/podutils:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/slice:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@ -53,12 +63,15 @@ go_test(
|
||||
srcs = [
|
||||
"canbeexposed_test.go",
|
||||
"helpers_test.go",
|
||||
"history_test.go",
|
||||
"logsforobject_test.go",
|
||||
"mapbasedselectorforobject_test.go",
|
||||
"objectpauser_test.go",
|
||||
"objectresumer_test.go",
|
||||
"portsforobject_test.go",
|
||||
"protocolsforobject_test.go",
|
||||
"rollback_test.go",
|
||||
"rollout_status_test.go",
|
||||
"updatepodspec_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
@ -73,13 +86,16 @@ go_test(
|
||||
"//staging/src/k8s.io/api/extensions/v1beta1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
|
||||
"//staging/src/k8s.io/apimachinery/pkg/watch:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/testing:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/scheme:go_default_library",
|
||||
"//staging/src/k8s.io/kubectl/pkg/util/podutils:go_default_library",
|
||||
],
|
||||
)
|
||||
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kubectl
|
||||
package polymorphichelpers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -34,7 +34,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
clientappsv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
|
||||
kapps "k8s.io/kubectl/pkg/apps"
|
||||
"k8s.io/kubectl/pkg/apps"
|
||||
deploymentutil "k8s.io/kubectl/pkg/util/deployment"
|
||||
sliceutil "k8s.io/kubectl/pkg/util/slice"
|
||||
describe "k8s.io/kubernetes/pkg/kubectl/describe/versioned"
|
||||
@ -54,27 +54,27 @@ type HistoryVisitor struct {
|
||||
result HistoryViewer
|
||||
}
|
||||
|
||||
func (v *HistoryVisitor) VisitDeployment(elem kapps.GroupKindElement) {
|
||||
func (v *HistoryVisitor) VisitDeployment(elem apps.GroupKindElement) {
|
||||
v.result = &DeploymentHistoryViewer{v.clientset}
|
||||
}
|
||||
|
||||
func (v *HistoryVisitor) VisitStatefulSet(kind kapps.GroupKindElement) {
|
||||
func (v *HistoryVisitor) VisitStatefulSet(kind apps.GroupKindElement) {
|
||||
v.result = &StatefulSetHistoryViewer{v.clientset}
|
||||
}
|
||||
|
||||
func (v *HistoryVisitor) VisitDaemonSet(kind kapps.GroupKindElement) {
|
||||
func (v *HistoryVisitor) VisitDaemonSet(kind apps.GroupKindElement) {
|
||||
v.result = &DaemonSetHistoryViewer{v.clientset}
|
||||
}
|
||||
|
||||
func (v *HistoryVisitor) VisitJob(kind kapps.GroupKindElement) {}
|
||||
func (v *HistoryVisitor) VisitPod(kind kapps.GroupKindElement) {}
|
||||
func (v *HistoryVisitor) VisitReplicaSet(kind kapps.GroupKindElement) {}
|
||||
func (v *HistoryVisitor) VisitReplicationController(kind kapps.GroupKindElement) {}
|
||||
func (v *HistoryVisitor) VisitCronJob(kind kapps.GroupKindElement) {}
|
||||
func (v *HistoryVisitor) VisitJob(kind apps.GroupKindElement) {}
|
||||
func (v *HistoryVisitor) VisitPod(kind apps.GroupKindElement) {}
|
||||
func (v *HistoryVisitor) VisitReplicaSet(kind apps.GroupKindElement) {}
|
||||
func (v *HistoryVisitor) VisitReplicationController(kind apps.GroupKindElement) {}
|
||||
func (v *HistoryVisitor) VisitCronJob(kind apps.GroupKindElement) {}
|
||||
|
||||
// HistoryViewerFor returns an implementation of HistoryViewer interface for the given schema kind
|
||||
func HistoryViewerFor(kind schema.GroupKind, c kubernetes.Interface) (HistoryViewer, error) {
|
||||
elem := kapps.GroupKindElement(kind)
|
||||
elem := apps.GroupKindElement(kind)
|
||||
visitor := &HistoryVisitor{
|
||||
clientset: c,
|
||||
}
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kubectl
|
||||
package polymorphichelpers
|
||||
|
||||
import (
|
||||
"reflect"
|
@ -20,11 +20,10 @@ import (
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
)
|
||||
|
||||
// historyViewer Returns a HistoryViewer for viewing change history
|
||||
func historyViewer(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (kubectl.HistoryViewer, error) {
|
||||
func historyViewer(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (HistoryViewer, error) {
|
||||
clientConfig, err := restClientGetter.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -34,5 +33,5 @@ func historyViewer(restClientGetter genericclioptions.RESTClientGetter, mapping
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return kubectl.HistoryViewerFor(mapping.GroupVersionKind.GroupKind(), external)
|
||||
return HistoryViewerFor(mapping.GroupVersionKind.GroupKind(), external)
|
||||
}
|
||||
|
@ -25,7 +25,6 @@ import (
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/client-go/rest"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
)
|
||||
|
||||
// LogsForObjectFunc is a function type that can tell you how to get logs for a runtime.object
|
||||
@ -41,13 +40,13 @@ type AttachablePodForObjectFunc func(restClientGetter genericclioptions.RESTClie
|
||||
var AttachablePodForObjectFn AttachablePodForObjectFunc = attachablePodForObject
|
||||
|
||||
// HistoryViewerFunc is a function type that can tell you how to view change history
|
||||
type HistoryViewerFunc func(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (kubectl.HistoryViewer, error)
|
||||
type HistoryViewerFunc func(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (HistoryViewer, error)
|
||||
|
||||
// HistoryViewerFn gives a way to easily override the function for unit testing if needed
|
||||
var HistoryViewerFn HistoryViewerFunc = historyViewer
|
||||
|
||||
// StatusViewerFunc is a function type that can tell you how to print rollout status
|
||||
type StatusViewerFunc func(mapping *meta.RESTMapping) (kubectl.StatusViewer, error)
|
||||
type StatusViewerFunc func(mapping *meta.RESTMapping) (StatusViewer, error)
|
||||
|
||||
// StatusViewerFn gives a way to easily override the function for unit testing if needed
|
||||
var StatusViewerFn StatusViewerFunc = statusViewer
|
||||
@ -102,7 +101,7 @@ type ObjectResumerFunc func(runtime.Object) ([]byte, error)
|
||||
var ObjectResumerFn ObjectResumerFunc = defaultObjectResumer
|
||||
|
||||
// RollbackerFunc gives a way to change the rollback version of the specified RESTMapping type
|
||||
type RollbackerFunc func(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (kubectl.Rollbacker, error)
|
||||
type RollbackerFunc func(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (Rollbacker, error)
|
||||
|
||||
// RollbackerFn gives a way to easily override the function for unit testing if needed
|
||||
var RollbackerFn RollbackerFunc = rollbacker
|
||||
|
@ -18,6 +18,7 @@ package polymorphichelpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
appsv1 "k8s.io/api/apps/v1"
|
||||
appsv1beta1 "k8s.io/api/apps/v1beta1"
|
||||
@ -25,7 +26,6 @@ import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/kubectl/generate"
|
||||
)
|
||||
|
||||
// mapBasedSelectorForObject returns the map-based selector associated with the provided object. If a
|
||||
@ -35,19 +35,19 @@ func mapBasedSelectorForObject(object runtime.Object) (string, error) {
|
||||
// TODO: replace with a swagger schema based approach (identify pod selector via schema introspection)
|
||||
switch t := object.(type) {
|
||||
case *corev1.ReplicationController:
|
||||
return generate.MakeLabels(t.Spec.Selector), nil
|
||||
return MakeLabels(t.Spec.Selector), nil
|
||||
|
||||
case *corev1.Pod:
|
||||
if len(t.Labels) == 0 {
|
||||
return "", fmt.Errorf("the pod has no labels and cannot be exposed")
|
||||
}
|
||||
return generate.MakeLabels(t.Labels), nil
|
||||
return MakeLabels(t.Labels), nil
|
||||
|
||||
case *corev1.Service:
|
||||
if t.Spec.Selector == nil {
|
||||
return "", fmt.Errorf("the service has no pod selector set")
|
||||
}
|
||||
return generate.MakeLabels(t.Spec.Selector), nil
|
||||
return MakeLabels(t.Spec.Selector), nil
|
||||
|
||||
case *extensionsv1beta1.Deployment:
|
||||
// "extensions" deployments use pod template labels if selector is not set.
|
||||
@ -65,7 +65,7 @@ func mapBasedSelectorForObject(object runtime.Object) (string, error) {
|
||||
if len(labels) == 0 {
|
||||
return "", fmt.Errorf("the deployment has no labels or selectors and cannot be exposed")
|
||||
}
|
||||
return generate.MakeLabels(labels), nil
|
||||
return MakeLabels(labels), nil
|
||||
|
||||
case *appsv1.Deployment:
|
||||
// "apps" deployments must have the selector set.
|
||||
@ -77,7 +77,7 @@ func mapBasedSelectorForObject(object runtime.Object) (string, error) {
|
||||
if len(t.Spec.Selector.MatchExpressions) > 0 {
|
||||
return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
|
||||
}
|
||||
return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
return MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
|
||||
case *appsv1beta2.Deployment:
|
||||
// "apps" deployments must have the selector set.
|
||||
@ -89,7 +89,7 @@ func mapBasedSelectorForObject(object runtime.Object) (string, error) {
|
||||
if len(t.Spec.Selector.MatchExpressions) > 0 {
|
||||
return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
|
||||
}
|
||||
return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
return MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
|
||||
case *appsv1beta1.Deployment:
|
||||
// "apps" deployments must have the selector set.
|
||||
@ -101,7 +101,7 @@ func mapBasedSelectorForObject(object runtime.Object) (string, error) {
|
||||
if len(t.Spec.Selector.MatchExpressions) > 0 {
|
||||
return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
|
||||
}
|
||||
return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
return MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
|
||||
case *extensionsv1beta1.ReplicaSet:
|
||||
// "extensions" replicasets use pod template labels if selector is not set.
|
||||
@ -119,7 +119,7 @@ func mapBasedSelectorForObject(object runtime.Object) (string, error) {
|
||||
if len(labels) == 0 {
|
||||
return "", fmt.Errorf("the replica set has no labels or selectors and cannot be exposed")
|
||||
}
|
||||
return generate.MakeLabels(labels), nil
|
||||
return MakeLabels(labels), nil
|
||||
|
||||
case *appsv1.ReplicaSet:
|
||||
// "apps" replicasets must have the selector set.
|
||||
@ -131,7 +131,7 @@ func mapBasedSelectorForObject(object runtime.Object) (string, error) {
|
||||
if len(t.Spec.Selector.MatchExpressions) > 0 {
|
||||
return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
|
||||
}
|
||||
return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
return MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
|
||||
case *appsv1beta2.ReplicaSet:
|
||||
// "apps" replicasets must have the selector set.
|
||||
@ -143,10 +143,18 @@ func mapBasedSelectorForObject(object runtime.Object) (string, error) {
|
||||
if len(t.Spec.Selector.MatchExpressions) > 0 {
|
||||
return "", fmt.Errorf("couldn't convert expressions - \"%+v\" to map-based selector format", t.Spec.Selector.MatchExpressions)
|
||||
}
|
||||
return generate.MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
return MakeLabels(t.Spec.Selector.MatchLabels), nil
|
||||
|
||||
default:
|
||||
return "", fmt.Errorf("cannot extract pod selector from %T", object)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func MakeLabels(labels map[string]string) string {
|
||||
out := []string{}
|
||||
for key, value := range labels {
|
||||
out = append(out, fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
return strings.Join(out, ",")
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kubectl
|
||||
package polymorphichelpers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -32,7 +32,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/util/json"
|
||||
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
kapps "k8s.io/kubectl/pkg/apps"
|
||||
"k8s.io/kubectl/pkg/apps"
|
||||
"k8s.io/kubectl/pkg/scheme"
|
||||
deploymentutil "k8s.io/kubectl/pkg/util/deployment"
|
||||
)
|
||||
@ -52,27 +52,27 @@ type RollbackVisitor struct {
|
||||
result Rollbacker
|
||||
}
|
||||
|
||||
func (v *RollbackVisitor) VisitDeployment(elem kapps.GroupKindElement) {
|
||||
func (v *RollbackVisitor) VisitDeployment(elem apps.GroupKindElement) {
|
||||
v.result = &DeploymentRollbacker{v.clientset}
|
||||
}
|
||||
|
||||
func (v *RollbackVisitor) VisitStatefulSet(kind kapps.GroupKindElement) {
|
||||
func (v *RollbackVisitor) VisitStatefulSet(kind apps.GroupKindElement) {
|
||||
v.result = &StatefulSetRollbacker{v.clientset}
|
||||
}
|
||||
|
||||
func (v *RollbackVisitor) VisitDaemonSet(kind kapps.GroupKindElement) {
|
||||
func (v *RollbackVisitor) VisitDaemonSet(kind apps.GroupKindElement) {
|
||||
v.result = &DaemonSetRollbacker{v.clientset}
|
||||
}
|
||||
|
||||
func (v *RollbackVisitor) VisitJob(kind kapps.GroupKindElement) {}
|
||||
func (v *RollbackVisitor) VisitPod(kind kapps.GroupKindElement) {}
|
||||
func (v *RollbackVisitor) VisitReplicaSet(kind kapps.GroupKindElement) {}
|
||||
func (v *RollbackVisitor) VisitReplicationController(kind kapps.GroupKindElement) {}
|
||||
func (v *RollbackVisitor) VisitCronJob(kind kapps.GroupKindElement) {}
|
||||
func (v *RollbackVisitor) VisitJob(kind apps.GroupKindElement) {}
|
||||
func (v *RollbackVisitor) VisitPod(kind apps.GroupKindElement) {}
|
||||
func (v *RollbackVisitor) VisitReplicaSet(kind apps.GroupKindElement) {}
|
||||
func (v *RollbackVisitor) VisitReplicationController(kind apps.GroupKindElement) {}
|
||||
func (v *RollbackVisitor) VisitCronJob(kind apps.GroupKindElement) {}
|
||||
|
||||
// RollbackerFor returns an implementation of Rollbacker interface for the given schema kind
|
||||
func RollbackerFor(kind schema.GroupKind, c kubernetes.Interface) (Rollbacker, error) {
|
||||
elem := kapps.GroupKindElement(kind)
|
||||
elem := apps.GroupKindElement(kind)
|
||||
visitor := &RollbackVisitor{
|
||||
clientset: c,
|
||||
}
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kubectl
|
||||
package polymorphichelpers
|
||||
|
||||
import (
|
||||
"reflect"
|
@ -20,11 +20,10 @@ import (
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
)
|
||||
|
||||
// Returns a Rollbacker for changing the rollback version of the specified RESTMapping type or an error
|
||||
func rollbacker(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (kubectl.Rollbacker, error) {
|
||||
func rollbacker(restClientGetter genericclioptions.RESTClientGetter, mapping *meta.RESTMapping) (Rollbacker, error) {
|
||||
clientConfig, err := restClientGetter.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -34,5 +33,5 @@ func rollbacker(restClientGetter genericclioptions.RESTClientGetter, mapping *me
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return kubectl.RollbackerFor(mapping.GroupVersionKind.GroupKind(), external)
|
||||
return RollbackerFor(mapping.GroupVersionKind.GroupKind(), external)
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kubectl
|
||||
package polymorphichelpers
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kubectl
|
||||
package polymorphichelpers
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -18,10 +18,9 @@ package polymorphichelpers
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
)
|
||||
|
||||
// statusViewer returns a StatusViewer for printing rollout status.
|
||||
func statusViewer(mapping *meta.RESTMapping) (kubectl.StatusViewer, error) {
|
||||
return kubectl.StatusViewerFor(mapping.GroupVersionKind.GroupKind())
|
||||
func statusViewer(mapping *meta.RESTMapping) (StatusViewer, error) {
|
||||
return StatusViewerFor(mapping.GroupVersionKind.GroupKind())
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package apps_test
|
||||
package apps
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
|
@ -14,13 +14,11 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package apps_test
|
||||
package apps
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"k8s.io/kubectl/pkg/apps"
|
||||
)
|
||||
|
||||
var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
@ -32,7 +30,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
})
|
||||
|
||||
It("should Visit DaemonSet iff the Kind is a DaemonSet", func() {
|
||||
kind := apps.GroupKindElement{
|
||||
kind := GroupKindElement{
|
||||
Kind: "DaemonSet",
|
||||
Group: "apps",
|
||||
}
|
||||
@ -41,7 +39,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
"DaemonSet": 1,
|
||||
}))
|
||||
|
||||
kind = apps.GroupKindElement{
|
||||
kind = GroupKindElement{
|
||||
Kind: "DaemonSet",
|
||||
Group: "extensions",
|
||||
}
|
||||
@ -52,7 +50,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
})
|
||||
|
||||
It("should Visit Deployment iff the Kind is a Deployment", func() {
|
||||
kind := apps.GroupKindElement{
|
||||
kind := GroupKindElement{
|
||||
Kind: "Deployment",
|
||||
Group: "apps",
|
||||
}
|
||||
@ -61,7 +59,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
"Deployment": 1,
|
||||
}))
|
||||
|
||||
kind = apps.GroupKindElement{
|
||||
kind = GroupKindElement{
|
||||
Kind: "Deployment",
|
||||
Group: "extensions",
|
||||
}
|
||||
@ -72,7 +70,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
})
|
||||
|
||||
It("should Visit Job iff the Kind is a Job", func() {
|
||||
kind := apps.GroupKindElement{
|
||||
kind := GroupKindElement{
|
||||
Kind: "Job",
|
||||
Group: "batch",
|
||||
}
|
||||
@ -84,7 +82,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
})
|
||||
|
||||
It("should Visit Pod iff the Kind is a Pod", func() {
|
||||
kind := apps.GroupKindElement{
|
||||
kind := GroupKindElement{
|
||||
Kind: "Pod",
|
||||
Group: "",
|
||||
}
|
||||
@ -93,7 +91,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
"Pod": 1,
|
||||
}))
|
||||
|
||||
kind = apps.GroupKindElement{
|
||||
kind = GroupKindElement{
|
||||
Kind: "Pod",
|
||||
Group: "core",
|
||||
}
|
||||
@ -104,7 +102,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
})
|
||||
|
||||
It("should Visit ReplicationController iff the Kind is a ReplicationController", func() {
|
||||
kind := apps.GroupKindElement{
|
||||
kind := GroupKindElement{
|
||||
Kind: "ReplicationController",
|
||||
Group: "",
|
||||
}
|
||||
@ -113,7 +111,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
"ReplicationController": 1,
|
||||
}))
|
||||
|
||||
kind = apps.GroupKindElement{
|
||||
kind = GroupKindElement{
|
||||
Kind: "ReplicationController",
|
||||
Group: "core",
|
||||
}
|
||||
@ -124,7 +122,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
})
|
||||
|
||||
It("should Visit ReplicaSet iff the Kind is a ReplicaSet", func() {
|
||||
kind := apps.GroupKindElement{
|
||||
kind := GroupKindElement{
|
||||
Kind: "ReplicaSet",
|
||||
Group: "extensions",
|
||||
}
|
||||
@ -135,7 +133,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
})
|
||||
|
||||
It("should Visit StatefulSet iff the Kind is a StatefulSet", func() {
|
||||
kind := apps.GroupKindElement{
|
||||
kind := GroupKindElement{
|
||||
Kind: "StatefulSet",
|
||||
Group: "apps",
|
||||
}
|
||||
@ -146,7 +144,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
})
|
||||
|
||||
It("should Visit CronJob iff the Kind is a CronJob", func() {
|
||||
kind := apps.GroupKindElement{
|
||||
kind := GroupKindElement{
|
||||
Kind: "CronJob",
|
||||
Group: "batch",
|
||||
}
|
||||
@ -157,7 +155,7 @@ var _ = Describe("When KindVisitor accepts a GroupKind", func() {
|
||||
})
|
||||
|
||||
It("should give an error if the Kind is unknown", func() {
|
||||
kind := apps.GroupKindElement{
|
||||
kind := GroupKindElement{
|
||||
Kind: "Unknown",
|
||||
Group: "apps",
|
||||
}
|
||||
@ -171,15 +169,15 @@ type TestKindVisitor struct {
|
||||
visits map[string]int
|
||||
}
|
||||
|
||||
var _ apps.KindVisitor = &TestKindVisitor{}
|
||||
var _ KindVisitor = &TestKindVisitor{}
|
||||
|
||||
func (t *TestKindVisitor) Visit(kind apps.GroupKindElement) { t.visits[kind.Kind]++ }
|
||||
func (t *TestKindVisitor) Visit(kind GroupKindElement) { t.visits[kind.Kind]++ }
|
||||
|
||||
func (t *TestKindVisitor) VisitDaemonSet(kind apps.GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitDeployment(kind apps.GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitJob(kind apps.GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitPod(kind apps.GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitReplicaSet(kind apps.GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitReplicationController(kind apps.GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitStatefulSet(kind apps.GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitCronJob(kind apps.GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitDaemonSet(kind GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitDeployment(kind GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitJob(kind GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitPod(kind GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitReplicaSet(kind GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitReplicationController(kind GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitStatefulSet(kind GroupKindElement) { t.Visit(kind) }
|
||||
func (t *TestKindVisitor) VisitCronJob(kind GroupKindElement) { t.Visit(kind) }
|
||||
|
Loading…
Reference in New Issue
Block a user