-
Notifications
You must be signed in to change notification settings - Fork 4
/
env.go
248 lines (191 loc) · 5.43 KB
/
env.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
/*
Copyright 2022-2023 ICS-FORTH.
Licensed 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 env
import (
"os"
"strconv"
"strings"
frisbeev1alpha1 "github.com/carv-ics-forth/frisbee/api/v1alpha1"
frisbeeclient "github.com/carv-ics-forth/frisbee/pkg/client"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"
// "k8s.io/cli-runtime/pkg/genericclioptions"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
)
var (
Default = New()
scheme = runtime.NewScheme()
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(frisbeev1alpha1.AddToScheme(scheme))
}
const (
// defaultMaxHistory sets the maximum number of tests to 0: unlimited.
defaultMaxHistory = 50
// defaultOutputType sets the output format.
defaultOutputType = "pretty"
)
type Path struct {
kubectlPath string
helmPath string
nodejsPath string
npmPath string
}
// EnvironmentSettings describes all the environment settings.
type EnvironmentSettings struct {
// Paths to external commands
Path
KubeConfig *rest.Config
KubeConfigPath string
// MaxHistory is the max tests history maintained.
MaxHistory int
// Debug indicates whether Frisbee is running in Debug mode.
Debug bool
// Debug indicates whether Frisbee CLI will provide hints for commands.
Hints bool
// OutputType indicate the format out message in the output.
OutputType string
// GoTemplate (if selected by OutputType type)
GoTemplate string
// cached objects
client *frisbeeclient.APIClient
}
func New() *EnvironmentSettings {
kubeconfig, err := config.GetConfig()
ui.ExitOnError("Failed to get config", err)
env := &EnvironmentSettings{
Path: Path{}, // will be set by LookupBinaries
KubeConfig: kubeconfig,
KubeConfigPath: os.Getenv("KUBECONFIG"),
// Operation
MaxHistory: envIntOr("FRISBEE_MAX_HISTORY", defaultMaxHistory),
Debug: envBoolOr("FRISBEE_DEBUG", false),
Hints: envBoolOr("FRISBEE_HINTS", false),
OutputType: envOr("FRISBEE_OUTPUT_TYPE", defaultOutputType),
GoTemplate: "",
client: nil,
}
/*
Locate external binaries
*/
env.LookupBinaries()
return env
}
// AddFlags binds flags to the given flagset.
func (env *EnvironmentSettings) AddFlags(cmd *cobra.Command) {
pfs := cmd.PersistentFlags()
// inherit the config flags
// env.Config.AddFlags(pfs)
// and add new ones
pfs.BoolVarP(&env.Debug, "debug", "d", env.Debug, "enable verbose output")
pfs.BoolVar(&env.Hints, "hints", env.Hints, "enable hints in the output")
}
func envOr(name, def string) string {
if v, ok := os.LookupEnv(name); ok {
return v
}
return def
}
func envOrPtr(name, def string) *string {
if v, ok := os.LookupEnv(name); ok {
return &v
}
return &def
}
func envBoolOr(name string, def bool) bool {
if name == "" {
return def
}
envVal := envOr(name, strconv.FormatBool(def))
ret, err := strconv.ParseBool(envVal)
if err != nil {
return def
}
return ret
}
func envIntOr(name string, def int) int {
if name == "" {
return def
}
envVal := envOr(name, strconv.Itoa(def))
ret, err := strconv.Atoi(envVal)
if err != nil {
return def
}
return ret
}
func envCSV(name string) (ls []string) {
trimmed := strings.Trim(os.Getenv(name), ", ")
if trimmed != "" {
ls = strings.Split(trimmed, ",")
}
return
}
/*
// SetNamespace sets the namespace in the configuration
func (env *EnvSettings) SetNamespace(namespace string) {
env.namespace = namespace
}
*/
// GetFrisbeeClient returns api client
func (env *EnvironmentSettings) GetFrisbeeClient() *frisbeeclient.APIClient {
if env.client != nil {
return env.client
}
// create generic client
genericClient, err := client.New(env.KubeConfig, client.Options{Scheme: scheme})
ui.ExitOnError("Setting up generic client", err)
c := frisbeeclient.NewDirectAPIClient(genericClient)
env.client = &c
return env.client
}
func (env *EnvironmentSettings) Hint(msg string, sub ...string) {
if env.Hints {
ui.Success(msg, sub...)
}
}
// Kubectl returns path to the kubectl binary.
func (p *Path) Kubectl() string {
if p.kubectlPath == "" {
ui.Fail(errors.Errorf("command requires 'kubectl' to be installed in your system"))
}
return p.kubectlPath
}
// Helm returns path to the helm binary.
func (p *Path) Helm() string {
if p.helmPath == "" {
ui.Fail(errors.Errorf("command requires 'helm' to be installed in your system"))
}
return p.helmPath
}
// NodeJS returns path to the node binary.
func (p *Path) NodeJS() string {
if p.nodejsPath == "" {
ui.Fail(errors.Errorf("command requires 'node' to be installed in your system"))
}
return p.nodejsPath
}
// NPM returns path to the node binary.
func (p *Path) NPM() string {
if p.npmPath == "" {
ui.Fail(errors.Errorf("command requires 'npm' to be installed in your system"))
}
return p.npmPath
}