Skip to content

Commit

Permalink
OCM-8058 | fix: Ensure --name option is required for working with Kub…
Browse files Browse the repository at this point in the history
…eletConfigs on HCP clusters
  • Loading branch information
robpblake committed May 16, 2024
1 parent 0e00dea commit 89ab77e
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 19 deletions.
17 changes: 11 additions & 6 deletions cmd/create/kubeletconfig/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,19 @@ func CreateKubeletConfigRunner(options *KubeletConfigOptions) rosa.CommandRunner
return fmt.Errorf("A KubeletConfig for cluster '%s' already exists. "+
"You should edit it via 'rosa edit kubeletconfig'", clusterKey)
}
}
} else {
options.Name, err = PromptForName(options.Name)
if err != nil {
return err
}

name, err := PromptForName(options.Name, false)
if err != nil {
return nil
err = options.ValidateForHypershift()
if err != nil {
return err
}
}

requestedPids, err := ValidateOrPromptForRequestedPidsLimit(options.PodPidsLimit, clusterKey, nil, r)
options.PodPidsLimit, err = ValidateOrPromptForRequestedPidsLimit(options.PodPidsLimit, clusterKey, nil, r)
if err != nil {
return err
}
Expand All @@ -102,7 +107,7 @@ func CreateKubeletConfigRunner(options *KubeletConfigOptions) rosa.CommandRunner
}

r.Reporter.Debugf("Creating KubeletConfig for cluster '%s'", clusterKey)
kubeletConfigArgs := ocm.KubeletConfigArgs{PodPidsLimit: requestedPids, Name: name}
kubeletConfigArgs := ocm.KubeletConfigArgs{PodPidsLimit: options.PodPidsLimit, Name: options.Name}

_, err = r.OCMClient.CreateKubeletConfig(cluster.ID(), kubeletConfigArgs)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions cmd/create/kubeletconfig/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift-online/ocm-sdk-go/testing"

