json6902.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Copyright 2019 The Kubernetes 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. http://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 kustomize contains helpers for working with embedded kustomize commands
  14. package kustomize
  15. import (
  16. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  17. "sigs.k8s.io/kustomize/pkg/ifc"
  18. "sigs.k8s.io/kustomize/pkg/patch"
  19. )
  20. // json6902 represents a json6902 patch
  21. type json6902 struct {
  22. // Target refers to a Kubernetes object that the json patch will be applied to
  23. *patch.Target
  24. // Patch contain the json patch as a string
  25. Patch string
  26. }
  27. // json6902Slice is a slice of json6902 patches.
  28. type json6902Slice []*json6902
  29. // newJSON6902FromFile returns a json6902 patch from a file
  30. func newJSON6902FromFile(f patch.Json6902, ldr ifc.Loader, file string) (*json6902, error) {
  31. patch, err := ldr.Load(file)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &json6902{
  36. Target: f.Target,
  37. Patch: string(patch),
  38. }, nil
  39. }
  40. // filterByResource returns all the json6902 patches in the json6902Slice corresponding to a given resource
  41. func (s *json6902Slice) filterByResource(r *unstructured.Unstructured) json6902Slice {
  42. var result json6902Slice
  43. for _, p := range *s {
  44. if p.Group == r.GroupVersionKind().Group &&
  45. p.Version == r.GroupVersionKind().Version &&
  46. p.Kind == r.GroupVersionKind().Kind &&
  47. p.Namespace == r.GetNamespace() &&
  48. p.Name == r.GetName() {
  49. result = append(result, p)
  50. }
  51. }
  52. return result
  53. }