secretsink_controller.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. )
  28. const (
  29. errFailedGetSecret = "could not get source secret"
  30. errPatchStatus = "error merging"
  31. )
  32. type Reconciler struct {
  33. client.Client
  34. Log logr.Logger
  35. Scheme *runtime.Scheme
  36. recorder record.EventRecorder
  37. RequeueInterval time.Duration
  38. ControllerClass string
  39. }
  40. func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  41. log := r.Log.WithValues("secretsink", req.NamespacedName)
  42. var ss esapi.SecretSink
  43. err := r.Get(ctx, req.NamespacedName, &ss)
  44. if apierrors.IsNotFound(err) {
  45. return ctrl.Result{}, nil
  46. } else if err != nil {
  47. log.Error(err, "unable to get SecretSink")
  48. return ctrl.Result{}, fmt.Errorf("get resource: %w", err)
  49. }
  50. p := client.MergeFrom(ss.DeepCopy())
  51. defer func() {
  52. err := r.Client.Status().Patch(ctx, &ss, p)
  53. if err != nil {
  54. log.Error(err, errPatchStatus)
  55. }
  56. }()
  57. _, err = r.GetSecret(ctx, ss)
  58. if err != nil {
  59. cond := NewSecretSinkCondition(esapi.SecretSinkReady, v1.ConditionFalse, "SecretSyncFailed", errFailedGetSecret)
  60. ss = SetSecretSinkCondition(ss, *cond)
  61. }
  62. cond := NewSecretSinkCondition(esapi.SecretSinkReady, v1.ConditionTrue, "SecretSynced", "SecretSink synced successfully")
  63. ss = SetSecretSinkCondition(ss, *cond)
  64. // Set status for SecretSink
  65. return ctrl.Result{}, nil
  66. }
  67. func (r *Reconciler) GetSecret(ctx context.Context, ss esapi.SecretSink) (*v1.Secret, error) {
  68. secretName := types.NamespacedName{Name: ss.Spec.Selector.Secret.Name, Namespace: ss.Namespace}
  69. secret := &v1.Secret{}
  70. err := r.Client.Get(ctx, secretName, secret)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return secret, nil
  75. }
  76. func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
  77. r.recorder = mgr.GetEventRecorderFor("secret-sink")
  78. return ctrl.NewControllerManagedBy(mgr).
  79. For(&esapi.SecretSink{}).
  80. Complete(r)
  81. }
  82. func NewSecretSinkCondition(condType esapi.SecretSinkConditionType, status v1.ConditionStatus, reason, message string) *esapi.SecretSinkStatusCondition {
  83. return &esapi.SecretSinkStatusCondition{
  84. Type: condType,
  85. Status: status,
  86. LastTransitionTime: metav1.Now(),
  87. Reason: reason,
  88. Message: message,
  89. }
  90. }
  91. func SetSecretSinkCondition(gs esapi.SecretSink, condition esapi.SecretSinkStatusCondition) esapi.SecretSink {
  92. status := gs.Status
  93. currentCond := GetSecretSinkCondition(status, condition.Type)
  94. if currentCond != nil && currentCond.Status == condition.Status &&
  95. currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {
  96. return gs
  97. }
  98. // Do not update lastTransitionTime if the status of the condition doesn't change.
  99. if currentCond != nil && currentCond.Status == condition.Status {
  100. condition.LastTransitionTime = currentCond.LastTransitionTime
  101. }
  102. status.Conditions = append(filterOutCondition(status.Conditions, condition.Type), condition)
  103. gs.Status = status
  104. return gs
  105. }
  106. // filterOutCondition returns an empty set of conditions with the provided type.
  107. func filterOutCondition(conditions []esapi.SecretSinkStatusCondition, condType esapi.SecretSinkConditionType) []esapi.SecretSinkStatusCondition {
  108. newConditions := make([]esapi.SecretSinkStatusCondition, 0, len(conditions))
  109. for _, c := range conditions {
  110. if c.Type == condType {
  111. continue
  112. }
  113. newConditions = append(newConditions, c)
  114. }
  115. return newConditions
  116. }
  117. // GetSecretStoreCondition returns the condition with the provided type.
  118. func GetSecretSinkCondition(status esapi.SecretSinkStatus, condType esapi.SecretSinkConditionType) *esapi.SecretSinkStatusCondition {
  119. for i := range status.Conditions {
  120. c := status.Conditions[i]
  121. if c.Type == condType {
  122. return &c
  123. }
  124. }
  125. return nil
  126. }