-
Notifications
You must be signed in to change notification settings - Fork 137
/
client.go
236 lines (179 loc) · 7.45 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
package istio
import (
"context"
"github.com/Dynatrace/dynatrace-operator/pkg/util/hasher"
"github.com/pkg/errors"
istiov1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
istioclientset "istio.io/client-go/pkg/clientset/versioned"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
type ClientBuilder func(config *rest.Config, scheme *runtime.Scheme, owner metav1.Object) (*Client, error)
// Client - an adapter for the external istioclientset library
type Client struct {
IstioClientset istioclientset.Interface
Scheme *runtime.Scheme
Owner metav1.Object
}
func NewClient(config *rest.Config, scheme *runtime.Scheme, owner metav1.Object) (*Client, error) {
istioClient, err := istioclientset.NewForConfig(config)
if err != nil {
log.Info("failed to initialize istio client", "error", err.Error())
return nil, errors.WithStack(err)
}
if owner == nil {
return nil, errors.New("can't create istio client for empty owner")
}
return &Client{
IstioClientset: istioClient,
Scheme: scheme,
Owner: owner,
}, nil
}
var _ ClientBuilder = NewClient
func (cl *Client) CheckIstioInstalled() (bool, error) {
_, err := cl.IstioClientset.Discovery().ServerResourcesForGroupVersion(IstioGVR)
if k8serrors.IsNotFound(err) {
return false, nil
}
return err == nil, err
}
func (cl *Client) GetVirtualService(ctx context.Context, name string) (*istiov1beta1.VirtualService, error) {
virtualService, err := cl.IstioClientset.NetworkingV1beta1().VirtualServices(cl.Owner.GetNamespace()).Get(ctx, name, metav1.GetOptions{})
if k8serrors.IsNotFound(err) {
return nil, nil //nolint: nilnil
} else if err != nil {
log.Info("failed to get current virtual service", "name", name, "error", err.Error())
return nil, errors.WithStack(err)
}
return virtualService, nil
}
func (cl *Client) CreateOrUpdateVirtualService(ctx context.Context, newVirtualService *istiov1beta1.VirtualService) error {
if newVirtualService == nil {
return errors.New("can't create virtual service based on nil object")
}
// the owner reference is created before the hash annotation is added
if err := controllerutil.SetControllerReference(cl.Owner, newVirtualService, cl.Scheme); err != nil {
return errors.WithStack(err)
}
delete(newVirtualService.Annotations, hasher.AnnotationHash)
err := hasher.AddAnnotation(newVirtualService)
if err != nil {
return errors.WithMessage(err, "failed to generate and hash annotation for virtual service")
}
oldVirtualService, err := cl.GetVirtualService(ctx, newVirtualService.Name)
if err != nil {
return err
}
if oldVirtualService == nil {
return cl.createVirtualService(ctx, newVirtualService)
}
if !hasher.IsAnnotationDifferent(oldVirtualService, newVirtualService) {
return nil
}
return cl.updateVirtualService(ctx, oldVirtualService, newVirtualService)
}
func (cl *Client) createVirtualService(ctx context.Context, virtualService *istiov1beta1.VirtualService) error {
if virtualService == nil {
return errors.New("can't create virtual service based on nil object")
}
_, err := cl.IstioClientset.NetworkingV1beta1().VirtualServices(cl.Owner.GetNamespace()).Create(ctx, virtualService, metav1.CreateOptions{})
if err != nil {
log.Info("failed to create virtual service", "name", virtualService.GetName(), "error", err.Error())
return errors.WithStack(err)
}
return nil
}
func (cl *Client) updateVirtualService(ctx context.Context, oldVirtualService, newVirtualService *istiov1beta1.VirtualService) error {
if oldVirtualService == nil || newVirtualService == nil {
return errors.New("can't update service entry based on nil object")
}
newVirtualService.ObjectMeta.ResourceVersion = oldVirtualService.ObjectMeta.ResourceVersion
_, err := cl.IstioClientset.NetworkingV1beta1().VirtualServices(cl.Owner.GetNamespace()).Update(ctx, newVirtualService, metav1.UpdateOptions{})
if err != nil {
log.Info("failed to update virtual service", "name", newVirtualService.GetName(), "error", err.Error())
return errors.WithStack(err)
}
return nil
}
func (cl *Client) DeleteVirtualService(ctx context.Context, name string) error {
err := cl.IstioClientset.NetworkingV1beta1().
VirtualServices(cl.Owner.GetNamespace()).
Delete(ctx, name, metav1.DeleteOptions{})
if !k8serrors.IsNotFound(err) {
log.Info("failed to remove virtual service", "name", name)
return errors.WithStack(err)
}
return nil
}
func (cl *Client) GetServiceEntry(ctx context.Context, name string) (*istiov1beta1.ServiceEntry, error) {
serviceEntry, err := cl.IstioClientset.NetworkingV1beta1().ServiceEntries(cl.Owner.GetNamespace()).Get(ctx, name, metav1.GetOptions{})
if k8serrors.IsNotFound(err) {
return nil, nil //nolint: nilnil
} else if err != nil {
log.Info("failed to get current service entry", "name", name, "error", err.Error())
return nil, errors.WithStack(err)
}
return serviceEntry, nil
}
func (cl *Client) CreateOrUpdateServiceEntry(ctx context.Context, newServiceEntry *istiov1beta1.ServiceEntry) error {
if newServiceEntry == nil {
return errors.New("can't create service entry based on nil object")
}
// the owner reference is created before the hash annotation is added
if err := controllerutil.SetControllerReference(cl.Owner, newServiceEntry, cl.Scheme); err != nil {
return errors.WithStack(err)
}
delete(newServiceEntry.Annotations, hasher.AnnotationHash)
err := hasher.AddAnnotation(newServiceEntry)
if err != nil {
return errors.WithMessage(err, "failed to generate and hash annotation for service entry")
}
oldServiceEntry, err := cl.GetServiceEntry(ctx, newServiceEntry.Name)
if err != nil {
return err
}
if oldServiceEntry == nil {
return cl.createServiceEntry(ctx, newServiceEntry)
}
if !hasher.IsAnnotationDifferent(oldServiceEntry, newServiceEntry) {
return nil
}
return cl.updateServiceEntry(ctx, oldServiceEntry, newServiceEntry)
}
func (cl *Client) createServiceEntry(ctx context.Context, serviceEntry *istiov1beta1.ServiceEntry) error {
if serviceEntry == nil {
return errors.New("can't create service entry based on nil object")
}
_, err := cl.IstioClientset.NetworkingV1beta1().ServiceEntries(cl.Owner.GetNamespace()).Create(ctx, serviceEntry, metav1.CreateOptions{})
if err != nil {
log.Info("failed to create service entry", "name", serviceEntry.GetName(), "error", err.Error())
return errors.WithStack(err)
}
return nil
}
func (cl *Client) updateServiceEntry(ctx context.Context, oldServiceEntry, newServiceEntry *istiov1beta1.ServiceEntry) error {
if oldServiceEntry == nil || newServiceEntry == nil {
return errors.New("can't update service entry based on nil object")
}
newServiceEntry.ObjectMeta.ResourceVersion = oldServiceEntry.ObjectMeta.ResourceVersion
_, err := cl.IstioClientset.NetworkingV1beta1().ServiceEntries(cl.Owner.GetNamespace()).Update(ctx, newServiceEntry, metav1.UpdateOptions{})
if err != nil {
log.Info("failed to update service entry", "name", newServiceEntry.GetName(), "error", err.Error())
return errors.WithStack(err)
}
return nil
}
func (cl *Client) DeleteServiceEntry(ctx context.Context, name string) error {
err := cl.IstioClientset.NetworkingV1beta1().
ServiceEntries(cl.Owner.GetNamespace()).
Delete(ctx, name, metav1.DeleteOptions{})
if !k8serrors.IsNotFound(err) {
log.Info("failed to remove service entry", "name", name)
return errors.WithStack(err)
}
return nil
}