secretsink_controller.go 4.0 KB

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