-
Notifications
You must be signed in to change notification settings - Fork 3k
/
endpoint.go
153 lines (137 loc) · 5.31 KB
/
endpoint.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
// Copyright 2016-2017 Authors of Cilium
//
// 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 (
"github.com/cilium/cilium/api/v1/client/endpoint"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/api"
pkgEndpointID "github.com/cilium/cilium/pkg/endpoint/id"
"github.com/cilium/cilium/pkg/labels"
)
// EndpointList returns a list of all endpoints
func (c *Client) EndpointList() ([]*models.Endpoint, error) {
resp, err := c.Endpoint.GetEndpoint(nil)
if err != nil {
return nil, Hint(err)
}
return resp.Payload, nil
}
// EndpointGet returns endpoint by ID
func (c *Client) EndpointGet(id string) (*models.Endpoint, error) {
params := endpoint.NewGetEndpointIDParams().WithID(id).WithTimeout(api.ClientTimeout)
resp, err := c.Endpoint.GetEndpointID(params)
if err != nil {
/* Since plugins rely on checking the error type, we don't wrap this
* with Hint(...)
*/
return nil, err
}
return resp.Payload, nil
}
// EndpointCreate creates a new endpoint
func (c *Client) EndpointCreate(ep *models.EndpointChangeRequest) error {
id := pkgEndpointID.NewCiliumID(ep.ID)
params := endpoint.NewPutEndpointIDParams().WithID(id).WithEndpoint(ep).WithTimeout(api.ClientTimeout)
_, err := c.Endpoint.PutEndpointID(params)
return Hint(err)
}
// EndpointPatch modifies the endpoint
func (c *Client) EndpointPatch(id string, ep *models.EndpointChangeRequest) error {
params := endpoint.NewPatchEndpointIDParams().WithID(id).WithEndpoint(ep).WithTimeout(api.ClientTimeout)
_, err := c.Endpoint.PatchEndpointID(params)
return Hint(err)
}
// EndpointDelete deletes endpoint
func (c *Client) EndpointDelete(id string) error {
params := endpoint.NewDeleteEndpointIDParams().WithID(id).WithTimeout(api.ClientTimeout)
_, _, err := c.Endpoint.DeleteEndpointID(params)
return Hint(err)
}
// EndpointLogGet returns endpoint log
func (c *Client) EndpointLogGet(id string) (models.EndpointStatusLog, error) {
params := endpoint.NewGetEndpointIDLogParams().WithID(id).WithTimeout(api.ClientTimeout)
resp, err := c.Endpoint.GetEndpointIDLog(params)
if err != nil {
return nil, Hint(err)
}
return resp.Payload, nil
}
// EndpointHealthGet returns endpoint healthz
func (c *Client) EndpointHealthGet(id string) (*models.EndpointHealth, error) {
params := endpoint.NewGetEndpointIDHealthzParams().WithID(id).WithTimeout(api.ClientTimeout)
resp, err := c.Endpoint.GetEndpointIDHealthz(params)
if err != nil {
return nil, Hint(err)
}
return resp.Payload, nil
}
// EndpointConfigGet returns endpoint configuration
func (c *Client) EndpointConfigGet(id string) (*models.EndpointConfigurationStatus, error) {
params := endpoint.NewGetEndpointIDConfigParams().WithID(id).WithTimeout(api.ClientTimeout)
resp, err := c.Endpoint.GetEndpointIDConfig(params)
if err != nil {
return nil, Hint(err)
}
return resp.Payload, nil
}
// EndpointConfigPatch modifies endpoint configuration
func (c *Client) EndpointConfigPatch(id string, cfg *models.EndpointConfigurationSpec) error {
params := endpoint.NewPatchEndpointIDConfigParams().WithID(id).WithTimeout(api.ClientTimeout)
if cfg != nil {
params.SetEndpointConfiguration(cfg)
}
_, err := c.Endpoint.PatchEndpointIDConfig(params)
return Hint(err)
}
// EndpointLabelsGet returns endpoint label configuration
func (c *Client) EndpointLabelsGet(id string) (*models.LabelConfiguration, error) {
params := endpoint.NewGetEndpointIDLabelsParams().WithID(id).WithTimeout(api.ClientTimeout)
resp, err := c.Endpoint.GetEndpointIDLabels(params)
if err != nil {
return nil, Hint(err)
}
return resp.Payload, nil
}
// EndpointLabelsPut modifies endpoint label configuration
// add: List of labels to add and enable. If the label is an orchestration
// system label which has been disabled before, it will be removed from
// the disabled list and readded to the orchestration list. Otherwise
// it will be added to the custom label list.
//
// delete: List of labels to delete. If the label is an orchestration system
// label, then it will be deleted from the orchestration list and
// added to the disabled list. Otherwise it will be removed from the
// custom list.
//
func (c *Client) EndpointLabelsPatch(id string, toAdd, toDelete models.Labels) error {
currentCfg, err := c.EndpointLabelsGet(id)
if err != nil {
return err
}
userLbl := labels.NewLabelsFromModel(currentCfg.Status.Realized.User)
for _, lbl := range toAdd {
lblParsed := labels.ParseLabel(lbl)
if _, found := userLbl[lblParsed.Key]; !found {
userLbl[lblParsed.Key] = lblParsed
}
}
for _, lbl := range toDelete {
lblParsed := labels.ParseLabel(lbl)
delete(userLbl, lblParsed.Key)
}
currentCfg.Spec.User = userLbl.GetModel()
params := endpoint.NewPatchEndpointIDLabelsParams().WithID(id).WithTimeout(api.ClientTimeout)
_, err = c.Endpoint.PatchEndpointIDLabels(params.WithConfiguration(currentCfg.Spec))
return Hint(err)
}