Skip to content

Commit

Permalink
Fix metrics port expose issue for triggers eventlistener
Browse files Browse the repository at this point in the history
  • Loading branch information
savitaashture committed May 12, 2021
1 parent f84fcfc commit 1e43d56
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 41 deletions.
29 changes: 28 additions & 1 deletion cmd/eventlistenersink/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"

dynamicClientset "github.com/tektoncd/triggers/pkg/client/dynamic/clientset"
Expand All @@ -30,6 +33,8 @@ import (
"github.com/tektoncd/triggers/pkg/sink"
"github.com/tektoncd/triggers/pkg/system"
"go.uber.org/zap"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -83,7 +88,29 @@ func main() {
profilingServer := profiling.NewServer(profilingHandler)
metrics.MemStatsOrDie(ctx)

sharedmain.WatchObservabilityConfigOrDie(ctx, configMapWatcher, profilingHandler, logger, EventListenerLogKey)
metricsPort, err := strconv.Atoi(os.Getenv("METRICS_PROMETHEUS_PORT"))
if err != nil {
logger.Fatalw("failed to convert string to int", zap.Error(err))
}
exp := metrics.ExporterOptions{
Component: strings.ReplaceAll(EventListenerLogKey, "-", "_"),
PrometheusPort: metricsPort,
ConfigMap: nil,
Secrets: sharedmain.SecretFetcher(ctx),
}
cmData, err := metrics.UpdateExporterFromConfigMapWithOpts(ctx, exp, logger)
if err != nil {
logger.Fatalw("Error in update exporter from ConfigMap ", zap.Error(err))
}
if _, err := kubeClient.CoreV1().ConfigMaps(system.GetNamespace()).Get(ctx, metrics.ConfigMapName(),
metav1.GetOptions{}); err == nil {
configMapWatcher.Watch(metrics.ConfigMapName(),
cmData,
profilingHandler.UpdateFromConfigMap)
} else if !apierrors.IsNotFound(err) {
logger.Fatalw("Error reading ConfigMap "+metrics.ConfigMapName(), zap.Error(err))
}

logger.Info("Starting configuration manager...")
if err := configMapWatcher.Start(ctx.Done()); err != nil {
logger.Fatalw("Failed to start configuration manager", zap.Error(err))
Expand Down
4 changes: 2 additions & 2 deletions config/controller-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ metadata:
spec:
ports:
- name: http-metrics
port: 9090
port: 9000
protocol: TCP
targetPort: 9090
targetPort: 9000
selector:
app.kubernetes.io/name: controller
app.kubernetes.io/component: controller
Expand Down
2 changes: 2 additions & 0 deletions config/controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ spec:
value: config-observability-triggers
- name: METRICS_DOMAIN
value: tekton.dev/triggers
- name: METRICS_PROMETHEUS_PORT
value: "9000"
securityContext:
allowPrivilegeEscalation: false
# User 65532 is the distroless nonroot user ID
Expand Down
2 changes: 1 addition & 1 deletion config/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ spec:
value: tekton.dev/triggers
ports:
- name: metrics
containerPort: 9090
containerPort: 9000
- name: profiling
containerPort: 8008
- name: https-webhook
Expand Down
2 changes: 1 addition & 1 deletion docs/eventlisteners.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ kubectl get pods --selector eventlistener=my-eventlistener

## Configuring metrics for `EventListeners`

The following pipeline metrics are available on the `eventlistener` Service on port `9090`.
The following pipeline metrics are available on the `eventlistener` Service on port `9000`.

| Name | Type | Labels/Tags | Status |
| ---------- | ----------- | ----------- | ----------- |
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ NAME TYPE CLUSTER-IP EXTERNAL-IP PORT
el-listener ClusterIP 10.100.151.220 <none> 8080/TCP 48s <--- this will receive the event
tekton-pipelines-controller ClusterIP 10.103.144.96 <none> 9090/TCP 8m34s
tekton-pipelines-webhook ClusterIP 10.96.198.4 <none> 443/TCP 8m34s
tekton-triggers-controller ClusterIP 10.102.221.96 <none> 9090/TCP 7m56s
tekton-triggers-controller ClusterIP 10.102.221.96 <none> 9000/TCP 7m56s
tekton-triggers-webhook ClusterIP 10.99.59.231 <none> 443/TCP 7m56s
```

Expand Down
55 changes: 26 additions & 29 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -65,8 +66,6 @@ const (
eventListenerServiceTLSPortName = "https-listener"
// eventListenerContainerPort defines the port exposed by the EventListener Container
eventListenerContainerPort = 8000
// eventListenerMetricsPort defines the port exposed by the EventListener metrics endpoint
eventListenerMetricsPort = 9090
// GeneratedResourcePrefix is the name prefix for resources generated in the
// EventListener reconciler
GeneratedResourcePrefix = "el"
Expand Down Expand Up @@ -274,7 +273,18 @@ func (r *Reconciler) reconcileDeployment(ctx context.Context, logger *zap.Sugare
return err
}

// METRICS_PROMETHEUS_PORT defines the port exposed by the EventListener metrics endpoint
// env METRICS_PROMETHEUS_PORT set by controller
metricsPort, err := strconv.Atoi(os.Getenv("METRICS_PROMETHEUS_PORT"))
if err != nil {
logger.Error(err)
return err
}
container := getContainer(el, r.config, nil)
container.Ports = append(container.Ports, corev1.ContainerPort{
ContainerPort: int32(metricsPort),
Protocol: corev1.ProtocolTCP,
})
container.VolumeMounts = []corev1.VolumeMount{{
Name: "config-logging",
MountPath: "/etc/config-logging",
Expand All @@ -294,9 +304,15 @@ func (r *Reconciler) reconcileDeployment(ctx context.Context, logger *zap.Sugare
Name: "METRICS_DOMAIN",
Value: triggersMetricsDomain,
})
// METRICS_PROMETHEUS_PORT defines the port exposed by the EventListener metrics endpoint
// env METRICS_PROMETHEUS_PORT set by controller
container.Env = append(container.Env, corev1.EnvVar{
Name: "METRICS_PROMETHEUS_PORT",
Value: os.Getenv("METRICS_PROMETHEUS_PORT"),
})
container = addCertsForSecureConnection(container, r.config)

deployment := getDeployment(el, r.config)
deployment := getDeployment(el, container, r.config)

existingDeployment, err := r.deploymentLister.Deployments(el.Namespace).Get(el.Status.Configuration.GeneratedResourceName)
switch {
Expand Down Expand Up @@ -460,6 +476,12 @@ func (r *Reconciler) reconcileCustomObject(ctx context.Context, logger *zap.Suga
Name: "METRICS_DOMAIN",
Value: triggersMetricsDomain,
})
// METRICS_PROMETHEUS_PORT defines the port exposed by the EventListener metrics endpoint
// env METRICS_PROMETHEUS_PORT set by controller
container.Env = append(container.Env, corev1.EnvVar{
Name: "METRICS_PROMETHEUS_PORT",
Value: os.Getenv("METRICS_PROMETHEUS_PORT"),
})

podlabels := mergeMaps(el.Labels, GenerateResourceLabels(el.Name, r.config.StaticResourceLabels))

Expand Down Expand Up @@ -659,7 +681,7 @@ func (r *Reconciler) reconcileCustomObject(ctx context.Context, logger *zap.Suga
return nil
}

func getDeployment(el *v1alpha1.EventListener, c Config) *appsv1.Deployment {
func getDeployment(el *v1alpha1.EventListener, container corev1.Container, c Config) *appsv1.Deployment {
var (
tolerations []corev1.Toleration
nodeSelector, annotations, podlabels map[string]string
Expand All @@ -681,28 +703,6 @@ func getDeployment(el *v1alpha1.EventListener, c Config) *appsv1.Deployment {
},
}}

container := getContainer(el, c, nil)
container.VolumeMounts = []corev1.VolumeMount{{
Name: "config-logging",
MountPath: "/etc/config-logging",
}}
container.Env = append(container.Env, corev1.EnvVar{
Name: "SYSTEM_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
}},
})
container.Env = append(container.Env, corev1.EnvVar{
Name: "CONFIG_OBSERVABILITY_NAME",
Value: metrics.ConfigMapName(),
})
container.Env = append(container.Env, corev1.EnvVar{
Name: "METRICS_DOMAIN",
Value: triggersMetricsDomain,
})

container = addCertsForSecureConnection(container, c)
for _, v := range container.Env {
// If TLS related env are set then mount secret volume which will be used while starting the eventlistener.
if v.Name == "TLS_CERT" {
Expand Down Expand Up @@ -850,9 +850,6 @@ func getContainer(el *v1alpha1.EventListener, c Config, pod *duckv1.WithPod) cor
Ports: []corev1.ContainerPort{{
ContainerPort: int32(eventListenerContainerPort),
Protocol: corev1.ProtocolTCP,
}, {
ContainerPort: int32(eventListenerMetricsPort),
Protocol: corev1.ProtocolTCP,
}},
Resources: resources,
Args: []string{
Expand Down
25 changes: 19 additions & 6 deletions pkg/reconciler/v1alpha1/eventlistener/eventlistener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func makeDeployment(ops ...func(d *appsv1.Deployment)) *appsv1.Deployment {
ContainerPort: int32(eventListenerContainerPort),
Protocol: corev1.ProtocolTCP,
}, {
ContainerPort: int32(eventListenerMetricsPort),
ContainerPort: int32(9000),
Protocol: corev1.ProtocolTCP,
}},
LivenessProbe: &corev1.Probe{
Expand Down Expand Up @@ -257,6 +257,9 @@ func makeDeployment(ops ...func(d *appsv1.Deployment)) *appsv1.Deployment {
}, {
Name: "METRICS_DOMAIN",
Value: "tekton.dev/triggers",
}, {
Name: "METRICS_PROMETHEUS_PORT",
Value: "9000",
}},
}},
Volumes: []corev1.Volume{{
Expand Down Expand Up @@ -298,7 +301,7 @@ var withTLSConfig = func(d *appsv1.Deployment) {
ContainerPort: int32(eventListenerContainerPort),
Protocol: corev1.ProtocolTCP,
}, {
ContainerPort: int32(eventListenerMetricsPort),
ContainerPort: int32(9000),
Protocol: corev1.ProtocolTCP,
}},
LivenessProbe: &corev1.Probe{
Expand Down Expand Up @@ -371,6 +374,9 @@ var withTLSConfig = func(d *appsv1.Deployment) {
}, {
Name: "METRICS_DOMAIN",
Value: "tekton.dev/triggers",
}, {
Name: "METRICS_PROMETHEUS_PORT",
Value: "9000",
}},
}}
d.Spec.Template.Spec.Volumes = []corev1.Volume{{
Expand Down Expand Up @@ -419,9 +425,6 @@ func makeWithPod(ops ...func(d *duckv1.WithPod)) *duckv1.WithPod {
Ports: []corev1.ContainerPort{{
ContainerPort: int32(eventListenerContainerPort),
Protocol: corev1.ProtocolTCP,
}, {
ContainerPort: int32(eventListenerMetricsPort),
Protocol: corev1.ProtocolTCP,
}},
Args: []string{
"--el-name=" + eventListenerName,
Expand All @@ -447,6 +450,9 @@ func makeWithPod(ops ...func(d *duckv1.WithPod)) *duckv1.WithPod {
}, {
Name: "METRICS_DOMAIN",
Value: "tekton.dev/triggers",
}, {
Name: "METRICS_PROMETHEUS_PORT",
Value: "9000",
}},
}},
Volumes: []corev1.Volume{{
Expand Down Expand Up @@ -577,7 +583,11 @@ func withDeletionTimestamp(el *v1alpha1.EventListener) {
}

func TestReconcile(t *testing.T) {
err := os.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")
err := os.Setenv("METRICS_PROMETHEUS_PORT", "9000")
if err != nil {
t.Fatal(err)
}
err = os.Setenv("SYSTEM_NAMESPACE", "tekton-pipelines")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -932,6 +942,9 @@ func TestReconcile(t *testing.T) {
}, {
Name: "METRICS_DOMAIN",
Value: "tekton.dev/triggers",
}, {
Name: "METRICS_PROMETHEUS_PORT",
Value: "9000",
}}
})

Expand Down

0 comments on commit 1e43d56

Please sign in to comment.