Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to configure a metrics port in pods #392

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/v1/loadtest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ type Server struct {
// Run describes a list of run containers. The container for the test server is always
// the first container on the list.
Run []corev1.Container `json:"run"`

MetricsPort int32 `json:"metricsPort,omitempty"`
}

// Client defines a component that sends traffic to a server component.
Expand Down Expand Up @@ -235,6 +237,8 @@ type Client struct {
// Run describes a list of run containers. The container for the test client is always
// the first container on the list.
Run []corev1.Container `json:"run"`

MetricsPort int32 `json:"metricsPort,omitempty"`
}

// Results defines where and how test results and artifacts should be
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/e2etest.grpc.io_loadtests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ spec:
image. If the user intends for the operator to clone and build
code, it must also manually set a build image."
type: string
metricsPort:
format: int32
type: integer
name:
description: "Name is a string that distinguishes this client
from others in the test. Explicitly setting a name is recommended
Expand Down Expand Up @@ -2896,6 +2899,9 @@ spec:
image. If the user intends for the operator to clone and build
code, it must also manually set a build image."
type: string
metricsPort:
format: int32
type: integer
name:
description: Name is a string that distinguishes this server
from others in the test. Since tests are currently limited
Expand Down
16 changes: 16 additions & 0 deletions podbuilder/podbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ func (pb *PodBuilder) PodForClient(client *grpcv1.Client) (*corev1.Pod, error) {
ContainerPort: config.DriverPort,
})

if client.MetricsPort != 0 {
runContainer.Ports = append(runContainer.Ports, corev1.ContainerPort{
Name: "metrics",
Protocol: corev1.ProtocolTCP,
ContainerPort: client.MetricsPort,
})
}

return pod, nil
}

Expand Down Expand Up @@ -285,6 +293,14 @@ func (pb *PodBuilder) PodForServer(server *grpcv1.Server) (*corev1.Pod, error) {
ContainerPort: config.DriverPort,
})

if server.MetricsPort != 0 {
runContainer.Ports = append(runContainer.Ports, corev1.ContainerPort{
Name: "metrics",
Protocol: corev1.ProtocolTCP,
ContainerPort: server.MetricsPort,
})
}

return pod, nil
}

Expand Down
30 changes: 30 additions & 0 deletions podbuilder/podbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,36 @@ var _ = Describe("PodBuilder", func() {
Expect(getValue("driver", "ContainerPort", runContainer.Ports)).To(BeEquivalentTo(config.DriverPort))
})

It("does not expose the metrics port if not set", func() {
client.Run = []corev1.Container{{}}
client.Run[0].Name = config.RunContainerName
client.Run[0].Command = []string{"go"}
client.Run[0].Args = []string{"run", "main.go"}

pod, err := builder.PodForClient(client)
Expect(err).ToNot(HaveOccurred())
Expect(pod.Spec.Containers).ToNot(BeEmpty())

runContainer := kubehelpers.ContainerForName(config.RunContainerName, pod.Spec.Containers)
Expect(getNames(runContainer.Ports)).NotTo(ContainElement("metrics"))
})

It("exposes the metrics port if set", func() {
client.Run = []corev1.Container{{}}
client.Run[0].Name = config.RunContainerName
client.Run[0].Command = []string{"go"}
client.Run[0].Args = []string{"run", "main.go"}
client.MetricsPort = 4242

pod, err := builder.PodForClient(client)
Expect(err).ToNot(HaveOccurred())
Expect(pod.Spec.Containers).ToNot(BeEmpty())

runContainer := kubehelpers.ContainerForName(config.RunContainerName, pod.Spec.Containers)
Expect(getNames(runContainer.Ports)).To(ContainElement("metrics"))
Expect(getValue("metrics", "ContainerPort", runContainer.Ports)).To(BeEquivalentTo(client.MetricsPort))
})

It("attached the env to other run containers", func() {
pod, err := builder.PodForClient(client)
Expect(err).ToNot(HaveOccurred())
Expand Down
Loading