Skip to content

Commit 75c0203

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#55257 from mahdix/history_visitor
Automatic merge from submit-queue (batch tested with PRs 55044, 55257, 55334). 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>. Refactor HistoryViewerFor to use Visitor design pattern Resolves kubernetes/kubectl#73 This is a follow up from kubernetes#54456 which is already reviewed.
2 parents 79d8586 + 1d2a14d commit 75c0203

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed

pkg/kubectl/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go_test(
1616
"deployment_test.go",
1717
"env_file_test.go",
1818
"generate_test.go",
19+
"history_test.go",
1920
"namespace_test.go",
2021
"pdb_test.go",
2122
"quota_test.go",
@@ -133,6 +134,7 @@ go_library(
133134
"//pkg/controller/deployment/util:go_default_library",
134135
"//pkg/controller/statefulset:go_default_library",
135136
"//pkg/credentialprovider:go_default_library",
137+
"//pkg/kubectl/apps:go_default_library",
136138
"//pkg/kubectl/resource:go_default_library",
137139
"//pkg/kubectl/util:go_default_library",
138140
"//pkg/kubectl/util/hash:go_default_library",

pkg/kubectl/history.go

+41-10
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import (
3737
clientextv1beta1 "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
3838
"k8s.io/kubernetes/pkg/api"
3939
apiv1 "k8s.io/kubernetes/pkg/api/v1"
40-
"k8s.io/kubernetes/pkg/apis/apps"
4140
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
41+
kapps "k8s.io/kubernetes/pkg/kubectl/apps"
4242
sliceutil "k8s.io/kubernetes/pkg/kubectl/util/slice"
4343
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
4444
)
@@ -52,16 +52,47 @@ type HistoryViewer interface {
5252
ViewHistory(namespace, name string, revision int64) (string, error)
5353
}
5454

55+
type HistoryVisitor struct {
56+
clientset kubernetes.Interface
57+
result HistoryViewer
58+
}
59+
60+
func (v *HistoryVisitor) VisitDeployment(elem kapps.GroupKindElement) {
61+
v.result = &DeploymentHistoryViewer{v.clientset}
62+
}
63+
64+
func (v *HistoryVisitor) VisitStatefulSet(kind kapps.GroupKindElement) {
65+
v.result = &StatefulSetHistoryViewer{v.clientset}
66+
}
67+
68+
func (v *HistoryVisitor) VisitDaemonSet(kind kapps.GroupKindElement) {
69+
v.result = &DaemonSetHistoryViewer{v.clientset}
70+
}
71+
72+
func (v *HistoryVisitor) VisitJob(kind kapps.GroupKindElement) {}
73+
func (v *HistoryVisitor) VisitPod(kind kapps.GroupKindElement) {}
74+
func (v *HistoryVisitor) VisitReplicaSet(kind kapps.GroupKindElement) {}
75+
func (v *HistoryVisitor) VisitReplicationController(kind kapps.GroupKindElement) {}
76+
77+
// HistoryViewerFor returns an implementation of HistoryViewer interface for the given schema kind
5578
func HistoryViewerFor(kind schema.GroupKind, c kubernetes.Interface) (HistoryViewer, error) {
56-
switch kind {
57-
case extensionsv1beta1.SchemeGroupVersion.WithKind("Deployment").GroupKind(), apps.Kind("Deployment"):
58-
return &DeploymentHistoryViewer{c}, nil
59-
case apps.Kind("StatefulSet"):
60-
return &StatefulSetHistoryViewer{c}, nil
61-
case extensionsv1beta1.SchemeGroupVersion.WithKind("DaemonSet").GroupKind(), apps.Kind("DaemonSet"):
62-
return &DaemonSetHistoryViewer{c}, nil
63-
}
64-
return nil, fmt.Errorf("no history viewer has been implemented for %q", kind)
79+
elem := kapps.GroupKindElement(kind)
80+
visitor := &HistoryVisitor{
81+
clientset: c,
82+
}
83+
84+
// Determine which HistoryViewer we need here
85+
err := elem.Accept(visitor)
86+
87+
if err != nil {
88+
return nil, fmt.Errorf("error retrieving history for %q, %v", kind.String(), err)
89+
}
90+
91+
if visitor.result == nil {
92+
return nil, fmt.Errorf("no history viewer has been implemented for %q", kind.String())
93+
}
94+
95+
return visitor.result, nil
6596
}
6697

6798
type DeploymentHistoryViewer struct {

pkg/kubectl/history_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kubectl
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"k8s.io/client-go/kubernetes/fake"
25+
)
26+
27+
var historytests = map[schema.GroupKind]reflect.Type{
28+
{Group: "apps", Kind: "DaemonSet"}: reflect.TypeOf(&DaemonSetHistoryViewer{}),
29+
{Group: "apps", Kind: "StatefulSet"}: reflect.TypeOf(&StatefulSetHistoryViewer{}),
30+
{Group: "apps", Kind: "Deployment"}: reflect.TypeOf(&DeploymentHistoryViewer{}),
31+
}
32+
33+
func TestHistoryViewerFor(t *testing.T) {
34+
fakeClientset := &fake.Clientset{}
35+
36+
for kind, expectedType := range historytests {
37+
result, err := HistoryViewerFor(kind, fakeClientset)
38+
if err != nil {
39+
t.Fatalf("error getting HistoryViewer for a %v: %v", kind.String(), err)
40+
}
41+
42+
if reflect.TypeOf(result) != expectedType {
43+
t.Fatalf("unexpected output type (%v was expected but got %v)", expectedType, reflect.TypeOf(result))
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)