Skip to content

Commit

Permalink
wip delete
Browse files Browse the repository at this point in the history
Signed-off-by: Thiago Pagotto <pagottoo@gmail.com>
  • Loading branch information
pagottoo committed Jul 22, 2022
1 parent 16f01c0 commit 0d4d8af
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
15 changes: 11 additions & 4 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package cmd
import (
"bytes"
"fmt"
"log"
"os"
"strings"

"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/aws"
"github.com/kubefirst/kubefirst/internal/reports"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"os"
"strings"
)

// todo delete the s3 buckets associated with the ~/.kubefirst file
// todo ask for user input to verify deletion?
// todo ask for user input to verify?
// cleanCmd removes all kubefirst resources locally for new execution.
Expand All @@ -25,6 +26,12 @@ re-create all Kubefirst files.`,

config := configs.ReadConfig()

destroyBuckets, err := cmd.Flags().GetBool("destroy-buckets")
if err != nil {
log.Panic(err)
}
aws.DestroyBucketsInUse(destroyBuckets)

// command line flags
rmLogsFolder, err := cmd.Flags().GetBool("rm-logs")
if err != nil {
Expand Down
14 changes: 4 additions & 10 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package cmd
import (
"bytes"
"fmt"
"log"
"os/exec"
"syscall"

"github.com/kubefirst/kubefirst/configs"
"github.com/kubefirst/kubefirst/internal/aws"
"github.com/kubefirst/kubefirst/internal/gitlab"
"github.com/kubefirst/kubefirst/internal/k8s"
"github.com/kubefirst/kubefirst/internal/terraform"
"github.com/spf13/cobra"
"log"
"os/exec"
"syscall"
)

// destroyCmd represents the destroy command
Expand Down Expand Up @@ -39,10 +39,6 @@ if the registry has already been deleted.`,
if err != nil {
log.Panic(err)
}
destroyBuckets, err := cmd.Flags().GetBool("destroy-buckets")
if err != nil {
log.Panic(err)
}

var kPortForwardOutb, kPortForwardErrb bytes.Buffer
kPortForward := exec.Command(config.KubectlClientPath, "--kubeconfig", config.KubeConfigPath, "-n", "gitlab", "port-forward", "svc/gitlab-webservice-default", "8888:8080")
Expand Down Expand Up @@ -100,8 +96,6 @@ if the registry has already been deleted.`,
log.Println("terraform destroy base")
terraform.DestroyBaseTerraform(skipBaseTerraform)
log.Println("terraform base destruction complete")
//TODO: move this step to `kubefirst clean` command and empty buckets and delete
aws.DestroyBucketsInUse(destroyBuckets)
fmt.Println("End of execution destroy")
},
}
Expand Down
46 changes: 35 additions & 11 deletions internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func BucketRand(dryRun bool, trackers map[string]*pkg.ActionTracker) {
viper.Set(fmt.Sprintf("bucket.%s.created", bucket), true)
viper.Set(fmt.Sprintf("bucket.%s.name", bucket), bucketName)
viper.WriteConfig()

PutTagKubefirstOnBuckets(bucketName, viper.GetString("cluster-name"))
}
log.Printf("bucket %s exists", viper.GetString(fmt.Sprintf("bucket.%s.name", bucket)))
}
Expand Down Expand Up @@ -261,11 +263,6 @@ func ReturnHostedZoneId(rawZoneId string) string {
}

func ListBucketsInUse() []string {
//Read flare file
//Iterate over buckets
//check if bucket exist
//buckets := make([]map[string]string, 0)
//var m map[string]string
var bucketsInUse []string
bucketsConfig := viper.AllKeys()
for _, bucketKey := range bucketsConfig {
Expand All @@ -279,7 +276,6 @@ func ListBucketsInUse() []string {
}

func DestroyBucket(bucketName string) {

s3Client := s3.New(GetAWSSession())

log.Printf("Attempt to delete: %s", bucketName)
Expand All @@ -295,21 +291,16 @@ func DestroyBucket(bucketName string) {
log.Println("Bucket Error:", aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Println(errHead.Error())
}
} else {
//if exist, we can delete it
_, err := s3Client.DeleteBucket(&s3.DeleteBucketInput{
Bucket: &bucketName,
})
if err != nil {
log.Panicf("failed to delete bucket "+bucketName, err.Error())
}

}

}

func GetAWSSession() *session.Session {
Expand Down Expand Up @@ -427,3 +418,36 @@ func DownloadBucket(bucket string, destFolder string) error {
}
return nil
}

func PutTagKubefirstOnBuckets(bucketName, clusterName string) {
svc := s3.New(session.New())
input := &s3.PutBucketTaggingInput{
Bucket: aws.String("bucketName"),
Tagging: &s3.Tagging{
TagSet: []*s3.Tag{
{
Key: aws.String("Provisioned-by"),
Value: aws.String("Kubefirst"),
},
{
Key: aws.String("ClusterName"),
Value: aws.String(clusterName),
},
},
},
}

result, err := svc.PutBucketTagging(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
log.Println(aerr.Error())
}
} else {
log.Println(err.Error())
}
return
}
log.Println(result)
}

0 comments on commit 0d4d8af

Please sign in to comment.