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 Prometheus trait compatible with Knative Serving #1478

Merged
merged 5 commits into from
Jun 3, 2020
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
31 changes: 21 additions & 10 deletions pkg/trait/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,17 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) {
options := []string{strconv.Itoa(t.Port), path.Join(prometheusJmxExporterConfigMountPath, prometheusJmxExporterConfigFileName)}
container.Args = append(container.Args, "-javaagent:dependencies/io.prometheus.jmx.jmx_prometheus_javaagent-0.3.1.jar="+strings.Join(options, ":"))

// Add the container port
// Configure the Prometheus container port
containerPort := t.getContainerPort()
container.Ports = append(container.Ports, *containerPort)
condition.Message = fmt.Sprintf("%s(%s/%d)", container.Name, containerPort.Name, containerPort.ContainerPort)
controller, err := e.DetermineControllerStrategy(t.Ctx, t.Client)
if err != nil {
return err
}
// Skip declaring the Prometheus port when Knative is enabled, as only one container port is supported
if controller != ControllerStrategyKnativeService {
container.Ports = append(container.Ports, *containerPort)
}
condition.Message = fmt.Sprintf("%s(%d)", container.Name, containerPort.ContainerPort)

// Retrieve the service or create a new one if the service trait is enabled
serviceEnabled := false
Expand All @@ -135,14 +142,18 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) {
serviceEnabled = serviceTrait.isEnabled()
}
if serviceEnabled {
// add a new service if not already created
// Add a new service if not already created
service = getServiceFor(e)
// Override the service name if none exists.
// This is required for Knative Serving, that checks no standard eponymous service exist
service.Name = service.Name + "-prometheus"
e.Resources.Add(service)
}
} else {
serviceEnabled = true
}

// Add the service port and service monitor resource
// A better strategy may be needed when the Knative profile is active
if serviceEnabled {
servicePort := t.getServicePort()
service.Spec.Ports = append(service.Spec.Ports, *servicePort)
Expand All @@ -168,7 +179,6 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) {

func (t *prometheusTrait) getContainerPort() *corev1.ContainerPort {
containerPort := corev1.ContainerPort{
Name: prometheusPortName,
ContainerPort: int32(t.Port),
Protocol: corev1.ProtocolTCP,
}
Expand All @@ -177,10 +187,11 @@ func (t *prometheusTrait) getContainerPort() *corev1.ContainerPort {

func (t *prometheusTrait) getServicePort() *corev1.ServicePort {
servicePort := corev1.ServicePort{
Name: prometheusPortName,
Port: int32(t.Port),
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(prometheusPortName),
Name: prometheusPortName,
Port: int32(t.Port),
Protocol: corev1.ProtocolTCP,
// Avoid relying on named port, as Knative enforces specific values used for content negotiation
TargetPort: intstr.FromInt(t.Port),
}
return &servicePort
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/trait/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ func TestApplyNominalPrometheusTraitDoesSucceed(t *testing.T) {

ports := container.Ports
assert.Len(t, ports, 1)
assert.Equal(t, "prometheus", ports[0].Name)
assert.Equal(t, int32(9779), ports[0].ContainerPort)
assert.Equal(t, corev1.ProtocolTCP, ports[0].Protocol)

service := environment.Resources.GetService(func(service *corev1.Service) bool {
return service.Name == "integration-name"
return service.Name == "integration-name-prometheus"
})
assert.NotNil(t, service)
assert.Len(t, service.Spec.Ports, 1)
Expand Down Expand Up @@ -103,7 +102,7 @@ func TestApplyPrometheusTraitWithoutContainerDoesNotSucceed(t *testing.T) {
assert.Equal(t, corev1.ConditionFalse, condition.Status)
}

func TestApplyPrometheusTraitWithServiceDoesNotSucceed(t *testing.T) {
func TestApplyPrometheusTraitWithServiceDoesSucceed(t *testing.T) {
trait, environment := createNominalPrometheusTest()
environment.Resources = kubernetes.NewCollection(
&appsv1.Deployment{
Expand Down Expand Up @@ -146,8 +145,8 @@ func TestApplyPrometheusTraitWithServiceDoesNotSucceed(t *testing.T) {

assert.Len(t, environment.Integration.Status.Conditions, 1)
condition := environment.Integration.Status.Conditions[0]
assert.Equal(t, v1.IntegrationConditionServiceNotAvailableReason, condition.Reason)
assert.Equal(t, corev1.ConditionFalse, condition.Status)
assert.Equal(t, v1.IntegrationConditionPrometheusAvailableReason, condition.Reason)
assert.Equal(t, corev1.ConditionTrue, condition.Status)
}

func TestPrometheusTraitGetServiceMonitor(t *testing.T) {
Expand Down