This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
crdinstaller.go
205 lines (180 loc) · 7.04 KB
/
crdinstaller.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
/*
Copyright (C) 2019 Synopsys, Inc.
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 blackduck
import (
"time"
horizonapi "github.com/blackducksoftware/horizon/pkg/api"
"github.com/blackducksoftware/horizon/pkg/components"
horizon "github.com/blackducksoftware/horizon/pkg/deployer"
v1 "github.com/blackducksoftware/synopsys-operator/pkg/api/blackduck/v1"
hubclientset "github.com/blackducksoftware/synopsys-operator/pkg/blackduck/client/clientset/versioned"
hubinformerv2 "github.com/blackducksoftware/synopsys-operator/pkg/blackduck/client/informers/externalversions/blackduck/v1"
"github.com/blackducksoftware/synopsys-operator/pkg/protoform"
"github.com/blackducksoftware/synopsys-operator/pkg/util"
"github.com/juju/errors"
routeclient "github.com/openshift/client-go/route/clientset/versioned/typed/route/v1"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
// CRDInstaller defines the specification for the CRD
type CRDInstaller struct {
config *protoform.Config
kubeConfig *rest.Config
kubeClient *kubernetes.Clientset
defaults interface{}
resyncPeriod time.Duration
indexers cache.Indexers
infomer cache.SharedIndexInformer
queue workqueue.RateLimitingInterface
handler *Handler
controller *Controller
hubClient *hubclientset.Clientset
threadiness int
stopCh <-chan struct{}
}
// NewCRDInstaller will create a CRD installer configuration
func NewCRDInstaller(config *protoform.Config, kubeConfig *rest.Config, kubeClient *kubernetes.Clientset, defaults interface{}, stopCh <-chan struct{}) *CRDInstaller {
crdInstaller := &CRDInstaller{config: config, kubeConfig: kubeConfig, kubeClient: kubeClient, defaults: defaults, threadiness: config.Threadiness, stopCh: stopCh}
crdInstaller.resyncPeriod = time.Duration(config.ResyncIntervalInSeconds) * time.Second
crdInstaller.indexers = cache.Indexers{}
return crdInstaller
}
// CreateClientSet will create the CRD client
func (c *CRDInstaller) CreateClientSet() error {
hubClient, err := hubclientset.NewForConfig(c.kubeConfig)
if err != nil {
return errors.Trace(err)
}
c.hubClient = hubClient
return nil
}
// Deploy will deploy the CRD and other relevant components
func (c *CRDInstaller) Deploy() error {
deployer, err := horizon.NewDeployer(c.kubeConfig)
if err != nil {
return err
}
// creation of default blackduck nginx certificate secret
certificate, key := CreateSelfSignedCert()
certificateSecret := components.NewSecret(horizonapi.SecretConfig{
Namespace: c.config.Namespace,
Name: "blackduck-certificate",
Type: horizonapi.SecretTypeOpaque,
})
certificateSecret.AddData(map[string][]byte{"WEBSERVER_CUSTOM_CERT_FILE": []byte(certificate), "WEBSERVER_CUSTOM_KEY_FILE": []byte(key)})
deployer.AddComponent(horizonapi.SecretComponent, certificateSecret)
_, err = util.GetSecret(c.kubeClient, c.config.Namespace, "blackduck-secret")
if err != nil {
sealKey, err := util.GetRandomString(32)
if err != nil {
log.Panicf("unable to generate the random string for SEAL_KEY due to %+v", err)
}
// creation of blackduck secret to store the seal key
operatorSecret := components.NewSecret(horizonapi.SecretConfig{
Name: "blackduck-secret",
Namespace: c.config.Namespace,
Type: horizonapi.SecretTypeOpaque,
})
operatorSecret.AddData(map[string][]byte{
"SEAL_KEY": []byte(sealKey),
})
operatorSecret.AddLabels(map[string]string{"app": "synopsys-operator", "component": "operator"})
deployer.AddComponent(horizonapi.SecretComponent, operatorSecret)
} else {
log.Warn("blackduck-secret already exists")
}
err = deployer.Run()
if err != nil {
log.Errorf("unable to create the black duck secrets due to %+v", err)
}
time.Sleep(5 * time.Second)
return err
}
// PostDeploy will call after deploying the CRD
func (c *CRDInstaller) PostDeploy() {
}
// CreateInformer will create a informer for the CRD
func (c *CRDInstaller) CreateInformer() {
c.infomer = hubinformerv2.NewBlackduckInformer(
c.hubClient,
c.config.CrdNamespace,
c.resyncPeriod,
c.indexers,
)
}
// CreateQueue will create a queue to process the CRD
func (c *CRDInstaller) CreateQueue() {
// create a new queue so that when the informer gets a resource that is either
// a result of listing or watching, we can add an idenfitying key to the queue
// so that it can be handled in the handler
c.queue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
}
// AddInformerEventHandler will add the event handlers for the informers
func (c *CRDInstaller) AddInformerEventHandler() {
c.infomer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
// convert the resource object into a key (in this case
// we are just doing it in the format of 'namespace/name')
key, err := cache.MetaNamespaceKeyFunc(obj)
if err == nil {
// add the key to the queue for the handler to get
c.queue.Add(key)
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
key, err := cache.MetaNamespaceKeyFunc(newObj)
if err == nil {
c.queue.Add(key)
}
},
DeleteFunc: func(obj interface{}) {
// DeletionHandlingMetaNamsespaceKeyFunc is a helper function that allows
// us to check the DeletedFinalStateUnknown existence in the event that
// a resource was deleted but it is still contained in the index
//
// this then in turn calls MetaNamespaceKeyFunc
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
log.Infof("delete hub: %s: %+v", key, obj)
if err == nil {
c.queue.Add(key)
}
},
})
}
// CreateHandler will create a CRD handler
func (c *CRDInstaller) CreateHandler() {
var routeClient *routeclient.RouteV1Client
if util.IsOpenshift(c.kubeClient) {
routeClient = util.GetRouteClient(c.kubeConfig, c.kubeClient, c.config.Namespace)
}
c.handler = NewHandler(c.config, c.kubeConfig, c.kubeClient, c.hubClient, c.defaults.(*v1.BlackduckSpec), make(chan bool, 1), nil, routeClient)
}
// CreateController will create a CRD controller
func (c *CRDInstaller) CreateController() {
c.controller = NewController(log.NewEntry(log.New()), c.queue, c.infomer, c.handler)
}
// Run will run the CRD controller
func (c *CRDInstaller) Run() {
go c.controller.Run(c.threadiness, c.stopCh)
}
// PostRun will run post CRD controller execution
func (c *CRDInstaller) PostRun() {
}