forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.go
142 lines (117 loc) · 3.28 KB
/
cluster.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
package vfsclientset
import (
"k8s.io/kops/pkg/client/simple"
"k8s.io/kops/upup/pkg/api"
k8sapi "k8s.io/kubernetes/pkg/api"
"fmt"
"k8s.io/kops/upup/pkg/api/registry"
"k8s.io/kubernetes/pkg/api/unversioned"
"time"
"k8s.io/kops/util/pkg/vfs"
"strings"
"os"
)
type ClusterVFS struct {
basePath vfs.Path
}
var _ simple.ClusterInterface = &ClusterVFS{}
func (c *ClusterVFS) Get(name string) (*api.Cluster, error) {
return c.find(name)
}
// Deprecated, but we need this for now..
func (c*ClusterVFS) ConfigBase(clusterName string) (vfs.Path, error) {
if clusterName == "" {
return nil, fmt.Errorf("clusterName is required")
}
configPath := c.basePath.Join(clusterName)
return configPath, nil
}
func (c *ClusterVFS) List(options k8sapi.ListOptions) (*api.ClusterList, error) {
names, err := c.listNames()
if err != nil {
return nil, err
}
var items []api.Cluster
for _, clusterName := range names {
cluster, err := c.find(clusterName)
if err != nil {
return nil, err
}
if cluster == nil {
return nil, fmt.Errorf("cluster not found %q", clusterName)
}
items = append(items, *cluster)
}
return &api.ClusterList{Items: items}, nil
}
func (r *ClusterVFS) Create(c *api.Cluster) (*api.Cluster, error) {
err := c.Validate(false)
if err != nil {
return nil, err
}
if c.CreationTimestamp.IsZero() {
c.CreationTimestamp = unversioned.NewTime(time.Now().UTC())
}
configPath := r.basePath.Join(c.Name, registry.PathCluster)
err = registry.WriteConfig(configPath, c, vfs.WriteOptionCreate)
if err != nil {
return nil, fmt.Errorf("error writing Cluster: %v", err)
}
return c, nil
}
func (r *ClusterVFS) Update(c *api.Cluster) (*api.Cluster, error) {
err := c.Validate(false)
if err != nil {
return nil, err
}
configPath := r.basePath.Join(c.Name, registry.PathCluster)
err = registry.WriteConfig(configPath, c, vfs.WriteOptionOnlyIfExists)
if err != nil {
return nil, fmt.Errorf("error writing cluster %q: %v", c.Name, err)
}
return c, nil
}
// List returns a slice containing all the cluster names
// It skips directories that don't look like clusters
func (r *ClusterVFS) listNames() ([]string, error) {
paths, err := r.basePath.ReadTree()
if err != nil {
return nil, fmt.Errorf("error reading state store: %v", err)
}
var keys []string
for _, p := range paths {
relativePath, err := vfs.RelativePath(r.basePath, p)
if err != nil {
return nil, err
}
if !strings.HasSuffix(relativePath, "/config") {
continue
}
key := strings.TrimSuffix(relativePath, "/config")
keys = append(keys, key)
}
return keys, nil
}
func (r *ClusterVFS) find(clusterName string) (*api.Cluster, error) {
if clusterName == "" {
return nil, fmt.Errorf("clusterName is required")
}
configPath := r.basePath.Join(clusterName, registry.PathCluster)
c := &api.Cluster{}
err := registry.ReadConfig(configPath, c)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("error reading cluster configuration %q: %v", clusterName, err)
}
// TODO: Split this out into real version updates / schema changes
if c.Spec.ConfigBase == "" {
configBase, err := r.ConfigBase(clusterName)
if err != nil {
return nil, fmt.Errorf("error building ConfigBase for cluster: %v", err)
}
c.Spec.ConfigBase = configBase.Path()
}
return c, nil
}