Skip to content

Commit

Permalink
add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Aug 31, 2024
1 parent c0ba5ce commit eeebee1
Show file tree
Hide file tree
Showing 8 changed files with 566 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,26 @@ import (
"tutorial.kubebuilder.io/project/test/utils"
)

// namespace where the project is deployed in
const namespace = "project-system"

// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// metricsServiceName store the name of the metrics service
const metricsServiceName = "project-controller-manager-metrics-service"

// controllerPodName stores the manager pod name
var controllerPodName string

const tokenRequestRawString = `{"apiVersion": "authentication.k8s.io/v1", "kind": "TokenRequest"}`

// tokenRequest is a trimmed down version of the authentication.k8s.io/v1/TokenRequest Type
// that we want to use for extracting the token.
type tokenRequest struct {
Status struct {
Token string `json:"token"`
} `json:"status"`
}

var _ = Describe("Manager", Ordered, func() {
// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand All @@ -53,8 +69,12 @@ var _ = Describe("controller", Ordered, func() {
// After all tests have been executed, clean up by undeploying the controller, uninstalling CRDs,
// and deleting the namespace.
AfterAll(func() {
By("cleaning up the curl pod for metrics")
cmd := exec.Command("kubectl", "delete", "pod", "curl-metrics", "-n", namespace)
_, _ = utils.Run(cmd)

By("undeploying the controller-manager")
cmd := exec.Command("make", "undeploy")
cmd = exec.Command("make", "undeploy")
_, _ = utils.Run(cmd)

By("uninstalling CRDs")
Expand All @@ -66,11 +86,9 @@ var _ = Describe("controller", Ordered, func() {
_, _ = utils.Run(cmd)
})

// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
// The Context block contains the actual tests that validate the manager's behavior.
Context("Manager", func() {
It("should run successfully", func() {
var controllerPodName string

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down Expand Up @@ -108,6 +126,52 @@ var _ = Describe("controller", Ordered, func() {
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})

It("should ensure the metrics endpoint is serving metrics", func() {
// Validate that metrics are exposed and accessible
By("validating that the metrics endpoint is serving as expected")
ensureMetricsServiceIsAvailable := func() error {
cmd := exec.Command("kubectl", "get",
"service", metricsServiceName,
"-n", namespace,
)
_, err := utils.Run(cmd)
return err
}
EventuallyWithOffset(2, ensureMetricsServiceIsAvailable, 2*time.Minute, time.Second).Should(Succeed(),
"Metrics service should be available")

By("creating a curl pod to access the metrics endpoint")
cmd := exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
"--namespace", namespace,
"--image=curlimages/curl:7.78.0",
"--", "/bin/sh", "-c", fmt.Sprintf("curl -v -k https://%s.%s.svc.cluster.local:8443/metrics",
metricsServiceName, namespace),
)
_, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to create curl pod")

By("validating that the curl pod is running as expected")
verifyCurlUp := func() error {
cmd = exec.Command("kubectl", "get",
"pods", "curl-metrics", "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := utils.Run(cmd)
ExpectWithOffset(3, err).NotTo(HaveOccurred())
if string(status) != "Succeeded" {
return fmt.Errorf("curl pod in %s status", status)
}
return nil
}
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())

By("validating that the metrics endpoint is serving metrics")
cmd = exec.Command("kubectl", "logs", "curl-metrics", "-n", namespace)
metricsOutput, err := utils.Run(cmd)
ExpectWithOffset(3, err).NotTo(HaveOccurred(), "Failed to retrieve logs from curl pod")
ExpectWithOffset(3, metricsOutput).To(ContainSubstring("< HTTP/1.1 200 OK"))
})

// TODO(user): Customize the e2e test suite to include
// additional scenarios specific to your project.
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,26 @@ import (
"example.com/memcached/test/utils"
)

// namespace where the project is deployed in
const namespace = "project-system"

// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
// metricsServiceName store the name of the metrics service
const metricsServiceName = "project-controller-manager-metrics-service"

// controllerPodName stores the manager pod name
var controllerPodName string

const tokenRequestRawString = `{"apiVersion": "authentication.k8s.io/v1", "kind": "TokenRequest"}`

// tokenRequest is a trimmed down version of the authentication.k8s.io/v1/TokenRequest Type
// that we want to use for extracting the token.
type tokenRequest struct {
Status struct {
Token string `json:"token"`
} `json:"status"`
}

var _ = Describe("Manager", Ordered, func() {
// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand All @@ -53,8 +69,12 @@ var _ = Describe("controller", Ordered, func() {
// After all tests have been executed, clean up by undeploying the controller, uninstalling CRDs,
// and deleting the namespace.
AfterAll(func() {
By("cleaning up the curl pod for metrics")
cmd := exec.Command("kubectl", "delete", "pod", "curl-metrics", "-n", namespace)
_, _ = utils.Run(cmd)

By("undeploying the controller-manager")
cmd := exec.Command("make", "undeploy")
cmd = exec.Command("make", "undeploy")
_, _ = utils.Run(cmd)

By("uninstalling CRDs")
Expand All @@ -66,11 +86,9 @@ var _ = Describe("controller", Ordered, func() {
_, _ = utils.Run(cmd)
})

// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
// The Context block contains the actual tests that validate the manager's behavior.
Context("Manager", func() {
It("should run successfully", func() {
var controllerPodName string

By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down Expand Up @@ -108,6 +126,52 @@ var _ = Describe("controller", Ordered, func() {
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})

It("should ensure the metrics endpoint is serving metrics", func() {
// Validate that metrics are exposed and accessible
By("validating that the metrics endpoint is serving as expected")
ensureMetricsServiceIsAvailable := func() error {
cmd := exec.Command("kubectl", "get",
"service", metricsServiceName,
"-n", namespace,
)
_, err := utils.Run(cmd)
return err
}
EventuallyWithOffset(2, ensureMetricsServiceIsAvailable, 2*time.Minute, time.Second).Should(Succeed(),
"Metrics service should be available")

By("creating a curl pod to access the metrics endpoint")
cmd := exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
"--namespace", namespace,
"--image=curlimages/curl:7.78.0",
"--", "/bin/sh", "-c", fmt.Sprintf("curl -v -k https://%s.%s.svc.cluster.local:8443/metrics",
metricsServiceName, namespace),
)
_, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to create curl pod")

By("validating that the curl pod is running as expected")
verifyCurlUp := func() error {
cmd = exec.Command("kubectl", "get",
"pods", "curl-metrics", "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := utils.Run(cmd)
ExpectWithOffset(3, err).NotTo(HaveOccurred())
if string(status) != "Succeeded" {
return fmt.Errorf("curl pod in %s status", status)
}
return nil
}
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())

By("validating that the metrics endpoint is serving metrics")
cmd = exec.Command("kubectl", "logs", "curl-metrics", "-n", namespace)
metricsOutput, err := utils.Run(cmd)
ExpectWithOffset(3, err).NotTo(HaveOccurred(), "Failed to retrieve logs from curl pod")
ExpectWithOffset(3, metricsOutput).To(ContainSubstring("< HTTP/1.1 200 OK"))
})

// TODO(user): Customize the e2e test suite to include
// additional scenarios specific to your project.
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,24 @@ import (
"{{ .Repo }}/test/utils"
)
// namespace where the project is deployed in
const namespace = "{{ .ProjectName }}-system"
// metricsServiceName store the name of the metrics service
const metricsServiceName = "{{ .ProjectName }}-controller-manager-metrics-service"
// controllerPodName stores the manager pod name
var controllerPodName string
const tokenRequestRawString = ` + "`{\"apiVersion\": \"authentication.k8s.io/v1\", \"kind\": \"TokenRequest\"}`" + `
// tokenRequest is a trimmed down version of the authentication.k8s.io/v1/TokenRequest Type
// that we want to use for extracting the token.
type tokenRequest struct {
Status struct {
Token string ` + "`json:\"token\"`" + `
} ` + "`json:\"status\"`" + `
}
// Define a set of end-to-end (e2e) tests to validate the behavior of the controller.
var _ = Describe("controller", Ordered, func() {
var _ = Describe("Manager", Ordered, func() {
// Before running the tests, set up the environment by creating the namespace,
// installing CRDs, and deploying the controller.
BeforeAll(func() {
Expand All @@ -80,8 +94,12 @@ var _ = Describe("controller", Ordered, func() {
// After all tests have been executed, clean up by undeploying the controller, uninstalling CRDs,
// and deleting the namespace.
AfterAll(func() {
By("cleaning up the curl pod for metrics")
cmd := exec.Command("kubectl", "delete", "pod", "curl-metrics", "-n", namespace)
_, _ = utils.Run(cmd)
By("undeploying the controller-manager")
cmd := exec.Command("make", "undeploy")
cmd = exec.Command("make", "undeploy")
_, _ = utils.Run(cmd)
By("uninstalling CRDs")
Expand All @@ -93,11 +111,9 @@ var _ = Describe("controller", Ordered, func() {
_, _ = utils.Run(cmd)
})
// The Context block contains the actual tests that validate the operator's behavior.
Context("Operator", func() {
// The Context block contains the actual tests that validate the manager's behavior.
Context("Manager", func() {
It("should run successfully", func() {
var controllerPodName string
By("validating that the controller-manager pod is running as expected")
verifyControllerUp := func() error {
// Get the name of the controller-manager pod
Expand Down Expand Up @@ -135,6 +151,52 @@ var _ = Describe("controller", Ordered, func() {
EventuallyWithOffset(1, verifyControllerUp, time.Minute, time.Second).Should(Succeed())
})
It("should ensure the metrics endpoint is serving metrics", func() {
// Validate that metrics are exposed and accessible
By("validating that the metrics endpoint is serving as expected")
ensureMetricsServiceIsAvailable := func() error {
cmd := exec.Command("kubectl", "get",
"service", metricsServiceName,
"-n", namespace,
)
_, err := utils.Run(cmd)
return err
}
EventuallyWithOffset(2, ensureMetricsServiceIsAvailable, 2*time.Minute, time.Second).Should(Succeed(),
"Metrics service should be available")
By("creating a curl pod to access the metrics endpoint")
cmd := exec.Command("kubectl", "run", "curl-metrics", "--restart=Never",
"--namespace", namespace,
"--image=curlimages/curl:7.78.0",
"--", "/bin/sh", "-c", fmt.Sprintf("curl -v -k https://%s.%s.svc.cluster.local:8443/metrics",
metricsServiceName, namespace),
)
_, err := utils.Run(cmd)
ExpectWithOffset(2, err).NotTo(HaveOccurred(), "Failed to create curl pod")
By("validating that the curl pod is running as expected")
verifyCurlUp := func() error {
cmd = exec.Command("kubectl", "get",
"pods", "curl-metrics", "-o", "jsonpath={.status.phase}",
"-n", namespace,
)
status, err := utils.Run(cmd)
ExpectWithOffset(3, err).NotTo(HaveOccurred())
if string(status) != "Succeeded" {
return fmt.Errorf("curl pod in %s status", status)
}
return nil
}
EventuallyWithOffset(2, verifyCurlUp, 240*time.Second, time.Second).Should(Succeed())
By("validating that the metrics endpoint is serving metrics")
cmd = exec.Command("kubectl", "logs", "curl-metrics", "-n", namespace)
metricsOutput, err := utils.Run(cmd)
ExpectWithOffset(3, err).NotTo(HaveOccurred(), "Failed to retrieve logs from curl pod")
ExpectWithOffset(3, metricsOutput).To(ContainSubstring("< HTTP/1.1 200 OK"))
})
// TODO(user): Customize the e2e test suite to include
// additional scenarios specific to your project.
})
Expand Down
Loading

0 comments on commit eeebee1

Please sign in to comment.