This repository has been archived by the owner on May 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
cmd.go
213 lines (169 loc) · 5.72 KB
/
cmd.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
/*
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 main
import (
"fmt"
"log"
"os"
"strings"
"time"
"crypto/tls"
"net/http"
bdscommon "github.com/blackducksoftware/ose-scanner/common"
"github.com/blackducksoftware/ose-scanner/arbiter/pkg/arbiter"
_ "github.com/openshift/origin/pkg/api/install"
osclient "github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/spf13/pflag"
"k8s.io/kubernetes/pkg/client/restclient"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
)
var bds_version string
var build_num string
var hub arbiter.HubParams
func main() {
pflag.Parse()
if !checkExpectedCmdlineParams() {
os.Exit(1)
}
config, err := restclient.InClusterConfig()
if err != nil {
log.Printf("Error getting in cluster config. Fallback to native config. Error message: %s", err)
config, err = clientcmd.DefaultClientConfig(pflag.NewFlagSet("empty", pflag.ContinueOnError)).ClientConfig()
if err != nil {
log.Printf("Error creating default client config: %s", err)
os.Exit(1)
}
}
kubeClient, err := kclient.New(config)
if err != nil {
log.Printf("Error creating cluster config: %s", err)
os.Exit(1)
}
openshiftClient, err := osclient.New(config)
if err != nil {
log.Printf("Error creating OpenShift client: %s", err)
os.Exit(2)
}
arb := arbiter.NewArbiter(openshiftClient, kubeClient, hub)
if !arb.ValidateConfig() {
log.Printf("Hub configuation information isn't valid. Please verify connectivity and values.")
os.Exit(1)
}
done := make(chan struct{})
defer close(done)
arb.Start()
arb.Load(done)
arb.ListenForControllers()
arb.Watch()
arb.Stop()
}
func init() {
log.SetFlags(log.LstdFlags)
log.SetOutput(os.Stdout)
log.Printf("Initializing Black Duck arbiter with version %s build %s\n", bds_version, build_num)
hub.Config = &bdscommon.HubConfig{}
hub.Version = bds_version
pflag.StringVar(&hub.Config.Host, "h", "REQUIRED", "The hostname of the Black Duck Hub server.")
pflag.StringVar(&hub.Config.Port, "p", "REQUIRED", "The port the Hub is communicating on")
pflag.StringVar(&hub.Config.Scheme, "s", "REQUIRED", "The communication scheme [http,https].")
pflag.StringVar(&hub.Config.User, "u", "REQUIRED", "The Black Duck Hub user")
pflag.StringVar(&hub.Config.Password, "w", "REQUIRED", "Password for the user.")
pflag.StringVar(&hub.Config.Insecure, "i", "OPTIONAL", "Allow insecure TLS.")
}
func checkExpectedCmdlineParams() bool {
if hub.Config.Host == "REQUIRED" {
val := os.Getenv("BDS_HOST")
if val == "" {
log.Println("-h host argument or BDS_HOST environment is required")
pflag.PrintDefaults()
return false
}
hub.Config.Host = val
}
if hub.Config.Port == "REQUIRED" {
val := os.Getenv("BDS_PORT")
if val == "" {
log.Println("-p port argument or BDS_PORT environment is required")
pflag.PrintDefaults()
return false
}
hub.Config.Port = val
}
if hub.Config.Scheme == "REQUIRED" {
val := os.Getenv("BDS_SCHEME")
if val == "" {
log.Println("-s scheme argument or BDS_SCHEME environment is required")
pflag.PrintDefaults()
return false
}
hub.Config.Scheme = val
}
if hub.Config.User == "REQUIRED" {
val := os.Getenv("BDS_USER")
if val == "" {
log.Println("-u username argument or BDS_USER environment is required")
pflag.PrintDefaults()
return false
}
hub.Config.User = val
}
if hub.Config.Password == "REQUIRED" {
val := os.Getenv("BDS_PASSWORD")
if val == "" {
log.Println("-w password argument or BDS_PASSWORD environment is required")
pflag.PrintDefaults()
return false
}
hub.Config.Password = val
}
if (strings.Compare(strings.ToLower(hub.Config.Scheme), "http") == 0 && strings.Compare(hub.Config.Port, "80") == 0) ||
(strings.Compare(strings.ToLower(hub.Config.Scheme), "https") == 0 && strings.Compare(hub.Config.Port, "443") == 0) {
hub.Config.Url = fmt.Sprintf("%s://%s", hub.Config.Scheme, hub.Config.Host)
} else {
hub.Config.Url = fmt.Sprintf("%s://%s:%s", hub.Config.Scheme, hub.Config.Host, hub.Config.Port)
}
insecureSkipVerify := false
if strings.Compare(strings.ToLower(hub.Config.Scheme), "https") == 0 {
if hub.Config.Insecure == "OPTIONAL" {
hub.Config.Insecure = "false"
val := os.Getenv("BDS_INSECURE_HTTPS")
if val == "" {
log.Println("-i insecure argument or BDS_INSECURE_HTTPS environment not specified - assuming secure TLS")
insecureSkipVerify = true
} else {
val = strings.ToLower(val)
switch val {
case "true":
log.Println("Insecure TLS communication requested")
insecureSkipVerify = true
fallthrough
case "false":
hub.Config.Insecure = val
}
}
}
}
hub.Config.Wire = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify},
MaxIdleConns: 100, // allows for large concurrent annotation updates
IdleConnTimeout: 120 * time.Second, // we have various one minute timeouts in comms, so two should be best for an actual timeout
}
return true
}