@@ -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+ }
0 commit comments