github.com/interconnectedcloud/qdr-operator@v0.0.0-20210826174505-576d2b33dac7/test/e2e/framework/deployment.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package framework
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	e2elog "github.com/interconnectedcloud/qdr-operator/test/e2e/framework/log"
    22  	appsv1 "k8s.io/api/apps/v1"
    23  	corev1 "k8s.io/api/core/v1"
    24  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/apimachinery/pkg/util/wait"
    28  	"k8s.io/client-go/kubernetes"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  )
    31  
    32  func (f *Framework) GetDeployment(name string) (*appsv1.Deployment, error) {
    33  	return f.KubeClient.AppsV1().Deployments(f.Namespace).Get(name, metav1.GetOptions{})
    34  }
    35  
    36  func (f *Framework) ListPodsForDeployment(deployment *appsv1.Deployment) (*corev1.PodList, error) {
    37  	selector, err := metav1.LabelSelectorAsSelector(deployment.Spec.Selector)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	listOps := metav1.ListOptions{LabelSelector: selector.String()}
    42  	return f.KubeClient.CoreV1().Pods(f.Namespace).List(listOps)
    43  }
    44  
    45  func WaitForDeployment(kubeclient kubernetes.Interface, namespace, name string, replicas int, retryInterval, timeout time.Duration) error {
    46  	err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
    47  		deployment, err := kubeclient.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{IncludeUninitialized: true})
    48  		if err != nil {
    49  			if apierrors.IsNotFound(err) {
    50  				e2elog.Logf("Waiting for availability of %s deployment\n", name)
    51  				return false, nil
    52  			}
    53  			return false, err
    54  		}
    55  
    56  		// For debugging purposes
    57  		e2elog.Logf("Waiting for full availability of %s deployment (Replicas: %d/%d - Available: %d/%d - Updated: %d/%d - Ready: %d/%d - Unavailable: %d/%d)\n",
    58  			name,
    59  			deployment.Status.Replicas, replicas,
    60  			deployment.Status.AvailableReplicas, replicas,
    61  			deployment.Status.UpdatedReplicas, replicas,
    62  			deployment.Status.ReadyReplicas, replicas,
    63  			deployment.Status.UnavailableReplicas, replicas)
    64  
    65  		totalReplicas := int(deployment.Status.Replicas) == replicas
    66  		available := int(deployment.Status.AvailableReplicas) == replicas
    67  		updated := int(deployment.Status.ReadyReplicas) == replicas
    68  		ready := int(deployment.Status.ReadyReplicas) == replicas
    69  		unavailable := int(deployment.Status.UnavailableReplicas) != 0
    70  
    71  		if totalReplicas && available && updated && ready && !unavailable {
    72  			return true, nil
    73  		}
    74  		return false, nil
    75  	})
    76  	if err != nil {
    77  		return err
    78  	}
    79  	e2elog.Logf("Deployment available (%d/%d)\n", replicas, replicas)
    80  	return nil
    81  }
    82  
    83  func (f *Framework) GetDaemonSet(name string) (*appsv1.DaemonSet, error) {
    84  	return f.KubeClient.AppsV1().DaemonSets(f.Namespace).Get(name, metav1.GetOptions{})
    85  }
    86  
    87  func WaitForDaemonSet(kubeclient kubernetes.Interface, namespace, name string, count int, retryInterval, timeout time.Duration) error {
    88  	err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
    89  		ds, err := kubeclient.AppsV1().DaemonSets(namespace).Get(name, metav1.GetOptions{IncludeUninitialized: true})
    90  		if err != nil {
    91  			if apierrors.IsNotFound(err) {
    92  				e2elog.Logf("Waiting for availability of %s daemon set\n", name)
    93  				return false, nil
    94  			}
    95  			return false, err
    96  		}
    97  
    98  		if int(ds.Status.NumberReady) >= count {
    99  			return true, nil
   100  		}
   101  		e2elog.Logf("Waiting for full availability of %s daemonset (%d/%d)\n", name, ds.Status.NumberReady, count)
   102  		return false, nil
   103  	})
   104  	if err != nil {
   105  		return err
   106  	}
   107  	e2elog.Logf("Daemonset ready (%d)\n", count)
   108  	return nil
   109  }
   110  
   111  func WaitForDeletion(dynclient client.Client, obj runtime.Object, retryInterval, timeout time.Duration) error {
   112  	key, err := client.ObjectKeyFromObject(obj)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	kind := obj.GetObjectKind().GroupVersionKind().Kind
   118  	ctx, cancel := context.WithTimeout(context.Background(), timeout)
   119  	defer cancel()
   120  	err = wait.Poll(retryInterval, timeout, func() (done bool, err error) {
   121  		err = dynclient.Get(ctx, key, obj)
   122  		if apierrors.IsNotFound(err) {
   123  			return true, nil
   124  		}
   125  		if err != nil {
   126  			return false, err
   127  		}
   128  		e2elog.Logf("Waiting for %s %s to be deleted\n", kind, key)
   129  		return false, nil
   130  	})
   131  	if err != nil {
   132  		return err
   133  	}
   134  	e2elog.Logf("%s %s was deleted\n", kind, key)
   135  	return nil
   136  }
   137  
   138  func WaitForDeploymentDeleted(ctx context.Context, kubeclient kubernetes.Interface, namespace, name string) error {
   139  	err := RetryWithContext(ctx, RetryInterval, func() (bool, error) {
   140  		deployment, err := kubeclient.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{IncludeUninitialized: true})
   141  		if err != nil {
   142  			if apierrors.IsNotFound(err) {
   143  				return true, nil
   144  			}
   145  			return false, err
   146  		}
   147  
   148  		// For debugging purposes
   149  		e2elog.Logf("Waiting for deletion of %s deployment (Replicas: %d/0 - Available: %d/0 - Updated: %d/0 - Ready: %d/0 - Unavailable: %d/0)\n",
   150  			name,
   151  			deployment.Status.Replicas,
   152  			deployment.Status.AvailableReplicas,
   153  			deployment.Status.UpdatedReplicas,
   154  			deployment.Status.ReadyReplicas,
   155  			deployment.Status.UnavailableReplicas)
   156  
   157  		return false, nil
   158  	})
   159  	if err != nil {
   160  		return err
   161  	}
   162  	e2elog.Logf("Deployment %s no longer present", name)
   163  	return nil
   164  }