externalsecret_controller.go 55 KB

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