Skip to content

Commit 3d2317f

Browse files
author
Jeff Peeler
committed
feat(olm,catalog): add TLS support for metrics
Two new command line arguments are added to pass the paths to the certificate and private key, "tls-cert" and "tls-key".
1 parent fb9a5b8 commit 3d2317f

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

cmd/catalog/main.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ var (
5353
"debug", false, "use debug log level")
5454

5555
version = flag.Bool("version", false, "displays olm version")
56+
57+
tlsKeyPath = flag.String(
58+
"tls-key", "", "Path to use for private key (requires tls-cert)")
59+
60+
tlsCertPath = flag.String(
61+
"tls-cert", "", "Path to use for certificate key (requires tls-key)")
5662
)
5763

5864
func init() {
@@ -83,17 +89,33 @@ func main() {
8389
}
8490
}
8591

92+
logger := log.New()
93+
if *debug {
94+
logger.SetLevel(log.DebugLevel)
95+
}
96+
logger.Infof("log level %s", logger.Level)
97+
98+
var useTLS bool
99+
if *tlsCertPath != "" && *tlsKeyPath == "" || *tlsCertPath == "" && *tlsKeyPath != "" {
100+
logger.Warn("both --tls-key and --tls-crt must be provided for TLS to be enabled, falling back to non-https")
101+
} else if *tlsCertPath == "" && *tlsKeyPath == "" {
102+
logger.Info("TLS keys not set, using non-https")
103+
} else {
104+
useTLS = true
105+
}
106+
86107
// Serve a health check.
87108
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
88109
w.WriteHeader(http.StatusOK)
89110
})
90111
go http.ListenAndServe(":8080", nil)
91112

92-
logger := log.New()
93-
if *debug {
94-
logger.SetLevel(log.DebugLevel)
113+
http.Handle("/metrics", promhttp.Handler())
114+
if useTLS {
115+
go http.ListenAndServeTLS(":8081", *tlsCertPath, *tlsKeyPath, nil)
116+
} else {
117+
go http.ListenAndServe(":8081", nil)
95118
}
96-
logger.Infof("log level %s", logger.Level)
97119

98120
// create a config client for operator status
99121
config, err := clientcmd.BuildConfigFromFlags("", *kubeConfigPath)
@@ -112,9 +134,6 @@ func main() {
112134
log.Panicf("error configuring operator: %s", err.Error())
113135
}
114136

115-
http.Handle("/metrics", promhttp.Handler())
116-
go http.ListenAndServe(":8081", nil)
117-
118137
ready, done, sync := catalogOperator.Run(stopCh)
119138
<-ready
120139

cmd/olm/main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ var (
4949
"debug", false, "use debug log level")
5050

5151
version = flag.Bool("version", false, "displays olm version")
52+
53+
tlsKeyPath = flag.String(
54+
"tls-key", "", "Path to use for private key (requires tls-cert)")
55+
56+
tlsCertPath = flag.String(
57+
"tls-cert", "", "Path to use for certificate key (requires tls-key)")
5258
)
5359

5460
func init() {
@@ -113,14 +119,27 @@ func main() {
113119
log.Fatalf("error configuring operator: %s", err.Error())
114120
}
115121

122+
var useTLS bool
123+
if *tlsCertPath != "" && *tlsKeyPath == "" || *tlsCertPath == "" && *tlsKeyPath != "" {
124+
logger.Warn("both --tls-key and --tls-crt must be provided for TLS to be enabled, falling back to non-https")
125+
} else if *tlsCertPath == "" && *tlsKeyPath == "" {
126+
logger.Info("TLS keys not set, using non-https")
127+
} else {
128+
useTLS = true
129+
}
130+
116131
// Serve a health check.
117132
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
118133
w.WriteHeader(http.StatusOK)
119134
})
120135
go http.ListenAndServe(":8080", nil)
121136

122137
http.Handle("/metrics", promhttp.Handler())
123-
go http.ListenAndServe(":8081", nil)
138+
if useTLS {
139+
go http.ListenAndServeTLS(":8081", *tlsCertPath, *tlsKeyPath, nil)
140+
} else {
141+
go http.ListenAndServe(":8081", nil)
142+
}
124143

125144
ready, done, sync := operator.Run(stopCh)
126145
<-ready

0 commit comments

Comments
 (0)