Skip to content

Commit

Permalink
Use -n option for in-cluster rest config
Browse files Browse the repository at this point in the history
Fixes #324

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Nov 11, 2024
1 parent 4048c66 commit 8233d98
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 13 deletions.
83 changes: 77 additions & 6 deletions internal/restconfig/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const (
user1Name = "user1"
user2Name = "user2"
defaultNS = "default-ns"
specifiedNS = "specified-ns"
)

var (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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++

Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())

Expand All @@ -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() {
Expand Down Expand Up @@ -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++

Expand All @@ -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())
})
})
})
}

Expand Down
18 changes: 11 additions & 7 deletions internal/restconfig/restconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8233d98

Please sign in to comment.