From 20faf5d931e4145fe0b106eab750e95a0376047f Mon Sep 17 00:00:00 2001 From: ymqytw Date: Mon, 29 Jan 2018 12:55:36 -0800 Subject: [PATCH] delete dead code --- pkg/kinflate/apps/apps_suite_test.go | 49 ---- pkg/kinflate/apps/kind_visitor.go | 95 -------- pkg/kinflate/apps/kind_visitor_test.go | 212 ------------------ .../pod_template_visitor.go | 84 ------- 4 files changed, 440 deletions(-) delete mode 100644 pkg/kinflate/apps/apps_suite_test.go delete mode 100644 pkg/kinflate/apps/kind_visitor.go delete mode 100644 pkg/kinflate/apps/kind_visitor_test.go delete mode 100644 pkg/kinflate/pod_template_visitor/pod_template_visitor.go diff --git a/pkg/kinflate/apps/apps_suite_test.go b/pkg/kinflate/apps/apps_suite_test.go deleted file mode 100644 index 8d0f7e5a1..000000000 --- a/pkg/kinflate/apps/apps_suite_test.go +++ /dev/null @@ -1,49 +0,0 @@ -/* -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 apps_test - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/ginkgo/config" - . "github.com/onsi/ginkgo/types" - . "github.com/onsi/gomega" - - "fmt" - "testing" -) - -func TestApps(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecsWithDefaultAndCustomReporters(t, "Apps Suite", []Reporter{newlineReporter{}}) -} - -// Print a newline after the default newlineReporter due to issue -// https://github.com/jstemmer/go-junit-report/issues/31 -type newlineReporter struct{} - -func (newlineReporter) SpecSuiteWillBegin(config GinkgoConfigType, summary *SuiteSummary) {} - -func (newlineReporter) BeforeSuiteDidRun(setupSummary *SetupSummary) {} - -func (newlineReporter) AfterSuiteDidRun(setupSummary *SetupSummary) {} - -func (newlineReporter) SpecWillRun(specSummary *SpecSummary) {} - -func (newlineReporter) SpecDidComplete(specSummary *SpecSummary) {} - -// SpecSuiteDidEnd Prints a newline between "35 Passed | 0 Failed | 0 Pending | 0 Skipped" and "--- PASS:" -func (newlineReporter) SpecSuiteDidEnd(summary *SuiteSummary) { fmt.Printf("\n") } diff --git a/pkg/kinflate/apps/kind_visitor.go b/pkg/kinflate/apps/kind_visitor.go deleted file mode 100644 index 756578807..000000000 --- a/pkg/kinflate/apps/kind_visitor.go +++ /dev/null @@ -1,95 +0,0 @@ -/* -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 apps - -import ( - "fmt" - - "k8s.io/apimachinery/pkg/runtime/schema" -) - -// KindVisitor is used with GroupKindElement to call a particular function depending on the -// Kind of a schema.GroupKind -type KindVisitor interface { - VisitDaemonSet(kind GroupKindElement) - VisitDeployment(kind GroupKindElement) - VisitJob(kind GroupKindElement) - VisitPod(kind GroupKindElement) - VisitReplicaSet(kind GroupKindElement) - VisitReplicationController(kind GroupKindElement) - VisitStatefulSet(kind GroupKindElement) - VisitCronJob(kind GroupKindElement) -} - -// GroupKindElement defines a Kubernetes API group elem -type GroupKindElement struct { - schema.GroupKind - // If true, ignore the error when the kind is not a workload type. - IgnoreNonWorkloadError bool -} - -// Accept calls the Visit method on visitor that corresponds to elem's Kind -func (elem GroupKindElement) Accept(visitor KindVisitor) error { - switch { - case elem.GroupMatch("apps", "extensions") && elem.Kind == "DaemonSet": - visitor.VisitDaemonSet(elem) - case elem.GroupMatch("apps", "extensions") && elem.Kind == "Deployment": - visitor.VisitDeployment(elem) - case elem.GroupMatch("batch") && elem.Kind == "Job": - visitor.VisitJob(elem) - case elem.GroupMatch("", "core") && elem.Kind == "Pod": - visitor.VisitPod(elem) - case elem.GroupMatch("apps", "extensions") && elem.Kind == "ReplicaSet": - visitor.VisitReplicaSet(elem) - case elem.GroupMatch("", "core") && elem.Kind == "ReplicationController": - visitor.VisitReplicationController(elem) - case elem.GroupMatch("apps") && elem.Kind == "StatefulSet": - visitor.VisitStatefulSet(elem) - case elem.GroupMatch("batch") && elem.Kind == "CronJob": - visitor.VisitCronJob(elem) - default: - if !elem.IgnoreNonWorkloadError { - return fmt.Errorf("no visitor method exists for %v", elem) - } - } - return nil -} - -// GroupMatch returns true if and only if elem's group matches one -// of the group arguments -func (elem GroupKindElement) GroupMatch(groups ...string) bool { - for _, g := range groups { - if elem.Group == g { - return true - } - } - return false -} - -// NoOpKindVisitor implements KindVisitor with no-op functions. -type NoOpKindVisitor struct{} - -var _ KindVisitor = &NoOpKindVisitor{} - -func (*NoOpKindVisitor) VisitDaemonSet(kind GroupKindElement) {} -func (*NoOpKindVisitor) VisitDeployment(kind GroupKindElement) {} -func (*NoOpKindVisitor) VisitJob(kind GroupKindElement) {} -func (*NoOpKindVisitor) VisitPod(kind GroupKindElement) {} -func (*NoOpKindVisitor) VisitReplicaSet(kind GroupKindElement) {} -func (*NoOpKindVisitor) VisitReplicationController(kind GroupKindElement) {} -func (*NoOpKindVisitor) VisitStatefulSet(kind GroupKindElement) {} -func (*NoOpKindVisitor) VisitCronJob(kind GroupKindElement) {} diff --git a/pkg/kinflate/apps/kind_visitor_test.go b/pkg/kinflate/apps/kind_visitor_test.go deleted file mode 100644 index e9d2e7964..000000000 --- a/pkg/kinflate/apps/kind_visitor_test.go +++ /dev/null @@ -1,212 +0,0 @@ -/* -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 apps_test - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/kubectl/pkg/kinflate/apps" -) - -var _ = Describe("When KindVisitor accepts a GroupKind", func() { - - var visitor *TestKindVisitor - - BeforeEach(func() { - visitor = &TestKindVisitor{map[string]int{}} - }) - - It("should Visit DaemonSet iff the Kind is a DaemonSet", func() { - kind := apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "DaemonSet", - Group: "apps", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "DaemonSet": 1, - })) - - kind = apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "DaemonSet", - Group: "extensions", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "DaemonSet": 2, - })) - }) - - It("should Visit Deployment iff the Kind is a Deployment", func() { - kind := apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "Deployment", - Group: "apps", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "Deployment": 1, - })) - - kind = apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "Deployment", - Group: "extensions", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "Deployment": 2, - })) - }) - - It("should Visit Job iff the Kind is a Job", func() { - kind := apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "Job", - Group: "batch", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "Job": 1, - })) - - }) - - It("should Visit Pod iff the Kind is a Pod", func() { - kind := apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "Pod", - Group: "", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "Pod": 1, - })) - - kind = apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "Pod", - Group: "core", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "Pod": 2, - })) - }) - - It("should Visit ReplicationController iff the Kind is a ReplicationController", func() { - kind := apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "ReplicationController", - Group: "", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "ReplicationController": 1, - })) - - kind = apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "ReplicationController", - Group: "core", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "ReplicationController": 2, - })) - }) - - It("should Visit ReplicaSet iff the Kind is a ReplicaSet", func() { - kind := apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "ReplicaSet", - Group: "extensions", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "ReplicaSet": 1, - })) - }) - - It("should Visit StatefulSet iff the Kind is a StatefulSet", func() { - kind := apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "StatefulSet", - Group: "apps", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "StatefulSet": 1, - })) - }) - - It("should Visit CronJob iff the Kind is a CronJob", func() { - kind := apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "CronJob", - Group: "batch", - }, - } - Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{ - "CronJob": 1, - })) - }) - - It("should give an error if the Kind is unknown", func() { - kind := apps.GroupKindElement{ - GroupKind: schema.GroupKind{ - Kind: "Unknown", - Group: "apps", - }, - } - Expect(kind.Accept(visitor)).Should(HaveOccurred()) - Expect(visitor.visits).To(Equal(map[string]int{})) - }) -}) - -// TestKindVisitor increments a value each time a Visit method was called -type TestKindVisitor struct { - visits map[string]int -} - -var _ apps.KindVisitor = &TestKindVisitor{} - -func (t *TestKindVisitor) Visit(kind apps.GroupKindElement) { t.visits[kind.Kind] += 1 } - -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) } diff --git a/pkg/kinflate/pod_template_visitor/pod_template_visitor.go b/pkg/kinflate/pod_template_visitor/pod_template_visitor.go deleted file mode 100644 index 88db555f7..000000000 --- a/pkg/kinflate/pod_template_visitor/pod_template_visitor.go +++ /dev/null @@ -1,84 +0,0 @@ -/* -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 pod_template_visitor - -import ( - "fmt" - - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - kapps "k8s.io/kubectl/pkg/kinflate/apps" -) - -type PodTemplateSpecVisitor struct { - Object *unstructured.Unstructured - MungeFn func(podTemplateSpec map[string]interface{}) error - Err error -} - -var _ kapps.KindVisitor = &PodTemplateSpecVisitor{} - -func (v *PodTemplateSpecVisitor) VisitDeployment(kind kapps.GroupKindElement) { - v.Err = v.mungePodTemplateSpec([]string{"spec", "template"}) -} - -func (v *PodTemplateSpecVisitor) VisitStatefulSet(kind kapps.GroupKindElement) { - v.Err = v.mungePodTemplateSpec([]string{"spec", "template"}) -} - -func (v *PodTemplateSpecVisitor) VisitDaemonSet(kind kapps.GroupKindElement) { - v.Err = v.mungePodTemplateSpec([]string{"spec", "template"}) -} - -func (v *PodTemplateSpecVisitor) VisitJob(kind kapps.GroupKindElement) { - v.Err = v.mungePodTemplateSpec([]string{"spec", "template"}) -} - -func (v *PodTemplateSpecVisitor) VisitReplicaSet(kind kapps.GroupKindElement) { - v.Err = v.mungePodTemplateSpec([]string{"spec", "template"}) -} - -func (v *PodTemplateSpecVisitor) VisitPod(kind kapps.GroupKindElement) {} - -func (v *PodTemplateSpecVisitor) VisitReplicationController(kind kapps.GroupKindElement) { - v.Err = v.mungePodTemplateSpec([]string{"spec", "template"}) -} - -func (v *PodTemplateSpecVisitor) VisitCronJob(kind kapps.GroupKindElement) { - v.Err = v.mungePodTemplateSpec([]string{"spec", "jobTemplate", "spec", "template"}) -} - -func walkMapPath(start map[string]interface{}, path []string) (map[string]interface{}, error) { - finish := start - for i := 0; i < len(path); i++ { - var ok bool - finish, ok = finish[path[i]].(map[string]interface{}) - if !ok { - return nil, fmt.Errorf("key:%s of path:%v not found in map:%v", path[i], path, start) - } - } - - return finish, nil -} - -func (v *PodTemplateSpecVisitor) mungePodTemplateSpec(pathToPodTemplateSpec []string) error { - obj := v.Object.UnstructuredContent() - podTemplateSpec, err := walkMapPath(obj, pathToPodTemplateSpec) - if err != nil { - return err - } - return v.MungeFn(podTemplateSpec) -}