...

Source file src/k8s.io/kubectl/pkg/apps/kind_visitor.go

Documentation: k8s.io/kubectl/pkg/apps

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package apps
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/runtime/schema"
    23  )
    24  
    25  // KindVisitor is used with GroupKindElement to call a particular function depending on the
    26  // Kind of a schema.GroupKind
    27  type KindVisitor interface {
    28  	VisitDaemonSet(kind GroupKindElement)
    29  	VisitDeployment(kind GroupKindElement)
    30  	VisitJob(kind GroupKindElement)
    31  	VisitPod(kind GroupKindElement)
    32  	VisitReplicaSet(kind GroupKindElement)
    33  	VisitReplicationController(kind GroupKindElement)
    34  	VisitStatefulSet(kind GroupKindElement)
    35  	VisitCronJob(kind GroupKindElement)
    36  }
    37  
    38  // GroupKindElement defines a Kubernetes API group elem
    39  type GroupKindElement schema.GroupKind
    40  
    41  // Accept calls the Visit method on visitor that corresponds to elem's Kind
    42  func (elem GroupKindElement) Accept(visitor KindVisitor) error {
    43  	switch {
    44  	case elem.GroupMatch("apps", "extensions") && elem.Kind == "DaemonSet":
    45  		visitor.VisitDaemonSet(elem)
    46  	case elem.GroupMatch("apps", "extensions") && elem.Kind == "Deployment":
    47  		visitor.VisitDeployment(elem)
    48  	case elem.GroupMatch("batch") && elem.Kind == "Job":
    49  		visitor.VisitJob(elem)
    50  	case elem.GroupMatch("", "core") && elem.Kind == "Pod":
    51  		visitor.VisitPod(elem)
    52  	case elem.GroupMatch("apps", "extensions") && elem.Kind == "ReplicaSet":
    53  		visitor.VisitReplicaSet(elem)
    54  	case elem.GroupMatch("", "core") && elem.Kind == "ReplicationController":
    55  		visitor.VisitReplicationController(elem)
    56  	case elem.GroupMatch("apps") && elem.Kind == "StatefulSet":
    57  		visitor.VisitStatefulSet(elem)
    58  	case elem.GroupMatch("batch") && elem.Kind == "CronJob":
    59  		visitor.VisitCronJob(elem)
    60  	default:
    61  		return fmt.Errorf("no visitor method exists for %v", elem)
    62  	}
    63  	return nil
    64  }
    65  
    66  // GroupMatch returns true if and only if elem's group matches one
    67  // of the group arguments
    68  func (elem GroupKindElement) GroupMatch(groups ...string) bool {
    69  	for _, g := range groups {
    70  		if elem.Group == g {
    71  			return true
    72  		}
    73  	}
    74  	return false
    75  }
    76  

View as plain text