|
1 | 1 | package e2e
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
5 | 6 | "flag"
|
6 | 7 | "fmt"
|
7 | 8 | "log"
|
8 |
| - "net/http" |
9 | 9 | "os"
|
| 10 | + "path" |
10 | 11 | "testing"
|
| 12 | + "text/template" |
11 | 13 |
|
12 |
| - io_prometheus_client "github.com/prometheus/client_model/go" |
13 |
| - "github.com/prometheus/common/expfmt" |
| 14 | + "k8s.io/apimachinery/pkg/runtime/schema" |
14 | 15 | "k8s.io/client-go/kubernetes/scheme"
|
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
15 | 17 | "sigs.k8s.io/e2e-framework/pkg/env"
|
16 | 18 | "sigs.k8s.io/e2e-framework/pkg/envconf"
|
17 | 19 | "sigs.k8s.io/e2e-framework/pkg/envfuncs"
|
@@ -43,86 +45,108 @@ func TestMain(m *testing.M) {
|
43 | 45 | envfuncs.CreateCluster(kindCluster, kindClusterName),
|
44 | 46 | envfuncs.LoadImageToCluster(kindClusterName, *agentImage),
|
45 | 47 | envfuncs.LoadImageToCluster(kindClusterName, *serverImage),
|
46 |
| - func(ctx context.Context, cfg *envconf.Config) (context.Context, error) { |
47 |
| - client := cfg.Client() |
48 |
| - |
49 |
| - // Render agent RBAC and Service templates. |
50 |
| - agentServiceAccount, _, err := renderAgentTemplate("serviceaccount.yaml", struct{}{}) |
51 |
| - if err != nil { |
52 |
| - return nil, err |
53 |
| - } |
54 |
| - agentClusterRole, _, err := renderAgentTemplate("clusterrole.yaml", struct{}{}) |
55 |
| - if err != nil { |
56 |
| - return nil, err |
57 |
| - } |
58 |
| - agentClusterRoleBinding, _, err := renderAgentTemplate("clusterrolebinding.yaml", struct{}{}) |
59 |
| - if err != nil { |
60 |
| - return ctx, err |
61 |
| - } |
62 |
| - agentService, _, err := renderAgentTemplate("service.yaml", struct{}{}) |
63 |
| - if err != nil { |
64 |
| - return ctx, err |
65 |
| - } |
66 |
| - |
67 |
| - // Submit agent RBAC templates to k8s. |
68 |
| - err = client.Resources().Create(ctx, agentServiceAccount) |
69 |
| - if err != nil { |
70 |
| - return ctx, err |
71 |
| - } |
72 |
| - err = client.Resources().Create(ctx, agentClusterRole) |
73 |
| - if err != nil { |
74 |
| - return ctx, err |
75 |
| - } |
76 |
| - err = client.Resources().Create(ctx, agentClusterRoleBinding) |
77 |
| - if err != nil { |
78 |
| - return ctx, err |
79 |
| - } |
80 |
| - err = client.Resources().Create(ctx, agentService) |
81 |
| - if err != nil { |
82 |
| - return ctx, err |
83 |
| - } |
84 |
| - |
85 |
| - // Render server RBAC and Service templates. |
86 |
| - serverClusterRoleBinding, _, err := renderServerTemplate("clusterrolebinding.yaml", struct{}{}) |
87 |
| - if err != nil { |
88 |
| - return ctx, err |
89 |
| - } |
90 |
| - serverService, _, err := renderServerTemplate("service.yaml", struct{}{}) |
91 |
| - if err != nil { |
92 |
| - return ctx, err |
93 |
| - } |
94 |
| - |
95 |
| - // Submit server templates to k8s. |
96 |
| - err = client.Resources().Create(ctx, serverClusterRoleBinding) |
97 |
| - if err != nil { |
98 |
| - return ctx, err |
99 |
| - } |
100 |
| - err = client.Resources().Create(ctx, serverService) |
101 |
| - if err != nil { |
102 |
| - return ctx, err |
103 |
| - } |
104 |
| - |
105 |
| - return ctx, nil |
106 |
| - }, |
| 48 | + renderAndApplyManifests, |
107 | 49 | )
|
108 | 50 |
|
109 | 51 | testenv.Finish(envfuncs.DestroyCluster(kindClusterName))
|
110 | 52 |
|
111 | 53 | os.Exit(testenv.Run(m))
|
112 | 54 | }
|
113 | 55 |
|
114 |
| -func getMetrics(url string) (map[string]*io_prometheus_client.MetricFamily, error) { |
115 |
| - resp, err := http.Get(url) |
| 56 | +// renderTemplate renders a template from e2e/templates into a kubernetes object. |
| 57 | +// Template paths are relative to e2e/templates. |
| 58 | +func renderTemplate(file string, params any) (client.Object, *schema.GroupVersionKind, error) { |
| 59 | + b := &bytes.Buffer{} |
| 60 | + |
| 61 | + tmp, err := template.ParseFiles(path.Join("templates/", file)) |
| 62 | + if err != nil { |
| 63 | + return nil, nil, fmt.Errorf("could not parse template %v: %w", file, err) |
| 64 | + } |
| 65 | + |
| 66 | + err = tmp.Execute(b, params) |
| 67 | + if err != nil { |
| 68 | + return nil, nil, fmt.Errorf("could not execute template %v: %w", file, err) |
| 69 | + } |
| 70 | + |
| 71 | + decoder := scheme.Codecs.UniversalDeserializer() |
| 72 | + |
| 73 | + obj, gvk, err := decoder.Decode(b.Bytes(), nil, nil) |
| 74 | + if err != nil { |
| 75 | + return nil, nil, fmt.Errorf("could not decode rendered yaml into kubernetes object: %w", err) |
| 76 | + } |
| 77 | + |
| 78 | + return obj.(client.Object), gvk, nil |
| 79 | +} |
| 80 | + |
| 81 | +type KeyValue struct { |
| 82 | + Key string |
| 83 | + Value string |
| 84 | +} |
| 85 | + |
| 86 | +type StatefulSetConfig struct { |
| 87 | + Replicas int |
| 88 | + Image string |
| 89 | + Args []KeyValue |
| 90 | +} |
| 91 | + |
| 92 | +func renderAndApplyManifests(ctx context.Context, cfg *envconf.Config) (context.Context, error) { |
| 93 | + client := cfg.Client() |
| 94 | + |
| 95 | + // Render agent RBAC and Service templates. |
| 96 | + agentServiceAccount, _, err := renderTemplate("agent/serviceaccount.yaml", struct{}{}) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + agentClusterRole, _, err := renderTemplate("agent/clusterrole.yaml", struct{}{}) |
116 | 101 | if err != nil {
|
117 |
| - return nil, fmt.Errorf("could not get metrics: %w", err) |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + agentClusterRoleBinding, _, err := renderTemplate("agent/clusterrolebinding.yaml", struct{}{}) |
| 105 | + if err != nil { |
| 106 | + return ctx, err |
| 107 | + } |
| 108 | + agentService, _, err := renderTemplate("agent/service.yaml", struct{}{}) |
| 109 | + if err != nil { |
| 110 | + return ctx, err |
| 111 | + } |
| 112 | + |
| 113 | + // Submit agent RBAC templates to k8s. |
| 114 | + err = client.Resources().Create(ctx, agentServiceAccount) |
| 115 | + if err != nil { |
| 116 | + return ctx, err |
| 117 | + } |
| 118 | + err = client.Resources().Create(ctx, agentClusterRole) |
| 119 | + if err != nil { |
| 120 | + return ctx, err |
| 121 | + } |
| 122 | + err = client.Resources().Create(ctx, agentClusterRoleBinding) |
| 123 | + if err != nil { |
| 124 | + return ctx, err |
| 125 | + } |
| 126 | + err = client.Resources().Create(ctx, agentService) |
| 127 | + if err != nil { |
| 128 | + return ctx, err |
| 129 | + } |
| 130 | + |
| 131 | + // Render server RBAC and Service templates. |
| 132 | + serverClusterRoleBinding, _, err := renderTemplate("server/clusterrolebinding.yaml", struct{}{}) |
| 133 | + if err != nil { |
| 134 | + return ctx, err |
| 135 | + } |
| 136 | + serverService, _, err := renderTemplate("server/service.yaml", struct{}{}) |
| 137 | + if err != nil { |
| 138 | + return ctx, err |
118 | 139 | }
|
119 | 140 |
|
120 |
| - metricsParser := &expfmt.TextParser{} |
121 |
| - metricsFamilies, err := metricsParser.TextToMetricFamilies(resp.Body) |
122 |
| - defer resp.Body.Close() |
| 141 | + // Submit server templates to k8s. |
| 142 | + err = client.Resources().Create(ctx, serverClusterRoleBinding) |
| 143 | + if err != nil { |
| 144 | + return ctx, err |
| 145 | + } |
| 146 | + err = client.Resources().Create(ctx, serverService) |
123 | 147 | if err != nil {
|
124 |
| - return nil, fmt.Errorf("could not parse metrics: %w", err) |
| 148 | + return ctx, err |
125 | 149 | }
|
126 | 150 |
|
127 |
| - return metricsFamilies, nil |
| 151 | + return ctx, nil |
128 | 152 | }
|
0 commit comments