github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/k8s/replicaset.go (about)

     1  package k8s
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	appsv1 "k8s.io/api/apps/v1"
     8  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  
    10  	"github.com/gruntwork-io/terratest/modules/testing"
    11  )
    12  
    13  // ListReplicaSets will look for replicasets in the given namespace that match the given filters and return them. This will
    14  // fail the test if there is an error.
    15  func ListReplicaSets(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) []appsv1.ReplicaSet {
    16  	replicaset, err := ListReplicaSetsE(t, options, filters)
    17  	require.NoError(t, err)
    18  	return replicaset
    19  }
    20  
    21  // ListReplicaSetsE will look for replicasets in the given namespace that match the given filters and return them.
    22  func ListReplicaSetsE(t testing.TestingT, options *KubectlOptions, filters metav1.ListOptions) ([]appsv1.ReplicaSet, error) {
    23  	clientset, err := GetKubernetesClientFromOptionsE(t, options)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  	replicasets, err := clientset.AppsV1().ReplicaSets(options.Namespace).List(context.Background(), filters)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	return replicasets.Items, nil
    32  }
    33  
    34  // GetReplicaSet returns a Kubernetes replicaset resource in the provided namespace with the given name. This will
    35  // fail the test if there is an error.
    36  func GetReplicaSet(t testing.TestingT, options *KubectlOptions, replicaSetName string) *appsv1.ReplicaSet {
    37  	replicaset, err := GetReplicaSetE(t, options, replicaSetName)
    38  	require.NoError(t, err)
    39  	return replicaset
    40  }
    41  
    42  // GetReplicaSetE returns a Kubernetes replicaset resource in the provided namespace with the given name.
    43  func GetReplicaSetE(t testing.TestingT, options *KubectlOptions, replicaSetName string) (*appsv1.ReplicaSet, error) {
    44  	clientset, err := GetKubernetesClientFromOptionsE(t, options)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return clientset.AppsV1().ReplicaSets(options.Namespace).Get(context.Background(), replicaSetName, metav1.GetOptions{})
    49  }