generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 98
/
config.go
177 lines (151 loc) · 4.4 KB
/
config.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
/*
Copyright 2021 The Kubernetes Authors.
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 envconf
import (
"encoding/hex"
"fmt"
"math/rand"
"regexp"
"time"
"sigs.k8s.io/e2e-framework/klient"
"sigs.k8s.io/e2e-framework/klient/conf"
"sigs.k8s.io/e2e-framework/pkg/flags"
)
// Config represents and environment configuration
type Config struct {
client klient.Client
namespace string
assessmentRegex *regexp.Regexp
featureRegex *regexp.Regexp
labels map[string]string
}
// New creates and initializes an empty environment configuration
func New() *Config {
return &Config{}
}
// NewWithKubeconfig is a convenience constructor function
// that creates a new environment configuration using a kubeconfig file.
func NewWithKubeconfig(kubecfg string) (*Config, error) {
client, err := klient.NewWithKubeConfigFile(kubecfg)
if err != nil {
return nil, err
}
cfg := New()
cfg.client = client
return cfg, nil
}
// NewFromFlags initializes an environment config using values
// from command-line argument flags. See package flags for supported flags.
func NewFromFlags() (*Config, error) {
flagset, err := flags.Parse()
if err != nil {
return nil, err
}
e := New()
if flagset.Assessment() != "" {
e.assessmentRegex = regexp.MustCompile(flagset.Assessment())
}
if flagset.Feature() != "" {
e.featureRegex = regexp.MustCompile(flagset.Feature())
}
// setup EnvConfig
e.labels = flagset.Labels()
e.namespace = flagset.Namespace()
kubecfg := flagset.Kubeconfig()
if kubecfg == "" {
kubecfg = conf.ResolveKubeConfigFile()
}
c, err := klient.NewWithKubeConfigFile(kubecfg)
if err != nil {
return nil, err
}
e.client = c
return e, nil
}
// WithKubeconfigFile creates a new klient.Client and injects it in the cfg
func (c *Config) WithKubeconfigFile(kubecfg string) (*Config, error) {
client, err := klient.NewWithKubeConfigFile(kubecfg)
if err != nil {
return nil, err
}
c.client = client
return c, nil
}
// WithClient used to update the environment klient.Client
func (c *Config) WithClient(client klient.Client) *Config {
c.client = client
return c
}
// Client returns the environment klient.Client
func (c *Config) Client() klient.Client {
return c.client
}
// WithNamespace updates the environment namespace value
func (c *Config) WithNamespace(ns string) *Config {
c.namespace = ns
return c
}
// WithRandomNamespace sets the environment's namespace
// to a random value
func (c *Config) WithRandomNamespace() *Config {
c.namespace = randNS()
return c
}
// Namespace returns the namespace for the environment
func (c *Config) Namespace() string {
return c.namespace
}
// WithAssessmentRegex sets the environment assessment regex filter
func (c *Config) WithAssessmentRegex(regex string) *Config {
c.assessmentRegex = regexp.MustCompile(regex)
return c
}
// AssessmentRegex returns the environment assessment filter
func (c *Config) AssessmentRegex() *regexp.Regexp {
return c.assessmentRegex
}
// WithFeatureRegex sets the environment's feature regex filter
func (c *Config) WithFeatureRegex(regex string) *Config {
c.featureRegex = regexp.MustCompile(regex)
return c
}
// FeatureRegex returns the environment's feature regex filter
func (c *Config) FeatureRegex() *regexp.Regexp {
return c.featureRegex
}
// WithLabels sets the environment label filters
func (c *Config) WithLabels(lbls map[string]string) *Config {
c.labels = lbls
return c
}
// Labels returns the environment's label filters
func (c *Config) Labels() map[string]string {
return c.labels
}
func randNS() string {
return RandomName("testns-", 32)
}
// RandomName generates a random name of n length with the provided
// prefix. If prefix is omitted, the then entire name is random char.
func RandomName(prefix string, n int) string {
if n == 0 {
n = 32
}
if len(prefix) >= n {
return prefix
}
rand.Seed(time.Now().UnixNano())
p := make([]byte, n)
rand.Read(p)
return fmt.Sprintf("%s-%s", prefix, hex.EncodeToString(p))[:n]
}