...

Source file src/k8s.io/kubectl/pkg/polymorphichelpers/updatepodspec.go

Documentation: k8s.io/kubectl/pkg/polymorphichelpers

     1  /*
     2  Copyright 2018 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 polymorphichelpers
    18  
    19  import (
    20  	"fmt"
    21  
    22  	appsv1 "k8s.io/api/apps/v1"
    23  	appsv1beta1 "k8s.io/api/apps/v1beta1"
    24  	appsv1beta2 "k8s.io/api/apps/v1beta2"
    25  	batchv1 "k8s.io/api/batch/v1"
    26  	batchv1beta1 "k8s.io/api/batch/v1beta1"
    27  	"k8s.io/api/core/v1"
    28  	extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
    29  	"k8s.io/apimachinery/pkg/runtime"
    30  )
    31  
    32  func updatePodSpecForObject(obj runtime.Object, fn func(*v1.PodSpec) error) (bool, error) {
    33  	switch t := obj.(type) {
    34  	case *v1.Pod:
    35  		return true, fn(&t.Spec)
    36  		// ReplicationController
    37  	case *v1.ReplicationController:
    38  		if t.Spec.Template == nil {
    39  			t.Spec.Template = &v1.PodTemplateSpec{}
    40  		}
    41  		return true, fn(&t.Spec.Template.Spec)
    42  
    43  		// Deployment
    44  	case *extensionsv1beta1.Deployment:
    45  		return true, fn(&t.Spec.Template.Spec)
    46  	case *appsv1beta1.Deployment:
    47  		return true, fn(&t.Spec.Template.Spec)
    48  	case *appsv1beta2.Deployment:
    49  		return true, fn(&t.Spec.Template.Spec)
    50  	case *appsv1.Deployment:
    51  		return true, fn(&t.Spec.Template.Spec)
    52  
    53  		// DaemonSet
    54  	case *extensionsv1beta1.DaemonSet:
    55  		return true, fn(&t.Spec.Template.Spec)
    56  	case *appsv1beta2.DaemonSet:
    57  		return true, fn(&t.Spec.Template.Spec)
    58  	case *appsv1.DaemonSet:
    59  		return true, fn(&t.Spec.Template.Spec)
    60  
    61  		// ReplicaSet
    62  	case *extensionsv1beta1.ReplicaSet:
    63  		return true, fn(&t.Spec.Template.Spec)
    64  	case *appsv1beta2.ReplicaSet:
    65  		return true, fn(&t.Spec.Template.Spec)
    66  	case *appsv1.ReplicaSet:
    67  		return true, fn(&t.Spec.Template.Spec)
    68  
    69  		// StatefulSet
    70  	case *appsv1beta1.StatefulSet:
    71  		return true, fn(&t.Spec.Template.Spec)
    72  	case *appsv1beta2.StatefulSet:
    73  		return true, fn(&t.Spec.Template.Spec)
    74  	case *appsv1.StatefulSet:
    75  		return true, fn(&t.Spec.Template.Spec)
    76  
    77  		// Job
    78  	case *batchv1.Job:
    79  		return true, fn(&t.Spec.Template.Spec)
    80  
    81  		// CronJob
    82  	case *batchv1beta1.CronJob:
    83  		return true, fn(&t.Spec.JobTemplate.Spec.Template.Spec)
    84  	case *batchv1.CronJob:
    85  		return true, fn(&t.Spec.JobTemplate.Spec.Template.Spec)
    86  
    87  	default:
    88  		return false, fmt.Errorf("the object is not a pod or does not have a pod template: %T", t)
    89  	}
    90  }
    91  

View as plain text