-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
164 lines (142 loc) · 4.18 KB
/
main.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
// Dagger module for GreenReviewsTooling functions.
package main
import (
"context"
"os"
"github.com/cncf-tags/green-reviews-tooling/internal/dagger"
"github.com/cncf-tags/green-reviews-tooling/pkg/pipeline"
)
const (
clusterName = "green-reviews-test"
sourceDir = "src"
)
type GreenReviewsTooling struct{}
// BenchmarkPipeline measures the sustainability footprint of CNCF projects.
func (m *GreenReviewsTooling) BenchmarkPipeline(ctx context.Context,
source *dagger.Directory,
cncfProject,
// +optional
config,
version,
benchmarkJobURL,
kubeconfig string,
benchmarkJobDurationMins int) (*dagger.Container, error) {
p, err := newPipeline(ctx, source, kubeconfig)
if err != nil {
return nil, err
}
return p.Benchmark(ctx, cncfProject, config, version, benchmarkJobURL, benchmarkJobDurationMins)
}
// BenchmarkPipelineTest tests the pipeline.
func (m *GreenReviewsTooling) BenchmarkPipelineTest(ctx context.Context,
source *dagger.Directory,
// +optional
// +default="falco"
cncfProject,
// +optional
// +default="modern-ebpf"
config,
// +optional
// +default="0.40.0"
version,
// +optional
// +default="https://raw.githubusercontent.com/falcosecurity/cncf-green-review-testing/2551137b1a09bd0594f76b09e82e08c98f95efd3/benchmark-tests/falco-benchmark-tests.yaml"
benchmarkJobURL,
// +optional
kubeconfig string,
// +optional
// +default=2
benchmarkJobDurationMins int) (*dagger.Container, error) {
p, err := newPipeline(ctx, source, kubeconfig)
if err != nil {
return nil, err
}
if kubeconfig == "" {
// This is a new k3s container so bootstrap flux and the monitoring stack.
_, err = p.SetupCluster(ctx)
if err != nil {
return nil, err
}
}
return p.Benchmark(ctx,
cncfProject,
config,
version,
benchmarkJobURL,
benchmarkJobDurationMins)
}
// SetupCluster installs cluster components in an empty cluster for CI/CD and
// local development.
func (m *GreenReviewsTooling) SetupCluster(ctx context.Context,
source *dagger.Directory,
// +optional
kubeconfig string) (*dagger.Container, error) {
p, err := newPipeline(ctx, source, kubeconfig)
if err != nil {
return nil, err
}
return p.SetupCluster(ctx)
}
// Terminal returns dagger interactive terminal configured with kubeconfig
// for trouble shooting.
func (m *GreenReviewsTooling) Terminal(ctx context.Context,
source *dagger.Directory,
// +optional
kubeconfig string) (*dagger.Container, error) {
p, err := newPipeline(ctx, source, kubeconfig)
if err != nil {
return nil, err
}
return p.Terminal(ctx)
}
// newPipeline creates a new benchmark pipeline.
func newPipeline(ctx context.Context, source *dagger.Directory, kubeconfig string) (*pipeline.Pipeline, error) {
var configFile *dagger.File
var err error
container := build(source)
if kubeconfig == "" {
// No kubeconfig so start a new k3s container using the k3s dagger
// module. See https://daggerverse.dev/mod/github.com/marcosnils/daggerverse/k3s
configFile, err = startK3sCluster(ctx)
if err != nil {
return nil, err
}
} else {
// Connect to an existing cluster via a kubeconfig.
configFile, err = getKubeconfig(kubeconfig)
if err != nil {
return nil, err
}
}
return pipeline.New(container, source, configFile)
}
// build builds a container image from the Dockerfile with any CLIs needed by
// the pipeline such as kubectl.
func build(src *dagger.Directory) *dagger.Container {
return dag.Container().
WithDirectory(sourceDir, src).
Directory(sourceDir).
DockerBuild().
WithMountedDirectory(sourceDir, src).
WithWorkdir(sourceDir)
}
// getKubeconfig returns a dagger file object pointing as the cluster kubeconfig.
func getKubeconfig(configFilePath string) (*dagger.File, error) {
contents, err := os.ReadFile(configFilePath)
if err != nil {
return nil, err
}
filePath := pipeline.KubeconfigPath
dir := dag.Directory().WithNewFile(filePath, string(contents))
return dir.File(filePath), nil
}
// startK3sCluster starts a new k3s container using the k3s dagger module and
// returns its kubeconfig.
func startK3sCluster(ctx context.Context) (*dagger.File, error) {
k3s := dag.K3S(clusterName)
kServer := k3s.Server()
if _, err := kServer.Start(ctx); err != nil {
return nil, err
}
return k3s.Config(), nil
}