Skip to content

Commit

Permalink
Define ElPort to config port of eventListener, set default value to 8080
Browse files Browse the repository at this point in the history
Expose the configurable capabilities of port of eventListener.
Remove servicePort orelse servicePort.Port always be 8080 because *ElPort is calculate when compile.
  • Loading branch information
cccfeng authored and tekton-robot committed Jan 7, 2020
1 parent eb052df commit 2e8809d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
1 change: 1 addition & 0 deletions config/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ spec:
"-logtostderr",
"-stderrthreshold", "INFO",
"-el-image", "github.com/tektoncd/triggers/cmd/eventlistenersink",
"-el-port", "8080"
]
volumeMounts:
- name: config-logging
Expand Down
26 changes: 13 additions & 13 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ const (
eventListenerControllerName = "EventListener"
// eventListenerConfigMapName is for the automatically created ConfigMap
eventListenerConfigMapName = "config-logging-triggers"
// Port defines the port for the EventListener to listen on
Port = 8080
// GeneratedResourcePrefix is the name prefix for resources generated in the
// EventListener reconciler
GeneratedResourcePrefix = "el"
Expand All @@ -61,19 +59,15 @@ var (
// The container that we use to run in the EventListener Pods
elImage = flag.String("el-image", "override-with-el:latest",
"The container image for the EventListener Pod.")
// ElPort defines the port for the EventListener to listen on
ElPort = flag.Int("el-port", 8080,
"The container port for the EventListener to listen on.")
// StaticResourceLabels is a map with all the labels that should be on
// all resources generated by the EventListener
StaticResourceLabels = map[string]string{
"app.kubernetes.io/managed-by": "EventListener",
"app.kubernetes.io/part-of": "Triggers",
}
servicePort = corev1.ServicePort{
Protocol: corev1.ProtocolTCP,
Port: int32(Port),
TargetPort: intstr.IntOrString{
IntVal: int32(Port),
},
}
)

// Reconciler implements controller.Reconciler for Configuration resources.
Expand Down Expand Up @@ -176,7 +170,13 @@ func (c *Reconciler) reconcileService(el *v1alpha1.EventListener) error {
Selector: mergeLabels(el.Labels, GenerateResourceLabels(el.Name)),
Type: el.Spec.ServiceType,
Ports: []corev1.ServicePort{
servicePort,
{
Protocol: corev1.ProtocolTCP,
Port: int32(*ElPort),
TargetPort: intstr.IntOrString{
IntVal: int32(*ElPort),
},
},
},
},
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func (c *Reconciler) reconcileService(el *v1alpha1.EventListener) error {
c.Logger.Errorf("Error creating EventListener Service: %s", err)
return err
}
el.Status.SetAddress(listenerHostname(service.Name, el.Namespace, Port))
el.Status.SetAddress(listenerHostname(service.Name, el.Namespace, *ElPort))
c.Logger.Infof("Created EventListener Service %s in Namespace %s", service.Name, el.Namespace)
default:
c.Logger.Error(err)
Expand Down Expand Up @@ -254,13 +254,13 @@ func (c *Reconciler) reconcileDeployment(el *v1alpha1.EventListener) error {
Name: "event-listener",
Image: *elImage,
Ports: []corev1.ContainerPort{{
ContainerPort: int32(Port),
ContainerPort: int32(*ElPort),
Protocol: corev1.ProtocolTCP,
}},
Args: []string{
"-el-name", el.Name,
"-el-namespace", el.Namespace,
"-port", strconv.Itoa(Port),
"-port", strconv.Itoa(*ElPort),
},
VolumeMounts: []corev1.VolumeMount{{
Name: "config-logging",
Expand Down
29 changes: 21 additions & 8 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/rand"
k8stest "k8s.io/client-go/testing"
"knative.dev/pkg/apis"
Expand Down Expand Up @@ -122,7 +123,7 @@ func Test_reconcileService(t *testing.T) {
eventListener1.Status.Address = &duckv1alpha1.Addressable{}
eventListener1.Status.Address.URL = &apis.URL{
Scheme: "http",
Host: listenerHostname(generatedResourceName, namespace, Port),
Host: listenerHostname(generatedResourceName, namespace, *ElPort),
}

eventListener2 := eventListener1.DeepCopy()
Expand All @@ -134,7 +135,13 @@ func Test_reconcileService(t *testing.T) {
Selector: generatedLabels,
Type: eventListener1.Spec.ServiceType,
Ports: []corev1.ServicePort{
servicePort,
{
Protocol: corev1.ProtocolTCP,
Port: int32(*ElPort),
TargetPort: intstr.IntOrString{
IntVal: int32(*ElPort),
},
},
},
},
}
Expand Down Expand Up @@ -271,14 +278,14 @@ func Test_reconcileDeployment(t *testing.T) {
Image: *elImage,
Ports: []corev1.ContainerPort{
{
ContainerPort: int32(Port),
ContainerPort: int32(*ElPort),
Protocol: corev1.ProtocolTCP,
},
},
Args: []string{
"-el-name", eventListenerName,
"-el-namespace", namespace,
"-port", strconv.Itoa(Port),
"-port", strconv.Itoa(*ElPort),
},
VolumeMounts: []corev1.VolumeMount{
{
Expand Down Expand Up @@ -458,7 +465,7 @@ func TestReconcile(t *testing.T) {
),
bldr.EventListenerStatus(
bldr.EventListenerConfig(generatedResourceName),
bldr.EventListenerAddress(listenerHostname(generatedResourceName, namespace, Port)),
bldr.EventListenerAddress(listenerHostname(generatedResourceName, namespace, *ElPort)),
bldr.EventListenerCondition(
v1alpha1.ServiceExists,
corev1.ConditionTrue,
Expand Down Expand Up @@ -511,13 +518,13 @@ func TestReconcile(t *testing.T) {
Name: "event-listener",
Image: *elImage,
Ports: []corev1.ContainerPort{{
ContainerPort: int32(Port),
ContainerPort: int32(*ElPort),
Protocol: corev1.ProtocolTCP,
}},
Args: []string{
"-el-name", eventListenerName,
"-el-namespace", namespace,
"-port", strconv.Itoa(Port),
"-port", strconv.Itoa(*ElPort),
},
VolumeMounts: []corev1.VolumeMount{{
Name: "config-logging",
Expand Down Expand Up @@ -567,7 +574,13 @@ func TestReconcile(t *testing.T) {
Selector: generatedLabels,
Type: eventListener1.Spec.ServiceType,
Ports: []corev1.ServicePort{
servicePort,
{
Protocol: corev1.ProtocolTCP,
Port: int32(*ElPort),
TargetPort: intstr.IntOrString{
IntVal: int32(*ElPort),
},
},
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions test/eventlistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ func TestEventListenerCreate(t *testing.T) {
t.Fatalf("Error listing EventListener sink pods: %s", err)
}

// Port forward sink pod for http request
portString := strconv.Itoa(eventReconciler.Port)
// ElPort forward sink pod for http request
portString := strconv.Itoa(*eventReconciler.ElPort)
podName := sinkPods.Items[0].Name
cmd := exec.Command("kubectl", "port-forward", podName, "-n", namespace, fmt.Sprintf("%s:%s", portString, portString))
err = cmd.Start()
Expand Down

0 comments on commit 2e8809d

Please sign in to comment.