Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor HistoryViewerFor to use Visitor design pattern #55257

Merged
merged 1 commit into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/kubectl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_test(
"deployment_test.go",
"env_file_test.go",
"generate_test.go",
"history_test.go",
"namespace_test.go",
"pdb_test.go",
"quota_test.go",
Expand Down Expand Up @@ -133,6 +134,7 @@ go_library(
"//pkg/controller/deployment/util:go_default_library",
"//pkg/controller/statefulset:go_default_library",
"//pkg/credentialprovider:go_default_library",
"//pkg/kubectl/apps:go_default_library",
"//pkg/kubectl/resource:go_default_library",
"//pkg/kubectl/util:go_default_library",
"//pkg/kubectl/util/hash:go_default_library",
Expand Down
51 changes: 41 additions & 10 deletions pkg/kubectl/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import (
clientextv1beta1 "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
"k8s.io/kubernetes/pkg/api"
apiv1 "k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/apis/apps"
deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
kapps "k8s.io/kubernetes/pkg/kubectl/apps"
sliceutil "k8s.io/kubernetes/pkg/kubectl/util/slice"
printersinternal "k8s.io/kubernetes/pkg/printers/internalversion"
)
Expand All @@ -52,16 +52,47 @@ type HistoryViewer interface {
ViewHistory(namespace, name string, revision int64) (string, error)
}

type HistoryVisitor struct {
clientset kubernetes.Interface
result HistoryViewer
}

func (v *HistoryVisitor) VisitDeployment(elem kapps.GroupKindElement) {
v.result = &DeploymentHistoryViewer{v.clientset}
}

func (v *HistoryVisitor) VisitStatefulSet(kind kapps.GroupKindElement) {
v.result = &StatefulSetHistoryViewer{v.clientset}
}

func (v *HistoryVisitor) VisitDaemonSet(kind kapps.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) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that we have to add these dummy functions for the resources that history viewer don't/won't support. It seems unavoidable if we choose to use visitor here. @pwittrock


// HistoryViewerFor returns an implementation of HistoryViewer interface for the given schema kind
func HistoryViewerFor(kind schema.GroupKind, c kubernetes.Interface) (HistoryViewer, error) {
switch kind {
case extensionsv1beta1.SchemeGroupVersion.WithKind("Deployment").GroupKind(), apps.Kind("Deployment"):
return &DeploymentHistoryViewer{c}, nil
case apps.Kind("StatefulSet"):
return &StatefulSetHistoryViewer{c}, nil
case extensionsv1beta1.SchemeGroupVersion.WithKind("DaemonSet").GroupKind(), apps.Kind("DaemonSet"):
return &DaemonSetHistoryViewer{c}, nil
}
return nil, fmt.Errorf("no history viewer has been implemented for %q", kind)
elem := kapps.GroupKindElement(kind)
visitor := &HistoryVisitor{
clientset: c,
}

// Determine which HistoryViewer we need here
err := elem.Accept(visitor)

if err != nil {
return nil, fmt.Errorf("error retrieving history for %q, %v", kind.String(), err)
}

if visitor.result == nil {
return nil, fmt.Errorf("no history viewer has been implemented for %q", kind.String())
}

return visitor.result, nil
}

type DeploymentHistoryViewer struct {
Expand Down
46 changes: 46 additions & 0 deletions pkg/kubectl/history_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2017 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 kubectl

import (
"reflect"
"testing"

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/fake"
)

var historytests = map[schema.GroupKind]reflect.Type{
{Group: "apps", Kind: "DaemonSet"}: reflect.TypeOf(&DaemonSetHistoryViewer{}),
{Group: "apps", Kind: "StatefulSet"}: reflect.TypeOf(&StatefulSetHistoryViewer{}),
{Group: "apps", Kind: "Deployment"}: reflect.TypeOf(&DeploymentHistoryViewer{}),
}

func TestHistoryViewerFor(t *testing.T) {
fakeClientset := &fake.Clientset{}

for kind, expectedType := range historytests {
result, err := HistoryViewerFor(kind, fakeClientset)
if err != nil {
t.Fatalf("error getting HistoryViewer for a %v: %v", kind.String(), err)
}

if reflect.TypeOf(result) != expectedType {
t.Fatalf("unexpected output type (%v was expected but got %v)", expectedType, reflect.TypeOf(result))
}
}
}