runtime_ref.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright © The ESO Authors
  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 clientmanager
  14. import (
  15. "fmt"
  16. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  17. pb "github.com/external-secrets/external-secrets/proto/provider"
  18. )
  19. func buildProviderReference(store esv1.GenericStore, sourceNamespace string) (*pb.ProviderReference, error) {
  20. spec := store.GetSpec()
  21. if spec == nil || spec.ProviderRef == nil {
  22. return nil, fmt.Errorf("%s spec.providerRef is required when spec.runtimeRef is set", store.GetKind())
  23. }
  24. namespace, err := resolveProviderRefNamespace(store, sourceNamespace)
  25. if err != nil {
  26. return nil, err
  27. }
  28. return &pb.ProviderReference{
  29. ApiVersion: spec.ProviderRef.APIVersion,
  30. Kind: spec.ProviderRef.Kind,
  31. Name: spec.ProviderRef.Name,
  32. Namespace: namespace,
  33. StoreRefKind: store.GetKind(),
  34. }, nil
  35. }
  36. func resolveProviderRefNamespace(store esv1.GenericStore, sourceNamespace string) (string, error) {
  37. ref := store.GetSpec().ProviderRef
  38. if store.GetKind() == esv1.SecretStoreKind {
  39. if ref.Namespace == "" {
  40. return store.GetNamespace(), nil
  41. }
  42. return ref.Namespace, nil
  43. }
  44. if ref.Namespace != "" {
  45. return ref.Namespace, nil
  46. }
  47. if sourceNamespace == "" {
  48. return "", fmt.Errorf("%s spec.providerRef.namespace requires a caller namespace", store.GetKind())
  49. }
  50. return sourceNamespace, nil
  51. }