From 120b21b11f1d37ab73dc7971ea99f2509e3fc01f Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Wed, 24 Jul 2019 20:02:05 -0700 Subject: [PATCH] Move pkg/kubectl/apps to staging Kubernetes-commit: 1b52c6180baa79e6fcbde911dce830c5266108dd --- go.mod | 19 ++-- go.sum | 4 - pkg/apps/apps_suite_test.go | 49 +++++++++ pkg/apps/kind_visitor.go | 75 ++++++++++++++ pkg/apps/kind_visitor_test.go | 185 ++++++++++++++++++++++++++++++++++ 5 files changed, 320 insertions(+), 12 deletions(-) create mode 100644 pkg/apps/apps_suite_test.go create mode 100644 pkg/apps/kind_visitor.go create mode 100644 pkg/apps/kind_visitor_test.go diff --git a/go.mod b/go.mod index 7297963aa..c156465d7 100644 --- a/go.mod +++ b/go.mod @@ -11,16 +11,18 @@ require ( github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de github.com/mitchellh/go-wordwrap v1.0.0 + github.com/onsi/ginkgo v1.8.0 + github.com/onsi/gomega v1.5.0 github.com/russross/blackfriday v1.5.2 github.com/sirupsen/logrus v1.4.2 // indirect github.com/spf13/cobra v0.0.4 github.com/spf13/pflag v1.0.3 golang.org/x/sys v0.0.0-20190616124812-15dcb6c0061f gotest.tools v2.2.0+incompatible // indirect - k8s.io/api v0.0.0-20190722141453-b90922c02518 - k8s.io/apimachinery v0.0.0-20190719140911-bfcf53abc9f8 - k8s.io/cli-runtime v0.0.0-20190717024643-59adbd30f884 - k8s.io/client-go v0.0.0-20190723023103-f16ff99a1d2d + k8s.io/api v0.0.0 + k8s.io/apimachinery v0.0.0 + k8s.io/cli-runtime v0.0.0 + k8s.io/client-go v0.0.0 k8s.io/klog v0.3.1 k8s.io/utils v0.0.0-20190607212802-c55fbcfc754a ) @@ -32,8 +34,9 @@ replace ( golang.org/x/sys => golang.org/x/sys v0.0.0-20190209173611-3b5209105503 golang.org/x/text => golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db golang.org/x/tools => golang.org/x/tools v0.0.0-20190313210603-aa82965741a9 - k8s.io/api => k8s.io/api v0.0.0-20190722141453-b90922c02518 - k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20190719140911-bfcf53abc9f8 - k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20190717024643-59adbd30f884 - k8s.io/client-go => k8s.io/client-go v0.0.0-20190723023103-f16ff99a1d2d + k8s.io/api => ../api + k8s.io/apimachinery => ../apimachinery + k8s.io/cli-runtime => ../cli-runtime + k8s.io/client-go => ../client-go + k8s.io/kubectl => ../kubectl ) diff --git a/go.sum b/go.sum index 07a4121a3..bf7aa85fb 100644 --- a/go.sum +++ b/go.sum @@ -179,10 +179,6 @@ gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= -k8s.io/api v0.0.0-20190722141453-b90922c02518/go.mod h1:1O0xzX/RAtnm7l+5VEUxZ1ysO2ghatfq/OZED4zM9kA= -k8s.io/apimachinery v0.0.0-20190719140911-bfcf53abc9f8/go.mod h1:sBJWIJZfxLhp7mRsRyuAE/NfKTr3kXGR1iaqg8O0gJo= -k8s.io/cli-runtime v0.0.0-20190717024643-59adbd30f884/go.mod h1:+ruWDTqAuYQj3fl0lA6wTha/YDI+opreeAT7WvYWgmY= -k8s.io/client-go v0.0.0-20190723023103-f16ff99a1d2d/go.mod h1:R4w0jH5cG9/E/GhYNzqN71DTLlyxJRwpceJYcGNKPGM= k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk= diff --git a/pkg/apps/apps_suite_test.go b/pkg/apps/apps_suite_test.go new file mode 100644 index 000000000..8d0f7e5a1 --- /dev/null +++ b/pkg/apps/apps_suite_test.go @@ -0,0 +1,49 @@ +/* +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/apps/kind_visitor.go b/pkg/apps/kind_visitor.go new file mode 100644 index 000000000..931c63b18 --- /dev/null +++ b/pkg/apps/kind_visitor.go @@ -0,0 +1,75 @@ +/* +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 schema.GroupKind + +// 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: + 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 +} diff --git a/pkg/apps/kind_visitor_test.go b/pkg/apps/kind_visitor_test.go new file mode 100644 index 000000000..9a1f69e45 --- /dev/null +++ b/pkg/apps/kind_visitor_test.go @@ -0,0 +1,185 @@ +/* +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/kubectl/pkg/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{ + Kind: "DaemonSet", + Group: "apps", + } + Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) + Expect(visitor.visits).To(Equal(map[string]int{ + "DaemonSet": 1, + })) + + kind = apps.GroupKindElement{ + 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{ + Kind: "Deployment", + Group: "apps", + } + Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) + Expect(visitor.visits).To(Equal(map[string]int{ + "Deployment": 1, + })) + + kind = apps.GroupKindElement{ + 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{ + 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{ + Kind: "Pod", + Group: "", + } + Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) + Expect(visitor.visits).To(Equal(map[string]int{ + "Pod": 1, + })) + + kind = apps.GroupKindElement{ + 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{ + Kind: "ReplicationController", + Group: "", + } + Expect(kind.Accept(visitor)).ShouldNot(HaveOccurred()) + Expect(visitor.visits).To(Equal(map[string]int{ + "ReplicationController": 1, + })) + + kind = apps.GroupKindElement{ + 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{ + 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{ + 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{ + 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{ + 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]++ } + +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) }