metadata.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 metadata
  13. import (
  14. "fmt"
  15. apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  16. "sigs.k8s.io/yaml"
  17. )
  18. const (
  19. APIVersion = "kubernetes.external-secrets.io/v1alpha1"
  20. Kind = "PushSecretMetadata"
  21. )
  22. type PushSecretMetadata[T any] struct {
  23. Kind string `json:"kind"`
  24. APIVersion string `json:"apiVersion"`
  25. Spec T `json:"spec,omitempty"`
  26. }
  27. // ParseMetadataParameters parses metadata with an arbitrary Spec.
  28. func ParseMetadataParameters[T any](data *apiextensionsv1.JSON) (*PushSecretMetadata[T], error) {
  29. if data == nil {
  30. return nil, nil
  31. }
  32. var metadata PushSecretMetadata[T]
  33. err := yaml.Unmarshal(data.Raw, &metadata, yaml.DisallowUnknownFields)
  34. if err != nil {
  35. return nil, fmt.Errorf("failed to parse %s %s: %w", APIVersion, Kind, err)
  36. }
  37. if metadata.APIVersion != APIVersion {
  38. return nil, fmt.Errorf("unexpected apiVersion %q, expected %q", metadata.APIVersion, APIVersion)
  39. }
  40. if metadata.Kind != Kind {
  41. return nil, fmt.Errorf("unexpected kind %q, expected %q", metadata.Kind, Kind)
  42. }
  43. return &metadata, nil
  44. }