forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instancegroup.go
171 lines (141 loc) · 4.89 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vfsclientset
import (
"fmt"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/kops/pkg/apis/kops"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/v1alpha1"
"k8s.io/kops/pkg/apis/kops/validation"
kopsinternalversion "k8s.io/kops/pkg/client/clientset_generated/clientset/typed/kops/internalversion"
"k8s.io/kops/util/pkg/vfs"
)
type InstanceGroupVFS struct {
commonVFS
clusterName string
cluster *kops.Cluster
}
type InstanceGroupMirror interface {
WriteMirror(ig *kops.InstanceGroup) error
}
var _ InstanceGroupMirror = &InstanceGroupVFS{}
func NewInstanceGroupMirror(cluster *kops.Cluster, configBase vfs.Path) InstanceGroupMirror {
if cluster == nil || cluster.Name == "" {
glog.Fatalf("cluster / cluster.Name is required")
}
clusterName := cluster.Name
kind := "InstanceGroup"
r := &InstanceGroupVFS{
cluster: cluster,
clusterName: clusterName,
}
r.init(kind, configBase.Join("instancegroup"), StoreVersion)
defaultReadVersion := v1alpha1.SchemeGroupVersion.WithKind(kind)
r.defaultReadVersion = &defaultReadVersion
r.validate = func(o runtime.Object) error {
return validation.ValidateInstanceGroup(o.(*kops.InstanceGroup))
}
return r
}
func newInstanceGroupVFS(c *VFSClientset, cluster *kops.Cluster) *InstanceGroupVFS {
if cluster == nil || cluster.Name == "" {
glog.Fatalf("cluster / cluster.Name is required")
}
clusterName := cluster.Name
kind := "InstanceGroup"
r := &InstanceGroupVFS{
cluster: cluster,
clusterName: clusterName,
}
r.init(kind, c.basePath.Join(clusterName, "instancegroup"), StoreVersion)
defaultReadVersion := v1alpha1.SchemeGroupVersion.WithKind(kind)
r.defaultReadVersion = &defaultReadVersion
r.validate = func(o runtime.Object) error {
return validation.ValidateInstanceGroup(o.(*kops.InstanceGroup))
}
return r
}
var _ kopsinternalversion.InstanceGroupInterface = &InstanceGroupVFS{}
func (c *InstanceGroupVFS) Get(name string, options metav1.GetOptions) (*api.InstanceGroup, error) {
if options.ResourceVersion != "" {
return nil, fmt.Errorf("ResourceVersion not supported in InstanceGroupVFS::Get")
}
o, err := c.find(name)
if err != nil {
return nil, err
}
if o == nil {
return nil, errors.NewNotFound(schema.GroupResource{Group: api.GroupName, Resource: "InstanceGroup"}, name)
}
ig := o.(*api.InstanceGroup)
c.addLabels(ig)
return ig, nil
}
func (c *InstanceGroupVFS) addLabels(ig *api.InstanceGroup) {
if ig.ObjectMeta.Labels == nil {
ig.ObjectMeta.Labels = make(map[string]string)
}
ig.ObjectMeta.Labels[api.LabelClusterName] = c.clusterName
}
func (c *InstanceGroupVFS) List(options metav1.ListOptions) (*api.InstanceGroupList, error) {
list := &api.InstanceGroupList{}
items, err := c.list(list.Items, options)
if err != nil {
return nil, err
}
list.Items = items.([]api.InstanceGroup)
for i := range list.Items {
c.addLabels(&list.Items[i])
}
return list, nil
}
func (c *InstanceGroupVFS) Create(g *api.InstanceGroup) (*api.InstanceGroup, error) {
err := c.create(c.cluster, g)
if err != nil {
return nil, err
}
return g, nil
}
func (c *InstanceGroupVFS) Update(g *api.InstanceGroup) (*api.InstanceGroup, error) {
err := c.update(c.cluster, g)
if err != nil {
return nil, err
}
return g, nil
}
func (c *InstanceGroupVFS) WriteMirror(g *api.InstanceGroup) error {
err := c.writeConfig(c.cluster, c.basePath.Join(g.Name), g)
if err != nil {
return fmt.Errorf("error writing %s: %v", c.kind, err)
}
return nil
}
func (c *InstanceGroupVFS) Delete(name string, options *metav1.DeleteOptions) error {
return c.delete(name, options)
}
func (r *InstanceGroupVFS) DeleteCollection(options *metav1.DeleteOptions, listOptions metav1.ListOptions) error {
return fmt.Errorf("InstanceGroupVFS DeleteCollection not implemented for vfs store")
}
func (r *InstanceGroupVFS) Watch(opts metav1.ListOptions) (watch.Interface, error) {
return nil, fmt.Errorf("InstanceGroupVFS Watch not implemented for vfs store")
}
func (r *InstanceGroupVFS) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *api.InstanceGroup, err error) {
return nil, fmt.Errorf("InstanceGroupVFS Patch not implemented for vfs store")
}