-
Notifications
You must be signed in to change notification settings - Fork 148
/
state.go
91 lines (72 loc) · 2.3 KB
/
state.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
package k8s
import (
"context"
"encoding/json"
"fmt"
"github.com/defenseunicorns/zarf/cli/config"
"github.com/defenseunicorns/zarf/cli/types"
"github.com/defenseunicorns/zarf/cli/internal/message"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
const (
ZarfNamespace = "zarf"
ZarfStateSecretName = "zarf-state"
ZarfStateDataKey = "state"
)
// LoadZarfState returns the current zarf/zarf-state secret data or an empty ZarfState
func LoadZarfState() types.ZarfState {
message.Debug("k8s.LoadZarfState()")
// The empty state that we will try to fill
state := types.ZarfState{}
// Set up the API connection
secretInterface := getZarfStateInterface()
// Try to get the zarf-state secret
if match, err := secretInterface.Get(context.TODO(), ZarfStateSecretName, metav1.GetOptions{}); err == nil {
_ = json.Unmarshal(match.Data[ZarfStateDataKey], &state)
}
message.Debug(state)
return state
}
// SaveZarfState takes a given state and makepersists it to the zarf/zarf-state secret
func SaveZarfState(state types.ZarfState) error {
message.Debugf("k8s.SaveZarfState(%v)", state)
// Convert the data back to JSON
data, err := json.Marshal(state)
if err != nil {
return fmt.Errorf("unable to json-encode the zarf state")
}
// Set up the data wrapper
dataWrapper := make(map[string][]byte)
dataWrapper[ZarfStateDataKey] = data
// The secret object
secret := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: ZarfStateSecretName,
Namespace: ZarfNamespace,
Labels: map[string]string{
config.ZarfManagedByLabel: "zarf",
},
},
Type: corev1.SecretTypeOpaque,
Data: dataWrapper,
}
message.Debug(secret)
// Attempt to create or replace the secret and return
if err := ReplaceSecret(secret); err != nil {
return fmt.Errorf("unable to create the zarf state secret")
}
return nil
}
// getZarfStateInterface returns a secret interface for the zarf namespace
func getZarfStateInterface() v1.SecretInterface {
message.Debug("k8s.getZarfStateInterface()")
clientSet := getClientset()
// Get interface for all secrets in the zarf namespace
return clientSet.CoreV1().Secrets(ZarfNamespace)
}