compatibility_store.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 store
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime/schema"
  19. "k8s.io/apimachinery/pkg/types"
  20. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  21. pb "github.com/external-secrets/external-secrets/proto/provider"
  22. )
  23. // CompatibilityStoreToSyntheticStore materializes a v1 SecretStore/ClusterSecretStore payload
  24. // into a GenericStore that existing v1 providers can consume.
  25. func CompatibilityStoreToSyntheticStore(store *pb.CompatibilityStore) (*SyntheticStore, error) {
  26. if store == nil {
  27. return nil, fmt.Errorf("compatibility store is nil")
  28. }
  29. if len(store.GetStoreSpecJson()) == 0 {
  30. return nil, fmt.Errorf("compatibility store spec is empty")
  31. }
  32. spec := &esv1.SecretStoreSpec{}
  33. if err := json.Unmarshal(store.GetStoreSpecJson(), spec); err != nil {
  34. return nil, fmt.Errorf("decode compatibility store spec: %w", err)
  35. }
  36. if spec.Provider == nil {
  37. return nil, fmt.Errorf("compatibility store provider config is required")
  38. }
  39. switch store.GetStoreKind() {
  40. case esv1.SecretStoreKind, esv1.ClusterSecretStoreKind:
  41. default:
  42. return nil, fmt.Errorf("unsupported compatibility store kind %q", store.GetStoreKind())
  43. }
  44. gvk := schema.GroupVersionKind{
  45. Group: esv1.SchemeGroupVersion.Group,
  46. Version: esv1.SchemeGroupVersion.Version,
  47. Kind: store.GetStoreKind(),
  48. }
  49. syntheticStore := &SyntheticStore{
  50. TypeMeta: metav1.TypeMeta{
  51. APIVersion: esv1.SchemeGroupVersion.String(),
  52. Kind: store.GetStoreKind(),
  53. },
  54. ObjectMeta: metav1.ObjectMeta{
  55. Name: store.GetStoreName(),
  56. Namespace: store.GetStoreNamespace(),
  57. UID: types.UID(store.GetStoreUid()),
  58. Generation: store.GetStoreGeneration(),
  59. },
  60. spec: spec,
  61. status: esv1.SecretStoreStatus{},
  62. gvk: gvk,
  63. }
  64. syntheticStore.SetGroupVersionKind(gvk)
  65. return syntheticStore, nil
  66. }