secretsink_controller.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package secretsink
  13. import (
  14. "context"
  15. "fmt"
  16. "time"
  17. "github.com/go-logr/logr"
  18. v1 "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apimachinery/pkg/types"
  23. "k8s.io/client-go/tools/record"
  24. ctrl "sigs.k8s.io/controller-runtime"
  25. "sigs.k8s.io/controller-runtime/pkg/client"
  26. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  27. v1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  28. )
  29. const (
  30. errFailedGetSecret = "could not get source secret"
  31. errPatchStatus = "error merging"
  32. errGetSecretStore = "could not get SecretStore %q, %w"
  33. errGetClusterSecretStore = "could not get ClusterSecretStore %q, %w"
  34. )
  35. type Reconciler struct {
  36. client.Client
  37. Log logr.Logger
  38. Scheme *runtime.Scheme
  39. recorder record.EventRecorder
  40. RequeueInterval time.Duration
  41. ControllerClass string
  42. }
  43. func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  44. log := r.Log.WithValues("secretsink", req.NamespacedName)
  45. var ss esapi.SecretSink
  46. err := r.Get(ctx, req.NamespacedName, &ss)
  47. if apierrors.IsNotFound(err) {
  48. return ctrl.Result{}, nil
  49. } else if err != nil {
  50. log.Error(err, "unable to get SecretSink")
  51. return ctrl.Result{}, fmt.Errorf("get resource: %w", err)
  52. }
  53. p := client.MergeFrom(ss.DeepCopy())
  54. defer func() {
  55. err := r.Client.Status().Patch(ctx, &ss, p)
  56. if err != nil {
  57. log.Error(err, errPatchStatus)
  58. }
  59. }()
  60. _, err = r.GetSecret(ctx, ss)
  61. if err != nil {
  62. cond := NewSecretSinkCondition(esapi.SecretSinkReady, v1.ConditionFalse, "SecretSyncFailed", errFailedGetSecret)
  63. ss = SetSecretSinkCondition(ss, *cond)
  64. }
  65. _, err = r.GetSecretStore(ctx, ss)
  66. if err != nil {
  67. cond := NewSecretSinkCondition(esapi.SecretSinkReady, v1.ConditionFalse, "SecretSyncFailed", errGetSecretStore)
  68. ss = SetSecretSinkCondition(ss, *cond)
  69. }
  70. cond := NewSecretSinkCondition(esapi.SecretSinkReady, v1.ConditionTrue, "SecretSynced", "SecretSink synced successfully")
  71. ss = SetSecretSinkCondition(ss, *cond)
  72. // Set status for SecretSink
  73. return ctrl.Result{}, nil
  74. }
  75. func (r *Reconciler) GetSecret(ctx context.Context, ss esapi.SecretSink) (*v1.Secret, error) {
  76. secretName := types.NamespacedName{Name: ss.Spec.Selector.Secret.Name, Namespace: ss.Namespace}
  77. secret := &v1.Secret{}
  78. err := r.Client.Get(ctx, secretName, secret)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return secret, nil
  83. }
  84. func (r *Reconciler) GetSecretStore(ctx context.Context, ss esapi.SecretSink) ([]v1beta1.GenericStore, error) {
  85. stores := make([]v1beta1.GenericStore, 0)
  86. for _, refStore := range ss.Spec.SecretStoreRefs {
  87. ref := types.NamespacedName{
  88. Name: refStore.Name,
  89. }
  90. if refStore.Kind == v1beta1.ClusterSecretStoreKind {
  91. var store v1beta1.ClusterSecretStore
  92. err := r.Get(ctx, ref, &store)
  93. if err != nil {
  94. return nil, fmt.Errorf(errGetClusterSecretStore, ref.Name, err)
  95. }
  96. stores = append(stores, &store)
  97. } else {
  98. ref.Namespace = ss.Namespace
  99. var store v1beta1.SecretStore
  100. err := r.Get(ctx, ref, &store)
  101. if err != nil {
  102. return nil, fmt.Errorf(errGetSecretStore, ref.Name, err)
  103. }
  104. stores = append(stores, &store)
  105. }
  106. }
  107. return stores, nil
  108. }
  109. func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
  110. r.recorder = mgr.GetEventRecorderFor("secret-sink")
  111. return ctrl.NewControllerManagedBy(mgr).
  112. For(&esapi.SecretSink{}).
  113. Complete(r)
  114. }
  115. func NewSecretSinkCondition(condType esapi.SecretSinkConditionType, status v1.ConditionStatus, reason, message string) *esapi.SecretSinkStatusCondition {
  116. return &esapi.SecretSinkStatusCondition{
  117. Type: condType,
  118. Status: status,
  119. LastTransitionTime: metav1.Now(),
  120. Reason: reason,
  121. Message: message,
  122. }
  123. }
  124. func SetSecretSinkCondition(gs esapi.SecretSink, condition esapi.SecretSinkStatusCondition) esapi.SecretSink {
  125. status := gs.Status
  126. currentCond := GetSecretSinkCondition(status, condition.Type)
  127. if currentCond != nil && currentCond.Status == condition.Status &&
  128. currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {
  129. return gs
  130. }
  131. // Do not update lastTransitionTime if the status of the condition doesn't change.
  132. if currentCond != nil && currentCond.Status == condition.Status {
  133. condition.LastTransitionTime = currentCond.LastTransitionTime
  134. }
  135. status.Conditions = append(filterOutCondition(status.Conditions, condition.Type), condition)
  136. gs.Status = status
  137. return gs
  138. }
  139. // filterOutCondition returns an empty set of conditions with the provided type.
  140. func filterOutCondition(conditions []esapi.SecretSinkStatusCondition, condType esapi.SecretSinkConditionType) []esapi.SecretSinkStatusCondition {
  141. newConditions := make([]esapi.SecretSinkStatusCondition, 0, len(conditions))
  142. for _, c := range conditions {
  143. if c.Type == condType {
  144. continue
  145. }
  146. newConditions = append(newConditions, c)
  147. }
  148. return newConditions
  149. }
  150. // GetSecretStoreCondition returns the condition with the provided type.
  151. func GetSecretSinkCondition(status esapi.SecretSinkStatus, condType esapi.SecretSinkConditionType) *esapi.SecretSinkStatusCondition {
  152. for i := range status.Conditions {
  153. c := status.Conditions[i]
  154. if c.Type == condType {
  155. return &c
  156. }
  157. }
  158. return nil
  159. }