-
Notifications
You must be signed in to change notification settings - Fork 12
/
ssl.go
107 lines (87 loc) · 2.68 KB
/
ssl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright (C) 2021-2023, Kubefirst
This program is licensed under MIT.
See the LICENSE file for more details.
*/
package ssl
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
//cmv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
//cm "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
"github.com/rs/zerolog/log"
"github.com/kubefirst/runtime/pkg"
"github.com/kubefirst/runtime/pkg/k8s"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/applyconfigurations/core/v1"
"sigs.k8s.io/yaml"
)
func Restore(backupDir, domainName, kubeconfigPath string) error {
sslSecretFiles, err := ioutil.ReadDir(backupDir + "/secrets")
if err != nil {
return err
}
clientset, err := k8s.GetClientSet(kubeconfigPath)
if err != nil {
return err
}
for _, secret := range sslSecretFiles {
// file is named with convention $namespace-$secretName.yaml
// todo link to backup source code
namespace := strings.Split(secret.Name(), "-")[0]
log.Info().Msg("creating secret: " + secret.Name())
f, err := os.ReadFile(backupDir + "/secrets/" + secret.Name())
if err != nil {
return err
}
secretData := &v1.SecretApplyConfiguration{}
err = yaml.Unmarshal(f, secretData)
if err != nil {
return err
}
sec, err := clientset.CoreV1().Secrets(namespace).Apply(context.Background(), secretData, metav1.ApplyOptions{FieldManager: "application/apply-patch"})
if err != nil {
return err
}
log.Info().Msgf("created secret: %s", sec.Name)
}
return nil
}
func Backup(backupDir, domainName, k1Dir, kubeconfigPath string) error {
clientset, err := k8s.GetClientSet(kubeconfigPath)
if err != nil {
return err
}
//* corev1 secret resources
secrets, err := clientset.CoreV1().Secrets("").List(context.Background(), metav1.ListOptions{})
if err != nil {
return err
}
for _, secret := range secrets.Items {
if strings.Contains(secret.Name, "-tls") {
log.Info().Msg("backing up secret (ns/resource): " + secret.Namespace + "/" + secret.Name)
// modify fields of secret for restore
secret.APIVersion = "v1"
secret.Kind = "Secret"
secret.SetManagedFields(nil)
secret.SetOwnerReferences(nil)
secret.SetAnnotations(nil)
secret.SetCreationTimestamp(metav1.Time{})
secret.SetResourceVersion("")
secret.SetUID("")
fileName := fmt.Sprintf("%s/%s-%s.yaml", backupDir+"/secrets", secret.Namespace, secret.Name)
log.Info().Msgf("writing file: %s\n\n", fileName)
yamlContent, err := yaml.Marshal(secret)
if err != nil {
return fmt.Errorf("unable to marshal yaml: %s", err)
}
pkg.CreateFile(fileName, yamlContent)
} else {
log.Info().Msgf("skipping secret: %s", secret.Name)
}
}
return nil
}