-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OCM-7266 | feat: Added support for delete of KubeletConfig for HCP cl…
…usters
- Loading branch information
Showing
12 changed files
with
356 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package kubeletconfig | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" | ||
. "github.com/openshift-online/ocm-sdk-go/testing" | ||
|
||
. "github.com/openshift/rosa/pkg/kubeletconfig" | ||
"github.com/openshift/rosa/pkg/output" | ||
. "github.com/openshift/rosa/pkg/test" | ||
) | ||
|
||
var _ = Describe("delete kubeletconfig", func() { | ||
|
||
It("Correctly builds the command", func() { | ||
cmd := NewDeleteKubeletConfigCommand() | ||
Expect(cmd).NotTo(BeNil()) | ||
|
||
Expect(cmd.Use).To(Equal(use)) | ||
Expect(cmd.Short).To(Equal(short)) | ||
Expect(cmd.Long).To(Equal(long)) | ||
Expect(cmd.Args).NotTo(BeNil()) | ||
Expect(cmd.Run).NotTo(BeNil()) | ||
|
||
Expect(cmd.Flags().Lookup("cluster")).NotTo(BeNil()) | ||
Expect(cmd.Flags().Lookup(PodPidsLimitOption)).To(BeNil()) | ||
Expect(cmd.Flags().Lookup(NameOption)).NotTo(BeNil()) | ||
}) | ||
|
||
Context("Delete KubeletConfig Runner", func() { | ||
|
||
var t *TestingRuntime | ||
|
||
BeforeEach(func() { | ||
t = NewTestRuntime() | ||
output.SetOutput("") | ||
}) | ||
|
||
AfterEach(func() { | ||
output.SetOutput("") | ||
}) | ||
|
||
It("Returns an error if the cluster does not exist", func() { | ||
t.ApiServer.AppendHandlers(RespondWithJSON(http.StatusOK, FormatClusterList(make([]*cmv1.Cluster, 0)))) | ||
t.SetCluster("cluster", nil) | ||
|
||
runner := DeleteKubeletConfigRunner(NewKubeletConfigOptions()) | ||
err := runner(context.Background(), t.RosaRuntime, nil, nil) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To( | ||
Equal("There is no cluster with identifier or name 'cluster'")) | ||
}) | ||
|
||
It("Deletes KubeletConfig by name for HCP Clusters", func() { | ||
|
||
cluster := MockCluster(func(c *cmv1.ClusterBuilder) { | ||
c.State(cmv1.ClusterStateReady) | ||
b := cmv1.HypershiftBuilder{} | ||
b.Enabled(true) | ||
c.Hypershift(&b) | ||
}) | ||
|
||
kubeletConfig := MockKubeletConfig(func(k *cmv1.KubeletConfigBuilder) { | ||
k.ID("testing").PodPidsLimit(5000).Name("testing") | ||
}) | ||
|
||
t.ApiServer.AppendHandlers( | ||
RespondWithJSON( | ||
http.StatusOK, FormatClusterList([]*cmv1.Cluster{cluster}))) | ||
t.ApiServer.AppendHandlers( | ||
RespondWithJSON(http.StatusOK, FormatKubeletConfigList([]*cmv1.KubeletConfig{kubeletConfig}))) | ||
t.ApiServer.RouteToHandler(http.MethodDelete, | ||
fmt.Sprintf("/api/clusters_mgmt/v1/clusters/%s/kubelet_configs/%s", cluster.ID(), kubeletConfig.ID()), | ||
RespondWithJSON(http.StatusOK, FormatResource(kubeletConfig))) | ||
t.SetCluster("cluster", cluster) | ||
|
||
options := NewKubeletConfigOptions() | ||
options.Name = "testing" | ||
|
||
runner := DeleteKubeletConfigRunner(options) | ||
t.StdOutReader.Record() | ||
|
||
err := runner(context.Background(), t.RosaRuntime, nil, nil) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
stdOut, _ := t.StdOutReader.Read() | ||
Expect(stdOut).To(Equal("INFO: Successfully deleted KubeletConfig for cluster 'cluster'\n")) | ||
}) | ||
|
||
It("Fails to delete KubeletConfig by name for HCP Clusters if the KubeletConfig does not exist", 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.ApiServer.AppendHandlers( | ||
RespondWithJSON(http.StatusOK, FormatKubeletConfigList([]*cmv1.KubeletConfig{}))) | ||
t.SetCluster("cluster", cluster) | ||
|
||
options := NewKubeletConfigOptions() | ||
options.Name = "testing" | ||
|
||
runner := DeleteKubeletConfigRunner(options) | ||
|
||
err := runner(context.Background(), t.RosaRuntime, nil, nil) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("Failed to delete KubeletConfig for cluster 'cluster'")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package kubeletconfig | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestDeleteKubeletConfig(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Delete KubeletConfig Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package kubeletconfig | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/openshift/rosa/pkg/interactive/confirm" | ||
"github.com/openshift/rosa/pkg/rosa" | ||
) | ||
|
||
const ( | ||
promptMessage = "%s the KubeletConfig for cluster '%s' will cause all non-Control Plane " + | ||
"nodes to reboot. This may cause outages to your applications. Do you wish to continue?" | ||
abortMessage = "%s of KubeletConfig for cluster '%s' aborted." | ||
OperationDelete KubeletOperation = "delete" | ||
OperationEdit KubeletOperation = "edit" | ||
OperationCreate KubeletOperation = "create" | ||
) | ||
|
||
type KubeletOperation string | ||
|
||
var ( | ||
singularTense = map[KubeletOperation]string{ | ||
OperationEdit: "Edit", | ||
OperationDelete: "Delete", | ||
OperationCreate: "Create", | ||
} | ||
|
||
futureTense = map[KubeletOperation]string{ | ||
OperationEdit: "Editing", | ||
OperationDelete: "Deleting", | ||
OperationCreate: "Creating", | ||
} | ||
) | ||
|
||
func PromptUserToAcceptWorkerNodeReboot(operation KubeletOperation, r *rosa.Runtime) bool { | ||
if !confirm.ConfirmRaw(buildPromptMessage(operation, r.GetClusterKey())) { | ||
r.Reporter.Infof(buildAbortMessage(operation, r.GetClusterKey())) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func buildAbortMessage(operation KubeletOperation, clusterKey string) string { | ||
return fmt.Sprintf(abortMessage, singularTense[operation], clusterKey) | ||
} | ||
|
||
func buildPromptMessage(operation KubeletOperation, clusterKey string) string { | ||
return fmt.Sprintf(promptMessage, futureTense[operation], clusterKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package kubeletconfig | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("KubeletConfig Input", func() { | ||
It("Generates the abort message", func() { | ||
msg := buildAbortMessage(OperationCreate, "foo") | ||
Expect(msg).To(Equal("Create of KubeletConfig for cluster 'foo' aborted.")) | ||
}) | ||
|
||
It("Generates the prompt message", func() { | ||
msg := buildPromptMessage(OperationCreate, "foo") | ||
Expect(msg).To(Equal("Creating the KubeletConfig for cluster 'foo' will cause all non-Control Plane " + | ||
"nodes to reboot. This may cause outages to your applications. Do you wish to continue?")) | ||
}) | ||
}) |
Oops, something went wrong.