-
Notifications
You must be signed in to change notification settings - Fork 596
/
configuration.go
193 lines (173 loc) · 5.27 KB
/
configuration.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
/*
* Copyright 2020 The Knative 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 prober
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"path"
"runtime"
"text/template"
"time"
"github.com/kelseyhightower/envconfig"
"knative.dev/eventing/test/lib/resources"
"knative.dev/eventing/test/upgrade/prober/sut"
duckv1 "knative.dev/pkg/apis/duck/v1"
pkgTest "knative.dev/pkg/test"
pkgupgrade "knative.dev/pkg/test/upgrade"
)
const (
defaultConfigName = "wathola-config"
defaultConfigHomedirPath = ".config/wathola"
defaultHomedir = "/home/nonroot"
defaultConfigFilename = "config.toml"
defaultHealthEndpoint = "/healthz"
defaultFinishedSleep = 5 * time.Second
// Silence will suppress notification about event duplicates.
Silence DuplicateAction = "silence"
// Warn will issue a warning message in case of a event duplicate.
Warn DuplicateAction = "warn"
// Error will fail test with an error in case of event duplicate.
Error DuplicateAction = "error"
prefix = "eventing_upgrade_tests"
)
var (
// ErrInvalidConfig is returned when the configuration is invalid.
ErrInvalidConfig = errors.New("invalid config")
)
// DuplicateAction is the action to take in case of duplicated events
type DuplicateAction string
// Config represents a configuration for prober.
type Config struct {
Wathola
Interval time.Duration
FinishedSleep time.Duration
Serving ServingConfig
FailOnErrors bool
OnDuplicate DuplicateAction
Ctx context.Context
}
// Wathola represents options related strictly to wathola testing tool.
type Wathola struct {
ConfigToml
ImageResolver
SystemUnderTest sut.SystemUnderTest
HealthEndpoint string
}
// ImageResolver will resolve the container image for given component.
type ImageResolver func(component string) string
// ConfigToml represents options of wathola config toml file.
type ConfigToml struct {
// ConfigTemplate is a template file that will be compiled to the configmap
ConfigTemplate string
ConfigMapName string
ConfigMountPoint string
ConfigFilename string
}
// ServingConfig represents a options for serving test component (wathola-forwarder).
type ServingConfig struct {
Use bool
ScaleToZero bool
}
// NewConfigOrFail will create a prober.Config or fail trying.
func NewConfigOrFail(c pkgupgrade.Context) *Config {
cfg, err := NewConfig()
if err != nil {
c.T.Fatal(err)
}
return cfg
}
// NewConfig will create a prober.Config or return error.
func NewConfig() (*Config, error) {
config := &Config{
Interval: Interval,
FinishedSleep: defaultFinishedSleep,
FailOnErrors: true,
OnDuplicate: Warn,
Ctx: context.Background(),
Serving: ServingConfig{
Use: false,
ScaleToZero: true,
},
Wathola: Wathola{
ImageResolver: pkgTest.ImagePath,
ConfigToml: ConfigToml{
ConfigTemplate: defaultConfigFilename,
ConfigMapName: defaultConfigName,
ConfigMountPoint: fmt.Sprintf("%s/%s", defaultHomedir, defaultConfigHomedirPath),
ConfigFilename: defaultConfigFilename,
},
HealthEndpoint: defaultHealthEndpoint,
SystemUnderTest: sut.NewDefault(),
},
}
if err := envconfig.Process(prefix, config); err != nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidConfig, err)
}
return config, nil
}
func (p *prober) deployConfiguration() {
sc := sut.Context{
Ctx: p.config.Ctx,
Log: p.log,
Client: p.client,
}
ref := resources.KnativeRefForService(receiverName, p.client.Namespace)
if p.config.Serving.Use {
ref = resources.KnativeRefForKservice(forwarderName, p.client.Namespace)
}
dest := duckv1.Destination{Ref: ref}
s := p.config.SystemUnderTest
endpoint := s.Deploy(sc, dest)
p.client.Cleanup(func() {
if tr, ok := s.(sut.HasTeardown); ok {
tr.Teardown(sc)
}
})
p.deployConfigToml(endpoint)
}
func (p *prober) deployConfigToml(endpoint interface{}) {
name := p.config.ConfigMapName
p.log.Infof("Deploying config map: \"%s/%s\"", p.client.Namespace, name)
configData := p.compileTemplate(p.config.ConfigTemplate, endpoint)
p.client.CreateConfigMapOrFail(name, p.client.Namespace, map[string]string{
p.config.ConfigFilename: configData,
})
}
func (p *prober) compileTemplate(templateName string, endpoint interface{}) string {
_, filename, _, _ := runtime.Caller(0)
templateFilepath := path.Join(path.Dir(filename), templateName)
templateBytes, err := ioutil.ReadFile(templateFilepath)
p.ensureNoError(err)
tmpl, err := template.New(templateName).Parse(string(templateBytes))
p.ensureNoError(err)
var buff bytes.Buffer
data := struct {
*Config
Namespace string
// Deprecated: use Endpoint
BrokerURL string
Endpoint interface{}
}{
p.config,
p.client.Namespace,
fmt.Sprintf("%v", endpoint),
endpoint,
}
p.ensureNoError(tmpl.Execute(&buff, data))
return buff.String()
}