-
Notifications
You must be signed in to change notification settings - Fork 23
/
autoscale-discovery.go
189 lines (167 loc) · 6.02 KB
/
autoscale-discovery.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
package discovery
import (
"context"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
"golang.org/x/exp/slices"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/cache"
"github.com/fluxninja/aperture/v2/pkg/etcd/election"
"github.com/fluxninja/aperture/v2/pkg/k8s"
"github.com/fluxninja/aperture/v2/pkg/log"
panichandler "github.com/fluxninja/aperture/v2/pkg/panic-handler"
)
func newControlPointDiscovery(election *election.Election, k8sClient k8s.K8sClient, controlPointStore AutoScaleControlPointStore) (*controlPointDiscovery, error) {
cpd := &controlPointDiscovery{
election: election,
controlPointStore: controlPointStore,
discoveryClient: k8sClient.GetClientSet().DiscoveryClient,
dynamicClient: k8sClient.GetDynamicClient(),
}
return cpd, nil
}
// controlPointDiscovery is a struct that helps with Kubernetes control point discovery.
type controlPointDiscovery struct {
waitGroup panichandler.WaitGroup
ctx context.Context
cancel context.CancelFunc
controlPointStore AutoScaleControlPointStore
discoveryClient discovery.DiscoveryInterface
dynamicClient dynamic.Interface
election *election.Election
}
// Start starts the Kubernetes control point discovery.
func (cpd *controlPointDiscovery) start() {
cpd.ctx, cpd.cancel = context.WithCancel(context.Background())
cpd.waitGroup.Go(func() {
operation := func() error {
// Proceed only if we are the leader
// FIXME this goroutine leaks if we never become a leader
for {
if cpd.election.IsLeader() {
// Proceed
break
} else {
// Check again in 5 seconds
time.Sleep(5 * time.Second)
}
}
// Discover all resources with /scale subresource
_, apiResourceListList, err := cpd.discoveryClient.ServerGroupsAndResources()
if err != nil {
log.Error().Err(err).Msg("Unable to get API resource list")
return err
}
scalableResources := []string{}
for _, apiResourceList := range apiResourceListList {
for _, apiResource := range apiResourceList.APIResources {
if apiResource.Kind == "Scale" {
// Get its parent resource
parentResource := strings.TrimSuffix(apiResource.Name, "/scale")
scalableResources = append(scalableResources, parentResource)
}
}
}
groupVersionResourceSet := make(map[schema.GroupVersionResource]string)
log.Info().Msgf("Scalable resources: %v", scalableResources)
for _, apiResourceList := range apiResourceListList {
for _, apiResource := range apiResourceList.APIResources {
// Check if apiResource.Name belongs to scalableResources
if slices.Contains(scalableResources, apiResource.Name) {
groupVersion, parseErr := schema.ParseGroupVersion(apiResourceList.GroupVersion)
if parseErr != nil {
log.Error().Err(parseErr).Msg("Unable to parse group version")
return parseErr
}
groupVersionResource := schema.GroupVersionResource{
Group: groupVersion.Group,
Version: groupVersion.Version,
Resource: apiResource.Name,
}
// Add to groupVersionResourceSet
groupVersionResourceSet[groupVersionResource] = apiResource.Kind
}
}
}
// Track each scalable resource
for groupVersionResource, kind := range groupVersionResourceSet {
log.Info().Msgf("Starting watch for Group: %s, Version: %s, Resource: %s", groupVersionResource.Group, groupVersionResource.Version, groupVersionResource.Resource)
resourceInterface := cpd.dynamicClient.Resource(groupVersionResource)
// watch for changes
_, controller := cache.NewInformer(
&cache.ListWatch{
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
return resourceInterface.List(cpd.ctx, options)
},
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
return resourceInterface.Watch(cpd.ctx, options)
},
},
&unstructured.Unstructured{},
0,
cpd.createResourceEventHandlerFuncs(groupVersionResource, kind),
)
resourceWatcher := resourceWatcher{
controller: controller,
groupVersionResource: groupVersionResource,
ctx: cpd.ctx,
}
resourceWatcher.goRun()
}
return nil
}
boff := backoff.NewConstantBackOff(5 * time.Second)
_ = backoff.Retry(operation, backoff.WithContext(boff, cpd.ctx))
})
}
func (cpd *controlPointDiscovery) createResourceEventHandlerFuncs(groupVersionResource schema.GroupVersionResource, kind string) cache.ResourceEventHandlerFuncs {
controlPointFromObject := func(obj interface{}) AutoScaleControlPoint {
// read the name of the resource
name := obj.(*unstructured.Unstructured).GetName()
namespace := obj.(*unstructured.Unstructured).GetNamespace()
return AutoScaleControlPoint{
Group: groupVersionResource.Group,
Version: groupVersionResource.Version,
Kind: kind,
Name: name,
Namespace: namespace,
}
}
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
controlPoint := controlPointFromObject(obj)
cpd.controlPointStore.Add(controlPoint)
},
UpdateFunc: func(_, obj interface{}) {
controlPoint := controlPointFromObject(obj)
cpd.controlPointStore.Update(controlPoint)
},
DeleteFunc: func(obj interface{}) {
controlPoint := controlPointFromObject(obj)
cpd.controlPointStore.Delete(controlPoint)
},
}
}
func (cpd *controlPointDiscovery) stop() {
cpd.cancel()
cpd.waitGroup.Wait()
}
type resourceWatcher struct {
controller cache.Controller
ctx context.Context
groupVersionResource schema.GroupVersionResource
}
func (rw *resourceWatcher) goRun() {
panichandler.Go(func() {
// Run controller
rw.controller.Run(rw.ctx.Done())
log.Info().Msg("Stopped kubernetes control point watcher for resource " + rw.groupVersionResource.String())
})
}