From 0ddd6df6367b1ae4c85a5a11ac14ba825dbcb5c6 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Thu, 31 Oct 2024 11:14:16 -0400 Subject: [PATCH] Use -n option for in-cluster rest config Fixes https://github.com/submariner-io/subctl/issues/324 Signed-off-by: Tom Pantelis --- internal/restconfig/producer_test.go | 83 ++++++++++++++++++++++++++-- internal/restconfig/restconfig.go | 18 +++--- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/internal/restconfig/producer_test.go b/internal/restconfig/producer_test.go index 8a0033e6..98576164 100644 --- a/internal/restconfig/producer_test.go +++ b/internal/restconfig/producer_test.go @@ -55,6 +55,7 @@ const ( user1Name = "user1" user2Name = "user2" defaultNS = "default-ns" + specifiedNS = "specified-ns" ) var ( @@ -187,7 +188,6 @@ func testRunOnSelectedContext() { }) It("should use the specified namespace", func() { - specifiedNS := "specified-ns" Expect(t.flags.Set(clientcmd.FlagNamespace, specifiedNS)).To(Succeed()) err := t.producer.RunOnSelectedContext(func(_ *cluster.Info, namespace string, _ reporter.Interface) error { @@ -244,9 +244,11 @@ func testRunOnSelectedContext() { t.producer.WithInClusterFlag() }) - It("should use the in-cluster config", func() { + JustBeforeEach(func() { Expect(t.flags.Set(restconfig.InCluster, "true")).To(Succeed()) + }) + It("should use the in-cluster config", func() { err := t.producer.RunOnSelectedContext(func(info *cluster.Info, namespace string, _ reporter.Interface) error { t.actualProcessed++ @@ -258,6 +260,27 @@ func testRunOnSelectedContext() { Expect(err).To(Succeed()) }) + + Context("and the namespace flag is specified", func() { + BeforeEach(func() { + t.producer.WithNamespace() + }) + + It("should use the specified namespace", func() { + Expect(t.flags.Set(clientcmd.FlagNamespace, specifiedNS)).To(Succeed()) + + err := t.producer.RunOnSelectedContext(func(info *cluster.Info, namespace string, _ reporter.Interface) error { + t.actualProcessed++ + + Expect(info.Name).To(Equal(restconfig.InCluster)) + Expect(namespace).To(Equal(specifiedNS)) + + return nil + }, reporter.Stdout()) + + Expect(err).To(Succeed()) + }) + }) }) When("there's no current context configured", func() { @@ -350,7 +373,6 @@ func testRunOnSelectedContexts() { }) It("should use the specified namespace", func() { - specifiedNS := "specified-ns" Expect(t.flags.Set(clientcmd.FlagNamespace, specifiedNS)).To(Succeed()) _, err := t.producer.RunOnSelectedContexts(func(_ []*cluster.Info, namespaces []string, _ reporter.Interface) error { @@ -392,6 +414,10 @@ func testRunOnSelectedContexts() { t.producer.WithInClusterFlag() }) + JustBeforeEach(func() { + Expect(t.flags.Set(restconfig.InCluster, "true")).To(Succeed()) + }) + It("should use the in-cluster config", func() { Expect(t.flags.Set(restconfig.InCluster, "true")).To(Succeed()) @@ -406,6 +432,29 @@ func testRunOnSelectedContexts() { Expect(err).To(Succeed()) }) + + Context("and the namespace flag is specified", func() { + BeforeEach(func() { + t.producer.WithNamespace() + }) + + It("should use the specified namespace", func() { + Expect(t.flags.Set(clientcmd.FlagNamespace, specifiedNS)).To(Succeed()) + + _, err := t.producer.RunOnSelectedContexts( + func(clusterInfos []*cluster.Info, namespaces []string, _ reporter.Interface) error { + t.actualProcessed++ + + Expect(clusterInfos).To(HaveLen(1)) + Expect(clusterInfos[0].Name).To(Equal(restconfig.InCluster)) + Expect(namespaces[0]).To(Equal(specifiedNS)) + + return nil + }, reporter.Stdout()) + + Expect(err).To(Succeed()) + }) + }) }) When("no contexts are specified", func() { @@ -552,13 +601,14 @@ func testRunOnAllContexts() { When("the in-cluster flag is specified", func() { BeforeEach(func() { t.producer.WithInClusterFlag() + t.expectedProcessed = 1 }) - It("should use the in-cluster config", func() { + JustBeforeEach(func() { Expect(t.flags.Set(restconfig.InCluster, "true")).To(Succeed()) + }) - t.expectedProcessed = 1 - + It("should use the in-cluster config", func() { err := t.producer.RunOnAllContexts(func(info *cluster.Info, namespace string, _ reporter.Interface) error { t.actualProcessed++ @@ -570,6 +620,27 @@ func testRunOnAllContexts() { Expect(err).To(Succeed()) }) + + Context("and the namespace flag is specified", func() { + BeforeEach(func() { + t.producer.WithNamespace() + }) + + It("should use the specified namespace", func() { + Expect(t.flags.Set(clientcmd.FlagNamespace, specifiedNS)).To(Succeed()) + + err := t.producer.RunOnAllContexts(func(info *cluster.Info, namespace string, _ reporter.Interface) error { + t.actualProcessed++ + + Expect(info.Name).To(Equal(restconfig.InCluster)) + Expect(namespace).To(Equal(namespace)) + + return nil + }, reporter.Stdout()) + + Expect(err).To(Succeed()) + }) + }) }) } diff --git a/internal/restconfig/restconfig.go b/internal/restconfig/restconfig.go index 94b29f52..bf09aa2d 100644 --- a/internal/restconfig/restconfig.go +++ b/internal/restconfig/restconfig.go @@ -189,7 +189,7 @@ type PerContextFn func(clusterInfo *cluster.Info, namespace string, status repor // RunOnSelectedContext runs the given function on the selected context. func (rcp *Producer) RunOnSelectedContext(function PerContextFn, status reporter.Interface) error { if rcp.inCluster { - return runInCluster(function, status) + return rcp.runInCluster(function, status) } if rcp.defaultClientConfig == nil { @@ -239,7 +239,7 @@ func createClusterInfo(clientConfig clientcmd.ClientConfig, overrides *clientcmd return cluster.NewInfo(restConfig.ClusterName, restConfig.Config) //nolint:wrapcheck // No need to wrap } -func runInCluster(function PerContextFn, status reporter.Interface) error { +func (rcp *Producer) runInCluster(function PerContextFn, status reporter.Interface) error { restConfig, err := GetInClusterConfig() if err != nil { return status.Error(err, "error retrieving the in-cluster configuration") @@ -251,9 +251,13 @@ func runInCluster(function PerContextFn, status reporter.Interface) error { return status.Error(err, "error building the cluster.Info for the in-cluster configuration") } - // In-cluster configurations don't specify a namespace, use the default - // When using the in-cluster configuration, that's the only configuration we want - return function(clusterInfo, "", status) + namespace := "" + + if rcp.defaultClientConfig != nil { + namespace = rcp.defaultClientConfig.overrides.Context.Namespace + } + + return function(clusterInfo, namespace, status) } // RunOnSelectedPrefixedContext runs the given function on the selected prefixed context. @@ -305,7 +309,7 @@ func (rcp *Producer) RunOnSelectedPrefixedContext(prefix string, function PerCon // Returns true if there was at least one selected context, false otherwise. func (rcp *Producer) RunOnSelectedContexts(function AllContextFn, status reporter.Interface) (bool, error) { if rcp.inCluster { - return true, runInCluster(func(clusterInfo *cluster.Info, namespace string, status reporter.Interface) error { + return true, rcp.runInCluster(func(clusterInfo *cluster.Info, namespace string, status reporter.Interface) error { return function([]*cluster.Info{clusterInfo}, []string{namespace}, status) }, status) } @@ -353,7 +357,7 @@ func (rcp *Producer) RunOnSelectedContexts(function AllContextFn, status reporte // Returns an error if no contexts are found. func (rcp *Producer) RunOnAllContexts(function PerContextFn, status reporter.Interface) error { if rcp.inCluster { - return runInCluster(function, status) + return rcp.runInCluster(function, status) } if rcp.defaultClientConfig == nil {