util.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 ctrlutil provides utility functions for controllers.
  14. package ctrlutil
  15. import (
  16. "fmt"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "github.com/external-secrets/external-secrets/pkg/esutils"
  19. )
  20. // GetResourceVersion returns a string representing the resource version of the object.
  21. // It is a combination of the generation and a hash of the labels and annotations.
  22. func GetResourceVersion(meta metav1.ObjectMeta) string {
  23. return fmt.Sprintf("%d-%s", meta.GetGeneration(), HashMeta(meta))
  24. }
  25. // HashMeta returns a hash of the metadata's labels and annotations.
  26. func HashMeta(m metav1.ObjectMeta) string {
  27. type meta struct {
  28. annotations map[string]string
  29. labels map[string]string
  30. }
  31. return esutils.ObjectHash(meta{
  32. annotations: m.Annotations,
  33. labels: m.Labels,
  34. })
  35. }