"github.com/openshift/rosa/pkg/interactive"
. "github.com/openshift/rosa/pkg/kubeletconfig"
"github.com/openshift/rosa/pkg/output"
. "github.com/openshift/rosa/pkg/test"
Expand Down Expand Up @@ -39,10 +40,12 @@ var _ = Describe("create kubeletconfig", func() {
BeforeEach(func() {
t = NewTestRuntime()
output.SetOutput("")
interactive.SetEnabled(false)
})

AfterEach(func() {
output.SetOutput("")
interactive.SetEnabled(false)
})

It("Returns an error if the cluster does not exist", func() {
Expand Down Expand Up @@ -121,6 +124,30 @@ var _ = Describe("create kubeletconfig", func() {
ContainSubstring("Failed getting KubeletConfig for cluster 'cluster'"))
})

It("Returns an error if no name specified for HCP KubeletConfig", func() {
cluster := MockCluster(func(c *cmv1.ClusterBuilder) {
c.State(cmv1.ClusterStateReady)
b := cmv1.HypershiftBuilder{}
b.Enabled(true)
c.Hypershift(&b)

})

t.ApiServer.AppendHandlers(
testing.RespondWithJSON(
http.StatusOK, FormatClusterList([]*cmv1.Cluster{cluster})))
t.SetCluster("cluster", cluster)

options := NewKubeletConfigOptions()
options.PodPidsLimit = 10000

runner := CreateKubeletConfigRunner(options)

err := runner(context.Background(), t.RosaRuntime, nil, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("The --name flag is required for Hosted Control Plane clusters."))
})

It("Creates the KubeletConfig for HCP clusters", func() {
cluster := MockCluster(func(c *cmv1.ClusterBuilder) {
c.State(cmv1.ClusterStateReady)
Expand All @@ -143,6 +170,7 @@ var _ = Describe("create kubeletconfig", func() {

options := NewKubeletConfigOptions()
options.PodPidsLimit = 10000
options.Name = "test"

runner := CreateKubeletConfigRunner(options)
t.StdOutReader.Record()
Expand Down Expand Up @@ -176,6 +204,7 @@ var _ = Describe("create kubeletconfig", func() {

options := NewKubeletConfigOptions()
options.PodPidsLimit = 10000
options.Name = "test"

runner := CreateKubeletConfigRunner(options)

Expand Down
2 changes: 1 addition & 1 deletion cmd/describe/kubeletconfig/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func DescribeKubeletConfigRunner(options *KubeletConfigOptions) rosa.CommandRunn
var exists bool

if cluster.Hypershift().Enabled() {
options.Name, err = PromptForName(options.Name, true)
options.Name, err = PromptForName(options.Name)
if err != nil {
return err
}
Expand Down
24 changes: 24 additions & 0 deletions cmd/describe/kubeletconfig/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ var _ = Describe("Describe KubeletConfig", func() {
Equal("The KubeletConfig specified does not exist for cluster 'cluster'"))
})

It("Returns an error if no name specified for HCP KubeletConfig", func() {
cluster := MockCluster(func(c *cmv1.ClusterBuilder) {
c.State(cmv1.ClusterStateReady)
b := cmv1.HypershiftBuilder{}
b.Enabled(true)
c.Hypershift(&b)

})

t.ApiServer.AppendHandlers(
RespondWithJSON(
http.StatusOK, FormatClusterList([]*cmv1.Cluster{cluster})))
t.SetCluster("cluster", cluster)

options := NewKubeletConfigOptions()
options.PodPidsLimit = 10000

runner := DescribeKubeletConfigRunner(options)

err := runner(context.Background(), t.RosaRuntime, nil, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("The --name flag is required for Hosted Control Plane clusters."))
})

It("Returns an error if failing to read HCP KubeletConfig", func() {
cluster := MockCluster(func(c *cmv1.ClusterBuilder) {
c.State(cmv1.ClusterStateReady)
Expand Down
7 changes: 5 additions & 2 deletions cmd/dlt/kubeletconfig/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ func DeleteKubeletConfigRunner(options *KubeletConfigOptions) rosa.CommandRunner
}

if cluster.Hypershift().Enabled() {
options.Name, err = PromptForName(options.Name, true)
options.Name, err = PromptForName(options.Name)
if err != nil {
return err
}
err = options.ValidateForHypershift()
if err != nil {
return err
}
options.ValidateForHypershift()
err = r.OCMClient.DeleteKubeletConfigByName(ctx, cluster.ID(), options.Name)
} else {
err = r.OCMClient.DeleteKubeletConfig(ctx, cluster.ID())
Expand Down
27 changes: 27 additions & 0 deletions cmd/dlt/kubeletconfig/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
. "github.com/openshift-online/ocm-sdk-go/testing"

"github.com/openshift/rosa/pkg/interactive"
. "github.com/openshift/rosa/pkg/kubeletconfig"
"github.com/openshift/rosa/pkg/output"
. "github.com/openshift/rosa/pkg/test"
Expand Down Expand Up @@ -39,10 +40,12 @@ var _ = Describe("delete kubeletconfig", func() {
BeforeEach(func() {
t = NewTestRuntime()
output.SetOutput("")
interactive.SetEnabled(false)
})

AfterEach(func() {
output.SetOutput("")
interactive.SetEnabled(false)
})

It("Returns an error if the cluster does not exist", func() {
Expand All @@ -56,6 +59,30 @@ var _ = Describe("delete kubeletconfig", func() {
Equal("There is no cluster with identifier or name 'cluster'"))
})

It("Returns an error if no name specified for HCP KubeletConfig", func() {
cluster := MockCluster(func(c *cmv1.ClusterBuilder) {
c.State(cmv1.ClusterStateReady)
b := cmv1.HypershiftBuilder{}
b.Enabled(true)
c.Hypershift(&b)

})

t.ApiServer.AppendHandlers(
RespondWithJSON(
http.StatusOK, FormatClusterList([]*cmv1.Cluster{cluster})))
t.SetCluster("cluster", cluster)

options := NewKubeletConfigOptions()
options.PodPidsLimit = 10000

runner := DeleteKubeletConfigRunner(options)

err := runner(context.Background(), t.RosaRuntime, nil, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("The --name flag is required for Hosted Control Plane clusters."))
})

It("Deletes KubeletConfig by name for HCP Clusters", func() {

cluster := MockCluster(func(c *cmv1.ClusterBuilder) {
Expand Down
7 changes: 5 additions & 2 deletions cmd/edit/kubeletconfig/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ func EditKubeletConfigRunner(options *KubeletConfigOptions) rosa.CommandRunner {
var exists bool

if cluster.Hypershift().Enabled() {
options.Name, err = PromptForName(options.Name, true)
options.Name, err = PromptForName(options.Name)
if err != nil {
return err
}

options.ValidateForHypershift()
err = options.ValidateForHypershift()
if err != nil {
return err
}
kubeletconfig, exists, err = r.OCMClient.FindKubeletConfigByName(ctx, cluster.ID(), options.Name)
} else {
kubeletconfig, exists, err = r.OCMClient.GetClusterKubeletConfig(cluster.ID())
Expand Down
24 changes: 24 additions & 0 deletions cmd/edit/kubeletconfig/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ var _ = Describe("edit kubeletconfig", func() {
Equal("There is no cluster with identifier or name 'cluster'"))
})

It("Returns an error if no name specified for HCP KubeletConfig", func() {
cluster := MockCluster(func(c *cmv1.ClusterBuilder) {
c.State(cmv1.ClusterStateReady)
b := cmv1.HypershiftBuilder{}
b.Enabled(true)
c.Hypershift(&b)

})

t.ApiServer.AppendHandlers(
RespondWithJSON(
http.StatusOK, FormatClusterList([]*cmv1.Cluster{cluster})))
t.SetCluster("cluster", cluster)

options := NewKubeletConfigOptions()
options.PodPidsLimit = 10000

runner := EditKubeletConfigRunner(options)

err := runner(context.Background(), t.RosaRuntime, nil, nil)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("The --name flag is required for Hosted Control Plane clusters."))
})

It("Returns an error if the cluster is not ready", func() {

cluster := MockCluster(func(c *cmv1.ClusterBuilder) {
Expand Down
10 changes: 3 additions & 7 deletions pkg/kubeletconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,15 @@ func GetInteractiveInput(maxPidsLimit int, kubeletConfig *v1.KubeletConfig) inte
}
}

func PromptForName(requestedName string, nameRequired bool) (string, error) {
func PromptForName(requestedName string) (string, error) {

if requestedName == "" && nameRequired {
interactive.Enable()
}

if interactive.Enabled() {
if requestedName == "" && interactive.Enabled() {
return interactive.GetString(interactive.Input{
Question: InteractiveNameHelpPrompt,
Help: InteractiveNameHelp,
Options: nil,
Default: requestedName,
Required: false,
Required: true,
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/kubeletconfig/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const (
MaxUnsafePodPidsLimit = 3694303
NameOption = "name"
NameOptionDefaultValue = ""
NameOptionUsage = "Name of the KubeletConfig"
NameOptionUsage = "Name of the KubeletConfig (required for Hosted Control Plane clusters)"
PodPidsLimitOption = "pod-pids-limit"
PodPidsLimitOptionUsage = "Sets the requested pod_pids_limit for this KubeletConfig."
PodPidsLimitOptionDefaultValue = 0
Expand Down

0 comments on commit 89ab77e

Please sign in to comment.