Skip to content

Commit 74d3ec7

Browse files
Merge pull request #809 from jpeeler/wip-test-mux
add logging and separate muxer for metrics
2 parents 6d9d255 + 3ab7211 commit 74d3ec7

File tree

7 files changed

+75
-20
lines changed

7 files changed

+75
-20
lines changed

cmd/catalog/main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,35 @@ func main() {
9999
if *tlsCertPath != "" && *tlsKeyPath == "" || *tlsCertPath == "" && *tlsKeyPath != "" {
100100
logger.Warn("both --tls-key and --tls-crt must be provided for TLS to be enabled, falling back to non-https")
101101
} else if *tlsCertPath == "" && *tlsKeyPath == "" {
102-
logger.Info("TLS keys not set, using non-https")
102+
logger.Info("TLS keys not set, using non-https for metrics")
103103
} else {
104+
logger.Info("TLS keys set, using https for metrics")
104105
useTLS = true
105106
}
106107

107108
// Serve a health check.
108-
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
109+
healthMux := http.NewServeMux()
110+
healthMux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
109111
w.WriteHeader(http.StatusOK)
110112
})
111-
go http.ListenAndServe(":8080", nil)
113+
go http.ListenAndServe(":8080", healthMux)
112114

113-
http.Handle("/metrics", promhttp.Handler())
115+
metricsMux := http.NewServeMux()
116+
metricsMux.Handle("/metrics", promhttp.Handler())
114117
if useTLS {
115-
go http.ListenAndServeTLS(":8081", *tlsCertPath, *tlsKeyPath, nil)
118+
go func() {
119+
err := http.ListenAndServeTLS(":8081", *tlsCertPath, *tlsKeyPath, metricsMux)
120+
if err != nil {
121+
logger.Errorf("Metrics (https) serving failed: %v", err)
122+
}
123+
}()
116124
} else {
117-
go http.ListenAndServe(":8081", nil)
125+
go func() {
126+
err := http.ListenAndServe(":8081", metricsMux)
127+
if err != nil {
128+
logger.Errorf("Metrics (http) serving failed: %v", err)
129+
}
130+
}()
118131
}
119132

120133
// create a config client for operator status

cmd/olm/main.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,40 @@ func main() {
123123
if *tlsCertPath != "" && *tlsKeyPath == "" || *tlsCertPath == "" && *tlsKeyPath != "" {
124124
logger.Warn("both --tls-key and --tls-crt must be provided for TLS to be enabled, falling back to non-https")
125125
} else if *tlsCertPath == "" && *tlsKeyPath == "" {
126-
logger.Info("TLS keys not set, using non-https")
126+
logger.Info("TLS keys not set, using non-https for metrics")
127127
} else {
128+
logger.Info("TLS keys set, using https for metrics")
128129
useTLS = true
129130
}
130131

131132
// Serve a health check.
132-
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
133+
healthMux := http.NewServeMux()
134+
healthMux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
133135
w.WriteHeader(http.StatusOK)
134136
})
135-
go http.ListenAndServe(":8080", nil)
137+
go func() {
138+
err := http.ListenAndServe(":8080", healthMux)
139+
if err != nil {
140+
logger.Errorf("Health serving failed: %v", err)
141+
}
142+
}()
136143

137-
http.Handle("/metrics", promhttp.Handler())
144+
metricsMux := http.NewServeMux()
145+
metricsMux.Handle("/metrics", promhttp.Handler())
138146
if useTLS {
139-
go http.ListenAndServeTLS(":8081", *tlsCertPath, *tlsKeyPath, nil)
147+
go func() {
148+
err := http.ListenAndServeTLS(":8081", *tlsCertPath, *tlsKeyPath, metricsMux)
149+
if err != nil {
150+
logger.Errorf("Metrics (https) serving failed: %v", err)
151+
}
152+
}()
140153
} else {
141-
go http.ListenAndServe(":8081", nil)
154+
go func() {
155+
err := http.ListenAndServe(":8081", metricsMux)
156+
if err != nil {
157+
logger.Errorf("Metrics (http) serving failed: %v", err)
158+
}
159+
}()
142160
}
143161

