...

Source file src/k8s.io/kubernetes/test/e2e/framework/resource/runtimeobj.go

Documentation: k8s.io/kubernetes/test/e2e/framework/resource

     1  /*
     2  Copyright 2019 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 resource
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	appsv1 "k8s.io/api/apps/v1"
    24  	autoscalingv1 "k8s.io/api/autoscaling/v1"
    25  	batchv1 "k8s.io/api/batch/v1"
    26  	v1 "k8s.io/api/core/v1"
    27  	extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/labels"
    30  	"k8s.io/apimachinery/pkg/runtime"
    31  	"k8s.io/apimachinery/pkg/runtime/schema"
    32  	clientset "k8s.io/client-go/kubernetes"
    33  )
    34  
    35  var (
    36  	kindReplicationController = schema.GroupKind{Kind: "ReplicationController"}
    37  	kindExtensionsReplicaSet  = schema.GroupKind{Group: "extensions", Kind: "ReplicaSet"}
    38  	kindAppsReplicaSet        = schema.GroupKind{Group: "apps", Kind: "ReplicaSet"}
    39  	kindExtensionsDeployment  = schema.GroupKind{Group: "extensions", Kind: "Deployment"}
    40  	kindAppsDeployment        = schema.GroupKind{Group: "apps", Kind: "Deployment"}
    41  	kindExtensionsDaemonSet   = schema.GroupKind{Group: "extensions", Kind: "DaemonSet"}
    42  	kindBatchJob              = schema.GroupKind{Group: "batch", Kind: "Job"}
    43  )
    44  
    45  // GetRuntimeObjectForKind returns a runtime.Object based on its GroupKind,
    46  // namespace and name.
    47  func GetRuntimeObjectForKind(ctx context.Context, c clientset.Interface, kind schema.GroupKind, ns, name string) (runtime.Object, error) {
    48  	switch kind {
    49  	case kindReplicationController:
    50  		return c.CoreV1().ReplicationControllers(ns).Get(ctx, name, metav1.GetOptions{})
    51  	case kindExtensionsReplicaSet, kindAppsReplicaSet:
    52  		return c.AppsV1().ReplicaSets(ns).Get(ctx, name, metav1.GetOptions{})
    53  	case kindExtensionsDeployment, kindAppsDeployment:
    54  		return c.AppsV1().Deployments(ns).Get(ctx, name, metav1.GetOptions{})
    55  	case kindExtensionsDaemonSet:
    56  		return c.AppsV1().DaemonSets(ns).Get(ctx, name, metav1.GetOptions{})
    57  	case kindBatchJob:
    58  		return c.BatchV1().Jobs(ns).Get(ctx, name, metav1.GetOptions{})
    59  	default:
    60  		return nil, fmt.Errorf("Unsupported kind when getting runtime object: %v", kind)
    61  	}
    62  }
    63  
    64  // GetSelectorFromRuntimeObject returns the labels for the given object.
    65  func GetSelectorFromRuntimeObject(obj runtime.Object) (labels.Selector, error) {
    66  	switch typed := obj.(type) {
    67  	case *v1.ReplicationController:
    68  		return labels.SelectorFromSet(typed.Spec.Selector), nil
    69  	case *extensionsv1beta1.ReplicaSet:
    70  		return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
    71  	case *appsv1.ReplicaSet:
    72  		return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
    73  	case *extensionsv1beta1.Deployment:
    74  		return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
    75  	case *appsv1.Deployment:
    76  		return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
    77  	case *extensionsv1beta1.DaemonSet:
    78  		return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
    79  	case *appsv1.DaemonSet:
    80  		return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
    81  	case *batchv1.Job:
    82  		return metav1.LabelSelectorAsSelector(typed.Spec.Selector)
    83  	case *autoscalingv1.Scale:
    84  		selector, err := metav1.ParseToLabelSelector(typed.Status.Selector)
    85  		if err != nil {
    86  			return nil, fmt.Errorf("Parsing selector for: %v encountered an error: %w", obj, err)
    87  		}
    88  		return metav1.LabelSelectorAsSelector(selector)
    89  	default:
    90  		return nil, fmt.Errorf("Unsupported kind when getting selector: %v", obj)
    91  	}
    92  }
    93  
    94  // GetReplicasFromRuntimeObject returns the number of replicas for the given
    95  // object.
    96  func GetReplicasFromRuntimeObject(obj runtime.Object) (int32, error) {
    97  	switch typed := obj.(type) {
    98  	case *v1.ReplicationController:
    99  		if typed.Spec.Replicas != nil {
   100  			return *typed.Spec.Replicas, nil
   101  		}
   102  		return 0, nil
   103  	case *extensionsv1beta1.ReplicaSet:
   104  		if typed.Spec.Replicas != nil {
   105  			return *typed.Spec.Replicas, nil
   106  		}
   107  		return 0, nil
   108  	case *appsv1.ReplicaSet:
   109  		if typed.Spec.Replicas != nil {
   110  			return *typed.Spec.Replicas, nil
   111  		}
   112  		return 0, nil
   113  	case *extensionsv1beta1.Deployment:
   114  		if typed.Spec.Replicas != nil {
   115  			return *typed.Spec.Replicas, nil
   116  		}
   117  		return 0, nil
   118  	case *appsv1.Deployment:
   119  		if typed.Spec.Replicas != nil {
   120  			return *typed.Spec.Replicas, nil
   121  		}
   122  		return 0, nil
   123  	case *extensionsv1beta1.DaemonSet:
   124  		return 0, nil
   125  	case *appsv1.DaemonSet:
   126  		return 0, nil
   127  	case *batchv1.Job:
   128  		// TODO: currently we use pause pods so that's OK. When we'll want to switch to Pods
   129  		// that actually finish we need a better way to do this.
   130  		if typed.Spec.Parallelism != nil {
   131  			return *typed.Spec.Parallelism, nil
   132  		}
   133  		return 0, nil
   134  	case *autoscalingv1.Scale:
   135  		return typed.Status.Replicas, nil
   136  	default:
   137  		return -1, fmt.Errorf("Unsupported kind when getting number of replicas: %v", obj)
   138  	}
   139  }
   140  

View as plain text