-
Notifications
You must be signed in to change notification settings - Fork 88
/
installation.go
78 lines (62 loc) · 1.98 KB
/
installation.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
package upstream
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
"k8s.io/client-go/kubernetes/scheme"
)
func LoadApplication(upstreamDir string) (*kotsv1beta1.Application, error) {
var application *kotsv1beta1.Application
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 == "Application" {
application = obj.(*kotsv1beta1.Application)
}
return nil
})
if err != nil {
return nil, errors.Wrap(err, "failed to walk archive dir")
}
return application, nil
}
func LoadInstallation(upstreamDir string) (*kotsv1beta1.Installation, error) {
content, err := ioutil.ReadFile(path.Join(upstreamDir, "userdata", "installation.yaml"))
if err != nil {
return nil, errors.Wrap(err, "failed to read existing installation")
}
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, gvk, err := decode(content, nil, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to decode installation")
}
if gvk.Group == "kots.io" && gvk.Version == "v1beta1" && gvk.Kind == "Installation" {
return obj.(*kotsv1beta1.Installation), nil
}
return nil, errors.Errorf("unexpected gvk in installation file: %s/%s/%s", gvk.Group, gvk.Version, gvk.Kind)
}
func SaveInstallation(installation *kotsv1beta1.Installation, upstreamDir string) error {
filename := path.Join(upstreamDir, "userdata", "installation.yaml")
err := ioutil.WriteFile(filename, mustMarshalInstallation(installation), 0644)
if err != nil {
return errors.Wrap(err, "failed to write installation")
}
return nil
}