This repository has been archived by the owner on May 1, 2020. It is now read-only.
/
ctl_alert.go
269 lines (252 loc) · 11.4 KB
/
ctl_alert.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
/*
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 alert
import (
// "encoding/json"
"fmt"
"strings"
"github.com/blackducksoftware/synopsys-operator/pkg/api"
alertapi "github.com/blackducksoftware/synopsys-operator/pkg/api/alert/v1"
"github.com/blackducksoftware/synopsys-operator/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// CRSpecBuilderFromCobraFlags uses Cobra commands, Cobra flags and other
// values to create an Alert CR's Spec.
//
// The fields in the CRSpecBuilderFromCobraFlags represent places where the values of the Cobra flags are stored.
//
// Usage: Use CRSpecBuilderFromCobraFlags to add flags to your Cobra Command for making an Alert Spec.
// When flags are used the correspoding value in this struct will by set. You can then
// generate the spec by telling CRSpecBuilderFromCobraFlags what flags were changed.
type CRSpecBuilderFromCobraFlags struct {
alertSpec *alertapi.AlertSpec
Version string
StandAlone string
ExposeService string
Port int32
EncryptionPassword string
EncryptionGlobalSalt string
Environs []string
PersistentStorage string
PVCName string
PVCStorageClass string
PVCSize string
AlertMemory string
CfsslMemory string
Registry string
RegistryNamespace string
PullSecrets []string
ImageRegistries []string
CertificateFilePath string
CertificateKeyFilePath string
JavaKeyStoreFilePath string
// SecurityContextFilePath string
}
// NewCRSpecBuilderFromCobraFlags creates a new CRSpecBuilderFromCobraFlags type
func NewCRSpecBuilderFromCobraFlags() *CRSpecBuilderFromCobraFlags {
return &CRSpecBuilderFromCobraFlags{
alertSpec: &alertapi.AlertSpec{},
}
}
// GetCRSpec returns a pointer to the AlertSpec as an interface{}
func (ctl *CRSpecBuilderFromCobraFlags) GetCRSpec() interface{} {
return *ctl.alertSpec
}
// SetCRSpec sets the alertSpec in the struct
func (ctl *CRSpecBuilderFromCobraFlags) SetCRSpec(spec interface{}) error {
convertedSpec, ok := spec.(alertapi.AlertSpec)
if !ok {
return fmt.Errorf("error setting Alert spec")
}
ctl.alertSpec = &convertedSpec
return nil
}
// Constants for predefined specs
const (
EmptySpec string = "empty"
DefaultSpec string = "default"
)
// SetPredefinedCRSpec sets the alertSpec to a predefined spec
func (ctl *CRSpecBuilderFromCobraFlags) SetPredefinedCRSpec(specType string) error {
switch specType {
case EmptySpec:
ctl.alertSpec = &alertapi.AlertSpec{}
case DefaultSpec:
ctl.alertSpec = util.GetAlertDefault()
ctl.alertSpec.PersistentStorage = true
ctl.alertSpec.StandAlone = util.BoolToPtr(true)
default:
return fmt.Errorf("Alert spec type '%s' is not valid", specType)
}
return nil
}
// AddCRSpecFlagsToCommand adds flags to a Cobra Command that are need for Alert's Spec.
// The flags map to fields in the CRSpecBuilderFromCobraFlags struct.
// master - if false, doesn't add flags that all Users shouldn't use
func (ctl *CRSpecBuilderFromCobraFlags) AddCRSpecFlagsToCommand(cmd *cobra.Command, master bool) {
cmd.Flags().StringVar(&ctl.Version, "version", ctl.Version, "Version of Alert")
cmd.Flags().StringVar(&ctl.StandAlone, "standalone", ctl.StandAlone, "If true, Alert runs in standalone mode [true|false]")
if master {
cmd.Flags().StringVar(&ctl.ExposeService, "expose-ui", util.NONE, "Service type to expose Alert's user interface [NODEPORT|LOADBALANCER|OPENSHIFT|NONE]")
} else {
cmd.Flags().StringVar(&ctl.ExposeService, "expose-ui", ctl.ExposeService, "Service type to expose Alert's user interface [NODEPORT|LOADBALANCER|OPENSHIFT|NONE]")
}
cmd.Flags().Int32Var(&ctl.Port, "port", ctl.Port, "Port of Alert")
cmd.Flags().StringVar(&ctl.EncryptionPassword, "encryption-password", ctl.EncryptionPassword, "Encryption Password for Alert")
cmd.Flags().StringVar(&ctl.EncryptionGlobalSalt, "encryption-global-salt", ctl.EncryptionGlobalSalt, "Encryption Global Salt for Alert")
cmd.Flags().StringSliceVar(&ctl.Environs, "environs", ctl.Environs, "Environment variables of Alert")
cmd.Flags().StringVar(&ctl.PersistentStorage, "persistent-storage", ctl.PersistentStorage, "If true, Alert has persistent storage [true|false]")
cmd.Flags().StringVar(&ctl.PVCName, "pvc-name", ctl.PVCName, "Name of the persistent volume claim")
cmd.Flags().StringVar(&ctl.PVCStorageClass, "pvc-storage-class", ctl.PVCStorageClass, "Storage class for the persistent volume claim")
cmd.Flags().StringVar(&ctl.PVCSize, "pvc-size", ctl.PVCSize, "Memory allocation of the persistent volume claim")
cmd.Flags().StringVar(&ctl.AlertMemory, "alert-memory", ctl.AlertMemory, "Memory allocation of Alert")
cmd.Flags().StringVar(&ctl.CfsslMemory, "cfssl-memory", ctl.CfsslMemory, "Memory allocation of CFSSL")
cmd.Flags().StringVar(&ctl.Registry, "registry", ctl.Registry, "Name of the registry to use for images e.g. docker.io/blackducksoftware")
cmd.Flags().StringSliceVar(&ctl.PullSecrets, "pull-secret-name", ctl.PullSecrets, "Only if the registry requires authentication")
cmd.Flags().StringSliceVar(&ctl.ImageRegistries, "image-registries", ctl.ImageRegistries, "List of image registries")
cmd.Flags().StringVar(&ctl.CertificateFilePath, "certificate-file-path", ctl.CertificateFilePath, "Absolute path to the PEM certificate to use for Alert")
cmd.Flags().StringVar(&ctl.CertificateKeyFilePath, "certificate-key-file-path", ctl.CertificateKeyFilePath, "Absolute path to the PEM certificate key for Alert")
cmd.Flags().StringVar(&ctl.JavaKeyStoreFilePath, "java-keystore-file-path", ctl.JavaKeyStoreFilePath, "Absolute path to the Java Keystore to use for Alert")
// cmd.Flags().StringVar(&ctl.SecurityContextFilePath, "security-context-file-path", ctl.SecurityContextFilePath, "Absolute path to a file containing a map of pod names to security contexts runAsUser, fsGroup, and runAsGroup")
}
// CheckValuesFromFlags returns an error if a value stored in the struct will not be able to be
// used in the AlertSpec
func (ctl *CRSpecBuilderFromCobraFlags) CheckValuesFromFlags(flagset *pflag.FlagSet) error {
if FlagWasSet(flagset, "encryption-password") {
encryptPassLength := len(ctl.EncryptionPassword)
if encryptPassLength > 0 && encryptPassLength < 16 {
return fmt.Errorf("flag EncryptionPassword is %d characters. Must be 16 or more characters", encryptPassLength)
}
}
if FlagWasSet(flagset, "encryption-global-salt") {
globalSaltLength := len(ctl.EncryptionGlobalSalt)
if globalSaltLength > 0 && globalSaltLength < 16 {
return fmt.Errorf("flag EncryptionGlobalSalt is %d characters. Must be 16 or more characters", globalSaltLength)
}
}
if FlagWasSet(flagset, "expose-ui") {
isValid := util.IsExposeServiceValid(ctl.ExposeService)
if !isValid {
return fmt.Errorf("expose ui must be '%s', '%s', '%s' or '%s'", util.NODEPORT, util.LOADBALANCER, util.OPENSHIFT, util.NONE)
}
}
return nil
}
// FlagWasSet returns true if a flag was changed and it exists, otherwise it returns false
func FlagWasSet(flagset *pflag.FlagSet, flagName string) bool {
if flagset.Lookup(flagName) != nil && flagset.Lookup(flagName).Changed {
return true
}
return false
}
// GenerateCRSpecFromFlags checks if a flag was changed and updates the alertSpec with the value that's stored
// in the corresponding struct field
func (ctl *CRSpecBuilderFromCobraFlags) GenerateCRSpecFromFlags(flagset *pflag.FlagSet) (interface{}, error) {
err := ctl.CheckValuesFromFlags(flagset)
if err != nil {
return nil, err
}
flagset.VisitAll(ctl.SetCRSpecFieldByFlag)
return *ctl.alertSpec, nil
}
// SetCRSpecFieldByFlag updates a field in the alertSpec if the flag was set by the user. It gets the
// value from the corresponding struct field
// Note: It should only handle values with a 1 to 1 mapping - struct-field to spec
func (ctl *CRSpecBuilderFromCobraFlags) SetCRSpecFieldByFlag(f *pflag.Flag) {
if f.Changed {
log.Debugf("flag '%s': CHANGED", f.Name)
switch f.Name {
case "version":
ctl.alertSpec.Version = ctl.Version
case "standalone":
standAloneVal := strings.ToUpper(ctl.StandAlone) == "TRUE"
ctl.alertSpec.StandAlone = &standAloneVal
case "expose-ui":
ctl.alertSpec.ExposeService = ctl.ExposeService
case "port":
ctl.alertSpec.Port = &ctl.Port
case "encryption-password":
ctl.alertSpec.EncryptionPassword = ctl.EncryptionPassword
case "encryption-global-salt":
ctl.alertSpec.EncryptionGlobalSalt = ctl.EncryptionGlobalSalt
case "persistent-storage":
ctl.alertSpec.PersistentStorage = strings.ToUpper(ctl.PersistentStorage) == "TRUE"
case "pvc-name":
ctl.alertSpec.PVCName = ctl.PVCName
case "pvc-storage-class":
ctl.alertSpec.PVCStorageClass = ctl.PVCStorageClass
case "pvc-size":
ctl.alertSpec.PVCSize = ctl.PVCSize
case "alert-memory":
ctl.alertSpec.AlertMemory = ctl.AlertMemory
case "cfssl-memory":
ctl.alertSpec.CfsslMemory = ctl.CfsslMemory
case "environs":
ctl.alertSpec.Environs = ctl.Environs
case "registry":
if ctl.alertSpec.RegistryConfiguration == nil {
ctl.alertSpec.RegistryConfiguration = &api.RegistryConfiguration{}
}
ctl.alertSpec.RegistryConfiguration.Registry = ctl.Registry
case "pull-secret-name":
if ctl.alertSpec.RegistryConfiguration == nil {
ctl.alertSpec.RegistryConfiguration = &api.RegistryConfiguration{}
}
ctl.alertSpec.RegistryConfiguration.PullSecrets = ctl.PullSecrets
case "image-registries":
ctl.alertSpec.ImageRegistries = ctl.ImageRegistries
case "certificate-file-path":
data, err := util.ReadFileData(ctl.CertificateFilePath)
if err != nil {
log.Fatalf("failed to read certificate file: %+v", err)
}
ctl.alertSpec.Certificate = data
case "certificate-key-file-path":
data, err := util.ReadFileData(ctl.CertificateKeyFilePath)
if err != nil {
log.Fatalf("failed to read certificate file: %+v", err)
}
ctl.alertSpec.CertificateKey = data
case "java-keystore-file-path":
data, err := util.ReadFileData(ctl.JavaKeyStoreFilePath)
if err != nil {
log.Fatalf("failed to read Java keystore file: %+v", err)
}
ctl.alertSpec.JavaKeyStore = data
// case "security-context-file-path":
// data, err := util.ReadFileData(ctl.SecurityContextFilePath)
// if err != nil {
// log.Errorf("failed to read security context file: %+v", err)
// return
// }
// SecurityContexts := map[string]api.SecurityContext{}
// err = json.Unmarshal([]byte(data), &SecurityContexts)
// if err != nil {
// log.Errorf("failed to unmarshal security contexts: %+v", err)
// return
// }
// ctl.alertSpec.SecurityContexts = SecurityContexts
default:
log.Debugf("flag '%s': NOT FOUND", f.Name)
}
} else {
log.Debugf("flag '%s': UNCHANGED", f.Name)
}
}