Skip to content

Commit

Permalink
Node port service match kubernetes resource
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcshane authored and tekton-robot committed May 6, 2024
1 parent b5fc165 commit 77151f1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
15 changes: 11 additions & 4 deletions pkg/reconciler/eventlistener/eventlistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ var (
"app.kubernetes.io/part-of": "Triggers",
"eventlistener": eventListenerName,
}

nodePortServicePortNumber int32 = 30300
)

// compareCondition compares two conditions based on their Type field.
Expand Down Expand Up @@ -605,11 +607,12 @@ func TestReconcile(t *testing.T) {
el.Spec.ServiceAccountName = updatedSa
})

elWithNodePortServiceType := makeEL(withStatus, func(el *v1beta1.EventListener) {
elWithNodePortServiceType := makeEL(func(el *v1beta1.EventListener) {
el.Spec.Resources.KubernetesResource = &v1beta1.KubernetesResource{
ServiceType: corev1.ServiceTypeNodePort,
ServicePort: ptr.Int32(nodePortServicePortNumber),
}
})
}, withStatus)

elWithTolerations := makeEL(withStatus, func(el *v1beta1.EventListener) {
el.Spec.Resources.KubernetesResource = &v1beta1.KubernetesResource{
Expand Down Expand Up @@ -647,9 +650,10 @@ func TestReconcile(t *testing.T) {
})
})

elWithKubernetesResource := makeEL(withStatus, func(el *v1beta1.EventListener) {
elWithKubernetesResource := makeEL(func(el *v1beta1.EventListener) {
el.Spec.Resources.KubernetesResource = &v1beta1.KubernetesResource{
ServiceType: corev1.ServiceTypeNodePort,
ServicePort: &nodePortServicePortNumber,
WithPodSpec: duckv1.WithPodSpec{
Template: duckv1.PodSpecable{
Spec: corev1.PodSpec{
Expand All @@ -671,7 +675,7 @@ func TestReconcile(t *testing.T) {
},
},
}
})
}, withStatus)

elWithCustomResourceForEnv := makeEL(withStatus, withKnativeStatus, func(el *v1beta1.EventListener) {
el.Spec.Resources.CustomResource = &v1beta1.CustomResource{
Expand Down Expand Up @@ -1014,10 +1018,13 @@ func TestReconcile(t *testing.T) {

elServiceTypeNodePort := makeService(func(s *corev1.Service) {
s.Spec.Type = corev1.ServiceTypeNodePort
s.Spec.Ports[0].Port = nodePortServicePortNumber
s.Spec.Ports[0].NodePort = nodePortServicePortNumber
})

elServiceWithUpdatedNodePort := makeService(func(s *corev1.Service) {
s.Spec.Type = corev1.ServiceTypeNodePort
s.Spec.Ports[0].Port = 30300
s.Spec.Ports[0].NodePort = 30000
})

Expand Down
8 changes: 8 additions & 0 deletions pkg/reconciler/eventlistener/resources/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ func withServiceTypeLoadBalancer(el *v1beta1.EventListener) {
}
}

func withNodePort30300(el *v1beta1.EventListener) {
port := int32(30300)
el.Spec.Resources.KubernetesResource = &v1beta1.KubernetesResource{
ServiceType: corev1.ServiceTypeNodePort,
ServicePort: &port,
}
}

func withServicePort80(el *v1beta1.EventListener) {
port := int32(80)
el.Spec.Resources.KubernetesResource = &v1beta1.KubernetesResource{
Expand Down
9 changes: 8 additions & 1 deletion pkg/reconciler/eventlistener/resources/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func MakeService(ctx context.Context, el *v1beta1.EventListener, c Config) *core
func ServicePort(el *v1beta1.EventListener, c Config) corev1.ServicePort {
var elCert, elKey string

nodePort := int32(0)
servicePortName := eventListenerServicePortName
servicePort := *c.Port

Expand All @@ -89,6 +90,9 @@ func ServicePort(el *v1beta1.EventListener, c Config) corev1.ServicePort {
}
if el.Spec.Resources.KubernetesResource.ServicePort != nil {
servicePort = int(*el.Spec.Resources.KubernetesResource.ServicePort)
if el.Spec.Resources.KubernetesResource.ServiceType == corev1.ServiceTypeNodePort {
nodePort = *el.Spec.Resources.KubernetesResource.ServicePort
}
}
}

Expand All @@ -112,14 +116,17 @@ func ServicePort(el *v1beta1.EventListener, c Config) corev1.ServicePort {
}
}

return corev1.ServicePort{
svc := corev1.ServicePort{
Name: servicePortName,
Protocol: corev1.ProtocolTCP,
Port: int32(servicePort),
TargetPort: intstr.IntOrString{
IntVal: int32(eventListenerContainerPort),
},
NodePort: nodePort,
}

return svc
}

// ListenerHostname returns the intended hostname for the EventListener service.
Expand Down
32 changes: 32 additions & 0 deletions pkg/reconciler/eventlistener/resources/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,38 @@ func TestService(t *testing.T) {
},
},
}}, {
name: "EventListener with node port: 30300",
el: makeEL(withNodePort30300, withStatus),
want: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: generatedResourceName,
Namespace: namespace,
Labels: map[string]string{
"app.kubernetes.io/managed-by": "EventListener",
"app.kubernetes.io/part-of": "Triggers",
"eventlistener": eventListenerName,
},
OwnerReferences: []metav1.OwnerReference{*kmeta.NewControllerRef(makeEL())},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeNodePort,
Ports: []corev1.ServicePort{{
Name: eventListenerServicePortName,
Protocol: corev1.ProtocolTCP,
Port: int32(30300),
NodePort: int32(30300),
TargetPort: intstr.IntOrString{
IntVal: int32(eventListenerContainerPort),
},
}, metricsPort},
Selector: map[string]string{
"app.kubernetes.io/managed-by": "EventListener",
"app.kubernetes.io/part-of": "Triggers",
"eventlistener": eventListenerName,
},
},
},
}, {
name: "EventListener with service port: 80",
el: makeEL(withServicePort80, withStatus),
want: &corev1.Service{
Expand Down

0 comments on commit 77151f1

Please sign in to comment.