-
Notifications
You must be signed in to change notification settings - Fork 90
/
identity.go
82 lines (66 loc) · 2.16 KB
/
identity.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
package upstream
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"k8s.io/client-go/kubernetes/scheme"
)
func LoadIdentity(upstreamDir string) (*kotsv1beta1.Identity, error) {
var identitySpec *kotsv1beta1.Identity
err := filepath.Walk(upstreamDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(content, nil, nil)
if err != nil {
return nil
}
if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "Identity" {
identitySpec = obj.(*kotsv1beta1.Identity)
}
return nil
})
if err != nil {
return nil, errors.Wrap(err, "failed to walk archive dir")
}
return identitySpec, nil
}
func LoadIdentityConfig(upstreamDir string) (*kotsv1beta1.IdentityConfig, error) {
filename := filepath.Join(upstreamDir, "userdata", "identityconfig.yaml")
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, errors.Wrap(err, "failed to stat identity Config")
}
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, errors.Wrap(err, "failed to read existing identity Config")
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(content, nil, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to decode identity Config")
}
if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "IdentityConfig" {
return obj.(*kotsv1beta1.IdentityConfig), nil
}
return nil, errors.Errorf("unexpected gvk in identity Config file: %s/%s/%s", gvk.Group, gvk.Version, gvk.Kind)
}
func SaveIdentityConfig(identityConfig *kotsv1beta1.IdentityConfig, upstreamDir string) error {
filename := filepath.Join(upstreamDir, "userdata", "identityconfig.yaml")
err := ioutil.WriteFile(filename, mustMarshalIdentityConfig(identityConfig), 0644)
if err != nil {
return errors.Wrap(err, "failed to write identity config")
}
return nil
}