Skip to content

Commit fceec10

Browse files
authored
replace cert manager with internal cert-controller (#66)
1 parent 462f97e commit fceec10

File tree

18 files changed

+441
-266
lines changed

18 files changed

+441
-266
lines changed

.golangci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ issues:
2525
linters:
2626
- dupl
2727
- lll
28+
- path: "pkg/*"
29+
linters:
30+
- lll
2831
linters:
2932
disable-all: true
3033
enable:

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ build: manifests generate fmt vet ## Build manager binary.
122122

123123
.PHONY: run
124124
run: manifests generate fmt vet ## Run a controller from your host (webhooks are disabled).
125-
ENABLE_WEBHOOKS=false go run ./cmd/unified/main.go --metrics-bind-address=localhost:0 --health-probe-bind-address=localhost:0
125+
NAMESPACE=dev ENABLE_WEBHOOKS=false go run ./cmd/unified/main.go --metrics-bind-address=localhost:0 --health-probe-bind-address=localhost:0
126126

127127
.PHONY: run-aw
128128
run-aw: manifests generate fmt vet ## Run a controller from your host (webhooks are disabled).
129-
ENABLE_WEBHOOKS=false go run ./cmd/standalone/main.go --metrics-bind-address=localhost:0 --health-probe-bind-address=localhost:0
129+
NAMESPACE=dev ENABLE_WEBHOOKS=false go run ./cmd/standalone/main.go --metrics-bind-address=localhost:0 --health-probe-bind-address=localhost:0
130130

131131

132132
# If you wish to build the manager image targeting other platforms you can use the --platform flag.

cmd/standalone/main.go

+36-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/tls"
2121
"flag"
2222
"os"
23+
"strings"
2324

2425
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2526
// to ensure that exec-entrypoint and run can make use of them.
@@ -29,7 +30,6 @@ import (
2930
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3031
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3132
ctrl "sigs.k8s.io/controller-runtime"
32-
"sigs.k8s.io/controller-runtime/pkg/healthz"
3333
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3434
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3535
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -60,7 +60,7 @@ func main() {
6060
var secureMetrics bool
6161
var enableHTTP2 bool
6262

63-
awConfig := config.NewConfig()
63+
awConfig := config.NewConfig(namespaceOrDie())
6464
awConfig.StandaloneMode = true
6565
awConfig.ManageJobsWithoutQueueName = false
6666

@@ -132,19 +132,27 @@ func main() {
132132
}
133133

134134
ctx := ctrl.SetupSignalHandler()
135-
err = controller.SetupWithManager(ctx, mgr, awConfig)
136-
if err != nil {
137-
setupLog.Error(err, "unable to start appwrapper controllers")
138-
os.Exit(1)
135+
certsReady := make(chan struct{})
136+
137+
if os.Getenv("ENABLE_WEBHOOKS") == "false" {
138+
close(certsReady)
139+
} else {
140+
if err := controller.SetupCertManagement(mgr, &awConfig.CertManagement, certsReady); err != nil {
141+
setupLog.Error(err, "Unable to set up cert rotation")
142+
os.Exit(1)
143+
}
139144
}
140145

141-
//+kubebuilder:scaffold:builder
142-
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
143-
setupLog.Error(err, "unable to set up health check")
146+
// Ascynchronous because controllers need to wait for certificate to be ready for webhooks to work
147+
go controller.SetupControllers(ctx, mgr, awConfig, certsReady, setupLog)
148+
149+
if err := controller.SetupIndexers(ctx, mgr, awConfig); err != nil {
150+
setupLog.Error(err, "unable to setup indexers")
144151
os.Exit(1)
145152
}
146-
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
147-
setupLog.Error(err, "unable to set up ready check")
153+
154+
if err := controller.SetupProbeEndpoints(mgr, certsReady); err != nil {
155+
setupLog.Error(err, "unable to setup probe endpoints")
148156
os.Exit(1)
149157
}
150158

@@ -154,3 +162,20 @@ func main() {
154162
os.Exit(1)
155163
}
156164
}
165+
166+
func namespaceOrDie() string {
167+
// This way assumes you've set the NAMESPACE environment variable either manually, when running
168+
// the operator standalone, or using the downward API, when running the operator in-cluster.
169+
if ns := os.Getenv("NAMESPACE"); ns != "" {
170+
return ns
171+
}
172+
173+
// Fall back to the namespace associated with the service account token, if available
174+
if data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
175+
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
176+
return ns
177+
}
178+
}
179+
180+
panic("unable to determine current namespace")
181+
}

cmd/unified/main.go

+36-11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/tls"
2121
"flag"
2222
"os"
23+
"strings"
2324

2425
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2526
// to ensure that exec-entrypoint and run can make use of them.
@@ -29,7 +30,6 @@ import (
2930
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3031
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3132
ctrl "sigs.k8s.io/controller-runtime"
32-
"sigs.k8s.io/controller-runtime/pkg/healthz"
3333
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3434
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3535
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -62,7 +62,7 @@ func main() {
6262
var secureMetrics bool
6363
var enableHTTP2 bool
6464

65-
awConfig := config.NewConfig()
65+
awConfig := config.NewConfig(namespaceOrDie())
6666

6767
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
6868
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
@@ -133,19 +133,27 @@ func main() {
133133
}
134134

135135
ctx := ctrl.SetupSignalHandler()
136-
err = controller.SetupWithManager(ctx, mgr, awConfig)
137-
if err != nil {
138-
setupLog.Error(err, "unable to start appwrapper controllers")
139-
os.Exit(1)
136+
certsReady := make(chan struct{})
137+
138+
if os.Getenv("ENABLE_WEBHOOKS") == "false" {
139+
close(certsReady)
140+
} else {
141+
if err := controller.SetupCertManagement(mgr, &awConfig.CertManagement, certsReady); err != nil {
142+
setupLog.Error(err, "Unable to set up cert rotation")
143+
os.Exit(1)
144+
}
140145
}
141146

142-
//+kubebuilder:scaffold:builder
143-
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
144-
setupLog.Error(err, "unable to set up health check")
147+
// Ascynchronous because controllers need to wait for certificate to be ready for webhooks to work
148+
go controller.SetupControllers(ctx, mgr, awConfig, certsReady, setupLog)
149+
150+
if err := controller.SetupIndexers(ctx, mgr, awConfig); err != nil {
151+
setupLog.Error(err, "unable to setup indexers")
145152
os.Exit(1)
146153
}
147-
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
148-
setupLog.Error(err, "unable to set up ready check")
154+
155+
if err := controller.SetupProbeEndpoints(mgr, certsReady); err != nil {
156+
setupLog.Error(err, "unable to setup probe endpoints")
149157
os.Exit(1)
150158
}
151159

@@ -155,3 +163,20 @@ func main() {
155163
os.Exit(1)
156164
}
157165
}
166+
167+
func namespaceOrDie() string {
168+
// This way assumes you've set the NAMESPACE environment variable either manually, when running
169+
// the operator standalone, or using the downward API, when running the operator in-cluster.
170+
if ns := os.Getenv("NAMESPACE"); ns != "" {
171+
return ns
172+
}
173+
174+
// Fall back to the namespace associated with the service account token, if available
175+
if data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
176+
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
177+
return ns
178+
}
179+
}
180+
181+
panic("unable to determine current namespace")
182+
}

config/crd/kustomization.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ patches:
1313

1414
# [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix.
1515
# patches here are for enabling the CA injection for each CRD
16-
- path: patches/cainjection_in_appwrappers.yaml
16+
#- path: patches/cainjection_in_appwrappers.yaml
1717
#+kubebuilder:scaffold:crdkustomizecainjectionpatch
1818

1919
# [WEBHOOK] To enable webhook, uncomment the following section

0 commit comments

Comments
 (0)