2022-02-15 11:52:52 +00:00
/ *
Copyright © 2022 ESO Maintainer team
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
2022-09-06 17:46:36 +00:00
http : //www.apache.org/licenses/LICENSE-2.0
2022-02-15 11:52:52 +00:00
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package cmd
import (
"os"
"time"
"github.com/spf13/cobra"
"go.uber.org/zap/zapcore"
2022-02-17 12:35:20 +00:00
v1 "k8s.io/api/core/v1"
2022-02-15 11:52:52 +00:00
ctrl "sigs.k8s.io/controller-runtime"
2022-02-17 12:35:20 +00:00
"sigs.k8s.io/controller-runtime/pkg/client"
2022-02-15 11:52:52 +00:00
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"github.com/external-secrets/external-secrets/pkg/controllers/crds"
2022-02-17 22:14:39 +00:00
"github.com/external-secrets/external-secrets/pkg/controllers/webhookconfig"
2022-02-15 11:52:52 +00:00
)
var certcontrollerCmd = & cobra . Command {
Use : "certcontroller" ,
2022-02-17 22:14:39 +00:00
Short : "Controller to manage certificates for external secrets CRDs and ValidatingWebhookConfigs" ,
Long : ` Controller to manage certificates for external secrets CRDs and ValidatingWebhookConfigs .
2022-02-15 11:52:52 +00:00
For more information visit https : //external-secrets.io`,
Run : func ( cmd * cobra . Command , args [ ] string ) {
var lvl zapcore . Level
2022-12-13 20:01:07 +00:00
var enc zapcore . TimeEncoder
lvlErr := lvl . UnmarshalText ( [ ] byte ( loglevel ) )
if lvlErr != nil {
setupLog . Error ( lvlErr , "error unmarshalling loglevel" )
os . Exit ( 1 )
}
encErr := enc . UnmarshalText ( [ ] byte ( zapTimeEncoding ) )
if encErr != nil {
setupLog . Error ( encErr , "error unmarshalling timeEncoding" )
2022-02-15 11:52:52 +00:00
os . Exit ( 1 )
}
2022-12-13 20:01:07 +00:00
opts := zap . Options {
Level : lvl ,
TimeEncoder : enc ,
}
logger := zap . New ( zap . UseFlagOptions ( & opts ) )
2022-02-15 11:52:52 +00:00
ctrl . SetLogger ( logger )
mgr , err := ctrl . NewManager ( ctrl . GetConfigOrDie ( ) , ctrl . Options {
2022-02-17 22:14:39 +00:00
Scheme : scheme ,
MetricsBindAddress : metricsAddr ,
HealthProbeBindAddress : healthzAddr ,
Port : 9443 ,
LeaderElection : enableLeaderElection ,
LeaderElectionID : "crd-certs-controller" ,
2022-02-17 12:35:20 +00:00
ClientDisableCacheFor : [ ] client . Object {
// the client creates a ListWatch for all resource kinds that
// are requested with .Get().
// We want to avoid to cache all secrets or configmaps in memory.
// The ES controller uses v1.PartialObjectMetadata for the secrets
// that he owns.
// see #721
& v1 . Secret { } ,
2022-02-17 22:14:39 +00:00
} ,
} )
2022-02-15 11:52:52 +00:00
if err != nil {
setupLog . Error ( err , "unable to start manager" )
os . Exit ( 1 )
}
2022-02-17 22:14:39 +00:00
crdctrl := crds . New ( mgr . GetClient ( ) , mgr . GetScheme ( ) ,
ctrl . Log . WithName ( "controllers" ) . WithName ( "webhook-certs-updater" ) ,
2022-12-13 19:56:30 +00:00
crdRequeueInterval , serviceName , serviceNamespace , secretName , secretNamespace , crdNames )
2022-02-17 22:14:39 +00:00
if err := crdctrl . SetupWithManager ( mgr , controller . Options {
2022-02-15 11:52:52 +00:00
MaxConcurrentReconciles : concurrent ,
} ) ; err != nil {
setupLog . Error ( err , errCreateController , "controller" , "CustomResourceDefinition" )
os . Exit ( 1 )
}
2022-02-17 22:14:39 +00:00
whc := webhookconfig . New ( mgr . GetClient ( ) , mgr . GetScheme ( ) ,
ctrl . Log . WithName ( "controllers" ) . WithName ( "webhook-certs-updater" ) ,
serviceName , serviceNamespace ,
secretName , secretNamespace , crdRequeueInterval )
if err := whc . SetupWithManager ( mgr , controller . Options {
MaxConcurrentReconciles : concurrent ,
} ) ; err != nil {
setupLog . Error ( err , errCreateController , "controller" , "WebhookConfig" )
os . Exit ( 1 )
}
err = mgr . AddReadyzCheck ( "crd-inject" , crdctrl . ReadyCheck )
if err != nil {
setupLog . Error ( err , "unable to add crd readyz check" )
os . Exit ( 1 )
}
err = mgr . AddReadyzCheck ( "validation-webhook-inject" , whc . ReadyCheck )
if err != nil {
setupLog . Error ( err , "unable to add webhook readyz check" )
os . Exit ( 1 )
}
2022-02-15 11:52:52 +00:00
setupLog . Info ( "starting manager" )
if err := mgr . Start ( ctrl . SetupSignalHandler ( ) ) ; err != nil {
setupLog . Error ( err , "problem running manager" )
os . Exit ( 1 )
}
} ,
}
func init ( ) {
rootCmd . AddCommand ( certcontrollerCmd )
certcontrollerCmd . Flags ( ) . StringVar ( & metricsAddr , "metrics-addr" , ":8080" , "The address the metric endpoint binds to." )
2022-02-17 22:14:39 +00:00
certcontrollerCmd . Flags ( ) . StringVar ( & healthzAddr , "healthz-addr" , ":8081" , "The address the health endpoint binds to." )
2022-02-15 11:52:52 +00:00
certcontrollerCmd . Flags ( ) . StringVar ( & serviceName , "service-name" , "external-secrets-webhook" , "Webhook service name" )
certcontrollerCmd . Flags ( ) . StringVar ( & serviceNamespace , "service-namespace" , "default" , "Webhook service namespace" )
certcontrollerCmd . Flags ( ) . StringVar ( & secretName , "secret-name" , "external-secrets-webhook" , "Secret to store certs for webhook" )
certcontrollerCmd . Flags ( ) . StringVar ( & secretNamespace , "secret-namespace" , "default" , "namespace of the secret to store certs" )
2022-12-13 19:56:30 +00:00
certcontrollerCmd . Flags ( ) . StringSliceVar ( & crdNames , "crd-names" , [ ] string { "externalsecrets.external-secrets.io" , "clustersecretstores.external-secrets.io" , "secretstores.external-secrets.io" } , "CRD names reconciled by the controller" )
2022-02-15 11:52:52 +00:00
certcontrollerCmd . Flags ( ) . BoolVar ( & enableLeaderElection , "enable-leader-election" , false ,
"Enable leader election for controller manager. " +
"Enabling this will ensure there is only one active controller manager." )
certcontrollerCmd . Flags ( ) . StringVar ( & loglevel , "loglevel" , "info" , "loglevel to use, one of: debug, info, warn, error, dpanic, panic, fatal" )
2022-12-13 20:01:07 +00:00
certcontrollerCmd . Flags ( ) . StringVar ( & zapTimeEncoding , "zap-time-encoding" , "epoch" , "Zap time encoding (one of 'epoch', 'millis', 'nano', 'iso8601', 'rfc3339' or 'rfc3339nano')" )
2022-02-15 11:52:52 +00:00
certcontrollerCmd . Flags ( ) . DurationVar ( & crdRequeueInterval , "crd-requeue-interval" , time . Minute * 5 , "Time duration between reconciling CRDs for new certs" )
}