-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.go
More file actions
230 lines (190 loc) · 6.17 KB
/
resources.go
File metadata and controls
230 lines (190 loc) · 6.17 KB
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
/*
Copyright 2024 DigitalOcean
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 do
import (
"context"
"fmt"
"math/rand"
"net/http"
"time"
"github.com/digitalocean/godo"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
v1informers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
v1lister "k8s.io/client-go/listers/core/v1"
"k8s.io/klog/v2"
)
const (
controllerSyncTagsPeriod = 15 * time.Minute
syncTagsTimeout = 1 * time.Minute
)
type tagMissingError struct {
error
}
type publicAccessFirewall struct {
name string
tags []string
}
type resources struct {
clusterID string
clusterVPCID string
firewall publicAccessFirewall
gclient *godo.Client
kclient kubernetes.Interface
}
// newResources initializes a new resources instance.
// kclient can only be set during the cloud. Initialize call since that is when
// the cloud provider framework provides us with a clientset. Fortunately, the
// initialization order guarantees that kclient won't be consumed prior to it
// being set.
func newResources(clusterID, clusterVPCID string, publicAccessFW publicAccessFirewall, gclient *godo.Client) *resources {
return &resources{
clusterID: clusterID,
clusterVPCID: clusterVPCID,
firewall: publicAccessFW,
gclient: gclient,
}
}
type syncer interface {
Sync(name string, period time.Duration, initialDelay time.Duration, stopCh <-chan struct{}, fn func() error)
}
type tickerSyncer struct{}
func (s *tickerSyncer) Sync(name string, period time.Duration, initialDelay time.Duration, stopCh <-chan struct{}, fn func() error) {
ticker := time.NewTicker(period)
defer ticker.Stop()
// manually call to avoid initial tick delay
if err := fn(); err != nil {
klog.Errorf("%s failed: %s", name, err)
}
select {
case <-time.After(initialDelay):
case <-stopCh:
return
}
for {
select {
case <-ticker.C:
if err := fn(); err != nil {
klog.Errorf("%s failed: %s", name, err)
}
case <-stopCh:
return
}
}
}
// ResourcesController is responsible for managing DigitalOcean cloud
// resources. It maintains a local state of the resources and
// synchronizes when needed.
type ResourcesController struct {
kclient kubernetes.Interface
svcLister v1lister.ServiceLister
resources *resources
syncer syncer
}
// NewResourcesController returns a new resource controller.
func NewResourcesController(r *resources, inf v1informers.ServiceInformer, client kubernetes.Interface) *ResourcesController {
r.kclient = client
return &ResourcesController{
resources: r,
kclient: client,
svcLister: inf.Lister(),
syncer: &tickerSyncer{},
}
}
// Run starts the resources controller loop.
func (r *ResourcesController) Run(stopCh <-chan struct{}) {
if r.resources.clusterID == "" {
klog.Info("No cluster ID configured -- skipping cluster dependent syncers.")
return
}
go r.syncer.Sync("tags syncer", controllerSyncTagsPeriod, time.Second*time.Duration(rand.Int31n(600)), stopCh, r.syncTags)
}
// syncTags synchronizes tags. Currently, this is only needed to associate
// cluster ID tags with LoadBalancer resources.
func (r *ResourcesController) syncTags() error {
ctx, cancel := context.WithTimeout(context.Background(), syncTagsTimeout)
defer cancel()
svcs, err := r.svcLister.List(labels.Everything())
if err != nil {
return fmt.Errorf("failed to list services: %s", err)
}
var lbSvcs []*corev1.Service
for _, svc := range svcs {
if svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
lbSvcs = append(lbSvcs, svc)
}
}
if len(lbSvcs) == 0 {
klog.V(5).Info("No load-balancers to tag because no LoadBalancer-typed services exist")
return nil
}
lbs, err := allLoadBalancerList(ctx, r.resources.gclient)
if err != nil {
return fmt.Errorf("failed to list load-balancers: %s", err)
}
// Collect tag resources for known load-balancers (i.e., services with
// type=LoadBalancer that either have our own LB ID annotation set or go by
// a matching name).
var res []godo.Resource
for _, svc := range lbSvcs {
id := findLoadBalancerID(svc, lbs)
// Load-balancers that have no LB ID set yet and were renamed directly
// (e.g., via the cloud control panel) would still be missed, so check
// again if we have found an ID.
if id != "" {
res = append(res, godo.Resource{
ID: id,
Type: godo.ResourceType(godo.LoadBalancerResourceType),
})
}
}
if len(res) == 0 {
return nil
}
tag := buildK8sTag(r.resources.clusterID)
// Tag collected resources with the cluster ID. If the tag does not exist
// (for reasons outlined below), we will create it and retry tagging again.
err = r.tagResources(res)
if _, ok := err.(tagMissingError); ok {
// Cluster ID tag has not been created yet. This should have happen
// when we set the tag on LB creation. For LBs that have been created
// prior to CCM using cluster IDs, however, we need to create the tag
// explicitly.
_, _, err = r.resources.gclient.Tags.Create(ctx, &godo.TagCreateRequest{
Name: tag,
})
if err != nil {
return fmt.Errorf("failed to create tag %q: %s", tag, err)
}
// Try tagging again, which should not fail anymore due to a missing
// tag.
err = r.tagResources(res)
}
if err != nil {
return fmt.Errorf("failed to tag LB resource(s) %v with tag %q: %s", res, tag, err)
}
return nil
}
func (r *ResourcesController) tagResources(res []godo.Resource) error {
ctx, cancel := context.WithTimeout(context.Background(), syncTagsTimeout)
defer cancel()
tag := buildK8sTag(r.resources.clusterID)
resp, err := r.resources.gclient.Tags.TagResources(ctx, tag, &godo.TagResourcesRequest{
Resources: res,
})
if resp != nil && resp.StatusCode == http.StatusNotFound {
return tagMissingError{fmt.Errorf("tag %q does not exist", tag)}
}
return err
}