k8s.io/kubernetes@v1.29.3/test/e2e/framework/rc/rc_utils.go (about)

     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 rc
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/onsi/ginkgo/v2"
    24  
    25  	v1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime/schema"
    28  	clientset "k8s.io/client-go/kubernetes"
    29  	scaleclient "k8s.io/client-go/scale"
    30  	e2edebug "k8s.io/kubernetes/test/e2e/framework/debug"
    31  	e2ekubectl "k8s.io/kubernetes/test/e2e/framework/kubectl"
    32  	e2eresource "k8s.io/kubernetes/test/e2e/framework/resource"
    33  	testutils "k8s.io/kubernetes/test/utils"
    34  	"k8s.io/utils/pointer"
    35  )
    36  
    37  // ByNameContainer returns a ReplicationController with specified name and container
    38  func ByNameContainer(name string, replicas int32, labels map[string]string, c v1.Container,
    39  	gracePeriod *int64) *v1.ReplicationController {
    40  
    41  	zeroGracePeriod := int64(0)
    42  
    43  	// Add "name": name to the labels, overwriting if it exists.
    44  	labels["name"] = name
    45  	if gracePeriod == nil {
    46  		gracePeriod = &zeroGracePeriod
    47  	}
    48  	return &v1.ReplicationController{
    49  		TypeMeta: metav1.TypeMeta{
    50  			Kind:       "ReplicationController",
    51  			APIVersion: "v1",
    52  		},
    53  		ObjectMeta: metav1.ObjectMeta{
    54  			Name: name,
    55  		},
    56  		Spec: v1.ReplicationControllerSpec{
    57  			Replicas: pointer.Int32(replicas),
    58  			Selector: map[string]string{
    59  				"name": name,
    60  			},
    61  			Template: &v1.PodTemplateSpec{
    62  				ObjectMeta: metav1.ObjectMeta{
    63  					Labels: labels,
    64  				},
    65  				Spec: v1.PodSpec{
    66  					Containers:                    []v1.Container{c},
    67  					TerminationGracePeriodSeconds: gracePeriod,
    68  				},
    69  			},
    70  		},
    71  	}
    72  }
    73  
    74  // DeleteRCAndWaitForGC deletes only the Replication Controller and waits for GC to delete the pods.
    75  func DeleteRCAndWaitForGC(ctx context.Context, c clientset.Interface, ns, name string) error {
    76  	// TODO (pohly): context support
    77  	return e2eresource.DeleteResourceAndWaitForGC(ctx, c, schema.GroupKind{Kind: "ReplicationController"}, ns, name)
    78  }
    79  
    80  // ScaleRC scales Replication Controller to be desired size.
    81  func ScaleRC(ctx context.Context, clientset clientset.Interface, scalesGetter scaleclient.ScalesGetter, ns, name string, size uint, wait bool) error {
    82  	return e2eresource.ScaleResource(ctx, clientset, scalesGetter, ns, name, size, wait, schema.GroupKind{Kind: "ReplicationController"}, v1.SchemeGroupVersion.WithResource("replicationcontrollers"))
    83  }
    84  
    85  // RunRC Launches (and verifies correctness) of a Replication Controller
    86  // and will wait for all pods it spawns to become "Running".
    87  func RunRC(ctx context.Context, config testutils.RCConfig) error {
    88  	ginkgo.By(fmt.Sprintf("creating replication controller %s in namespace %s", config.Name, config.Namespace))
    89  	config.NodeDumpFunc = e2edebug.DumpNodeDebugInfo
    90  	config.ContainerDumpFunc = e2ekubectl.LogFailedContainers
    91  	return testutils.RunRC(ctx, config)
    92  }