144162
ready, done, sync := operator.Run(stopCh)

deploy/chart/templates/0000_50_olm_07-olm-operator.deployment.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ spec:
8888
- name: serving-cert
8989
secret:
9090
secretName: olm-operator-serving-cert
91-
optional: true
9291
{{ end }}
9392
{{- if .Values.olm.nodeSelector }}
9493
nodeSelector:

deploy/chart/templates/0000_50_olm_08-catalog-operator.deployment.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ spec:
8484
- name: serving-cert
8585
secret:
8686
secretName: catalog-operator-serving-cert
87-
optional: true
8887
{{ end }}
8988
{{- if .Values.catalog.nodeSelector }}
9089
nodeSelector:

manifests/0000_50_olm_07-olm-operator.deployment.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ spec:
6666
- name: serving-cert
6767
secret:
6868
secretName: olm-operator-serving-cert
69-
optional: true
7069

7170
nodeSelector:
7271
beta.kubernetes.io/os: linux

manifests/0000_50_olm_08-catalog-operator.deployment.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ spec:
6363
- name: serving-cert
6464
secret:
6565
secretName: catalog-operator-serving-cert
66-
optional: true
6766

6867
nodeSelector:
6968
beta.kubernetes.io/os: linux

test/e2e/metrics_e2e_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
package e2e
44

55
import (
6-
"fmt"
76
"testing"
87

98
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
109
log "github.com/sirupsen/logrus"
1110
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/util/net"
1212
)
1313

1414
// TestMetrics tests the metrics endpoint of the OLM pod.
@@ -26,21 +26,49 @@ func TestMetricsEndpoint(t *testing.T) {
2626
}
2727

2828
podName := podList.Items[0].GetName()
29+
log.Infof("Looking at pod %v in namespace %v", podName, operatorNamespace)
2930

30-
rawOutput, err := getMetricsFromPod(t, c, podName, operatorNamespace, 8080)
31+
rawOutput, err := getMetricsFromPod(t, c, podName, operatorNamespace, "8081")
3132
if err != nil {
3233
t.Fatalf("Metrics test failed: %v\n", err)
3334
}
3435

3536
log.Debugf("Metrics:\n%v", rawOutput)
3637
}
3738

38-
func getMetricsFromPod(t *testing.T, client operatorclient.ClientInterface, podName string, namespace string, port int) (string, error) {
39+
func getMetricsFromPod(t *testing.T, client operatorclient.ClientInterface, podName string, namespace string, port string) (string, error) {
40+
olmPod, err := client.KubernetesInterface().CoreV1().Pods(namespace).Get(podName, metav1.GetOptions{})
41+
if err != nil {
42+
return "", err
43+
}
44+
if len(olmPod.Spec.Containers) != 1 {
45+
t.Fatalf("Expected only 1 container in olm-operator pod, got %v", len(olmPod.Spec.Containers))
46+
}
47+
48+
var foundCert bool
49+
var foundKey bool
50+
// assuming -tls-cert and -tls-key aren't used anywhere else as a parameter value
51+
for _, param := range olmPod.Spec.Containers[0].Args {
52+
if param == "-tls-cert" {
53+
foundCert = true
54+
} else if param == "-tls-key" {
55+
foundKey = true
56+
}
57+
}
58+
59+
var scheme string
60+
if foundCert && foundKey {
61+
scheme = "https"
62+
} else {
63+
scheme = "http"
64+
}
65+
log.Infof("Retrieving metrics using scheme %v\n", scheme)
66+
3967
rawOutput, err := client.KubernetesInterface().CoreV1().RESTClient().Get().
4068
Namespace(namespace).
4169
Resource("pods").
4270
SubResource("proxy").
43-
Name(fmt.Sprintf("%v:%v", podName, port)).
71+
Name(net.JoinSchemeNamePort(scheme, podName, port)).
4472
Suffix("metrics").
4573
Do().Raw()
4674
if err != nil {

0 commit comments

Comments
 (0)