forked from projectcalico/libcalico-go
/
client.go
363 lines (309 loc) · 11.4 KB
/
client.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (c) 2016 Tigera, Inc. All rights reserved.
// 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 client
import (
"encoding/hex"
goerrors "errors"
"fmt"
"io/ioutil"
"reflect"
"github.com/kelseyhightower/envconfig"
yaml "github.com/projectcalico/go-yaml-wrapper"
"github.com/projectcalico/libcalico-go/lib/api"
"github.com/projectcalico/libcalico-go/lib/api/unversioned"
"github.com/projectcalico/libcalico-go/lib/backend"
bapi "github.com/projectcalico/libcalico-go/lib/backend/api"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/errors"
"github.com/projectcalico/libcalico-go/lib/validator"
"github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
)
// Client contains
type Client struct {
// The backend client is currently public to allow access to datastore
// specific functions that are used by calico/node. This is a temporary
// measure and users of the client API should not assume that the backend
// will be available in the future.
Backend bapi.Client
}
// New returns a connected Client. The ClientConfig can either be created explicitly,
// or can be loaded from a config file or environment variables using the LoadClientConfig() function.
func New(config api.CalicoAPIConfig) (*Client, error) {
var err error
cc := Client{}
if cc.Backend, err = backend.NewClient(config); err != nil {
return nil, err
}
return &cc, err
}
// NewFromEnv loads the config from ENV variables and returns a connected Client.
func NewFromEnv() (*Client, error) {
config, err := LoadClientConfigFromEnvironment()
if err != nil {
return nil, err
}
return New(*config)
}
// Nodes returns an interface for managing node resources.
func (c *Client) Nodes() NodeInterface {
return newNodes(c)
}
// Policies returns an interface for managing policy resources.
func (c *Client) Policies() PolicyInterface {
return newPolicies(c)
}
// IPPools returns an interface for managing IP pool resources.
func (c *Client) IPPools() IPPoolInterface {
return newIPPools(c)
}
// Profiles returns an interface for managing profile resources.
func (c *Client) Profiles() ProfileInterface {
return newProfiles(c)
}
// HostEndpoints returns an interface for managing host endpoint resources.
func (c *Client) HostEndpoints() HostEndpointInterface {
return newHostEndpoints(c)
}
// WorkloadEndpoints returns an interface for managing workload endpoint resources.
func (c *Client) WorkloadEndpoints() WorkloadEndpointInterface {
return newWorkloadEndpoints(c)
}
// BGPPeers returns an interface for managing BGP peer resources.
func (c *Client) BGPPeers() BGPPeerInterface {
return newBGPPeers(c)
}
// IPAM returns an interface for managing IP address assignment and releasing.
func (c *Client) IPAM() IPAMInterface {
return newIPAM(c)
}
// Config returns an interface for managing system configuration..
func (c *Client) Config() ConfigInterface {
return newConfigs(c)
}
// EnsureInitialized is used to ensure the backend datastore is correctly
// initialized for use by Calico. This method may be called multiple times, and
// will have no effect if the datastore is already correctly initialized.
//
// Most Calico deployment scenarios will automatically implicitly invoke this
// method and so a general consumer of this API can assume that the datastore
// is already initialized.
func (c *Client) EnsureInitialized() error {
// Perform datastore specific initialization first.
if err := c.Backend.EnsureInitialized(); err != nil {
return err
}
// Ensure a cluster GUID is set for the deployment. We do this
// irrespective of how Calico is deployed.
kv := &model.KVPair{
Key: model.GlobalConfigKey{Name: "ClusterGUID"},
Value: fmt.Sprintf("%v", hex.EncodeToString(uuid.NewV4().Bytes())),
}
if _, err := c.Backend.Create(kv); err == nil {
log.WithField("ClusterGUID", kv.Value).Info("Assigned cluster GUID")
} else {
if _, ok := err.(errors.ErrorResourceAlreadyExists); !ok {
log.WithError(err).WithField("ClusterGUID", kv.Value).Warn("Failed to assign cluster GUID")
return err
}
log.Infof("Using previously configured cluster GUID")
}
return nil
}
// LoadClientConfig loads the ClientConfig from the specified file (if specified)
// or from environment variables (if the file is not specified).
func LoadClientConfig(filename string) (*api.CalicoAPIConfig, error) {
// Override / merge with values loaded from the specified file.
if filename != "" {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
c, err := LoadClientConfigFromBytes(b)
if err != nil {
return nil, fmt.Errorf("syntax error in %s: %s", filename, err)
}
return c, nil
} else {
return LoadClientConfigFromEnvironment()
}
}
// LoadClientConfig loads the ClientConfig from the supplied bytes containing
// YAML or JSON format data.
func LoadClientConfigFromBytes(b []byte) (*api.CalicoAPIConfig, error) {
var c api.CalicoAPIConfig
// Default the backend type to be etcd v2. This will be overridden if
// explicitly specified in the file.
log.Info("Loading config from JSON or YAML data")
c = api.CalicoAPIConfig{
Spec: api.CalicoAPIConfigSpec{
DatastoreType: api.EtcdV2,
},
}
if err := yaml.UnmarshalStrict(b, &c); err != nil {
return nil, err
}
// Validate the version and kind.
if c.APIVersion != unversioned.VersionCurrent {
return nil, goerrors.New("invalid config file: unknown APIVersion '" + c.APIVersion + "'")
}
if c.Kind != "calicoApiConfig" {
return nil, goerrors.New("invalid config file: expected kind 'calicoApiConfig', got '" + c.Kind + "'")
}
log.Info("Datastore type: ", c.Spec.DatastoreType)
return &c, nil
}
// LoadClientConfig loads the ClientConfig from the specified file (if specified)
// or from environment variables (if the file is not specified).
func LoadClientConfigFromEnvironment() (*api.CalicoAPIConfig, error) {
c := api.NewCalicoAPIConfig()
// Load client config from environment variables.
log.Info("Loading config from environment")
if err := envconfig.Process("calico", &c.Spec); err != nil {
return nil, err
}
return c, nil
}
// Interface used to convert between backend and API representations of our
// objects.
type conversionHelper interface {
convertAPIToKVPair(unversioned.Resource) (*model.KVPair, error)
convertKVPairToAPI(*model.KVPair) (unversioned.Resource, error)
convertMetadataToKey(unversioned.ResourceMetadata) (model.Key, error)
convertMetadataToListInterface(unversioned.ResourceMetadata) (model.ListInterface, error)
}
//TODO Plumb through revision data so that front end can do atomic operations.
// Untyped interface for creating an API object. This is called from the
// typed interface. This assumes a 1:1 mapping between the API resource and
// the backend object.
func (c *Client) create(apiObject unversioned.ResourceObject, helper conversionHelper) error {
// Validate the supplied data before writing to the datastore.
if err := validator.Validate(apiObject); err != nil {
return err
}
if err := validator.ValidateMetadataIDsAssigned(apiObject.GetResourceMetadata()); err != nil {
return err
}
if d, err := helper.convertAPIToKVPair(apiObject); err != nil {
return err
} else if d, err = c.Backend.Create(d); err != nil {
return err
} else {
return nil
}
}
// Untyped interface for updating an API object. This is called from the
// typed interface.
func (c *Client) update(apiObject unversioned.ResourceObject, helper conversionHelper) error {
// Validate the supplied data before writing to the datastore.
if err := validator.Validate(apiObject); err != nil {
return err
}
if err := validator.ValidateMetadataIDsAssigned(apiObject.GetResourceMetadata()); err != nil {
return err
}
if d, err := helper.convertAPIToKVPair(apiObject); err != nil {
return err
} else if d, err = c.Backend.Update(d); err != nil {
return err
} else {
return nil
}
}
// Untyped interface for applying an API object. This is called from the
// typed interface.
func (c *Client) apply(apiObject unversioned.ResourceObject, helper conversionHelper) error {
// Validate the supplied data before writing to the datastore.
if err := validator.Validate(apiObject); err != nil {
return err
}
if err := validator.ValidateMetadataIDsAssigned(apiObject.GetResourceMetadata()); err != nil {
return err
}
if d, err := helper.convertAPIToKVPair(apiObject); err != nil {
return err
} else if d, err = c.Backend.Apply(d); err != nil {
return err
} else {
return nil
}
}
// Untyped get interface for deleting a single API object. This is called from the typed
// interface.
func (c *Client) delete(metadata unversioned.ResourceMetadata, helper conversionHelper) error {
// Validate the supplied Metadata.
if err := validator.Validate(metadata); err != nil {
return err
}
if err := validator.ValidateMetadataIDsAssigned(metadata); err != nil {
return err
}
// Convert the Metadata to a Key and combine with the Metadata revision to create
// a KVPair for the delete operation. At the moment only the WorkloadEndpoint Get
// operations fills in the revision information.
if k, err := helper.convertMetadataToKey(metadata); err != nil {
return err
} else if err := c.Backend.Delete(&model.KVPair{Key: k, Revision: metadata.GetObjectMetadata().Revision}); err != nil {
return err
} else {
return nil
}
}
// Untyped get interface for getting a single API object. This is called from the typed
// interface. The result is
func (c *Client) get(metadata unversioned.ResourceMetadata, helper conversionHelper) (unversioned.Resource, error) {
// Validate the supplied Metadata.
if err := validator.Validate(metadata); err != nil {
return nil, err
}
if err := validator.ValidateMetadataIDsAssigned(metadata); err != nil {
return nil, err
}
if k, err := helper.convertMetadataToKey(metadata); err != nil {
return nil, err
} else if d, err := c.Backend.Get(k); err != nil {
return nil, err
} else if a, err := helper.convertKVPairToAPI(d); err != nil {
return nil, err
} else {
return a, nil
}
}
// Untyped get interface for getting a list of API objects. This is called from the typed
// interface. This updates the Items slice in the supplied List resource object.
func (c *Client) list(metadata unversioned.ResourceMetadata, helper conversionHelper, listp interface{}) error {
// Validate the supplied Metadata.
if err := validator.Validate(metadata); err != nil {
return err
}
if l, err := helper.convertMetadataToListInterface(metadata); err != nil {
return err
} else if dos, err := c.Backend.List(l); err != nil {
return err
} else {
// The supplied resource list object will have an Items field. Append the
// enumerated resources to this field.
e := reflect.ValueOf(listp).Elem()
f := e.FieldByName("Items")
i := reflect.ValueOf(f.Interface())
for _, d := range dos {
if a, err := helper.convertKVPairToAPI(d); err != nil {
return err
} else {
i = reflect.Append(i, reflect.ValueOf(a).Elem())
}
}
f.Set(i)
}
return nil
}