-
Notifications
You must be signed in to change notification settings - Fork 1
/
census.go
289 lines (255 loc) · 8.79 KB
/
census.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
// Package census surveys container image names in a Kubernetes cluster.
package census
import (
"context"
"fmt"
"io"
"log"
"sort"
"github.com/DataDog/datadog-go/statsd"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchV1beta1 "k8s.io/api/batch/v1beta1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/pager"
)
// A Taker surveys container image names in a Kubernetes cluster.
type Taker interface {
SurveyDeployedImages(ctx context.Context) (deployed []string, err error)
}
// PodSpecLister is implemented for resource kinds which contain a PodSpec and
// can be listed via the Kubernetes API.
type PodSpecLister interface {
// List returns the result of a List method on the clientset for the
// resource kind associated with the PodSpecLister.
List(ctx context.Context, clientset kubernetes.Interface) (runtime.Object, error)
// GetPodSpec returns the PodSpec associated with obj, which will be of
// the same type as the elements of the list returned by the List
// method.
GetPodSpec(ctx context.Context, obj runtime.Object) (v1.PodSpec, error)
}
// CronJobLister lists the PodSpecs of all CronJobs in a Kubernetes cluster.
var CronJobLister PodSpecLister = &cronJobLister{}
// DaemonSetLister lists the PodSpecs of all DaemonSets in a Kubernetes cluster.
var DaemonSetLister PodSpecLister = &daemonSetLister{}
// DeploymentLister lists the PodSpecs of all Deployments in a Kubernetes cluster.
var DeploymentLister PodSpecLister = &deploymentLister{}
// JobLister lists the PodSpecs of all Jobs in a Kubernetes cluster.
var JobLister PodSpecLister = &jobLister{}
// StatefulSetLister lists the PodSpecs of all StatefulSets in a Kubernetes cluster.
var StatefulSetLister PodSpecLister = &statefulSetLister{}
// A Client is a configurable Taker wrapping kubernetes.Interface.
type Client struct {
clientset kubernetes.Interface
listers []PodSpecLister
pageSize uint
logger *log.Logger
statsd statsd.ClientInterface
}
// An Option is an option applied when creating a Client.
type Option func(c *Client)
// WithLister adds a PodSpecLister for a Client to survey.
func WithLister(lister PodSpecLister) Option {
return func(c *Client) {
c.listers = append(c.listers, lister)
}
}
// WithPageSize sets the maximum number of responses a Client should request in
// a single Kubernetes API call.
func WithPageSize(size uint) Option {
return func(c *Client) {
if size <= 0 {
return
}
c.pageSize = size
}
}
// WithLogger sets a logger for a Client to output to.
func WithLogger(logger *log.Logger) Option {
return func(c *Client) { c.logger = logger }
}
// WithStatsdClient sets a statsd client to use to report metrics from a Client.
func WithStatsdClient(client statsd.ClientInterface) Option {
return func(c *Client) { c.statsd = client }
}
// NewDefaultClient returns a Taker that surveys CronJob, DaemonSet, Deployment,
// Job, and StatefulSet resources from clientset.
func NewDefaultClient(clientset kubernetes.Interface, opts ...Option) (*Client, error) {
opts = append(
opts,
WithLister(DeploymentLister),
WithLister(DaemonSetLister),
WithLister(CronJobLister),
WithLister(JobLister),
WithLister(StatefulSetLister),
)
return NewClient(clientset, opts...)
}
// NewClient returns a Taker that surveys resources from clientset. If no
// WithLister option is included in opts, no resources will be surveyed.
func NewClient(clientset kubernetes.Interface, opts ...Option) (*Client, error) {
if clientset == nil {
return nil, fmt.Errorf("clientset must not be nil")
}
c := &Client{
clientset: clientset,
logger: log.New(io.Discard, "", 0),
statsd: &statsd.NoOpClient{},
}
for _, opt := range opts {
opt(c)
}
return c, nil
}
// SurveyDeployedImages returns the image references of the containers and init containers
// of the PodSpecs surveyed by t.
func (c *Client) SurveyDeployedImages(ctx context.Context) ([]string, error) {
var span tracer.Span
span, ctx = tracer.StartSpanFromContext(ctx, "census.Client.SurveyDeployedImages")
defer span.Finish()
defer c.statsd.Flush()
imageSet := make(map[string]interface{})
for _, l := range c.listers {
pager := pager.New(func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
return l.List(ctx, c.clientset)
})
if err := pager.EachListItem(
ctx,
metav1.ListOptions{
Limit: int64(c.pageSize),
},
func(obj runtime.Object) error {
spec, err := l.GetPodSpec(ctx, obj)
if err != nil {
return fmt.Errorf("error getting PodSpec from resource: %w", err)
}
containers := append(spec.Containers, spec.InitContainers...)
for _, c := range containers {
imageSet[c.Image] = nil
}
return nil
},
); err != nil {
span.Finish(tracer.WithError(err))
return nil, fmt.Errorf("error listing resources: %w", err)
}
c.logger.Printf("listed images from PodSpecLister %T", l)
}
imageRefs := make([]string, 0, len(imageSet))
for image := range imageSet {
imageRefs = append(imageRefs, image)
}
sort.Sort(sort.StringSlice(imageRefs))
c.logger.Printf("surveyed %d unique deployed images", len(imageRefs))
c.statsd.Gauge("census.survey_deployed_images", float64(len(imageRefs)), nil, 1)
return imageRefs, nil
}
type cronJobLister struct{}
func (l *cronJobLister) List(ctx context.Context, clientset kubernetes.Interface) (runtime.Object, error) {
list, err := clientset.BatchV1beta1().CronJobs("").List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing CronJobs: %w", err)
}
return list, nil
}
func (l *cronJobLister) GetPodSpec(ctx context.Context, obj runtime.Object) (v1.PodSpec, error) {
if obj == nil {
return v1.PodSpec{}, fmt.Errorf("obj must not be nil")
}
cronJob, ok := obj.(*batchV1beta1.CronJob)
if !ok {
return v1.PodSpec{}, fmt.Errorf(
"error asserting type of list item as CronJob: got type %T",
cronJob,
)
}
return cronJob.Spec.JobTemplate.Spec.Template.Spec, nil
}
type daemonSetLister struct{}
func (l *daemonSetLister) List(ctx context.Context, clientset kubernetes.Interface) (runtime.Object, error) {
list, err := clientset.AppsV1().DaemonSets("").List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing DaemonSets: %w", err)
}
return list, nil
}
func (l *daemonSetLister) GetPodSpec(ctx context.Context, obj runtime.Object) (v1.PodSpec, error) {
if obj == nil {
return v1.PodSpec{}, fmt.Errorf("obj must not be nil")
}
daemonSet, ok := obj.(*appsv1.DaemonSet)
if !ok {
return v1.PodSpec{}, fmt.Errorf(
"error asserting type of list item as DaemonSet: got type %T",
daemonSet,
)
}
return daemonSet.Spec.Template.Spec, nil
}
type deploymentLister struct{}
func (l *deploymentLister) List(ctx context.Context, clientset kubernetes.Interface) (runtime.Object, error) {
list, err := clientset.AppsV1().Deployments("").List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing Deployments: %w", err)
}
return list, nil
}
func (l *deploymentLister) GetPodSpec(ctx context.Context, obj runtime.Object) (v1.PodSpec, error) {
if obj == nil {
return v1.PodSpec{}, fmt.Errorf("obj must not be nil")
}
deployment, ok := obj.(*appsv1.Deployment)
if !ok {
return v1.PodSpec{}, fmt.Errorf(
"error asserting type of list item as Deployment: got type %T",
deployment,
)
}
return deployment.Spec.Template.Spec, nil
}
type jobLister struct{}
func (l *jobLister) List(ctx context.Context, clientset kubernetes.Interface) (runtime.Object, error) {
list, err := clientset.BatchV1().Jobs("").List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing Jobs: %w", err)
}
return list, nil
}
func (l *jobLister) GetPodSpec(ctx context.Context, obj runtime.Object) (v1.PodSpec, error) {
if obj == nil {
return v1.PodSpec{}, fmt.Errorf("obj must not be nil")
}
job, ok := obj.(*batchv1.Job)
if !ok {
return v1.PodSpec{}, fmt.Errorf(
"error asserting type of list item as Job: got type %T",
job,
)
}
return job.Spec.Template.Spec, nil
}
type statefulSetLister struct{}
func (l *statefulSetLister) List(ctx context.Context, clientset kubernetes.Interface) (runtime.Object, error) {
list, err := clientset.AppsV1().StatefulSets("").List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing DaemonSets: %w", err)
}
return list, nil
}
func (l *statefulSetLister) GetPodSpec(ctx context.Context, obj runtime.Object) (v1.PodSpec, error) {
if obj == nil {
return v1.PodSpec{}, fmt.Errorf("obj must not be nil")
}
statefulSet, ok := obj.(*appsv1.StatefulSet)
if !ok {
return v1.PodSpec{}, fmt.Errorf(
"error asserting type of list item as StatefulSet: got type %T",
statefulSet,
)
}
return statefulSet.Spec.Template.Spec, nil
}