Skip to content

Commit 35a99c5

Browse files
cleanup e2e tests for go/v4: enhancement for metrics checks
1 parent 6d8b808 commit 35a99c5

File tree

2 files changed

+116
-105
lines changed

2 files changed

+116
-105
lines changed

test/e2e/utils/test_context.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,21 @@ func (t *TestContext) CreateManagerNamespace() error {
270270
return err
271271
}
272272

273-
// LabelAllNamespacesToWarnAboutRestricted will label all namespaces so that we can verify
273+
// LabelNamespacesToWarnAboutRestricted will label all namespaces so that we can verify
274274
// if a warning with `Warning: would violate PodSecurity` will be raised when the manifests are applied
275-
func (t *TestContext) LabelAllNamespacesToWarnAboutRestricted() error {
276-
_, err := t.Kubectl.Command("label", "--overwrite", "ns", "--all",
275+
func (t *TestContext) LabelNamespacesToWarnAboutRestricted() error {
276+
_, err := t.Kubectl.Command("label", "--overwrite", "ns", t.Kubectl.Namespace,
277277
"pod-security.kubernetes.io/warn=restricted")
278278
return err
279279
}
280280

281+
// RemoveNamespaceLabelToWarnAboutRestricted will remove the `pod-security.kubernetes.io/warn` label
282+
// from the specified namespace
283+
func (t *TestContext) RemoveNamespaceLabelToWarnAboutRestricted() error {
284+
_, err := t.Kubectl.Command("label", "ns", t.Kubectl.Namespace, "pod-security.kubernetes.io/warn-")
285+
return err
286+
}
287+
281288
// LoadImageToKindCluster loads a local docker image to the kind cluster
282289
func (t *TestContext) LoadImageToKindCluster() error {
283290
cluster := "kind"

test/e2e/v4/plugin_cluster_test.go

Lines changed: 106 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ import (
2525
"strings"
2626
"time"
2727

28-
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
29-
3028
. "github.com/onsi/ginkgo/v2"
31-
3229
. "github.com/onsi/gomega"
3330

31+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
3432
"sigs.k8s.io/kubebuilder/v4/test/e2e/utils"
3533
)
3634

@@ -43,9 +41,18 @@ var _ = Describe("kubebuilder", func() {
4341
kbc, err = utils.NewTestContext(util.KubebuilderBinName, "GO111MODULE=on")
4442
Expect(err).NotTo(HaveOccurred())
4543
Expect(kbc.Prepare()).To(Succeed())
44+
45+
if kbc.IsRestricted {
46+
By("labeling all namespaces to warn about restricted")
47+
err = kbc.LabelNamespacesToWarnAboutRestricted()
48+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
49+
}
4650
})
4751

4852
AfterEach(func() {
53+
By("By removing restricted namespace label")
54+
_ = kbc.RemoveNamespaceLabelToWarnAboutRestricted()
55+
4956
By("clean up API objects created during the test")
5057
kbc.CleanupManifests(filepath.Join("config", "default"))
5158

@@ -85,10 +92,6 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
8592
err = kbc.CreateManagerNamespace()
8693
ExpectWithOffset(1, err).NotTo(HaveOccurred())
8794

88-
By("labeling all namespaces to warn about restricted")
89-
err = kbc.LabelAllNamespacesToWarnAboutRestricted()
90-
ExpectWithOffset(1, err).NotTo(HaveOccurred())
91-
9295
By("updating the go.mod")
9396
err = kbc.Tidy()
9497
ExpectWithOffset(1, err).NotTo(HaveOccurred())
@@ -175,9 +178,6 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
175178
ExpectWithOffset(1, podOutput).To(ContainSubstring("health-probe-bind-address"),
176179
"Expected manager pod to have --health-probe-bind-address flag")
177180

178-
By("validating the metrics endpoint")
179-
_ = curlMetrics(kbc, hasMetrics)
180-
181181
if hasWebhook {
182182
By("validating that cert-manager has provisioned the certificate Secret")
183183
EventuallyWithOffset(1, func() error {
@@ -255,13 +255,18 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
255255

256256
if hasMetrics {
257257
By("checking the metrics values to validate that the created resource object gets reconciled")
258-
metricsOutput := curlMetrics(kbc, hasMetrics)
258+
metricsOutput := getMetricsOutput(kbc)
259259
ExpectWithOffset(1, metricsOutput).To(ContainSubstring(fmt.Sprintf(
260260
`controller_runtime_reconcile_total{controller="%s",result="success"} 1`,
261261
strings.ToLower(kbc.Kind),
262262
)))
263263
}
264264

265+
if !hasMetrics {
266+
By("validating the metrics endpoint is not working")
267+
metricsShouldBeUnavailable(kbc)
268+
}
269+
265270
if hasWebhook {
266271
By("validating that mutating and validating webhooks are working fine")
267272
cnt, err := kbc.Kubectl.Get(
@@ -273,112 +278,111 @@ func Run(kbc *utils.TestContext, hasWebhook, isToUseInstaller, hasMetrics bool)
273278
ExpectWithOffset(1, err).NotTo(HaveOccurred())
274279
ExpectWithOffset(1, count).To(BeNumerically("==", 5))
275280
}
276-
277281
}
278282

279-
// curlMetrics curl's the /metrics endpoint, returning all logs once a 200 status is returned.
280-
func curlMetrics(kbc *utils.TestContext, hasMetrics bool) string {
283+
// getMetricsOutput return the metrics output from curl pod
284+
func getMetricsOutput(kbc *utils.TestContext) string {
281285
var metricsOutput string
282-
if hasMetrics {
283-
By("validating that the controller-manager service is available")
284-
_, err := kbc.Kubectl.Get(
286+
By("validating that the controller-manager service is available")
287+
_, err := kbc.Kubectl.Get(
288+
true,
289+
"service", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
290+
)
291+
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Controller-manager service should exist")
292+
293+
By("ensuring the service endpoint is ready")
294+
eventuallyCheckServiceEndpoint := func() error {
295+
output, err := kbc.Kubectl.Get(
285296
true,
286-
"service", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
297+
"endpoints", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
298+
"-o", "jsonpath={.subsets[*].addresses[*].ip}",
287299
)
288-
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Controller-manager service should exist")
289-
290-
By("ensuring the service endpoint is ready")
291-
eventuallyCheckServiceEndpoint := func() error {
292-
output, err := kbc.Kubectl.Get(
293-
true,
294-
"endpoints", fmt.Sprintf("e2e-%s-controller-manager-metrics-service", kbc.TestSuffix),
295-
"-o", "jsonpath={.subsets[*].addresses[*].ip}",
296-
)
297-
if err != nil {
298-
return err
299-
}
300-
if output == "" {
301-
return fmt.Errorf("no endpoints found")
302-
}
303-
return nil
300+
if err != nil {
301+
return err
304302
}
305-
EventuallyWithOffset(2, eventuallyCheckServiceEndpoint, 2*time.Minute, time.Second).Should(Succeed(),
306-
"Service endpoint should be ready")
307-
308-
By("creating a curl pod to access the metrics endpoint")
309-
// nolint:lll
310-
cmdOpts := []string{
311-
"run", "curl",
312-
"--restart=Never",
313-
"--namespace", kbc.Kubectl.Namespace,
314-
"--image=curlimages/curl:7.78.0",
315-
"--",
316-
"/bin/sh", "-c", fmt.Sprintf("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics",
317-
kbc.TestSuffix, kbc.Kubectl.Namespace),
303+
if output == "" {
304+
return fmt.Errorf("no endpoints found")
318305
}
319-
_, err = kbc.Kubectl.CommandInNamespace(cmdOpts...)
320-
ExpectWithOffset(2, err).NotTo(HaveOccurred())
306+
return nil
307+
}
308+
EventuallyWithOffset(2, eventuallyCheckServiceEndpoint, 2*time.Minute, time.Second).Should(Succeed(),
309+
"Service endpoint should be ready")
321310

322-
By("validating that the curl pod is running as expected")
323-
verifyCurlUp := func() error {
324-
status, err := kbc.Kubectl.Get(
325-
true,
326-
"pods", "curl", "-o", "jsonpath={.status.phase}")
327-
ExpectWithOffset(3, err).NotTo(HaveOccurred())
328-
if status != "Succeeded" {
329-
return fmt.Errorf("curl pod in %s status", status)
330-
}
331-
return nil
332-
}
333-
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())
311+
By("creating a curl pod to access the metrics endpoint")
312+
cmdOpts := cmdOptsToCreateCurlPod(kbc)
313+
_, err = kbc.Kubectl.CommandInNamespace(cmdOpts...)
314+
ExpectWithOffset(2, err).NotTo(HaveOccurred())
334315

335-
By("validating that the metrics endpoint is serving as expected")
336-
getCurlLogs := func() string {
337-
metricsOutput, err = kbc.Kubectl.Logs("curl")
338-
ExpectWithOffset(3, err).NotTo(HaveOccurred())
339-
return metricsOutput
340-
}
341-
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("< HTTP/1.1 200 OK"))
342-
} else {
343-
By("creating a curl pod to access the metrics endpoint")
344-
// nolint:lll
345-
cmdOpts := []string{
346-
"run", "curl",
347-
"--restart=Never",
348-
"--namespace", kbc.Kubectl.Namespace,
349-
"--image=curlimages/curl:7.78.0",
350-
"--",
351-
"/bin/sh", "-c", fmt.Sprintf("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics",
352-
kbc.TestSuffix, kbc.Kubectl.Namespace),
316+
By("validating that the curl pod is running as expected")
317+
verifyCurlUp := func() error {
318+
status, err := kbc.Kubectl.Get(
319+
true,
320+
"pods", "curl", "-o", "jsonpath={.status.phase}")
321+
ExpectWithOffset(3, err).NotTo(HaveOccurred())
322+
if status != "Succeeded" {
323+
return fmt.Errorf("curl pod in %s status", status)
353324
}
354-
_, err := kbc.Kubectl.CommandInNamespace(cmdOpts...)
355-
ExpectWithOffset(2, err).NotTo(HaveOccurred())
325+
return nil
326+
}
327+
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())
356328

357-
By("validating that the curl pod fail as expected")
358-
verifyCurlUp := func() error {
359-
status, err := kbc.Kubectl.Get(
360-
true,
361-
"pods", "curl", "-o", "jsonpath={.status.phase}")
362-
ExpectWithOffset(3, err).NotTo(HaveOccurred())
363-
if status != "Failed" {
364-
return fmt.Errorf(
365-
"curl pod in %s status when should fail with an error", status)
366-
}
367-
return nil
368-
}
369-
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())
329+
By("validating that the metrics endpoint is serving as expected")
330+
getCurlLogs := func() string {
331+
metricsOutput, err = kbc.Kubectl.Logs("curl")
332+
ExpectWithOffset(3, err).NotTo(HaveOccurred())
333+
return metricsOutput
334+
}
335+
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("< HTTP/1.1 200 OK"))
336+
removeCurlPod(kbc)
337+
return metricsOutput
338+
}
339+
340+
func metricsShouldBeUnavailable(kbc *utils.TestContext) {
341+
By("creating a curl pod to access the metrics endpoint")
342+
cmdOpts := cmdOptsToCreateCurlPod(kbc)
343+
_, err := kbc.Kubectl.CommandInNamespace(cmdOpts...)
344+
ExpectWithOffset(2, err).NotTo(HaveOccurred())
370345

371-
By("validating that the metrics endpoint is not working as expected")
372-
getCurlLogs := func() string {
373-
metricsOutput, err := kbc.Kubectl.Logs("curl")
374-
ExpectWithOffset(3, err).NotTo(HaveOccurred())
375-
return metricsOutput
346+
By("validating that the curl pod fail as expected")
347+
verifyCurlUp := func() error {
348+
status, err := kbc.Kubectl.Get(
349+
true,
350+
"pods", "curl", "-o", "jsonpath={.status.phase}")
351+
ExpectWithOffset(3, err).NotTo(HaveOccurred())
352+
if status != "Failed" {
353+
return fmt.Errorf(
354+
"curl pod in %s status when should fail with an error", status)
376355
}
377-
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("Could not resolve host"))
356+
return nil
357+
}
358+
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())
359+
360+
By("validating that the metrics endpoint is not working as expected")
361+
getCurlLogs := func() string {
362+
metricsOutput, err := kbc.Kubectl.Logs("curl")
363+
ExpectWithOffset(3, err).NotTo(HaveOccurred())
364+
return metricsOutput
378365
}
366+
EventuallyWithOffset(2, getCurlLogs, 10*time.Second, time.Second).Should(ContainSubstring("Could not resolve host"))
367+
removeCurlPod(kbc)
368+
}
369+
370+
func cmdOptsToCreateCurlPod(kbc *utils.TestContext) []string {
371+
// nolint:lll
372+
cmdOpts := []string{
373+
"run", "curl",
374+
"--restart=Never",
375+
"--namespace", kbc.Kubectl.Namespace,
376+
"--image=curlimages/curl:7.78.0",
377+
"--",
378+
"/bin/sh", "-c", fmt.Sprintf("curl -v -k http://e2e-%s-controller-manager-metrics-service.%s.svc.cluster.local:8080/metrics",
379+
kbc.TestSuffix, kbc.Kubectl.Namespace),
380+
}
381+
return cmdOpts
382+
}
383+
384+
func removeCurlPod(kbc *utils.TestContext) {
379385
By("cleaning up the curl pod")
380386
_, err := kbc.Kubectl.Delete(true, "pods/curl")
381387
ExpectWithOffset(3, err).NotTo(HaveOccurred())
382-
383-
return metricsOutput
384388
}

0 commit comments

Comments
 (0)