Skip to content

Commit

Permalink
Add support for NodePort in Jaeger Query Service (#1394)
Browse files Browse the repository at this point in the history
This PR should allow users to expose Jaeger's Query service as a NodePort at a specific port. Earlier, the Jaeger Operator did not support specifying a port value and would depend on K8s to randomly select an apt port value. This should resolve this blocker.

Resolves #1307
  • Loading branch information
csp197 authored Mar 27, 2021
1 parent 7c24a0f commit 52b4c70
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/jaegertracing/v1/jaeger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ type JaegerQuerySpec struct {
// See https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
ServiceType v1.ServiceType `json:"serviceType,omitempty"`

// +optional
// NodePort represents the port at which the NodePort service to allocate
NodePort int32 `json:"nodePort,omitempty"`

// +optional
// TracingEnabled if set to false adds the JAEGER_DISABLED environment flag and removes the injected
// agent container from the query component to disable tracing requests to the query service.
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/jaegertracing/v1/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions pkg/service/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ func NewQueryService(jaeger *v1.Jaeger, selector map[string]string) *corev1.Serv
annotations["service.alpha.openshift.io/serving-cert-secret-name"] = GetTLSSecretNameForQueryService(jaeger)
}

ports := []corev1.ServicePort{
{
Name: getPortNameForQueryService(jaeger),
Port: int32(GetPortForQueryService(jaeger)),
TargetPort: intstr.FromInt(getTargetPortForQueryService(jaeger)),
},
}
if jaeger.Spec.Query.ServiceType == corev1.ServiceTypeNodePort {
ports[0].NodePort = GetNodePortForQueryService(jaeger)
}

return &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
Expand All @@ -41,13 +52,7 @@ func NewQueryService(jaeger *v1.Jaeger, selector map[string]string) *corev1.Serv
Spec: corev1.ServiceSpec{
Selector: selector,
Type: getTypeForQueryService(jaeger),
Ports: []corev1.ServicePort{
{
Name: getPortNameForQueryService(jaeger),
Port: int32(GetPortForQueryService(jaeger)),
TargetPort: intstr.FromInt(getTargetPortForQueryService(jaeger)),
},
},
Ports: ports,
},
}
}
Expand Down Expand Up @@ -90,3 +95,8 @@ func getTypeForQueryService(jaeger *v1.Jaeger) corev1.ServiceType {
}
return corev1.ServiceTypeClusterIP
}

// GetNodePortForQueryService returns the query service NodePort for this Jaeger instance
func GetNodePortForQueryService(jaeger *v1.Jaeger) int32 {
return jaeger.Spec.Query.NodePort
}
19 changes: 19 additions & 0 deletions pkg/service/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestQueryServiceNodePortWithIngress(t *testing.T) {
assert.Len(t, svc.Spec.Ports, 1)
assert.Equal(t, int32(16686), svc.Spec.Ports[0].Port)
assert.Equal(t, "http-query", svc.Spec.Ports[0].Name)
assert.Equal(t, int32(0), svc.Spec.Ports[0].NodePort)
assert.Equal(t, intstr.FromInt(16686), svc.Spec.Ports[0].TargetPort)
assert.Equal(t, svc.Spec.Type, corev1.ServiceTypeNodePort) // make sure we get a NodePort service
}
Expand All @@ -84,3 +85,21 @@ func TestQueryServiceLoadBalancerWithIngress(t *testing.T) {
assert.Equal(t, intstr.FromInt(16686), svc.Spec.Ports[0].TargetPort)
assert.Equal(t, svc.Spec.Type, corev1.ServiceTypeLoadBalancer) // make sure we get a LoadBalancer service
}

func TestQueryServiceSpecifiedNodePortWithIngress(t *testing.T) {
name := "TestQueryServiceSpecifiedNodePortWithIngress"
selector := map[string]string{"app": "myapp", "jaeger": name, "jaeger-component": "query"}

jaeger := v1.NewJaeger(types.NamespacedName{Name: name})
jaeger.Spec.Query.ServiceType = corev1.ServiceTypeNodePort
jaeger.Spec.Query.NodePort = 32767
svc := NewQueryService(jaeger, selector)

assert.Equal(t, "testqueryservicespecifiednodeportwithingress-query", svc.ObjectMeta.Name)
assert.Len(t, svc.Spec.Ports, 1)
assert.Equal(t, int32(16686), svc.Spec.Ports[0].Port)
assert.Equal(t, "http-query", svc.Spec.Ports[0].Name)
assert.Equal(t, int32(32767), svc.Spec.Ports[0].NodePort) // make sure we get the same NodePort as set above
assert.Equal(t, intstr.FromInt(16686), svc.Spec.Ports[0].TargetPort)
assert.Equal(t, svc.Spec.Type, corev1.ServiceTypeNodePort)
}

0 comments on commit 52b4c70

Please sign in to comment.