kind_visitor.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. Copyright 2017 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 apps
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/runtime/schema"
  17. )
  18. // KindVisitor is used with GroupKindElement to call a particular function depending on the
  19. // Kind of a schema.GroupKind
  20. type KindVisitor interface {
  21. VisitDaemonSet(kind GroupKindElement)
  22. VisitDeployment(kind GroupKindElement)
  23. VisitJob(kind GroupKindElement)
  24. VisitPod(kind GroupKindElement)
  25. VisitReplicaSet(kind GroupKindElement)
  26. VisitReplicationController(kind GroupKindElement)
  27. VisitStatefulSet(kind GroupKindElement)
  28. VisitCronJob(kind GroupKindElement)
  29. }
  30. // GroupKindElement defines a Kubernetes API group elem
  31. type GroupKindElement schema.GroupKind
  32. // Accept calls the Visit method on visitor that corresponds to elem's Kind
  33. func (elem GroupKindElement) Accept(visitor KindVisitor) error {
  34. switch {
  35. case elem.GroupMatch("apps", "extensions") && elem.Kind == "DaemonSet":
  36. visitor.VisitDaemonSet(elem)
  37. case elem.GroupMatch("apps", "extensions") && elem.Kind == "Deployment":
  38. visitor.VisitDeployment(elem)
  39. case elem.GroupMatch("batch") && elem.Kind == "Job":
  40. visitor.VisitJob(elem)
  41. case elem.GroupMatch("", "core") && elem.Kind == "Pod":
  42. visitor.VisitPod(elem)
  43. case elem.GroupMatch("apps", "extensions") && elem.Kind == "ReplicaSet":
  44. visitor.VisitReplicaSet(elem)
  45. case elem.GroupMatch("", "core") && elem.Kind == "ReplicationController":
  46. visitor.VisitReplicationController(elem)
  47. case elem.GroupMatch("apps") && elem.Kind == "StatefulSet":
  48. visitor.VisitStatefulSet(elem)
  49. case elem.GroupMatch("batch") && elem.Kind == "CronJob":
  50. visitor.VisitCronJob(elem)
  51. default:
  52. return fmt.Errorf("no visitor method exists for %v", elem)
  53. }
  54. return nil
  55. }
  56. // GroupMatch returns true if and only if elem's group matches one
  57. // of the group arguments
  58. func (elem GroupKindElement) GroupMatch(groups ...string) bool {
  59. for _, g := range groups {
  60. if elem.Group == g {
  61. return true
  62. }
  63. }
  64. return false
  65. }