123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- /*
- Copyright 2017 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package deployment
- import (
- "context"
- "fmt"
- "time"
- "github.com/onsi/ginkgo"
- apps "k8s.io/api/apps/v1"
- v1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/util/uuid"
- "k8s.io/apimachinery/pkg/util/wait"
- "k8s.io/apimachinery/pkg/watch"
- clientset "k8s.io/client-go/kubernetes"
- watchtools "k8s.io/client-go/tools/watch"
- deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
- "k8s.io/kubernetes/test/e2e/framework"
- e2elog "k8s.io/kubernetes/test/e2e/framework/log"
- testutils "k8s.io/kubernetes/test/utils"
- imageutils "k8s.io/kubernetes/test/utils/image"
- )
- // UpdateDeploymentWithRetries updates the specified deployment with retries.
- func UpdateDeploymentWithRetries(c clientset.Interface, namespace, name string, applyUpdate testutils.UpdateDeploymentFunc) (*apps.Deployment, error) {
- return testutils.UpdateDeploymentWithRetries(c, namespace, name, applyUpdate, e2elog.Logf, poll, pollShortTimeout)
- }
- // CheckDeploymentRevisionAndImage checks if the input deployment's and its new replica set's revision and image are as expected.
- func CheckDeploymentRevisionAndImage(c clientset.Interface, ns, deploymentName, revision, image string) error {
- return testutils.CheckDeploymentRevisionAndImage(c, ns, deploymentName, revision, image)
- }
- // WatchRecreateDeployment watches Recreate deployments and ensures no new pods will run at the same time with
- // old pods.
- func WatchRecreateDeployment(c clientset.Interface, d *apps.Deployment) error {
- if d.Spec.Strategy.Type != apps.RecreateDeploymentStrategyType {
- return fmt.Errorf("deployment %q does not use a Recreate strategy: %s", d.Name, d.Spec.Strategy.Type)
- }
- w, err := c.AppsV1().Deployments(d.Namespace).Watch(metav1.SingleObject(metav1.ObjectMeta{Name: d.Name, ResourceVersion: d.ResourceVersion}))
- if err != nil {
- return err
- }
- status := d.Status
- condition := func(event watch.Event) (bool, error) {
- d := event.Object.(*apps.Deployment)
- status = d.Status
- if d.Status.UpdatedReplicas > 0 && d.Status.Replicas != d.Status.UpdatedReplicas {
- _, allOldRSs, err := deploymentutil.GetOldReplicaSets(d, c.AppsV1())
- newRS, nerr := deploymentutil.GetNewReplicaSet(d, c.AppsV1())
- if err == nil && nerr == nil {
- e2elog.Logf("%+v", d)
- logReplicaSetsOfDeployment(d, allOldRSs, newRS)
- logPodsOfDeployment(c, d, append(allOldRSs, newRS))
- }
- return false, fmt.Errorf("deployment %q is running new pods alongside old pods: %#v", d.Name, status)
- }
- return *(d.Spec.Replicas) == d.Status.Replicas &&
- *(d.Spec.Replicas) == d.Status.UpdatedReplicas &&
- d.Generation <= d.Status.ObservedGeneration, nil
- }
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
- defer cancel()
- _, err = watchtools.UntilWithoutRetry(ctx, w, condition)
- if err == wait.ErrWaitTimeout {
- err = fmt.Errorf("deployment %q never completed: %#v", d.Name, status)
- }
- return err
- }
- // NewDeployment returns a deployment spec with the specified argument.
- func NewDeployment(deploymentName string, replicas int32, podLabels map[string]string, imageName, image string, strategyType apps.DeploymentStrategyType) *apps.Deployment {
- zero := int64(0)
- return &apps.Deployment{
- ObjectMeta: metav1.ObjectMeta{
- Name: deploymentName,
- Labels: podLabels,
- },
- Spec: apps.DeploymentSpec{
- Replicas: &replicas,
- Selector: &metav1.LabelSelector{MatchLabels: podLabels},
- Strategy: apps.DeploymentStrategy{
- Type: strategyType,
- },
- Template: v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: podLabels,
- },
- Spec: v1.PodSpec{
- TerminationGracePeriodSeconds: &zero,
- Containers: []v1.Container{
- {
- Name: imageName,
- Image: image,
- SecurityContext: &v1.SecurityContext{},
- },
- },
- },
- },
- },
- }
- }
- // CreateDeployment creates a deployment.
- func CreateDeployment(client clientset.Interface, replicas int32, podLabels map[string]string, nodeSelector map[string]string, namespace string, pvclaims []*v1.PersistentVolumeClaim, command string) (*apps.Deployment, error) {
- deploymentSpec := testDeployment(replicas, podLabels, nodeSelector, namespace, pvclaims, false, command)
- deployment, err := client.AppsV1().Deployments(namespace).Create(deploymentSpec)
- if err != nil {
- return nil, fmt.Errorf("deployment %q Create API error: %v", deploymentSpec.Name, err)
- }
- e2elog.Logf("Waiting deployment %q to complete", deploymentSpec.Name)
- err = WaitForDeploymentComplete(client, deployment)
- if err != nil {
- return nil, fmt.Errorf("deployment %q failed to complete: %v", deploymentSpec.Name, err)
- }
- return deployment, nil
- }
- // GetPodsForDeployment gets pods for the given deployment
- func GetPodsForDeployment(client clientset.Interface, deployment *apps.Deployment) (*v1.PodList, error) {
- replicaSet, err := deploymentutil.GetNewReplicaSet(deployment, client.AppsV1())
- if err != nil {
- return nil, fmt.Errorf("Failed to get new replica set for deployment %q: %v", deployment.Name, err)
- }
- if replicaSet == nil {
- return nil, fmt.Errorf("expected a new replica set for deployment %q, found none", deployment.Name)
- }
- podListFunc := func(namespace string, options metav1.ListOptions) (*v1.PodList, error) {
- return client.CoreV1().Pods(namespace).List(options)
- }
- rsList := []*apps.ReplicaSet{replicaSet}
- podList, err := deploymentutil.ListPods(deployment, rsList, podListFunc)
- if err != nil {
- return nil, fmt.Errorf("Failed to list Pods of Deployment %q: %v", deployment.Name, err)
- }
- return podList, nil
- }
- // RunDeployment runs a delopyment with the specified config.
- func RunDeployment(config testutils.DeploymentConfig) error {
- ginkgo.By(fmt.Sprintf("creating deployment %s in namespace %s", config.Name, config.Namespace))
- config.NodeDumpFunc = framework.DumpNodeDebugInfo
- config.ContainerDumpFunc = framework.LogFailedContainers
- return testutils.RunDeployment(config)
- }
- // testDeployment creates a deployment definition based on the namespace. The deployment references the PVC's
- // name. A slice of BASH commands can be supplied as args to be run by the pod
- func testDeployment(replicas int32, podLabels map[string]string, nodeSelector map[string]string, namespace string, pvclaims []*v1.PersistentVolumeClaim, isPrivileged bool, command string) *apps.Deployment {
- if len(command) == 0 {
- command = "trap exit TERM; while true; do sleep 1; done"
- }
- zero := int64(0)
- deploymentName := "deployment-" + string(uuid.NewUUID())
- deploymentSpec := &apps.Deployment{
- ObjectMeta: metav1.ObjectMeta{
- Name: deploymentName,
- Namespace: namespace,
- },
- Spec: apps.DeploymentSpec{
- Replicas: &replicas,
- Selector: &metav1.LabelSelector{
- MatchLabels: podLabels,
- },
- Template: v1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{
- Labels: podLabels,
- },
- Spec: v1.PodSpec{
- TerminationGracePeriodSeconds: &zero,
- Containers: []v1.Container{
- {
- Name: "write-pod",
- Image: imageutils.GetE2EImage(imageutils.BusyBox),
- Command: []string{"/bin/sh"},
- Args: []string{"-c", command},
- SecurityContext: &v1.SecurityContext{
- Privileged: &isPrivileged,
- },
- },
- },
- RestartPolicy: v1.RestartPolicyAlways,
- },
- },
- },
- }
- var volumeMounts = make([]v1.VolumeMount, len(pvclaims))
- var volumes = make([]v1.Volume, len(pvclaims))
- for index, pvclaim := range pvclaims {
- volumename := fmt.Sprintf("volume%v", index+1)
- volumeMounts[index] = v1.VolumeMount{Name: volumename, MountPath: "/mnt/" + volumename}
- volumes[index] = v1.Volume{Name: volumename, VolumeSource: v1.VolumeSource{PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ClaimName: pvclaim.Name, ReadOnly: false}}}
- }
- deploymentSpec.Spec.Template.Spec.Containers[0].VolumeMounts = volumeMounts
- deploymentSpec.Spec.Template.Spec.Volumes = volumes
- if nodeSelector != nil {
- deploymentSpec.Spec.Template.Spec.NodeSelector = nodeSelector
- }
- return deploymentSpec
- }
|