Skip to content

Commit

Permalink
fix path to upload and fmt to log
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 19, 2022
1 parent 62e5686 commit 6663f10
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
2 changes: 2 additions & 0 deletions cmd/backupSsl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"log"

"github.com/kubefirst/kubefirst/internal/ssl"
Expand All @@ -19,6 +20,7 @@ where can be used on provisioning phase with the flag --recycle-ssl`,
if err != nil {
log.Panic(err)
}
fmt.Println("Backup certificates finished successfully")
},
}

Expand Down
4 changes: 3 additions & 1 deletion configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Config struct {

// todo: move it back
KubefirstVersion string

CertsPath string
}

func ReadConfig() *Config {
Expand Down Expand Up @@ -65,7 +67,7 @@ func ReadConfig() *Config {
config.KubeConfigPath = fmt.Sprintf("%s/gitops/terraform/base/kubeconfig", config.K1FolderPath)
config.TerraformPath = fmt.Sprintf("%s/tools/terraform", config.K1FolderPath)
config.HelmClientPath = fmt.Sprintf("%s/tools/helm", config.K1FolderPath)

config.CertsPath = fmt.Sprintf("%s/ssl", config.K1FolderPath)
config.TerraformVersion = "1.0.11"

// todo adopt latest helmVersion := "v3.9.0"
Expand Down
2 changes: 1 addition & 1 deletion internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,6 @@ func UploadFile(bucket, key, fileName string) error {
if err != nil {
return fmt.Errorf("failed to upload file, %v", err)
}
fmt.Printf("file uploaded to, %s\n", result.Location)
log.Printf("file uploaded to, %s\n", result.Location)
return nil
}
36 changes: 22 additions & 14 deletions internal/ssl/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/ghodss/yaml"
"github.com/kubefirst/kubefirst/configs"
Expand All @@ -27,14 +29,19 @@ func getItemsToBackup(apiGroup string, apiVersion string, resourceType string, n

k8sClient := dynamic.NewForConfigOrDie(k8sConfig)

//creating folder to store certificates backup, and continue if exists.
if err := os.Mkdir(fmt.Sprintf("%s", config.CertsPath), os.ModePerm); err != nil {
log.Printf("error: could not create directory %q - it must exist to continue. error is: %s", config.CertsPath, err)
}

var files []string
var items []unstructured.Unstructured
for _, namespace := range namespaces {
if len(jqQuery) > 0 {
fmt.Println("getting resources and filtering using jq")
log.Println("getting resources and filtering using jq")
items, err = k8s.GetResourcesByJq(k8sClient, context.TODO(), apiGroup, apiVersion, resourceType, namespace, jqQuery)
} else {
fmt.Println("getting resources")
log.Println("getting resources")
items, err = k8s.GetResourcesDynamically(k8sClient, context.TODO(), apiGroup, apiVersion, resourceType, namespace)
}

Expand All @@ -55,7 +62,7 @@ func getItemsToBackup(apiGroup string, apiVersion string, resourceType string, n

fileName := fmt.Sprintf("%s.%s", item.GetName(), "yaml")
//TODO: test if kubeconfigpath is the correct value to write the files together another k1rst files
fullFileName := filepath.Join(config.KubeConfigPath, fileName)
fullFileName := filepath.Join(config.CertsPath, fileName)
err = pkg.CreateFile(fullFileName, yamlObj)
if err != nil {
return nil, err
Expand All @@ -70,45 +77,46 @@ func getItemsToBackup(apiGroup string, apiVersion string, resourceType string, n
//func GetBackupCertificates(apiGroup string, apiVersion string, resourceTypes []string, namespace string) ([]string, error) {
// GetBackupCertificates create a backup of Certificates on AWS S3 in yaml files
func GetBackupCertificates() (string, error) {
config := configs.ReadConfig()

fmt.Println("GetBackupCertificates called")
log.Println("GetBackupCertificates called")
bucketName := fmt.Sprintf("k1-%s", viper.GetString("aws.hostedzonename"))
path := "cert-manager"
//path := "cert-manager"
aws.CreateBucket(false, bucketName)

fmt.Println("getting certificates")
log.Println("getting certificates")
namespaces := []string{"argo", "atlantis", "chartmuseum", "gitlab", "vault"}
certificates, err := getItemsToBackup("cert-manager.io", "v1", "certificates", namespaces, "")
if err != nil {
log.Panic(err)
}
for _, cert := range certificates {
fullPath := fmt.Sprintf("%s/cert-%s", path, cert)
fmt.Println(fullPath)
fullPath := strings.Replace(cert, config.CertsPath, "/certs", 1)
log.Println(fullPath)
aws.UploadFile(bucketName, fullPath, cert)
}

fmt.Println("getting secrets")
log.Println("getting secrets")
query := ".metadata.annotations[\"cert-manager.io/issuer-kind\"] == \"ClusterIssuer\""
secrets, err := getItemsToBackup("", "v1", "secrets", namespaces, query)
if err != nil {
log.Panic(err)
}
for _, secret := range secrets {
fullPath := fmt.Sprintf("%s/secret-%s", path, secret)
fmt.Println(fullPath)
fullPath := strings.Replace(secret, config.CertsPath, "/secrets", 1)
log.Println(fullPath)
aws.UploadFile(bucketName, fullPath, secret)
}

emptyNS := []string{""}
fmt.Println("getting clusterissuers")
log.Println("getting clusterissuers")
clusterIssuers, err := getItemsToBackup("cert-manager.io", "v1", "clusterissuers", emptyNS, "")
if err != nil {
log.Panic(err)
}
for _, clusterissuer := range clusterIssuers {
fullPath := fmt.Sprintf("%s/clusterissuer-%s", path, clusterissuer)
fmt.Println(fullPath)
fullPath := strings.Replace(clusterissuer, config.CertsPath, "/clusterissuers", 1)
log.Println(fullPath)
aws.UploadFile(bucketName, fullPath, clusterissuer)
}

Expand Down

0 comments on commit 6663f10

Please sign in to comment.