forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instancegroup.go
120 lines (100 loc) · 2.81 KB
/
instancegroup.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
package vfsclientset
import (
"k8s.io/kops/pkg/client/simple"
"k8s.io/kops/upup/pkg/api"
k8sapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"time"
"fmt"
"os"
"github.com/golang/glog"
"k8s.io/kops/util/pkg/vfs"
"k8s.io/kops/upup/pkg/api/registry"
)
type InstanceGroupVFS struct {
clusterBasePath vfs.Path
}
var _ simple.ClusterInterface = &ClusterVFS{}
func (c *InstanceGroupVFS) Get(name string) (*api.InstanceGroup, error) {
group := &api.InstanceGroup{}
err := registry.ReadConfig(c.clusterBasePath.Join("instancegroup", name), group)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("error reading InstanceGroup %q: %v", name, err)
}
return group, nil
}
func (c *InstanceGroupVFS) List(options k8sapi.ListOptions) (*api.InstanceGroupList, error) {
items, err := c.readAll()
if err != nil {
return nil, err
}
ret := &api.InstanceGroupList{}
for _, i := range items {
ret.Items = append(ret.Items, *i)
}
return ret, nil
}
func (c *InstanceGroupVFS) Create(g *api.InstanceGroup) (*api.InstanceGroup, error) {
err := g.Validate(true)
if err != nil {
return nil, err
}
if g.CreationTimestamp.IsZero() {
g.CreationTimestamp = unversioned.NewTime(time.Now().UTC())
}
err = registry.WriteConfig(c.clusterBasePath.Join("instancegroup", g.Name), g, vfs.WriteOptionCreate)
if err != nil {
return nil, fmt.Errorf("error writing InstanceGroup: %v", err)
}
return g, nil
}
func (c *InstanceGroupVFS) Update(g *api.InstanceGroup) (*api.InstanceGroup, error) {
err := g.Validate(true)
if err != nil {
return nil, err
}
err = registry.WriteConfig(c.clusterBasePath.Join("instancegroup", g.Name), g, vfs.WriteOptionOnlyIfExists)
if err != nil {
return nil, fmt.Errorf("error writing InstanceGroup %q: %v", g.Name, err)
}
return g, nil
}
func (c *InstanceGroupVFS) Delete(name string, options *k8sapi.DeleteOptions) (error) {
p := c.clusterBasePath.Join("instancegroup", name)
err := p.Remove()
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("error deleting instancegroup configuration %q: %v", name, err)
}
return nil
}
func (c *InstanceGroupVFS) listNames() ([]string, error) {
keys, err := listChildNames(c.clusterBasePath.Join("instancegroup"))
if err != nil {
return nil, fmt.Errorf("error listing instancegroups in state store: %v", err)
}
return keys, nil
}
func (r *InstanceGroupVFS) readAll() ([]*api.InstanceGroup, error) {
names, err := r.listNames()
if err != nil {
return nil, err
}
var instancegroups []*api.InstanceGroup
for _, name := range names {
g, err := r.Get(name)
if err != nil {
return nil, err
}
if g == nil {
glog.Warningf("InstanceGroup was listed, but then not found %q", name)
}
instancegroups = append(instancegroups, g)
}
return instancegroups, nil
}