root.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Copyright © 2022 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "os"
  16. "time"
  17. "github.com/spf13/cobra"
  18. "go.uber.org/zap/zapcore"
  19. v1 "k8s.io/api/core/v1"
  20. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. clientgoscheme "k8s.io/client-go/kubernetes/scheme"
  23. // To allow using gcp auth.
  24. _ "k8s.io/client-go/plugin/pkg/client/auth"
  25. ctrl "sigs.k8s.io/controller-runtime"
  26. "sigs.k8s.io/controller-runtime/pkg/client"
  27. "sigs.k8s.io/controller-runtime/pkg/controller"
  28. "sigs.k8s.io/controller-runtime/pkg/log/zap"
  29. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  30. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  31. "github.com/external-secrets/external-secrets/pkg/controllers/clusterexternalsecret"
  32. "github.com/external-secrets/external-secrets/pkg/controllers/externalsecret"
  33. "github.com/external-secrets/external-secrets/pkg/controllers/secretstore"
  34. awsauth "github.com/external-secrets/external-secrets/pkg/provider/aws/auth"
  35. )
  36. var (
  37. scheme = runtime.NewScheme()
  38. setupLog = ctrl.Log.WithName("setup")
  39. dnsName string
  40. certDir string
  41. metricsAddr string
  42. healthzAddr string
  43. controllerClass string
  44. enableLeaderElection bool
  45. enableSecretsCache bool
  46. enableConfigMapsCache bool
  47. concurrent int
  48. port int
  49. clientQPS float32
  50. clientBurst int
  51. loglevel string
  52. namespace string
  53. enableClusterStoreReconciler bool
  54. enableClusterExternalSecretReconciler bool
  55. enableFloodGate bool
  56. storeRequeueInterval time.Duration
  57. serviceName, serviceNamespace string
  58. secretName, secretNamespace string
  59. crdRequeueInterval time.Duration
  60. certCheckInterval time.Duration
  61. certLookaheadInterval time.Duration
  62. enableAWSSession bool
  63. )
  64. const (
  65. errCreateController = "unable to create controller"
  66. )
  67. func init() {
  68. _ = clientgoscheme.AddToScheme(scheme)
  69. _ = esv1beta1.AddToScheme(scheme)
  70. _ = esv1alpha1.AddToScheme(scheme)
  71. _ = apiextensionsv1.AddToScheme(scheme)
  72. }
  73. var rootCmd = &cobra.Command{
  74. Use: "external-secrets",
  75. Short: "operator that reconciles ExternalSecrets and SecretStores",
  76. Long: `For more information visit https://external-secrets.io`,
  77. Run: func(cmd *cobra.Command, args []string) {
  78. var lvl zapcore.Level
  79. // the client creates a ListWatch for all resource kinds that
  80. // are requested with .Get().
  81. // We want to avoid to cache all secrets or configmaps in memory.
  82. // The ES controller uses v1.PartialObjectMetadata for the secrets
  83. // that he owns.
  84. // see #721
  85. cacheList := make([]client.Object, 0)
  86. if !enableSecretsCache {
  87. cacheList = append(cacheList, &v1.Secret{})
  88. }
  89. if !enableConfigMapsCache {
  90. cacheList = append(cacheList, &v1.ConfigMap{})
  91. }
  92. err := lvl.UnmarshalText([]byte(loglevel))
  93. if err != nil {
  94. setupLog.Error(err, "error unmarshalling loglevel")
  95. os.Exit(1)
  96. }
  97. logger := zap.New(zap.Level(lvl))
  98. ctrl.SetLogger(logger)
  99. config := ctrl.GetConfigOrDie()
  100. config.QPS = clientQPS
  101. config.Burst = clientBurst
  102. mgr, err := ctrl.NewManager(config, ctrl.Options{
  103. Scheme: scheme,
  104. MetricsBindAddress: metricsAddr,
  105. Port: 9443,
  106. LeaderElection: enableLeaderElection,
  107. LeaderElectionID: "external-secrets-controller",
  108. ClientDisableCacheFor: cacheList,
  109. Namespace: namespace,
  110. })
  111. if err != nil {
  112. setupLog.Error(err, "unable to start manager")
  113. os.Exit(1)
  114. }
  115. if err = (&secretstore.StoreReconciler{
  116. Client: mgr.GetClient(),
  117. Log: ctrl.Log.WithName("controllers").WithName("SecretStore"),
  118. Scheme: mgr.GetScheme(),
  119. ControllerClass: controllerClass,
  120. RequeueInterval: storeRequeueInterval,
  121. }).SetupWithManager(mgr); err != nil {
  122. setupLog.Error(err, errCreateController, "controller", "SecretStore")
  123. os.Exit(1)
  124. }
  125. if enableClusterStoreReconciler {
  126. if err = (&secretstore.ClusterStoreReconciler{
  127. Client: mgr.GetClient(),
  128. Log: ctrl.Log.WithName("controllers").WithName("ClusterSecretStore"),
  129. Scheme: mgr.GetScheme(),
  130. ControllerClass: controllerClass,
  131. RequeueInterval: storeRequeueInterval,
  132. }).SetupWithManager(mgr); err != nil {
  133. setupLog.Error(err, errCreateController, "controller", "ClusterSecretStore")
  134. os.Exit(1)
  135. }
  136. }
  137. if err = (&externalsecret.Reconciler{
  138. Client: mgr.GetClient(),
  139. Log: ctrl.Log.WithName("controllers").WithName("ExternalSecret"),
  140. Scheme: mgr.GetScheme(),
  141. ControllerClass: controllerClass,
  142. RequeueInterval: time.Hour,
  143. ClusterSecretStoreEnabled: enableClusterStoreReconciler,
  144. EnableFloodGate: enableFloodGate,
  145. }).SetupWithManager(mgr, controller.Options{
  146. MaxConcurrentReconciles: concurrent,
  147. }); err != nil {
  148. setupLog.Error(err, errCreateController, "controller", "ExternalSecret")
  149. os.Exit(1)
  150. }
  151. if enableClusterExternalSecretReconciler {
  152. if err = (&clusterexternalsecret.Reconciler{
  153. Client: mgr.GetClient(),
  154. Log: ctrl.Log.WithName("controllers").WithName("ClusterExternalSecret"),
  155. Scheme: mgr.GetScheme(),
  156. RequeueInterval: time.Hour,
  157. }).SetupWithManager(mgr, controller.Options{
  158. MaxConcurrentReconciles: concurrent,
  159. }); err != nil {
  160. setupLog.Error(err, errCreateController, "controller", "ClusterExternalSecret")
  161. os.Exit(1)
  162. }
  163. }
  164. if enableAWSSession {
  165. awsauth.EnableCache = true
  166. }
  167. setupLog.Info("starting manager")
  168. if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
  169. setupLog.Error(err, "problem running manager")
  170. os.Exit(1)
  171. }
  172. },
  173. }
  174. func Execute() {
  175. cobra.CheckErr(rootCmd.Execute())
  176. }
  177. func init() {
  178. rootCmd.Flags().StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
  179. rootCmd.Flags().StringVar(&controllerClass, "controller-class", "default", "the controller is instantiated with a specific controller name and filters ES based on this property")
  180. rootCmd.Flags().BoolVar(&enableLeaderElection, "enable-leader-election", false,
  181. "Enable leader election for controller manager. "+
  182. "Enabling this will ensure there is only one active controller manager.")
  183. rootCmd.Flags().IntVar(&concurrent, "concurrent", 1, "The number of concurrent ExternalSecret reconciles.")
  184. rootCmd.Flags().Float32Var(&clientQPS, "client-qps", 0, "QPS configuration to be passed to rest.Client")
  185. rootCmd.Flags().IntVar(&clientBurst, "client-burst", 0, "Maximum Burst allowed to be passed to rest.Client")
  186. rootCmd.Flags().StringVar(&loglevel, "loglevel", "info", "loglevel to use, one of: debug, info, warn, error, dpanic, panic, fatal")
  187. rootCmd.Flags().StringVar(&namespace, "namespace", "", "watch external secrets scoped in the provided namespace only. ClusterSecretStore can be used but only work if it doesn't reference resources from other namespaces")
  188. rootCmd.Flags().BoolVar(&enableClusterStoreReconciler, "enable-cluster-store-reconciler", true, "Enable cluster store reconciler.")
  189. rootCmd.Flags().BoolVar(&enableClusterExternalSecretReconciler, "enable-cluster-external-secret-reconciler", true, "Enable cluster external secret reconciler.")
  190. rootCmd.Flags().BoolVar(&enableSecretsCache, "enable-secrets-caching", false, "Enable secrets caching for external-secrets pod.")
  191. rootCmd.Flags().BoolVar(&enableConfigMapsCache, "enable-configmaps-caching", false, "Enable secrets caching for external-secrets pod.")
  192. rootCmd.Flags().DurationVar(&storeRequeueInterval, "store-requeue-interval", time.Minute*5, "Default Time duration between reconciling (Cluster)SecretStores")
  193. rootCmd.Flags().BoolVar(&enableFloodGate, "enable-flood-gate", true, "Enable flood gate. External secret will be reconciled only if the ClusterStore or Store have an healthy or unknown state.")
  194. rootCmd.Flags().BoolVar(&enableAWSSession, "experimental-enable-aws-session-cache", false, "Enable experimental AWS session cache. External secret will reuse the AWS session without creating a new one on each request.")
  195. }