Skip to content

Commit

Permalink
Add support for metrics port in client & server
Browse files Browse the repository at this point in the history
  • Loading branch information
fdfzcq committed Mar 18, 2024
1 parent 6724325 commit ad26267
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
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("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"}
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

0 comments on commit ad26267

Please sign in to comment.