This repository has been archived by the owner on May 9, 2019. It is now read-only.
/
controller.go
283 lines (212 loc) · 6.76 KB
/
controller.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
/*
Copyright (C) 2016 Black Duck Software, Inc.
http://www.blackducksoftware.com/
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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 controller
import (
"log"
"os"
"sync"
bdscommon "github.com/blackducksoftware/ose-scanner/common"
osclient "github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/spf13/pflag"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/runtime"
)
type HubParams struct {
Config *bdscommon.HubConfig
Scanner string
Workers int
Version string
}
type Controller struct {
openshiftClient *osclient.Client
kubeClient *kclient.Client
mapper meta.RESTMapper
typer runtime.ObjectTyper
f *clientcmd.Factory
jobQueue chan Job
wait sync.WaitGroup
images map[string]*ScanImage
annotation *bdscommon.Annotator
sync.RWMutex
hubParams *HubParams
}
func NewController(os *osclient.Client, kc *kclient.Client, hub *HubParams) *Controller {
f := clientcmd.New(pflag.NewFlagSet("empty", pflag.ContinueOnError))
mapper, typer := f.Object(false)
jobQueue := make(chan Job, hub.Workers)
return &Controller{
openshiftClient: os,
kubeClient: kc,
mapper: mapper,
typer: typer,
f: f,
jobQueue: jobQueue,
images: make(map[string]*ScanImage),
annotation: bdscommon.NewAnnotator(hub.Version, hub.Config.Host),
hubParams: hub,
}
}
func (c *Controller) Start(arb *Arbiter) {
log.Println("Starting controller ....")
dispatcher := NewDispatcher(c.jobQueue, c.hubParams.Workers)
arb.heartbeat()
dispatcher.Run(arb)
return
}
func (c *Controller) Watch() {
log.Println("Starting watcher ....")
watcher := NewWatcher(c.openshiftClient, c)
watcher.Run()
return
}
func (c *Controller) Stop() {
log.Println("Waiting for scan queue to drain before stopping...")
c.wait.Wait()
log.Println("Scan queue empty.")
log.Println("Controller stopped.")
return
}
func (c *Controller) Load(done <-chan struct{}) {
log.Println("Starting load of existing images ...")
c.getImages(done)
log.Println("Done load of existing images.")
return
}
func (c *Controller) AddImage(ID string, Reference string) {
c.Lock()
defer c.Unlock()
image, ok := c.images[Reference]
if !ok {
imageItem := newScanImage(ID, Reference, c.annotation, c.hubParams.Config, c.hubParams.Scanner)
if imageItem == nil {
log.Printf("Image %s:%s not present in node. Not adding to image map.\n", Reference, ID)
return
}
c.queueImage(imageItem, Reference)
} else if ID != image.imageId {
log.Printf("Image %s already in image map with imageId %s but have imageId %s\n", Reference, image.imageId, ID)
validOriginal := image.exists()
originalScanned := image.scanned
if !validOriginal {
log.Printf("Removing original imageId %s as it no longer exists on this node.\n", image.imageId)
delete(c.images, Reference)
}
newImage := newScanImage(ID, Reference, c.annotation, c.hubParams.Config, c.hubParams.Scanner)
if newImage == nil {
log.Printf("Requested imageId %s does not exist on this node. Skipping.\n", Reference)
return
} else if validOriginal {
// we have both an original and an update which is valid
if originalScanned {
// since we see the original, and its been scanned, and it has the same pullspec, then we replace it and refresh
log.Printf("Replacing queued and scanned imageId %s with %s for image %s\n", image.imageId, newImage.imageId, Reference)
c.queueImage(newImage, Reference)
} else if image.imageId != newImage.imageId {
/*
* in this case we have both a unscanned original and an unscanned update
* not quite certain what to do here just yet TODO
*/
log.Printf("***ImageId %s hasn't been scanned, but we've found imageId %s for image %s as a replacement ****\n", image.imageId, newImage.imageId, Reference)
c.queueImage(newImage, Reference)
}
} else {
// add the image to queue
c.queueImage(newImage, Reference)
}
}
}
func (c *Controller) queueImage(imageItem *ScanImage, Reference string) {
job := Job{
ScanImage: imageItem,
controller: c,
}
ok, info := job.GetAnnotationInfo()
if !ok {
log.Printf("Error testing prior image status for image %s\n", imageItem.digest)
}
if !c.annotation.IsScanNeeded(info, imageItem.sha, c.hubParams.Config) {
log.Printf("Image %s previously scanned. Skipping scan.\n", imageItem.digest)
imageItem.scanned = true
return
}
c.images[Reference] = imageItem
log.Printf("Added %s:%s to image map\n", imageItem.digest, imageItem.imageId)
job.Load()
c.jobQueue <- job
}
func (c *Controller) imageScanned(Reference string) bool {
c.Lock()
defer c.Unlock()
imageItem, ok := c.images[Reference]
if !ok {
log.Printf("Requested scan status for unknown image %s\n", Reference)
return true
}
return imageItem.scanned
}
func (c *Controller) RemoveImage(ID string, Reference string) {
c.Lock()
defer c.Unlock()
_, ok := c.images[Reference]
if ok {
delete(c.images, Reference)
log.Printf("Removed %s from map\n", Reference)
}
}
func (c *Controller) getImages(done <-chan struct{}) {
imageList, err := c.openshiftClient.Images().List(kapi.ListOptions{})
if err != nil {
log.Println(err)
return
}
if imageList == nil {
log.Println("No images")
return
}
for _, image := range imageList.Items {
c.AddImage(image.DockerImageMetadata.ID, image.DockerImageReference)
}
return
}
func (c *Controller) ValidateConfig() bool {
hubServer := bdscommon.NewHubServer(c.hubParams.Config)
defer hubServer.Logout()
return hubServer.Login()
}
func (c *Controller) ValidateDockerConfig() bool {
docker := NewDocker()
if docker.client == nil {
log.Printf("Unable to connect to Docker runtime\n")
return false
}
_, err := docker.client.Info()
if err != nil {
log.Printf("Unable to connect to Docker runtime. %s\n", err)
return false
}
log.Printf("Validated Docker runtime connection\n")
return true
}
func init() {
log.SetFlags(log.LstdFlags)
log.SetOutput(os.Stdout)
}