forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statestore.go
143 lines (119 loc) · 3.3 KB
/
statestore.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package fi
import (
"fmt"
"k8s.io/kops/upup/pkg/fi/utils"
"k8s.io/kops/upup/pkg/fi/vfs"
"os"
"strings"
)
type WriteOption string
const (
WriteOptionCreate WriteOption = "Create"
WriteOptionOnlyIfExists WriteOption = "IfExists"
)
type StateStore interface {
// VFSPath returns the path where the StateStore is stored
VFSPath() vfs.Path
CA() CAStore
Secrets() SecretStore
ReadConfig(path string, config interface{}) error
WriteConfig(path string, config interface{}, options ...WriteOption) error
// ListChildren returns a list of all (direct) children of the specified path
// It only returns the raw names, not the prefixes
ListChildren(pathPrefix string) ([]string, error)
}
type VFSStateStore struct {
location vfs.Path
keystore CAStore
secrets SecretStore
}
var _ StateStore = &VFSStateStore{}
func NewVFSStateStore(base vfs.Path, clusterName string) *VFSStateStore {
location := base.Join(clusterName)
s := &VFSStateStore{
location: location,
}
s.keystore = NewVFSCAStore(location.Join("pki"))
s.secrets = NewVFSSecretStore(location.Join("secrets"))
return s
}
func (s *VFSStateStore) CA() CAStore {
return s.keystore
}
func (s *VFSStateStore) VFSPath() vfs.Path {
return s.location
}
func (s *VFSStateStore) Secrets() SecretStore {
return s.secrets
}
func (s *VFSStateStore) ListChildren(pathPrefix string) ([]string, error) {
vfsPath := s.location.Join(pathPrefix)
children, err := vfsPath.ReadDir()
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("error listing children of %s: %v", pathPrefix, err)
}
var names []string
for _, child := range children {
names = append(names, child.Base())
}
return names, nil
}
func (s *VFSStateStore) ReadConfig(path string, config interface{}) error {
if path == "" {
return fmt.Errorf("path is required")
}
configPath := s.location.Join(path)
data, err := configPath.ReadFile()
if err != nil {
if os.IsNotExist(err) {
return err
}
return fmt.Errorf("error reading configuration file %s: %v", configPath, err)
}
// Yaml can't parse empty strings
configString := string(data)
configString = strings.TrimSpace(configString)
if configString != "" {
err = utils.YamlUnmarshal([]byte(configString), config)
if err != nil {
return fmt.Errorf("error parsing configuration: %v", err)
}
}
return nil
}
func (s *VFSStateStore) WriteConfig(path string, config interface{}, writeOptions ...WriteOption) error {
configPath := s.location.Join(path)
data, err := utils.YamlMarshal(config)
if err != nil {
return fmt.Errorf("error marshalling configuration: %v", err)
}
create := false
for _, writeOption := range writeOptions {
switch writeOption {
case WriteOptionCreate:
create = true
case WriteOptionOnlyIfExists:
_, err = configPath.ReadFile()
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("cannot update configuration file %s: does not exist", configPath)
}
return fmt.Errorf("error checking if configuration file %s exists already: %v", configPath, err)
}
default:
return fmt.Errorf("unknown write option: %q", writeOption)
}
}
if create {
err = configPath.CreateFile(data)
} else {
err = configPath.WriteFile(data)
}
if err != nil {
return fmt.Errorf("error writing configuration file %s: %v", configPath, err)
}
return nil
}