externalsecret_controller.go 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. /*
  2. Copyright © The ESO Authors
  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. https://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 externalsecret implements the controller for managing ExternalSecret resources
  14. package externalsecret
  15. import (
  16. "context"
  17. "encoding/json"
  18. "errors"
  19. "fmt"
  20. "maps"
  21. "slices"
  22. "strings"
  23. "time"
  24. "github.com/go-logr/logr"
  25. "github.com/prometheus/client_golang/prometheus"
  26. robfigcron "github.com/robfig/cron/v3"
  27. v1 "k8s.io/api/core/v1"
  28. "k8s.io/apimachinery/pkg/api/equality"
  29. apierrors "k8s.io/apimachinery/pkg/api/errors"
  30. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  31. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  32. "k8s.io/apimachinery/pkg/fields"
  33. "k8s.io/apimachinery/pkg/labels"
  34. "k8s.io/apimachinery/pkg/runtime"
  35. "k8s.io/apimachinery/pkg/runtime/schema"
  36. "k8s.io/apimachinery/pkg/types"
  37. "k8s.io/client-go/rest"
  38. "k8s.io/client-go/tools/record"
  39. "k8s.io/utils/ptr"
  40. ctrl "sigs.k8s.io/controller-runtime"
  41. "sigs.k8s.io/controller-runtime/pkg/builder"
  42. "sigs.k8s.io/controller-runtime/pkg/client"
  43. "sigs.k8s.io/controller-runtime/pkg/controller"
  44. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  45. "sigs.k8s.io/controller-runtime/pkg/event"
  46. "sigs.k8s.io/controller-runtime/pkg/handler"
  47. "sigs.k8s.io/controller-runtime/pkg/predicate"
  48. "sigs.k8s.io/controller-runtime/pkg/reconcile"
  49. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  50. // Metrics.
  51. "github.com/external-secrets/external-secrets/pkg/controllers/externalsecret/esmetrics"
  52. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  53. ctrlutil "github.com/external-secrets/external-secrets/pkg/controllers/util"
  54. "github.com/external-secrets/external-secrets/runtime/esutils"
  55. "github.com/external-secrets/external-secrets/runtime/esutils/resolvers"
  56. // Loading registered generators.
  57. _ "github.com/external-secrets/external-secrets/pkg/register"
  58. )
  59. const (
  60. fieldOwnerTemplate = "externalsecrets.external-secrets.io/%v"
  61. fieldOwnerTemplateSha = "externalsecrets.external-secrets.io/sha3/%x"
  62. // ExternalSecretFinalizer is the finalizer for ExternalSecret resources.
  63. ExternalSecretFinalizer = "externalsecrets.external-secrets.io/externalsecret-cleanup"
  64. // condition messages for "SecretSynced" reason.
  65. msgSynced = "secret synced"
  66. msgSyncedRetain = "secret retained due to DeletionPolicy=Retain"
  67. // condition messages for "SecretDeleted" reason.
  68. msgDeleted = "secret deleted due to DeletionPolicy=Delete"
  69. // condition messages for "SecretMissing" reason.
  70. msgMissing = "secret will not be created due to CreationPolicy=Merge"
  71. // condition messages for "SecretSyncedError" reason.
  72. msgErrorGetSecretData = "could not get secret data from provider"
  73. msgErrorDeleteSecret = "could not delete secret"
  74. msgErrorDeleteOrphaned = "could not delete orphaned secrets"
  75. msgErrorUpdateSecret = "could not update secret"
  76. msgErrorUpdateImmutable = "could not update secret, target is immutable"
  77. msgErrorBecomeOwner = "failed to take ownership of target secret"
  78. msgErrorIsOwned = "target is owned by another ExternalSecret"
  79. // log messages.
  80. logErrorGetES = "unable to get ExternalSecret"
  81. logErrorUpdateESStatus = "unable to update ExternalSecret status"
  82. logErrorGetSecret = "unable to get Secret"
  83. logErrorPatchSecret = "unable to patch Secret"
  84. logErrorSecretCacheNotSynced = "controller caches for Secret are not in sync"
  85. logErrorUnmanagedStore = "unable to determine if store is managed"
  86. // log messages for mutating / destructive secret operations, emitted at
  87. // V(1) so they are opt-in. They only ever carry key names, never values.
  88. logSecretDeleted = "deleted secret"
  89. logManagedSecretDeleted = "deleted managed secret"
  90. logSecretDeletedOrphaned = "deleted orphaned secret"
  91. logSecretDataChanged = "secret data keys changed"
  92. // error formats.
  93. errConvert = "error applying conversion strategy %s to keys: %w"
  94. errRewrite = "error applying rewrite to keys: %w"
  95. errDecode = "error applying decoding strategy %s to data: %w"
  96. errGenerate = "error using generator: %w"
  97. errInvalidKeys = "invalid secret keys (TIP: use rewrite or conversionStrategy to change keys): %w"
  98. errFetchTplFrom = "error fetching templateFrom data: %w"
  99. errApplyTemplate = "could not apply template: %w"
  100. errExecTpl = "could not execute template: %w"
  101. errMutate = "unable to mutate secret %s: %w"
  102. errUpdate = "unable to update secret %s: %w"
  103. errUpdateNotFound = "unable to update secret %s: not found"
  104. errDeleteCreatePolicy = "unable to delete secret %s: creationPolicy=%s is not Owner"
  105. errSecretCachesNotSynced = "controller caches for secret %s are not in sync"
  106. // event messages.
  107. eventCreated = "secret created"
  108. eventUpdated = "secret updated"
  109. eventDeleted = "secret deleted due to DeletionPolicy=Delete"
  110. eventDeletedOrphaned = "secret deleted because it was orphaned"
  111. eventMissingProviderSecret = "secret does not exist at provider using spec.dataFrom[%d]"
  112. eventMissingProviderSecretKey = "secret does not exist at provider using spec.dataFrom[%d] (key=%s)"
  113. // cacheSyncRetryDelay is used when partial and full secret caches are temporarily out of sync.
  114. cacheSyncRetryDelay = 200 * time.Millisecond
  115. )
  116. // these errors are explicitly defined so we can detect them with `errors.Is()`.
  117. var (
  118. ErrSecretImmutable = fmt.Errorf("secret is immutable")
  119. ErrSecretIsOwned = fmt.Errorf("secret is owned by another ExternalSecret")
  120. ErrSecretSetCtrlRef = fmt.Errorf("could not set controller reference on secret")
  121. ErrSecretRemoveCtrlRef = fmt.Errorf("could not remove controller reference on secret")
  122. )
  123. const (
  124. indexESTargetSecretNameField = ".metadata.targetSecretName"
  125. indexESTargetResourceField = ".spec.target.resource"
  126. )
  127. // Reconciler reconciles a ExternalSecret object.
  128. type Reconciler struct {
  129. client.Client
  130. SecretClient client.Client
  131. APIReader client.Reader
  132. EnableSecretAPIReadOnCacheMismatch bool
  133. Log logr.Logger
  134. Scheme *runtime.Scheme
  135. RestConfig *rest.Config
  136. ControllerClass string
  137. RequeueInterval time.Duration
  138. ClusterSecretStoreEnabled bool
  139. EnableFloodGate bool
  140. EnableGeneratorState bool
  141. AllowGenericTargets bool
  142. recorder record.EventRecorder
  143. // informerManager manages dynamic informers for generic targets
  144. informerManager InformerManager
  145. }
  146. // Reconcile implements the main reconciliation loop
  147. // for watched objects (ExternalSecret, ClusterSecretStore and SecretStore),
  148. // and updates/creates a Kubernetes secret based on them.
  149. func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
  150. log := r.Log.WithValues("ExternalSecret", req.NamespacedName)
  151. resourceLabels := ctrlmetrics.RefineNonConditionMetricLabels(map[string]string{"name": req.Name, "namespace": req.Namespace})
  152. start := time.Now()
  153. syncCallsError := esmetrics.GetCounterVec(esmetrics.SyncCallsErrorKey)
  154. // use closures to dynamically update resourceLabels
  155. defer func() {
  156. esmetrics.GetGaugeVec(esmetrics.ExternalSecretReconcileDurationKey).With(resourceLabels).Set(float64(time.Since(start)))
  157. esmetrics.GetCounterVec(esmetrics.SyncCallsKey).With(resourceLabels).Inc()
  158. }()
  159. externalSecret := &esv1.ExternalSecret{}
  160. err = r.Get(ctx, req.NamespacedName, externalSecret)
  161. if err != nil {
  162. if apierrors.IsNotFound(err) {
  163. // NOTE: this does not actually set the condition on the ExternalSecret, because it does not exist
  164. // this is a hack to disable metrics for deleted ExternalSecrets, see:
  165. // https://github.com/external-secrets/external-secrets/pull/612
  166. conditionSynced := NewExternalSecretCondition(esv1.ExternalSecretDeleted, v1.ConditionFalse, esv1.ConditionReasonSecretDeleted, "Secret was deleted")
  167. SetExternalSecretCondition(&esv1.ExternalSecret{
  168. ObjectMeta: metav1.ObjectMeta{
  169. Name: req.Name,
  170. Namespace: req.Namespace,
  171. },
  172. }, *conditionSynced)
  173. return ctrl.Result{}, nil
  174. }
  175. log.Error(err, logErrorGetES)
  176. syncCallsError.With(resourceLabels).Inc()
  177. return ctrl.Result{}, err
  178. }
  179. // Handle deletion with finalizer
  180. if !externalSecret.GetDeletionTimestamp().IsZero() {
  181. // Always attempt cleanup to handle edge case where finalizer might be removed externally
  182. if err := r.cleanupManagedSecrets(ctx, log, externalSecret); err != nil {
  183. log.Error(err, "failed to cleanup managed secrets")
  184. return ctrl.Result{}, err
  185. }
  186. // Release informer for generic targets
  187. if isGenericTarget(externalSecret) && r.informerManager != nil {
  188. gvk := getTargetGVK(externalSecret)
  189. esName := types.NamespacedName{Name: externalSecret.Name, Namespace: externalSecret.Namespace}
  190. if err := r.informerManager.ReleaseInformer(ctx, gvk, esName); err != nil {
  191. log.Error(err, "failed to release informer for generic target",
  192. "group", gvk.Group,
  193. "version", gvk.Version,
  194. "kind", gvk.Kind)
  195. }
  196. }
  197. // Remove finalizer if it exists
  198. // Use Patch instead of Update to avoid claiming ownership of spec fields like refreshInterval
  199. patch := client.MergeFrom(externalSecret.DeepCopy())
  200. if updated := controllerutil.RemoveFinalizer(externalSecret, ExternalSecretFinalizer); updated {
  201. if err := r.Patch(ctx, externalSecret, patch); err != nil {
  202. return ctrl.Result{}, err
  203. }
  204. }
  205. return ctrl.Result{}, nil
  206. }
  207. // Add finalizer if it doesn't exist
  208. // Use Patch instead of Update to avoid claiming ownership of spec fields like refreshInterval
  209. patch := client.MergeFrom(externalSecret.DeepCopy())
  210. if updated := controllerutil.AddFinalizer(externalSecret, ExternalSecretFinalizer); updated {
  211. if err := r.Patch(ctx, externalSecret, patch); err != nil {
  212. return ctrl.Result{}, err
  213. }
  214. }
  215. // if extended metrics is enabled, refine the time series vector
  216. resourceLabels = ctrlmetrics.RefineLabels(resourceLabels, externalSecret.Labels)
  217. // skip this ExternalSecret if it uses a ClusterSecretStore and the feature is disabled
  218. if shouldSkipClusterSecretStore(r, externalSecret) {
  219. log.V(1).Info("skipping ExternalSecret, ClusterSecretStore feature is disabled")
  220. return ctrl.Result{}, nil
  221. }
  222. // skip this ExternalSecret if it uses any SecretStore not managed by this controller
  223. skip, err := shouldSkipUnmanagedStore(ctx, req.Namespace, r, externalSecret)
  224. if err != nil {
  225. log.Error(err, logErrorUnmanagedStore)
  226. syncCallsError.With(resourceLabels).Inc()
  227. return ctrl.Result{}, err
  228. }
  229. if skip {
  230. log.V(1).Info("skipping ExternalSecret, uses unmanaged SecretStore")
  231. return ctrl.Result{}, nil
  232. }
  233. // if this is a generic target, use a different reconciliation path
  234. if isGenericTarget(externalSecret) {
  235. // update the status of the ExternalSecret when this function returns, if needed
  236. currentStatus := *externalSecret.Status.DeepCopy()
  237. defer func() {
  238. if equality.Semantic.DeepEqual(currentStatus, externalSecret.Status) {
  239. return
  240. }
  241. updateErr := r.Status().Update(ctx, externalSecret)
  242. if updateErr != nil && !apierrors.IsConflict(updateErr) {
  243. log.Error(updateErr, logErrorUpdateESStatus)
  244. }
  245. }()
  246. // validate generic target configuration early
  247. if err := r.validateGenericTarget(log, externalSecret); err != nil {
  248. r.markAsFailed("invalid generic target", err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonSecretSyncedError)
  249. return ctrl.Result{}, nil // don't requeue as this is a configuration error that is not recoverable
  250. }
  251. return r.reconcileGenericTarget(ctx, externalSecret, log, start, resourceLabels, syncCallsError)
  252. }
  253. // the target secret name defaults to the ExternalSecret name, if not explicitly set
  254. secretName := externalSecret.Spec.Target.Name
  255. if secretName == "" {
  256. secretName = externalSecret.Name
  257. }
  258. // fetch the existing secret (from the partial cache)
  259. // - please note that the ~partial cache~ is different from the ~full cache~
  260. // so there can be race conditions between the two caches
  261. // - the WatchesMetadata(v1.Secret{}) in SetupWithManager() is using the partial cache
  262. // so we might receive a reconcile request before the full cache is updated
  263. // - furthermore, when `--enable-managed-secrets-caching` is true, the full cache
  264. // will ONLY include secrets with the "managed" label, so we cant use the full cache
  265. // to reliably determine if a secret exists or not
  266. secretPartial := &metav1.PartialObjectMetadata{}
  267. secretPartial.SetGroupVersionKind(v1.SchemeGroupVersion.WithKind("Secret"))
  268. err = r.Get(ctx, client.ObjectKey{Name: secretName, Namespace: externalSecret.Namespace}, secretPartial)
  269. if err != nil && !apierrors.IsNotFound(err) {
  270. log.Error(err, logErrorGetSecret, "secretName", secretName, "secretNamespace", externalSecret.Namespace)
  271. syncCallsError.With(resourceLabels).Inc()
  272. return ctrl.Result{}, err
  273. }
  274. // if the secret exists but does not have the "managed" label, add the label
  275. // using a PATCH so it is visible in the cache, then requeue immediately
  276. if secretPartial.UID != "" && secretPartial.Labels[esv1.LabelManaged] != esv1.LabelManagedValue {
  277. fqdn := fqdnFor(externalSecret.Name)
  278. patch := client.MergeFrom(secretPartial.DeepCopy())
  279. if secretPartial.Labels == nil {
  280. secretPartial.Labels = make(map[string]string)
  281. }
  282. secretPartial.Labels[esv1.LabelManaged] = esv1.LabelManagedValue
  283. err = r.Patch(ctx, secretPartial, patch, client.FieldOwner(fqdn))
  284. if err != nil {
  285. log.Error(err, logErrorPatchSecret, "secretName", secretName, "secretNamespace", externalSecret.Namespace)
  286. syncCallsError.With(resourceLabels).Inc()
  287. return ctrl.Result{}, err
  288. }
  289. return ctrl.Result{Requeue: true}, nil
  290. }
  291. // fetch existing secret (from the full cache)
  292. // NOTE: we are using the `r.SecretClient` which we only use for managed secrets.
  293. // when `enableManagedSecretsCache` is true, this is a cached client that only sees our managed secrets,
  294. // otherwise it will be the normal controller-runtime client which may be cached or make direct API calls,
  295. // depending on if `enabledSecretCache` is true or false.
  296. existingSecret := &v1.Secret{}
  297. err = r.SecretClient.Get(ctx, client.ObjectKey{Name: secretName, Namespace: externalSecret.Namespace}, existingSecret)
  298. if err != nil && !apierrors.IsNotFound(err) {
  299. log.Error(err, logErrorGetSecret, "secretName", secretName, "secretNamespace", externalSecret.Namespace)
  300. syncCallsError.With(resourceLabels).Inc()
  301. return ctrl.Result{}, err
  302. }
  303. // ensure the full cache is up-to-date
  304. // NOTE: this prevents race conditions between the partial and full cache.
  305. // if enabled, we verify against the API server before retrying to avoid unnecessary error backoff
  306. // when the cache is temporarily stale.
  307. existingSecret, cacheNotSynced, getErr := r.resolveSecretCacheMismatch(ctx, client.ObjectKey{Name: secretName, Namespace: externalSecret.Namespace}, secretPartial, existingSecret)
  308. if getErr != nil && !apierrors.IsNotFound(getErr) {
  309. log.Error(getErr, logErrorGetSecret, "secretName", secretName, "secretNamespace", externalSecret.Namespace)
  310. syncCallsError.With(resourceLabels).Inc()
  311. return ctrl.Result{}, getErr
  312. }
  313. if cacheNotSynced {
  314. log.V(1).Info(logErrorSecretCacheNotSynced, "secretName", secretName, "secretNamespace", externalSecret.Namespace)
  315. return ctrl.Result{RequeueAfter: cacheSyncRetryDelay}, nil
  316. }
  317. // refresh will be skipped if ALL the following conditions are met:
  318. // 1. refresh interval is not 0
  319. // 2. resource generation of the ExternalSecret has not changed
  320. // 3. the last refresh time of the ExternalSecret is within the refresh interval
  321. // 4. the target secret is valid:
  322. // - it exists
  323. // - it has the correct "managed" label
  324. // - it has the correct "data-hash" annotation
  325. if !shouldRefresh(externalSecret) && isSecretValid(existingSecret, externalSecret) {
  326. log.V(1).Info("skipping refresh")
  327. return r.getRequeueResult(externalSecret), nil
  328. }
  329. // update status of the ExternalSecret when this function returns, if needed.
  330. // NOTE: we use the ability of deferred functions to update named return values `result` and `err`
  331. // NOTE: we dereference the DeepCopy of the status field because status fields are NOT pointers,
  332. // so otherwise the `equality.Semantic.DeepEqual` will always return false.
  333. currentStatus := *externalSecret.Status.DeepCopy()
  334. defer func() {
  335. // if the status has not changed, we don't need to update it
  336. if equality.Semantic.DeepEqual(currentStatus, externalSecret.Status) {
  337. return
  338. }
  339. // update the status of the ExternalSecret, storing any error in a new variable
  340. // if there was no new error, we don't need to change the `result` or `err` values
  341. updateErr := r.Status().Update(ctx, externalSecret)
  342. if updateErr == nil {
  343. return
  344. }
  345. // if we got an update conflict, we should requeue immediately
  346. if apierrors.IsConflict(updateErr) {
  347. log.V(1).Info("conflict while updating status, will requeue")
  348. // we only explicitly request a requeue if the main function did not return an `err`.
  349. // otherwise, we get an annoying log saying that results are ignored when there is an error,
  350. // as errors are always retried.
  351. if err == nil {
  352. result = ctrl.Result{Requeue: true}
  353. }
  354. return
  355. }
  356. // for other errors, log and update the `err` variable if there is no error already
  357. // so the reconciler will requeue the request
  358. log.Error(updateErr, logErrorUpdateESStatus)
  359. if err == nil {
  360. err = updateErr
  361. }
  362. }()
  363. // retrieve the provider secret data.
  364. dataMap, err := r.GetProviderSecretData(ctx, externalSecret)
  365. if err != nil {
  366. r.markAsFailed(msgErrorGetSecretData, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonSecretSyncedError)
  367. return ctrl.Result{}, err
  368. }
  369. // if no data was found we can delete the secret if needed.
  370. if len(dataMap) == 0 {
  371. switch externalSecret.Spec.Target.DeletionPolicy {
  372. // delete secret and return early.
  373. case esv1.DeletionPolicyDelete:
  374. // safeguard that we only can delete secrets we own.
  375. // this is also implemented in the es validation webhook.
  376. // NOTE: this error cant be fixed by retrying so we don't return an error (which would requeue immediately)
  377. creationPolicy := externalSecret.Spec.Target.CreationPolicy
  378. if creationPolicy != esv1.CreatePolicyOwner {
  379. err = fmt.Errorf(errDeleteCreatePolicy, secretName, creationPolicy)
  380. r.markAsFailed(msgErrorDeleteSecret, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonSecretSyncedError)
  381. return ctrl.Result{}, nil
  382. }
  383. // delete the secret, if it exists
  384. if existingSecret.UID != "" {
  385. err = r.Delete(ctx, existingSecret)
  386. if err != nil && !apierrors.IsNotFound(err) {
  387. r.markAsFailed(msgErrorDeleteSecret, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonSecretSyncedError)
  388. return ctrl.Result{}, err
  389. }
  390. log.V(1).Info(logSecretDeleted, "secret", secretName, "namespace", externalSecret.Namespace, "reason", "DeletionPolicy=Delete and provider returned no data")
  391. r.recorder.Event(externalSecret, v1.EventTypeNormal, esv1.ReasonDeleted, eventDeleted)
  392. }
  393. r.markAsDone(externalSecret, start, log, esv1.ConditionReasonSecretDeleted, msgDeleted)
  394. return r.getRequeueResult(externalSecret), nil
  395. // In case provider secrets don't exist the kubernetes secret will be kept as-is.
  396. case esv1.DeletionPolicyRetain:
  397. r.markAsDone(externalSecret, start, log, esv1.ConditionReasonSecretSynced, msgSyncedRetain)
  398. return r.getRequeueResult(externalSecret), nil
  399. // noop, handled below
  400. case esv1.DeletionPolicyMerge:
  401. }
  402. }
  403. // mutationFunc is a function which can be applied to a secret to make it match the desired state.
  404. mutationFunc := func(secret *v1.Secret) error {
  405. // initialize maps within the secret so it's safe to set values
  406. if secret.Annotations == nil {
  407. secret.Annotations = make(map[string]string)
  408. }
  409. if secret.Labels == nil {
  410. secret.Labels = make(map[string]string)
  411. }
  412. if secret.Data == nil {
  413. secret.Data = make(map[string][]byte)
  414. }
  415. // set the immutable flag on the secret if requested by the ExternalSecret
  416. if externalSecret.Spec.Target.Immutable {
  417. secret.Immutable = new(true)
  418. }
  419. // only apply the template if the secret is mutable or if the secret is new (has no UID)
  420. // otherwise we would mutate an object that is immutable and already exists
  421. objectDoesNotExistOrCanBeMutated := secret.GetUID() == "" || !externalSecret.Spec.Target.Immutable
  422. if objectDoesNotExistOrCanBeMutated {
  423. // get the list of keys that are managed by this ExternalSecret
  424. keys, err := getManagedDataKeys(secret, externalSecret.Name)
  425. if err != nil {
  426. return err
  427. }
  428. // remove any data keys that are managed by this ExternalSecret, so we can re-add them
  429. // this ensures keys added by templates are not left behind when they are removed from the template
  430. for _, key := range keys {
  431. delete(secret.Data, key)
  432. }
  433. // WARNING: this will remove any labels or annotations managed by this ExternalSecret
  434. // so any updates to labels and annotations should be done AFTER this point
  435. err = r.ApplyTemplate(ctx, externalSecret, secret, dataMap)
  436. if err != nil {
  437. return fmt.Errorf(errApplyTemplate, err)
  438. }
  439. }
  440. if err := r.applyOwnership(externalSecret, secret); err != nil {
  441. return err
  442. }
  443. secret.Labels[esv1.LabelManaged] = esv1.LabelManagedValue
  444. secret.Annotations[esv1.AnnotationDataHash] = esutils.ObjectHash(secret.Data)
  445. return nil
  446. }
  447. switch externalSecret.Spec.Target.CreationPolicy {
  448. case esv1.CreatePolicyNone:
  449. log.V(1).Info("secret creation skipped due to CreationPolicy=None")
  450. err = nil
  451. case esv1.CreatePolicyMerge:
  452. // update the secret, if it exists
  453. if existingSecret.UID != "" {
  454. err = r.updateSecret(ctx, log, existingSecret, mutationFunc, externalSecret, secretName)
  455. } else {
  456. // if the secret does not exist, we wait until the next refresh interval
  457. // rather than returning an error which would requeue immediately
  458. r.markAsDone(externalSecret, start, log, esv1.ConditionReasonSecretMissing, msgMissing)
  459. return r.getRequeueResult(externalSecret), nil
  460. }
  461. case esv1.CreatePolicyOrphan:
  462. // create the secret, if it does not exist
  463. if existingSecret.UID == "" {
  464. err = r.createSecret(ctx, mutationFunc, externalSecret, secretName)
  465. } else {
  466. // if the secret exists, we should update it
  467. err = r.updateSecret(ctx, log, existingSecret, mutationFunc, externalSecret, secretName)
  468. }
  469. case esv1.CreatePolicyOwner:
  470. // we may have orphaned secrets to clean up,
  471. // for example, if the target secret name was changed
  472. err = r.deleteOrphanedSecrets(ctx, log, externalSecret, secretName)
  473. if err != nil {
  474. r.markAsFailed(msgErrorDeleteOrphaned, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonSecretSyncedError)
  475. return ctrl.Result{}, err
  476. }
  477. // create the secret, if it does not exist
  478. if existingSecret.UID == "" {
  479. err = r.createSecret(ctx, mutationFunc, externalSecret, secretName)
  480. } else {
  481. // if the secret exists, we should update it
  482. err = r.updateSecret(ctx, log, existingSecret, mutationFunc, externalSecret, secretName)
  483. }
  484. }
  485. if err != nil {
  486. // if we got an update conflict, we should requeue immediately
  487. if apierrors.IsConflict(err) {
  488. log.V(1).Info("conflict while updating secret, will requeue")
  489. return ctrl.Result{Requeue: true}, nil
  490. }
  491. // detect errors indicating that we failed to set ourselves as the owner of the secret
  492. // NOTE: this error cant be fixed by retrying so we don't return an error (which would requeue immediately)
  493. if errors.Is(err, ErrSecretSetCtrlRef) {
  494. r.markAsFailed(msgErrorBecomeOwner, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonSecretSyncedError)
  495. return ctrl.Result{}, nil
  496. }
  497. // detect errors indicating that the secret has another ExternalSecret as owner
  498. // NOTE: this error cant be fixed by retrying so we don't return an error (which would requeue immediately)
  499. if errors.Is(err, ErrSecretIsOwned) {
  500. r.markAsFailed(msgErrorIsOwned, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonSecretSyncedError)
  501. return ctrl.Result{}, nil
  502. }
  503. // detect errors indicating that the secret is immutable
  504. // NOTE: this error cant be fixed by retrying so we don't return an error (which would requeue immediately)
  505. if errors.Is(err, ErrSecretImmutable) {
  506. r.markAsFailed(msgErrorUpdateImmutable, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonSecretSyncedError)
  507. return ctrl.Result{}, nil
  508. }
  509. r.markAsFailed(msgErrorUpdateSecret, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonSecretSyncedError)
  510. return ctrl.Result{}, err
  511. }
  512. r.markAsDone(externalSecret, start, log, esv1.ConditionReasonSecretSynced, msgSynced)
  513. return r.getRequeueResult(externalSecret), nil
  514. }
  515. // reconcileGenericTarget handles reconciliation for generic targets (ConfigMaps, Custom Resources).
  516. func (r *Reconciler) reconcileGenericTarget(
  517. ctx context.Context,
  518. externalSecret *esv1.ExternalSecret,
  519. log logr.Logger,
  520. start time.Time,
  521. resourceLabels map[string]string,
  522. syncCallsError *prometheus.CounterVec,
  523. ) (ctrl.Result, error) {
  524. var existing *unstructured.Unstructured
  525. if externalSecret.Spec.Target.CreationPolicy == esv1.CreatePolicyMerge ||
  526. externalSecret.Spec.Target.CreationPolicy == esv1.CreatePolicyOrphan ||
  527. externalSecret.Spec.Target.CreationPolicy == esv1.CreatePolicyOwner {
  528. var getErr error
  529. existing, getErr = r.getGenericResource(ctx, log, externalSecret)
  530. if getErr != nil && !apierrors.IsNotFound(getErr) {
  531. r.markAsFailed("could not get target resource", getErr, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonResourceSyncedError)
  532. return ctrl.Result{}, getErr
  533. }
  534. }
  535. valid, err := isGenericTargetValid(existing, externalSecret)
  536. if err != nil {
  537. log.V(1).Info("unable to validate target", "error", err)
  538. return ctrl.Result{}, err
  539. }
  540. if !shouldRefresh(externalSecret) && valid {
  541. log.V(1).Info("skipping refresh of generic target")
  542. return r.getRequeueResult(externalSecret), nil
  543. }
  544. dataMap, err := r.GetProviderSecretData(ctx, externalSecret)
  545. if err != nil {
  546. r.markAsFailed(msgErrorGetSecretData, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonResourceSyncedError)
  547. return ctrl.Result{}, err
  548. }
  549. if len(dataMap) == 0 {
  550. switch externalSecret.Spec.Target.DeletionPolicy {
  551. case esv1.DeletionPolicyDelete:
  552. creationPolicy := externalSecret.Spec.Target.CreationPolicy
  553. if creationPolicy != esv1.CreatePolicyOwner {
  554. err = fmt.Errorf("unable to delete resource: creationPolicy=%s is not Owner", creationPolicy)
  555. r.markAsFailed("could not delete resource", err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonResourceSyncedError)
  556. return ctrl.Result{}, nil
  557. }
  558. err = r.deleteGenericResource(ctx, log, externalSecret)
  559. if err != nil {
  560. r.markAsFailed("could not delete resource", err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonResourceSyncedError)
  561. return ctrl.Result{}, err
  562. }
  563. r.markAsDone(externalSecret, start, log, esv1.ConditionReasonResourceDeleted, msgDeleted)
  564. return r.getRequeueResult(externalSecret), nil
  565. case esv1.DeletionPolicyRetain:
  566. r.markAsDone(externalSecret, start, log, esv1.ConditionReasonResourceSynced, msgSyncedRetain)
  567. return r.getRequeueResult(externalSecret), nil
  568. case esv1.DeletionPolicyMerge:
  569. }
  570. }
  571. // For Merge policy with existing resource, pass it to applyTemplateToManifest
  572. // so templates are applied to the existing resource instead of creating a new one
  573. var baseObj *unstructured.Unstructured
  574. if externalSecret.Spec.Target.CreationPolicy == esv1.CreatePolicyMerge && existing != nil {
  575. baseObj = existing
  576. }
  577. // render the template for the manifest
  578. obj, err := r.applyTemplateToManifest(ctx, externalSecret, dataMap, baseObj)
  579. if err != nil {
  580. r.markAsFailed("could not apply template to manifest", err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonResourceSyncedError)
  581. return ctrl.Result{}, err
  582. }
  583. // handle creation policies
  584. switch externalSecret.Spec.Target.CreationPolicy {
  585. case esv1.CreatePolicyNone:
  586. log.V(1).Info("resource creation skipped due to CreationPolicy=None")
  587. err = nil
  588. case esv1.CreatePolicyMerge:
  589. // for Merge policy, only update if resource exists
  590. if existing == nil || existing.GetUID() == "" {
  591. r.markAsDone(externalSecret, start, log, esv1.ConditionReasonResourceMissing, "resource will not be created due to CreationPolicy=Merge")
  592. return r.getRequeueResult(externalSecret), nil
  593. }
  594. obj.SetResourceVersion(existing.GetResourceVersion())
  595. obj.SetUID(existing.GetUID())
  596. // update the existing resource
  597. err = r.updateGenericResource(ctx, log, externalSecret, obj)
  598. case esv1.CreatePolicyOrphan, esv1.CreatePolicyOwner:
  599. if existing != nil {
  600. obj.SetResourceVersion(existing.GetResourceVersion())
  601. obj.SetUID(existing.GetUID())
  602. err = r.updateGenericResource(ctx, log, externalSecret, obj)
  603. } else {
  604. err = r.createGenericResource(ctx, log, externalSecret, obj)
  605. }
  606. }
  607. if err != nil {
  608. // if we got an update conflict, requeue immediately
  609. if apierrors.IsConflict(err) {
  610. log.V(1).Info("conflict while updating resource, will requeue")
  611. return ctrl.Result{RequeueAfter: 1 * time.Second}, nil
  612. }
  613. r.markAsFailed(msgErrorUpdateSecret, err, externalSecret, syncCallsError.With(resourceLabels), esv1.ConditionReasonResourceSyncedError)
  614. return ctrl.Result{}, err
  615. }
  616. if externalSecret.Spec.Target.CreationPolicy != esv1.CreatePolicyNone {
  617. gvk := getTargetGVK(externalSecret)
  618. esName := types.NamespacedName{Name: externalSecret.Name, Namespace: externalSecret.Namespace}
  619. if _, err := r.informerManager.EnsureInformer(ctx, gvk, esName); err != nil {
  620. log.Error(err, "failed to register informer for generic target, drift detection may not work",
  621. "group", gvk.Group,
  622. "version", gvk.Version,
  623. "kind", gvk.Kind)
  624. }
  625. }
  626. r.markAsDone(externalSecret, start, log, esv1.ConditionReasonResourceSynced, msgSynced)
  627. return r.getRequeueResult(externalSecret), nil
  628. }
  629. // getRequeueResult create a result with requeueAfter based on the ExternalSecret refresh interval.
  630. func (r *Reconciler) getRequeueResult(externalSecret *esv1.ExternalSecret) ctrl.Result {
  631. // default to the global requeue interval
  632. // note, this will never be used because the CRD has a default value of 1 hour
  633. refreshInterval := r.RequeueInterval
  634. if externalSecret.Spec.RefreshInterval != nil {
  635. refreshInterval = externalSecret.Spec.RefreshInterval.Duration
  636. }
  637. // if the refresh interval is <= 0, we should not requeue
  638. if refreshInterval <= 0 {
  639. return ctrl.Result{}
  640. }
  641. // if the last refresh time is not set, requeue after the refresh interval
  642. // note, this should not happen, as we only call this function on ExternalSecrets
  643. // that have been reconciled at least once
  644. if externalSecret.Status.RefreshTime.IsZero() {
  645. return ctrl.Result{RequeueAfter: refreshInterval}
  646. }
  647. timeSinceLastRefresh := time.Since(externalSecret.Status.RefreshTime.Time)
  648. // if the last refresh time is in the future, we should requeue immediately
  649. // note, this should not happen, as we always refresh an ExternalSecret
  650. // that has a last refresh time in the future
  651. if timeSinceLastRefresh < 0 {
  652. return ctrl.Result{Requeue: true}
  653. }
  654. // if there is time remaining, requeue after the remaining time
  655. if timeSinceLastRefresh < refreshInterval {
  656. return ctrl.Result{RequeueAfter: refreshInterval - timeSinceLastRefresh}
  657. }
  658. // otherwise, requeue immediately
  659. return ctrl.Result{Requeue: true}
  660. }
  661. func (r *Reconciler) markAsDone(externalSecret *esv1.ExternalSecret, start time.Time, log logr.Logger, reason, msg string) {
  662. oldReadyCondition := esv1.GetExternalSecretCondition(externalSecret.Status, esv1.ExternalSecretReady)
  663. newReadyCondition := NewExternalSecretCondition(esv1.ExternalSecretReady, v1.ConditionTrue, reason, msg)
  664. SetExternalSecretCondition(externalSecret, *newReadyCondition)
  665. externalSecret.Status.RefreshTime = metav1.NewTime(start)
  666. externalSecret.Status.SyncedResourceVersion = ctrlutil.GetResourceVersion(externalSecret.ObjectMeta)
  667. // if the status or reason has changed, log at the appropriate verbosity level
  668. if oldReadyCondition == nil || oldReadyCondition.Status != newReadyCondition.Status || oldReadyCondition.Reason != newReadyCondition.Reason {
  669. if newReadyCondition.Reason == esv1.ConditionReasonSecretDeleted {
  670. log.Info("deleted secret")
  671. } else {
  672. log.Info("reconciled secret")
  673. }
  674. } else {
  675. log.V(1).Info("reconciled secret")
  676. }
  677. }
  678. func (r *Reconciler) markAsFailed(msg string, err error, externalSecret *esv1.ExternalSecret, counter prometheus.Counter, reason string) {
  679. r.recorder.Event(externalSecret, v1.EventTypeWarning, esv1.ReasonUpdateFailed, err.Error())
  680. conditionSynced := NewExternalSecretCondition(esv1.ExternalSecretReady, v1.ConditionFalse, reason, msg)
  681. SetExternalSecretCondition(externalSecret, *conditionSynced)
  682. counter.Inc()
  683. }
  684. // applyOwnership handles ownership-related logic for target resources (Secrets and generic targets).
  685. // It detects ownership conflicts with other ExternalSecrets, sets or removes the controller reference
  686. // based on CreationPolicy, and manages the LabelOwner label.
  687. func (r *Reconciler) applyOwnership(es *esv1.ExternalSecret, target client.Object) error {
  688. // get information about the current owner of the target
  689. // - we ignore the API version as it can change over time
  690. // - we ignore the UID for consistency with the SetControllerReference function
  691. currentOwner := metav1.GetControllerOf(target)
  692. ownerIsESKind := false
  693. ownerIsCurrentES := false
  694. if currentOwner != nil {
  695. currentOwnerGK := schema.FromAPIVersionAndKind(currentOwner.APIVersion, currentOwner.Kind).GroupKind()
  696. ownerIsESKind = currentOwnerGK.String() == esv1.ExtSecretGroupKind
  697. ownerIsCurrentES = ownerIsESKind && currentOwner.Name == es.Name
  698. }
  699. // if another ExternalSecret is the owner, we should return an error
  700. // otherwise the controller will fight with itself to update the target.
  701. // note, this does not prevent other controllers from owning the target.
  702. if ownerIsESKind && !ownerIsCurrentES {
  703. return fmt.Errorf("%w: %s", ErrSecretIsOwned, currentOwner.Name)
  704. }
  705. // if the CreationPolicy is Owner, we should set ourselves as the owner of the target
  706. if es.Spec.Target.CreationPolicy == esv1.CreatePolicyOwner {
  707. if err := controllerutil.SetControllerReference(es, target, r.Scheme); err != nil {
  708. return fmt.Errorf("%w: %w", ErrSecretSetCtrlRef, err)
  709. }
  710. }
  711. // if the creation policy is not Owner, we should remove ourselves as the owner
  712. // this could happen if the creation policy was changed after the target was created
  713. if es.Spec.Target.CreationPolicy != esv1.CreatePolicyOwner && ownerIsCurrentES {
  714. if err := controllerutil.RemoveControllerReference(es, target, r.Scheme); err != nil {
  715. return fmt.Errorf("%w: %w", ErrSecretRemoveCtrlRef, err)
  716. }
  717. }
  718. // we also use a label to keep track of the owner of the target
  719. // this lets us remove targets that are no longer needed if the target name changes
  720. labels := target.GetLabels()
  721. if labels == nil {
  722. labels = make(map[string]string)
  723. }
  724. if es.Spec.Target.CreationPolicy == esv1.CreatePolicyOwner {
  725. labels[esv1.LabelOwner] = esutils.ObjectHash(fmt.Sprintf("%v/%v", es.Namespace, es.Name))
  726. } else {
  727. delete(labels, esv1.LabelOwner)
  728. }
  729. target.SetLabels(labels)
  730. return nil
  731. }
  732. func (r *Reconciler) cleanupManagedSecrets(ctx context.Context, log logr.Logger, externalSecret *esv1.ExternalSecret) error {
  733. // Only delete resources if DeletionPolicy is Delete
  734. if externalSecret.Spec.Target.DeletionPolicy != esv1.DeletionPolicyDelete {
  735. log.V(1).Info("skipping resource deletion due to DeletionPolicy", "policy", externalSecret.Spec.Target.DeletionPolicy)
  736. return nil
  737. }
  738. // if this is a generic target, use deleteGenericResource
  739. if isGenericTarget(externalSecret) {
  740. return r.deleteGenericResource(ctx, log, externalSecret)
  741. }
  742. // handle Secret deletion
  743. secretName := externalSecret.Spec.Target.Name
  744. if secretName == "" {
  745. secretName = externalSecret.Name
  746. }
  747. var secret v1.Secret
  748. err := r.Get(ctx, types.NamespacedName{Name: secretName, Namespace: externalSecret.Namespace}, &secret)
  749. if err != nil {
  750. if apierrors.IsNotFound(err) {
  751. return nil
  752. }
  753. return err
  754. }
  755. // Only delete if we own it
  756. if metav1.IsControlledBy(&secret, externalSecret) {
  757. if err := r.Delete(ctx, &secret); err != nil && !apierrors.IsNotFound(err) {
  758. return err
  759. }
  760. log.V(1).Info(logManagedSecretDeleted, "secret", secretName, "namespace", externalSecret.Namespace, "reason", "ExternalSecret deleted")
  761. }
  762. return nil
  763. }
  764. func (r *Reconciler) deleteOrphanedSecrets(ctx context.Context, log logr.Logger, externalSecret *esv1.ExternalSecret, secretName string) error {
  765. ownerLabel := esutils.ObjectHash(fmt.Sprintf("%v/%v", externalSecret.Namespace, externalSecret.Name))
  766. // we use a PartialObjectMetadataList to avoid loading the full secret objects
  767. // and because the Secrets partials are always cached due to WatchesMetadata() in SetupWithManager()
  768. secretListPartial := &metav1.PartialObjectMetadataList{}
  769. secretListPartial.SetGroupVersionKind(v1.SchemeGroupVersion.WithKind("SecretList"))
  770. listOpts := &client.ListOptions{
  771. LabelSelector: labels.SelectorFromSet(map[string]string{
  772. esv1.LabelOwner: ownerLabel,
  773. }),
  774. Namespace: externalSecret.Namespace,
  775. }
  776. if err := r.List(ctx, secretListPartial, listOpts); err != nil {
  777. return err
  778. }
  779. // delete all secrets that are not the target secret
  780. for _, secretPartial := range secretListPartial.Items {
  781. if secretPartial.GetName() != secretName {
  782. err := r.Delete(ctx, &secretPartial)
  783. if err != nil && !apierrors.IsNotFound(err) {
  784. return err
  785. }
  786. log.V(1).Info(logSecretDeletedOrphaned, "secret", secretPartial.GetName(), "namespace", externalSecret.Namespace)
  787. r.recorder.Event(externalSecret, v1.EventTypeNormal, esv1.ReasonDeleted, eventDeletedOrphaned)
  788. }
  789. }
  790. return nil
  791. }
  792. // createSecret creates a new secret with the given mutation function.
  793. func (r *Reconciler) createSecret(ctx context.Context, mutationFunc func(secret *v1.Secret) error, es *esv1.ExternalSecret, secretName string) error {
  794. fqdn := fqdnFor(es.Name)
  795. // define and mutate the new secret
  796. newSecret := &v1.Secret{
  797. ObjectMeta: metav1.ObjectMeta{
  798. Name: secretName,
  799. Namespace: es.Namespace,
  800. Labels: map[string]string{},
  801. Annotations: map[string]string{},
  802. },
  803. Data: make(map[string][]byte),
  804. }
  805. if err := mutationFunc(newSecret); err != nil {
  806. return err
  807. }
  808. // note, we set field owner even for Create
  809. if err := r.Create(ctx, newSecret, client.FieldOwner(fqdn)); err != nil {
  810. return err
  811. }
  812. // set the binding reference to the secret
  813. // https://github.com/external-secrets/external-secrets/pull/2263
  814. es.Status.Binding = v1.LocalObjectReference{Name: newSecret.Name}
  815. r.recorder.Event(es, v1.EventTypeNormal, esv1.ReasonCreated, eventCreated)
  816. return nil
  817. }
  818. func (r *Reconciler) updateSecret(ctx context.Context, log logr.Logger, existingSecret *v1.Secret, mutationFunc func(secret *v1.Secret) error, es *esv1.ExternalSecret, secretName string) error {
  819. fqdn := fqdnFor(es.Name)
  820. // fail if the secret does not exist
  821. // this should never happen because we check this before calling this function
  822. if existingSecret.UID == "" {
  823. return fmt.Errorf(errUpdateNotFound, secretName)
  824. }
  825. // set the binding reference to the secret
  826. // https://github.com/external-secrets/external-secrets/pull/2263
  827. es.Status.Binding = v1.LocalObjectReference{Name: secretName}
  828. // mutate a copy of the existing secret with the mutation function
  829. updatedSecret := existingSecret.DeepCopy()
  830. if err := mutationFunc(updatedSecret); err != nil {
  831. return fmt.Errorf(errMutate, updatedSecret.Name, err)
  832. }
  833. // if the secret does not need to be updated, return early
  834. if equality.Semantic.DeepEqual(existingSecret, updatedSecret) {
  835. return nil
  836. }
  837. // if the existing secret is immutable, we can only update the object metadata
  838. if ptr.Deref(existingSecret.Immutable, false) {
  839. // check if the metadata was changed
  840. metadataChanged := !equality.Semantic.DeepEqual(existingSecret.ObjectMeta, updatedSecret.ObjectMeta)
  841. // check if the immutable data/type was changed
  842. var dataChanged bool
  843. if metadataChanged {
  844. // update the `existingSecret` object with the metadata from `updatedSecret`
  845. // this lets us compare the objects to see if the immutable data/type was changed
  846. existingSecret.ObjectMeta = *updatedSecret.ObjectMeta.DeepCopy()
  847. dataChanged = !equality.Semantic.DeepEqual(existingSecret, updatedSecret)
  848. // because we use labels and annotations to keep track of the secret,
  849. // we need to update the metadata, regardless of if the immutable data was changed
  850. // NOTE: we are using the `existingSecret` object here, as we ONLY want to update the metadata,
  851. // and we previously copied the metadata from the `updatedSecret` object
  852. if err := r.Update(ctx, existingSecret, client.FieldOwner(fqdn)); err != nil {
  853. // if we get a conflict, we should return early to requeue immediately
  854. // note, we don't wrap this error so we can handle it in the caller
  855. if apierrors.IsConflict(err) {
  856. return err
  857. }
  858. return fmt.Errorf(errUpdate, existingSecret.Name, err)
  859. }
  860. } else {
  861. // we know there was some change in the secret (or we would have returned early)
  862. // we know the metadata was NOT changed (metadataChanged == false)
  863. // so, the only thing that could have changed is the immutable data/type fields
  864. dataChanged = true
  865. }
  866. // if the immutable data was changed, we should return an error
  867. if dataChanged {
  868. return fmt.Errorf(errUpdate, existingSecret.Name, ErrSecretImmutable)
  869. }
  870. }
  871. // update the secret
  872. if err := r.Update(ctx, updatedSecret, client.FieldOwner(fqdn)); err != nil {
  873. // if we get a conflict, we should return early to requeue immediately
  874. // note, we don't wrap this error so we can handle it in the caller
  875. if apierrors.IsConflict(err) {
  876. return err
  877. }
  878. return fmt.Errorf(errUpdate, updatedSecret.Name, err)
  879. }
  880. // only compute the key diff when debug verbosity is active (--loglevel=debug /
  881. // log.level=debug in Helm). skipping it by default avoids per-reconcile
  882. // allocation on every managed secret. we only ever log key names, never values.
  883. if log.V(1).Enabled() {
  884. added, updated, removed, emptied := diffSecretDataKeys(existingSecret.Data, updatedSecret.Data)
  885. if len(added) > 0 || len(updated) > 0 || len(removed) > 0 {
  886. log.V(1).Info(logSecretDataChanged,
  887. "secret", secretName,
  888. "namespace", es.Namespace,
  889. "added", added,
  890. "updated", updated,
  891. "removed", removed,
  892. "emptied", emptied,
  893. )
  894. }
  895. }
  896. r.recorder.Event(es, v1.EventTypeNormal, esv1.ReasonUpdated, eventUpdated)
  897. return nil
  898. }
  899. // getManagedDataKeys returns the list of data keys in a secret which are managed by a specified owner.
  900. func getManagedDataKeys(secret *v1.Secret, fieldOwner string) ([]string, error) {
  901. return getManagedFieldKeys(secret, fieldOwner, func(fields map[string]any) []string {
  902. dataFields := fields["f:data"]
  903. if dataFields == nil {
  904. return nil
  905. }
  906. df, ok := dataFields.(map[string]any)
  907. if !ok {
  908. return nil
  909. }
  910. return slices.Collect(maps.Keys(df))
  911. })
  912. }
  913. func getManagedFieldKeys(
  914. secret *v1.Secret,
  915. fieldOwner string,
  916. process func(fields map[string]any) []string,
  917. ) ([]string, error) {
  918. fqdn := fqdnFor(fieldOwner)
  919. var keys []string
  920. for _, v := range secret.ObjectMeta.ManagedFields {
  921. if v.Manager != fqdn {
  922. continue
  923. }
  924. fields := make(map[string]any)
  925. err := json.Unmarshal(v.FieldsV1.Raw, &fields)
  926. if err != nil {
  927. return nil, fmt.Errorf("error unmarshaling managed fields: %w", err)
  928. }
  929. for _, key := range process(fields) {
  930. if key == "." {
  931. continue
  932. }
  933. keys = append(keys, strings.TrimPrefix(key, "f:"))
  934. }
  935. }
  936. return keys, nil
  937. }
  938. func shouldSkipClusterSecretStore(r *Reconciler, es *esv1.ExternalSecret) bool {
  939. return !r.ClusterSecretStoreEnabled && es.Spec.SecretStoreRef.Kind == esv1.ClusterSecretStoreKind
  940. }
  941. // shouldSkipUnmanagedStore iterates over all secretStore references in the externalSecret spec,
  942. // fetches the store and evaluates the controllerClass property.
  943. // Returns true if any storeRef points to store with a non-matching controllerClass.
  944. func shouldSkipUnmanagedStore(ctx context.Context, namespace string, r *Reconciler, es *esv1.ExternalSecret) (bool, error) {
  945. var storeList []esv1.SecretStoreRef
  946. if es.Spec.SecretStoreRef.Name != "" {
  947. storeList = append(storeList, es.Spec.SecretStoreRef)
  948. }
  949. for _, ref := range es.Spec.Data {
  950. if ref.SourceRef != nil {
  951. storeList = append(storeList, ref.SourceRef.SecretStoreRef)
  952. }
  953. }
  954. for _, ref := range es.Spec.DataFrom {
  955. if ref.SourceRef != nil && ref.SourceRef.SecretStoreRef != nil {
  956. storeList = append(storeList, *ref.SourceRef.SecretStoreRef)
  957. }
  958. // verify that generator's controllerClass matches
  959. if ref.SourceRef != nil && ref.SourceRef.GeneratorRef != nil {
  960. _, obj, err := resolvers.GeneratorRef(ctx, r.Client, r.Scheme, namespace, ref.SourceRef.GeneratorRef)
  961. if err != nil {
  962. if apierrors.IsNotFound(err) {
  963. // skip non-existent generators
  964. continue
  965. }
  966. if errors.Is(err, resolvers.ErrUnableToGetGenerator) {
  967. // skip generators that we can't get (e.g. due to being invalid)
  968. continue
  969. }
  970. return false, err
  971. }
  972. skipGenerator, err := shouldSkipGenerator(r, obj)
  973. if err != nil {
  974. return false, err
  975. }
  976. if skipGenerator {
  977. return true, nil
  978. }
  979. }
  980. }
  981. for _, ref := range storeList {
  982. var store esv1.GenericStore
  983. switch ref.Kind {
  984. case esv1.SecretStoreKind, "":
  985. store = &esv1.SecretStore{}
  986. case esv1.ClusterSecretStoreKind:
  987. store = &esv1.ClusterSecretStore{}
  988. namespace = ""
  989. default:
  990. return false, fmt.Errorf("unsupported secret store kind: %s", ref.Kind)
  991. }
  992. err := r.Get(ctx, types.NamespacedName{
  993. Name: ref.Name,
  994. Namespace: namespace,
  995. }, store)
  996. if err != nil {
  997. if apierrors.IsNotFound(err) {
  998. // skip non-existent stores
  999. continue
  1000. }
  1001. return false, err
  1002. }
  1003. class := store.GetSpec().Controller
  1004. if class != "" && class != r.ControllerClass {
  1005. return true, nil
  1006. }
  1007. }
  1008. return false, nil
  1009. }
  1010. // isWithinSyncWindow reports whether 'at' falls inside the window that opened
  1011. // at the most-recent firing of 'sched' before 'at'. robfig's Next() is strictly
  1012. // exclusive, so we back up by (duration + 1s) to find that firing.
  1013. func isWithinSyncWindow(sched robfigcron.Schedule, duration time.Duration, at time.Time) bool {
  1014. prev := sched.Next(at.Add(-duration - time.Second))
  1015. return !prev.IsZero() && !prev.After(at) && !at.After(prev.Add(duration))
  1016. }
  1017. // cronParser is the standard 5-field (no-seconds) parser shared across all
  1018. // sync-window checks within this controller.
  1019. var cronParser = robfigcron.NewParser(
  1020. robfigcron.Minute | robfigcron.Hour | robfigcron.Dom |
  1021. robfigcron.Month | robfigcron.Dow | robfigcron.Descriptor,
  1022. )
  1023. // isPeriodicRefreshAllowedByWindows returns true when the SyncWindows on 'es'
  1024. // collectively permit a periodic refresh at time 'at'.
  1025. //
  1026. // - No windows: always allow.
  1027. // - kind=deny: deny when any window is active; allow otherwise.
  1028. // - kind=allow: allow when at least one window is active; deny otherwise.
  1029. //
  1030. // Windows with an unparseable Schedule are silently ignored (treated as
  1031. // inactive) so a typo does not permanently block syncs.
  1032. func isPeriodicRefreshAllowedByWindows(es *esv1.ExternalSecret, at time.Time) bool {
  1033. sw := es.Spec.SyncWindows
  1034. if sw == nil || len(sw.Windows) == 0 {
  1035. return true
  1036. }
  1037. anyActive := false
  1038. for _, w := range sw.Windows {
  1039. sched, err := cronParser.Parse(w.Schedule)
  1040. if err != nil {
  1041. // A schedule that fails to parse is skipped rather than aborting the
  1042. // whole evaluation. The kubebuilder pattern marker rejects malformed
  1043. // schedules at admission, so this is a defensive log for any value
  1044. // that slips past validation (e.g. a parser/regex mismatch).
  1045. ctrl.Log.V(1).Info("ignoring unparseable sync window schedule",
  1046. "ExternalSecret", es.Namespace+"/"+es.Name,
  1047. "schedule", w.Schedule,
  1048. "error", err.Error())
  1049. continue
  1050. }
  1051. if isWithinSyncWindow(sched, w.Duration.Duration, at) {
  1052. anyActive = true
  1053. break
  1054. }
  1055. }
  1056. allowed := true
  1057. switch sw.Kind {
  1058. case esv1.SyncWindowDeny:
  1059. allowed = !anyActive
  1060. case esv1.SyncWindowAllow:
  1061. allowed = anyActive
  1062. }
  1063. if !allowed {
  1064. ctrl.Log.V(1).Info("periodic refresh blocked by SyncWindow",
  1065. "ExternalSecret", es.Namespace+"/"+es.Name,
  1066. "kind", sw.Kind)
  1067. }
  1068. return allowed
  1069. }
  1070. func shouldRefresh(es *esv1.ExternalSecret) bool {
  1071. switch es.Spec.RefreshPolicy {
  1072. case esv1.RefreshPolicyCreatedOnce:
  1073. if es.Status.SyncedResourceVersion == "" || es.Status.RefreshTime.IsZero() {
  1074. return true
  1075. }
  1076. return false
  1077. case esv1.RefreshPolicyOnChange:
  1078. if es.Status.SyncedResourceVersion == "" || es.Status.RefreshTime.IsZero() {
  1079. return true
  1080. }
  1081. return es.Status.SyncedResourceVersion != ctrlutil.GetResourceVersion(es.ObjectMeta)
  1082. case esv1.RefreshPolicyPeriodic:
  1083. return shouldRefreshPeriodic(es)
  1084. default:
  1085. return shouldRefreshPeriodic(es)
  1086. }
  1087. }
  1088. func shouldRefreshPeriodic(es *esv1.ExternalSecret) bool {
  1089. // if the refresh interval is 0, and we have synced previously, we should not refresh
  1090. if es.Spec.RefreshInterval.Duration <= 0 && es.Status.SyncedResourceVersion != "" {
  1091. return false
  1092. }
  1093. // if the ExternalSecret has been updated, we should refresh
  1094. if es.Status.SyncedResourceVersion != ctrlutil.GetResourceVersion(es.ObjectMeta) {
  1095. return true
  1096. }
  1097. // if the last refresh time is zero, we should refresh
  1098. if es.Status.RefreshTime.IsZero() {
  1099. return true
  1100. }
  1101. now := time.Now()
  1102. // if the last refresh time is in the future, we should refresh
  1103. if es.Status.RefreshTime.Time.After(now) {
  1104. return true
  1105. }
  1106. // if the last refresh time + refresh interval is before now, we should refresh
  1107. if !es.Status.RefreshTime.Add(es.Spec.RefreshInterval.Duration).Before(now) {
  1108. return false
  1109. }
  1110. // check sync windows before triggering a refresh
  1111. return isPeriodicRefreshAllowedByWindows(es, now)
  1112. }
  1113. // isSecretValid checks if the secret exists, and it's data is consistent with the calculated hash.
  1114. func isSecretValid(existingSecret *v1.Secret, es *esv1.ExternalSecret) bool {
  1115. // Secret is always valid with `CreationPolicy=Orphan`
  1116. if es.Spec.Target.CreationPolicy == esv1.CreatePolicyOrphan {
  1117. return true
  1118. }
  1119. if existingSecret.UID == "" {
  1120. return false
  1121. }
  1122. // if the managed label is missing or incorrect, then it's invalid
  1123. if existingSecret.Labels[esv1.LabelManaged] != esv1.LabelManagedValue {
  1124. return false
  1125. }
  1126. // if the data-hash annotation is missing or incorrect, then it's invalid
  1127. // this is how we know if the data has chanced since we last updated the secret
  1128. if existingSecret.Annotations[esv1.AnnotationDataHash] != esutils.ObjectHash(existingSecret.Data) {
  1129. return false
  1130. }
  1131. return true
  1132. }
  1133. func isGenericTargetValid(existingTarget *unstructured.Unstructured, es *esv1.ExternalSecret) (bool, error) {
  1134. if es.Spec.Target.CreationPolicy == esv1.CreatePolicyOrphan {
  1135. return true, nil
  1136. }
  1137. if existingTarget == nil || existingTarget.GetUID() == "" {
  1138. return false, nil
  1139. }
  1140. if existingTarget.GetLabels()[esv1.LabelManaged] != esv1.LabelManagedValue {
  1141. return false, nil
  1142. }
  1143. hash, err := genericTargetContentHash(existingTarget)
  1144. if err != nil {
  1145. return false, fmt.Errorf("failed to hash target: %w", err)
  1146. }
  1147. if existingTarget.GetAnnotations()[esv1.AnnotationDataHash] != hash {
  1148. return false, nil
  1149. }
  1150. return true, nil
  1151. }
  1152. // genericTargetContentHash computes a hash over the hashable content of an unstructured object.
  1153. // It uses the "spec" field if present, otherwise falls back to "data".
  1154. func genericTargetContentHash(obj *unstructured.Unstructured) (string, error) {
  1155. content := obj.Object
  1156. switch {
  1157. case content["spec"] != nil:
  1158. return esutils.ObjectHash(content["spec"]), nil
  1159. case content["data"] != nil:
  1160. return esutils.ObjectHash(content["data"]), nil
  1161. default:
  1162. return "", errors.New("generic target content does not have a spec or data field for content hashing")
  1163. }
  1164. }
  1165. // SetupWithManager returns a new controller builder that will be started by the provided Manager.
  1166. func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opts controller.Options) error {
  1167. r.recorder = mgr.GetEventRecorderFor("external-secrets")
  1168. if r.APIReader == nil {
  1169. r.APIReader = mgr.GetAPIReader()
  1170. }
  1171. // Initialize informer manager only if generic targets are allowed
  1172. if r.AllowGenericTargets && r.informerManager == nil {
  1173. r.informerManager = NewInformerManager(ctx, mgr.GetCache(), r.Client, r.Log.WithName("informer-manager"))
  1174. }
  1175. // index ExternalSecrets based on the target secret name,
  1176. // this lets us quickly find all ExternalSecrets which target a specific Secret
  1177. if err := mgr.GetFieldIndexer().IndexField(ctx, &esv1.ExternalSecret{}, indexESTargetSecretNameField, func(obj client.Object) []string {
  1178. es := obj.(*esv1.ExternalSecret)
  1179. // Don't index generic targets here (they use indexESTargetResourceField)
  1180. if isGenericTarget(es) {
  1181. return nil
  1182. }
  1183. // if the target name is set, use that as the index
  1184. if es.Spec.Target.Name != "" {
  1185. return []string{es.Spec.Target.Name}
  1186. }
  1187. // otherwise, use the ExternalSecret name
  1188. return []string{es.Name}
  1189. }); err != nil {
  1190. return err
  1191. }
  1192. // index ExternalSecrets based on the target resource (GVK + name)
  1193. // this lets us quickly find all ExternalSecrets which target a specific generic resource
  1194. if err := mgr.GetFieldIndexer().IndexField(ctx, &esv1.ExternalSecret{}, indexESTargetResourceField, func(obj client.Object) []string {
  1195. es := obj.(*esv1.ExternalSecret)
  1196. if !r.AllowGenericTargets || !isGenericTarget(es) {
  1197. return nil
  1198. }
  1199. gvk := getTargetGVK(es)
  1200. targetName := getTargetName(es)
  1201. // Index format: "group/version/kind/name"
  1202. return []string{fmt.Sprintf("%s/%s/%s/%s", gvk.Group, gvk.Version, gvk.Kind, targetName)}
  1203. }); err != nil {
  1204. return err
  1205. }
  1206. // predicate function to ignore secret events unless they have the "managed" label
  1207. secretHasESLabel := predicate.NewPredicateFuncs(func(object client.Object) bool {
  1208. value, hasLabel := object.GetLabels()[esv1.LabelManaged]
  1209. return hasLabel && value == esv1.LabelManagedValue
  1210. })
  1211. // filter ExternalSecret updates to avoid requeueing on status-only changes.
  1212. externalSecretPredicate := predicate.Funcs{
  1213. UpdateFunc: func(e event.UpdateEvent) bool {
  1214. return shouldEnqueueExternalSecretUpdate(e.ObjectOld, e.ObjectNew)
  1215. },
  1216. }
  1217. // Build the controller
  1218. builder := ctrl.NewControllerManagedBy(mgr).
  1219. WithOptions(opts).
  1220. For(&esv1.ExternalSecret{}, builder.WithPredicates(externalSecretPredicate)).
  1221. // we cant use Owns(), as we don't set ownerReferences when the creationPolicy is not Owner.
  1222. // we use WatchesMetadata() to reduce memory usage, as otherwise we have to process full secret objects.
  1223. WatchesMetadata(
  1224. &v1.Secret{},
  1225. handler.EnqueueRequestsFromMapFunc(r.findObjectsForSecret),
  1226. builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}, secretHasESLabel),
  1227. )
  1228. // Watch generic targets dynamically via the informer manager
  1229. // Only add this watch source if the feature is enabled
  1230. if r.AllowGenericTargets {
  1231. builder = builder.WatchesRawSource(r.informerManager.Source())
  1232. }
  1233. return builder.Complete(r)
  1234. }
  1235. // shouldEnqueueExternalSecretUpdate returns true for spec/metadata updates that can affect reconciliation behavior,
  1236. // while ignoring status-only updates.
  1237. func shouldEnqueueExternalSecretUpdate(oldObj, newObj client.Object) bool {
  1238. oldES, oldOK := oldObj.(*esv1.ExternalSecret)
  1239. newES, newOK := newObj.(*esv1.ExternalSecret)
  1240. if !oldOK || !newOK {
  1241. return true
  1242. }
  1243. if oldES.GetGeneration() != newES.GetGeneration() {
  1244. return true
  1245. }
  1246. if !equality.Semantic.DeepEqual(oldES.GetLabels(), newES.GetLabels()) {
  1247. return true
  1248. }
  1249. if !equality.Semantic.DeepEqual(oldES.GetAnnotations(), newES.GetAnnotations()) {
  1250. return true
  1251. }
  1252. if !equality.Semantic.DeepEqual(oldES.GetFinalizers(), newES.GetFinalizers()) {
  1253. return true
  1254. }
  1255. oldDeletion := oldES.GetDeletionTimestamp()
  1256. newDeletion := newES.GetDeletionTimestamp()
  1257. if oldDeletion == nil && newDeletion == nil {
  1258. return false
  1259. }
  1260. if oldDeletion == nil || newDeletion == nil {
  1261. return true
  1262. }
  1263. return !oldDeletion.Equal(newDeletion)
  1264. }
  1265. // resolveSecretCacheMismatch optionally uses a direct API read when the partial
  1266. // and full secret caches disagree. It returns the secret to continue with,
  1267. // whether the caches should still be treated as out of sync, and any read error.
  1268. func (r *Reconciler) resolveSecretCacheMismatch(ctx context.Context, key client.ObjectKey, secretPartial *metav1.PartialObjectMetadata, existingSecret *v1.Secret) (*v1.Secret, bool, error) {
  1269. if secretPartial.UID == existingSecret.UID && secretPartial.ResourceVersion == existingSecret.ResourceVersion {
  1270. return existingSecret, false, nil
  1271. }
  1272. if !r.EnableSecretAPIReadOnCacheMismatch {
  1273. return nil, true, nil
  1274. }
  1275. authoritativeSecret := &v1.Secret{}
  1276. secretReader := r.APIReader
  1277. if secretReader == nil {
  1278. secretReader = r.SecretClient
  1279. }
  1280. err := secretReader.Get(ctx, key, authoritativeSecret)
  1281. if err != nil && !apierrors.IsNotFound(err) {
  1282. return nil, false, err
  1283. }
  1284. if secretPartial.UID != authoritativeSecret.UID || secretPartial.ResourceVersion != authoritativeSecret.ResourceVersion {
  1285. return nil, true, nil
  1286. }
  1287. return authoritativeSecret, false, nil
  1288. }
  1289. func (r *Reconciler) findObjectsForSecret(ctx context.Context, secret client.Object) []reconcile.Request {
  1290. externalSecretsList := &esv1.ExternalSecretList{}
  1291. listOps := &client.ListOptions{
  1292. FieldSelector: fields.OneTermEqualSelector(indexESTargetSecretNameField, secret.GetName()),
  1293. Namespace: secret.GetNamespace(),
  1294. }
  1295. err := r.List(ctx, externalSecretsList, listOps)
  1296. if err != nil {
  1297. return []reconcile.Request{}
  1298. }
  1299. requests := make([]reconcile.Request, len(externalSecretsList.Items))
  1300. for i := range externalSecretsList.Items {
  1301. requests[i] = reconcile.Request{
  1302. NamespacedName: types.NamespacedName{
  1303. Name: externalSecretsList.Items[i].GetName(),
  1304. Namespace: externalSecretsList.Items[i].GetNamespace(),
  1305. },
  1306. }
  1307. }
  1308. return requests
  1309. }