Skip to content

Commit

Permalink
Merge pull request #2853 from bryan-cox/HOSTEDCP-1029
Browse files Browse the repository at this point in the history
HOSTEDCP-1029: Add HCP CLI Command to Destroy a Cluster on AWS
  • Loading branch information
openshift-merge-robot authored Aug 4, 2023
2 parents 35a6c16 + dfd3152 commit 2aa885e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 16 deletions.
6 changes: 3 additions & 3 deletions cmd/cluster/aws/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func NewCreateCommand(opts *core.CreateOptions) *cobra.Command {
}

if len(opts.CredentialSecretName) == 0 {
if err := isRequiredOption("aws-creds", opts.AWSPlatform.AWSCredentialsFile); err != nil {
if err := IsRequiredOption("aws-creds", opts.AWSPlatform.AWSCredentialsFile); err != nil {
return err
}
if err := isRequiredOption("pull-secret", opts.PullSecretFile); err != nil {
if err := IsRequiredOption("pull-secret", opts.PullSecretFile); err != nil {
return err
}
} else {
Expand Down Expand Up @@ -249,7 +249,7 @@ func applyPlatformSpecificsValues(ctx context.Context, exampleOptions *apifixtur
}

// IsRequiredOption returns a cobra style error message when the flag value is empty
func isRequiredOption(flag string, value string) error {
func IsRequiredOption(flag string, value string) error {
if len(value) == 0 {
return fmt.Errorf("required flag(s) \"%s\" not set", flag)
}
Expand Down
36 changes: 23 additions & 13 deletions cmd/cluster/aws/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,15 @@ func NewDestroyCommand(opts *core.DestroyOptions) *cobra.Command {
cmd.Flags().StringVar(&opts.AWSPlatform.Region, "region", opts.AWSPlatform.Region, "Cluster's region; inferred from the hosted cluster by default")
cmd.Flags().StringVar(&opts.AWSPlatform.BaseDomain, "base-domain", opts.AWSPlatform.BaseDomain, "Cluster's base domain; inferred from the hosted cluster by default")
cmd.Flags().StringVar(&opts.AWSPlatform.BaseDomainPrefix, "base-domain-prefix", opts.AWSPlatform.BaseDomainPrefix, "Cluster's base domain prefix; inferred from the hosted cluster by default")
cmd.Flags().StringVar(&opts.CredentialSecretName, "secret-creds", opts.CredentialSecretName, "A kubernete's secret with a platform credential, pull-secret and base-domain. The secret must exist in the supplied \"--namespace\"")
cmd.Flags().StringVar(&opts.CredentialSecretName, "secret-creds", opts.CredentialSecretName, "A Kubernetes secret with a platform credential, pull-secret and base-domain. The secret must exist in the supplied \"--namespace\"")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
if len(opts.CredentialSecretName) == 0 {
if err := isRequiredOption("aws-creds", opts.AWSPlatform.AWSCredentialsFile); err != nil {
return err
}
} else {
//Check the secret exists now, otherwise stop
opts.Log.Info("Retrieving credentials secret", "namespace", opts.Namespace, "name", opts.CredentialSecretName)
if _, err := util.GetSecret(opts.CredentialSecretName, opts.Namespace); err != nil {
return err
}
err := ValidateCredentialInfo(opts)
if err != nil {
return err
}

if err := DestroyCluster(cmd.Context(), opts); err != nil {
if err = DestroyCluster(cmd.Context(), opts); err != nil {
log.Log.Error(err, "Failed to destroy cluster")
return err
}
Expand Down Expand Up @@ -109,7 +102,6 @@ func destroyPlatformSpecifics(ctx context.Context, o *core.DestroyOptions) error
}

func DestroyCluster(ctx context.Context, o *core.DestroyOptions) error {

hostedCluster, err := core.GetCluster(ctx, o)
if err != nil {
return err
Expand Down Expand Up @@ -144,3 +136,21 @@ func DestroyCluster(ctx context.Context, o *core.DestroyOptions) error {

return core.DestroyCluster(ctx, hostedCluster, o, destroyPlatformSpecifics)
}

// ValidateCredentialInfo validates if the credentials secret name is empty, the aws-creds is not empty; validates if
// the credentials secret is not empty, that it can be retrieved.
func ValidateCredentialInfo(opts *core.DestroyOptions) error {
if len(opts.CredentialSecretName) == 0 {
if err := IsRequiredOption("aws-creds", opts.AWSPlatform.AWSCredentialsFile); err != nil {
return err
}
} else {
//Check the secret exists now, otherwise stop
opts.Log.Info("Retrieving credentials secret", "namespace", opts.Namespace, "name", opts.CredentialSecretName)
if _, err := util.GetSecret(opts.CredentialSecretName, opts.Namespace); err != nil {
return err
}
}

return nil
}
45 changes: 45 additions & 0 deletions cmd/cluster/aws/destroy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package aws

import (
"github.com/openshift/hypershift/cmd/cluster/core"
"testing"

. "github.com/onsi/gomega"
)

func Test_ValidateCredentialInfo(t *testing.T) {
tests := map[string]struct {
inputOptions *core.DestroyOptions
expectError bool
}{
"when CredentialSecretName is blank and aws-creds is also blank": {
inputOptions: &core.DestroyOptions{
CredentialSecretName: "",
AWSPlatform: core.AWSPlatformDestroyOptions{
AWSCredentialsFile: "",
},
},
expectError: true,
},
"when CredentialSecretName is blank and aws-creds is not blank": {
inputOptions: &core.DestroyOptions{
CredentialSecretName: "",
AWSPlatform: core.AWSPlatformDestroyOptions{
AWSCredentialsFile: "asdf",
},
},
expectError: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
g := NewGomegaWithT(t)
err := ValidateCredentialInfo(test.inputOptions)
if test.expectError {
g.Expect(err).To(HaveOccurred())
} else {
g.Expect(err).To(BeNil())
}
})
}
}
46 changes: 46 additions & 0 deletions product-cli/cmd/cluster/aws/destroy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package aws

import (
"github.com/spf13/cobra"

hypershiftaws "github.com/openshift/hypershift/cmd/cluster/aws"
"github.com/openshift/hypershift/cmd/cluster/core"
"github.com/openshift/hypershift/cmd/log"
)

func NewDestroyCommand(opts *core.DestroyOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "aws",
Short: "Destroys a HostedCluster and its associated infrastructure on AWS.",
SilenceUsage: true,
}

opts.AWSPlatform = core.AWSPlatformDestroyOptions{
AWSCredentialsFile: "",
PreserveIAM: false,
Region: "us-east-1",
}

cmd.Flags().StringVar(&opts.AWSPlatform.AWSCredentialsFile, "aws-creds", opts.AWSPlatform.AWSCredentialsFile, "Filepath to an AWS credentials file.")
cmd.Flags().BoolVar(&opts.AWSPlatform.PreserveIAM, "preserve-iam", opts.AWSPlatform.PreserveIAM, "If set to true, skip deleting IAM. Otherwise, destroy any default generated IAM along with other infrastructure.")
cmd.Flags().StringVar(&opts.AWSPlatform.Region, "region", opts.AWSPlatform.Region, "A HostedCluster's region.")
cmd.Flags().StringVar(&opts.AWSPlatform.BaseDomain, "base-domain", opts.AWSPlatform.BaseDomain, "A HostedCluster's base domain.")
cmd.Flags().StringVar(&opts.AWSPlatform.BaseDomainPrefix, "base-domain-prefix", opts.AWSPlatform.BaseDomainPrefix, "A HostedCluster's base domain prefix.")
cmd.Flags().StringVar(&opts.CredentialSecretName, "secret-creds", opts.CredentialSecretName, "A Kubernetes secret with a platform credentials: pull-secret and base-domain. The secret must exist in the supplied \"--namespace\".")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
err := hypershiftaws.ValidateCredentialInfo(opts)
if err != nil {
return err
}

if err = hypershiftaws.DestroyCluster(cmd.Context(), opts); err != nil {
log.Log.Error(err, "Failed to destroy cluster")
return err
}

return nil
}

return cmd
}
2 changes: 2 additions & 0 deletions product-cli/cmd/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/openshift/hypershift/cmd/cluster/core"
"github.com/openshift/hypershift/cmd/log"
"github.com/openshift/hypershift/product-cli/cmd/cluster/agent"
"github.com/openshift/hypershift/product-cli/cmd/cluster/aws"
"github.com/openshift/hypershift/product-cli/cmd/cluster/kubevirt"
)

Expand Down Expand Up @@ -99,6 +100,7 @@ func NewDestroyCommands() *cobra.Command {
_ = cmd.MarkPersistentFlagRequired("name")

cmd.AddCommand(agent.NewDestroyCommand(opts))
cmd.AddCommand(aws.NewDestroyCommand(opts))
cmd.AddCommand(kubevirt.NewDestroyCommand(opts))

return cmd
Expand Down

0 comments on commit 2aa885e

Please sign in to comment.