Skip to content

Commit 2080e21

Browse files
committed
Reorganize TLS changes
1 parent fcf4660 commit 2080e21

File tree

6 files changed

+46
-32
lines changed

6 files changed

+46
-32
lines changed

cmd/manager/main.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"crypto/tls"
2120
"crypto/x509"
2221
"flag"
2322
"fmt"
24-
"log"
25-
"net/http"
2623
"net/url"
2724
"os"
28-
"time"
2925

3026
"github.com/spf13/pflag"
3127
"go.uber.org/zap/zapcore"
@@ -48,6 +44,7 @@ import (
4844
catalogclient "github.com/operator-framework/operator-controller/internal/catalogmetadata/client"
4945
"github.com/operator-framework/operator-controller/internal/controllers"
5046
"github.com/operator-framework/operator-controller/internal/handler"
47+
"github.com/operator-framework/operator-controller/internal/httputil"
5148
"github.com/operator-framework/operator-controller/internal/labels"
5249
"github.com/operator-framework/operator-controller/internal/version"
5350
"github.com/operator-framework/operator-controller/pkg/features"
@@ -82,11 +79,11 @@ func main() {
8279
systemNamespace string
8380
unpackImage string
8481
provisionerStorageDirectory string
85-
tlsCert string
82+
caCert string
8683
)
8784
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
8885
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
89-
flag.StringVar(&tlsCert, "tls-cert", "", "The TLS certificate to use for verifying HTTPS connections to the Catalogd web server.")
86+
flag.StringVar(&caCert, "ca-cert", "", "The TLS certificate to use for verifying HTTPS connections to the Catalogd web server.")
9087
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
9188
"Enable leader election for controller manager. "+
9289
"Enabling this will ensure there is only one active controller manager.")
@@ -156,23 +153,9 @@ func main() {
156153
os.Exit(1)
157154
}
158155

159-
httpClient := &http.Client{Timeout: 10 * time.Second}
160-
161-
if tlsCert != "" {
162-
cert, err := os.ReadFile(tlsCert)
163-
if err != nil {
164-
log.Fatalf("Failed to read certificate file: %v", err)
165-
}
166-
caCertPool := x509.NewCertPool()
167-
caCertPool.AppendCertsFromPEM(cert)
168-
tlsConfig := &tls.Config{
169-
RootCAs: caCertPool,
170-
MinVersion: tls.VersionTLS12,
171-
}
172-
tlsTransport := &http.Transport{
173-
TLSClientConfig: tlsConfig,
174-
}
175-
httpClient.Transport = tlsTransport
156+
httpClient, err := httputil.BuildHTTPClient(caCert)
157+
if err != nil {
158+
setupLog.Error(err, "unable to create catalogd http client")
176159
}
177160

178161
cl := mgr.GetClient()

config/base/default/kustomization.yaml renamed to config/base/kustomization.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namePrefix: operator-controller-
1515
# someName: someValue
1616

1717
resources:
18-
- ../crd
19-
- ../rbac
20-
- ../manager
18+
- crd
19+
- rbac
20+
- manager
2121
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
2222
# crd/kustomization.yaml
2323
#- ../webhook

config/base/manager/manager.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,6 @@ spec:
114114
terminationGracePeriodSeconds: 10
115115
volumes:
116116
- name: cache
117-
emptyDir: {}
117+
emptyDir: {}
118118
- name: bundle-cache
119119
emptyDir: {}

config/overlays/tls/kustomization.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ namespace: operator-controller-system
66
# "wordpress" becomes "alices-wordpress".
77
# Note that it should also match with the prefix (text before '-') of the namespace
88
# field above.
9-
namePrefix: operator-controller-
109

1110
# the following config is for teaching kustomize how to do var substitution
1211
apiVersion: kustomize.config.k8s.io/v1beta1
1312
kind: Kustomization
1413
resources:
15-
- ../../base/crd
16-
- ../../base/rbac
17-
- ../../base/manager
14+
- ../../base
1815

1916
patches:
2017
- target:

config/overlays/tls/patches/manager_deployment_cert.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
value: {"name":"ca-certificate", "readOnly": true, "mountPath":"/var/certs"}
77
- op: add
88
path: /spec/template/spec/containers/0/args/-
9-
value: "--tls-cert=/var/certs/tls.crt"
9+
value: "--ca-cert=/var/certs/tls.crt"

internal/httputil/httputil.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package httputil
2+
3+
import (
4+
"crypto/tls"
5+
"crypto/x509"
6+
"net/http"
7+
"os"
8+
"time"
9+
)
10+
11+
func BuildHTTPClient(caCert string) (*http.Client, error) {
12+
httpClient := &http.Client{Timeout: 10 * time.Second}
13+
14+
if caCert != "" {
15+
// tlsFileWatcher, err := certwatcher.New(caCert, "")
16+
17+
cert, err := os.ReadFile(caCert)
18+
if err != nil {
19+
return nil, err
20+
}
21+
caCertPool := x509.NewCertPool()
22+
caCertPool.AppendCertsFromPEM(cert)
23+
tlsConfig := &tls.Config{
24+
RootCAs: caCertPool,
25+
MinVersion: tls.VersionTLS12,
26+
}
27+
tlsTransport := &http.Transport{
28+
TLSClientConfig: tlsConfig,
29+
}
30+
httpClient.Transport = tlsTransport
31+
}
32+
33+
return httpClient, nil
34+
}

0 commit comments

Comments
 (0)