secretsink_controller.go 4.0 KB

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