/
context.go
313 lines (264 loc) · 10.3 KB
/
context.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package deployframework
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
olmclientv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/typed/operators/v1"
olmclientv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/typed/operators/v1alpha1"
"github.com/sirupsen/logrus"
apiextclientv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
metering "github.com/operator-framework/operator-metering/pkg/apis/metering/v1"
"github.com/operator-framework/operator-metering/pkg/deploy"
meteringclient "github.com/operator-framework/operator-metering/pkg/generated/clientset/versioned/typed/metering/v1"
"github.com/operator-framework/operator-metering/test/reportingframework"
)
var (
httpScheme = "http"
localAddr = "127.0.0.1"
apiPort = 8100
metricsPort = 8101
pprofPort = 8102
healthCheckEndpoint = "healthy"
healthCheckTimeoutPeriod = 5 * time.Minute
waitingForPodsTimeoutPeriod = 20 * time.Minute
waitingForServiceAccountTimePeriod = 10 * time.Minute
useHTTPSAPI = true
useRouteForReportingAPI = true
useKubeProxyForReportingAPI = false
reportingAPIURL string
)
// DeployerCtx contains all the information needed to manage the
// full lifecycle of a single metering deployment
type DeployerCtx struct {
TargetPodsCount int
Namespace string
KubeConfigPath string
TestCaseOutputPath string
HackScriptPath string
MeteringOperatorImageRepo string
MeteringOperatorImageTag string
RunTestLocal bool
RunDevSetup bool
ExtraLocalEnvVars []string
LocalCtx *LocalCtx
Deployer *deploy.Deployer
Logger logrus.FieldLogger
Config *rest.Config
Client kubernetes.Interface
APIExtClient apiextclientv1beta1.CustomResourceDefinitionsGetter
MeteringClient meteringclient.MeteringV1Interface
OLMV1Client olmclientv1.OperatorsV1Interface
OLMV1Alpha1Client olmclientv1alpha1.OperatorsV1alpha1Interface
}
// NewDeployerCtx constructs and returns a new DeployerCtx object
func (df *DeployFramework) NewDeployerCtx(
namespace,
meteringOperatorImageRepo,
meteringOperatorImageTag,
reportingOperatorImageRepo,
reportingOperatorImageTag,
outputPath string,
extraLocalEnvVars []string,
spec metering.MeteringConfigSpec,
) (*DeployerCtx, error) {
cfg, err := df.NewDeployerConfig(namespace, meteringOperatorImageRepo, meteringOperatorImageTag, reportingOperatorImageRepo, reportingOperatorImageTag, spec)
if err != nil {
return nil, err
}
hackScriptDir, err := filepath.Abs(filepath.Join(df.RepoDir, hackScriptDirName))
if err != nil {
return nil, fmt.Errorf("failed to get the absolute path to the hack script directory: %v", err)
}
_, err = os.Stat(hackScriptDir)
if err != nil {
return nil, fmt.Errorf("failed to successfully stat the %s path to the hack script directory: %v", hackScriptDir, err)
}
targetPodCount := defaultTargetPods
if df.RunLocal {
var replicas int32
if cfg.MeteringConfig.Spec.ReportingOperator != nil && cfg.MeteringConfig.Spec.ReportingOperator.Spec != nil {
cfg.MeteringConfig.Spec.ReportingOperator.Spec.Replicas = &replicas
}
targetPodCount -= 2
}
df.Logger.Debugf("Deployer config: %+v", cfg)
deployer, err := deploy.NewDeployer(*cfg, df.Logger, df.Client, df.APIExtClient, df.MeteringClient, df.OLMV1Client, df.OLMV1Alpha1Client)
if err != nil {
return nil, fmt.Errorf("failed to create a new deployer instance: %v", err)
}
return &DeployerCtx{
Namespace: namespace,
TargetPodsCount: targetPodCount,
TestCaseOutputPath: outputPath,
MeteringOperatorImageRepo: meteringOperatorImageRepo,
MeteringOperatorImageTag: meteringOperatorImageTag,
Deployer: deployer,
HackScriptPath: hackScriptDir,
ExtraLocalEnvVars: extraLocalEnvVars,
RunTestLocal: df.RunLocal,
RunDevSetup: df.RunDevSetup,
KubeConfigPath: df.KubeConfigPath,
Logger: df.Logger,
Config: df.Config,
Client: df.Client,
APIExtClient: df.APIExtClient,
MeteringClient: df.MeteringClient,
OLMV1Client: df.OLMV1Client,
OLMV1Alpha1Client: df.OLMV1Alpha1Client,
}, nil
}
// NewLocalCtx returns a new LocalCtx object
func (ctx *DeployerCtx) NewLocalCtx() *LocalCtx {
meteringOperatorImage := fmt.Sprintf("%s:%s", ctx.MeteringOperatorImageRepo, ctx.MeteringOperatorImageTag)
return &LocalCtx{
Namespace: ctx.Namespace,
KubeConfigPath: ctx.KubeConfigPath,
BasePath: ctx.TestCaseOutputPath,
HackScriptPath: ctx.HackScriptPath,
ExtraReportingOperatorEnvVars: ctx.ExtraLocalEnvVars,
Logger: ctx.Logger,
MeteringOperatorImage: meteringOperatorImage,
}
}
// Setup handles the process of deploying metering, and waiting for all the necessary
// resources to become ready in order to proceeed with running the reporting tests.
// This returns an initialized reportingframework object, or an error if there is any.
func (ctx *DeployerCtx) Setup(expectInstallErr bool) (*reportingframework.ReportingFramework, error) {
var (
installErrMsg string
routeBearerToken string
installErr bool
)
// If we expect an install error, and there was an install error, then delay returning
// that error message until after the reportingframework has been constructed.
err := ctx.Deployer.Install()
if err != nil {
installErr = true
installErrMsg = fmt.Sprintf("failed to install metering: %v", err)
if !expectInstallErr {
return nil, fmt.Errorf(installErrMsg)
}
}
if !installErr {
if ctx.RunTestLocal {
ctx.LocalCtx = ctx.NewLocalCtx()
err = ctx.LocalCtx.RunMeteringOperatorLocal()
if err != nil {
return nil, fmt.Errorf("failed to run the metering-operator docker container: %v", err)
}
}
ctx.Logger.Infof("Waiting for the metering pods to be ready")
start := time.Now()
initialDelay := 10 * time.Second
if ctx.RunDevSetup {
initialDelay = 1 * time.Second
}
pw := &PodWaiter{
InitialDelay: initialDelay,
TimeoutPeriod: waitingForPodsTimeoutPeriod,
Client: ctx.Client,
Logger: ctx.Logger.WithField("component", "podWaiter"),
}
err = pw.WaitForPods(ctx.Namespace, ctx.TargetPodsCount)
if err != nil {
return nil, fmt.Errorf("error waiting for metering pods to become ready: %v", err)
}
ctx.Logger.Infof("Installing metering took %v", time.Since(start))
ctx.Logger.Infof("Getting the service account %s", reportingOperatorServiceAccountName)
routeBearerToken, err = GetServiceAccountToken(
ctx.Client,
initialDelay,
waitingForServiceAccountTimePeriod,
ctx.Namespace,
reportingOperatorServiceAccountName,
)
if err != nil {
return nil, fmt.Errorf("failed to get the route bearer token: %v", err)
}
if ctx.RunTestLocal {
useHTTPSAPI = false
useRouteForReportingAPI = false
useKubeProxyForReportingAPI = false
reportingAPIURL = fmt.Sprintf("%s://%s:%d", httpScheme, localAddr, apiPort)
err = ctx.LocalCtx.RunReportingOperatorLocal(apiPort, metricsPort, pprofPort, routeBearerToken)
if err != nil {
return nil, fmt.Errorf("failed to run the reporting-operator locally: %v", err)
}
reportingAPIHealthCheckURL := fmt.Sprintf("%s/%s", reportingAPIURL, healthCheckEndpoint)
err := waitForURLToReportStatusOK(ctx.Logger, reportingAPIHealthCheckURL, healthCheckTimeoutPeriod)
if err != nil {
return nil, fmt.Errorf("failed to wait for the reporting-operator to become healthy: %v", err)
}
}
}
reportResultsPath := filepath.Join(ctx.TestCaseOutputPath, reportResultsDir)
err = os.Mkdir(reportResultsPath, 0777)
if err != nil {
return nil, fmt.Errorf("failed to create the report results directory %s: %v", reportResultsPath, err)
}
ctx.Logger.Infof("Report results directory: %s", reportResultsPath)
rf, err := reportingframework.New(
useHTTPSAPI,
useKubeProxyForReportingAPI,
useRouteForReportingAPI,
ctx.Namespace,
routeBearerToken,
reportingAPIURL,
reportResultsPath,
ctx.Config,
ctx.Client,
ctx.MeteringClient,
)
if err != nil {
return nil, fmt.Errorf("failed to construct a reportingframework: %v", err)
}
if installErrMsg != "" {
return rf, fmt.Errorf(installErrMsg)
}
return rf, nil
}
// Teardown is a method that creates the resource and container logging
// directories, then populates those directories by executing the
// cleanup bash script, while streaming the script output
// to stdout. Once the cleanup script has finished execution, we can
// uninstall the metering stack and return an error if there is any.
func (ctx *DeployerCtx) Teardown() error {
var errArr []string
logger := ctx.Logger.WithFields(logrus.Fields{"component": "cleanup"})
if ctx.RunTestLocal && ctx.LocalCtx != nil {
err := ctx.LocalCtx.CleanupLocal()
if err != nil {
errArr = append(errArr, fmt.Sprintf("failed to successfully cleanup local resources: %v", err))
}
}
// Check if the user wants to run the E2E suite using the developer setup.
// If true, we skip the process of deleting and logging of the metering
// resources that were provisioned during the manual install.
if !ctx.RunDevSetup {
relPath := filepath.Join(ctx.HackScriptPath, cleanupScriptName)
targetScriptDir, err := filepath.Abs(relPath)
if err != nil {
errArr = append(errArr, fmt.Sprintf("failed to get the absolute path from '%s': %v", relPath, err))
}
_, err = os.Stat(targetScriptDir)
if err != nil {
errArr = append(errArr, fmt.Sprintf("failed to stat the '%s' path: %v", targetScriptDir, err))
}
err = runCleanupScript(logger, ctx.Namespace, ctx.TestCaseOutputPath, targetScriptDir)
if err != nil {
errArr = append(errArr, fmt.Sprintf("failed to successfully run the cleanup script: %v", err))
}
err = ctx.Deployer.Uninstall()
if err != nil {
errArr = append(errArr, fmt.Sprintf("failed to uninstall metering: %v", err))
}
}
if len(errArr) != 0 {
return fmt.Errorf(strings.Join(errArr, "\n"))
}
return nil
}