github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/integration_tests/k8s/k8sapi.go (about)

     1  /*
     2  Copyright 2019 The OpenEBS 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 k8s
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"time"
    24  
    25  	apis "github.com/openebs/node-disk-manager/api/v1alpha1"
    26  	appsv1 "k8s.io/api/apps/v1"
    27  	corev1 "k8s.io/api/core/v1"
    28  	rbacv1 "k8s.io/api/rbac/v1"
    29  	apiextensionsV1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    30  	"k8s.io/apimachinery/pkg/api/resource"
    31  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  	"sigs.k8s.io/controller-runtime/pkg/client"
    33  )
    34  
    35  // The wait time for all k8s API related operations
    36  const k8sWaitTime = 40 * time.Second
    37  
    38  // The wait time for reconciliation loop to run
    39  const k8sReconcileTime = 10 * time.Second
    40  
    41  // ListPodStatus returns the list of all pods in the given Namespace along
    42  // with their status
    43  func (c K8sClient) ListPodStatus() (map[string]string, error) {
    44  	pods := make(map[string]string)
    45  	podList, err := c.ClientSet.CoreV1().Pods(Namespace).List(context.TODO(), metav1.ListOptions{})
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	for _, pod := range podList.Items {
    50  		pods[pod.Name] = string(pod.Status.Phase)
    51  	}
    52  	return pods, nil
    53  }
    54  
    55  // ListNodeStatus returns list of all nodes(node name) in the cluster along with
    56  // their status
    57  func (c K8sClient) ListNodeStatus() (map[string]string, error) {
    58  	nodes := make(map[string]string)
    59  	nodeList, err := c.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	for _, node := range nodeList.Items {
    64  		nodes[node.Name] = string(node.Status.Phase)
    65  	}
    66  	return nodes, nil
    67  }
    68  
    69  // ListBlockDevices returns list of BlockDeviceCR in the cluster
    70  func (c K8sClient) ListBlockDevices() (*apis.BlockDeviceList, error) {
    71  	bdList := &apis.BlockDeviceList{
    72  		TypeMeta: metav1.TypeMeta{
    73  			Kind:       "BlockDevice",
    74  			APIVersion: "openebs.io/v1alpha1",
    75  		},
    76  	}
    77  
    78  	err := c.RunTimeClient.List(context.TODO(), bdList)
    79  	if err != nil {
    80  		return nil, fmt.Errorf("cannot list blockdevices. Error :%v", err)
    81  	}
    82  	return bdList, nil
    83  }
    84  
    85  // GetBlockDeviceClaim from Namespace and name
    86  func (c K8sClient) GetBlockDeviceClaim(NameSpace, Name string) (*apis.BlockDeviceClaim, error) {
    87  	bdc := &apis.BlockDeviceClaim{}
    88  	err := c.RunTimeClient.Get(context.TODO(), client.ObjectKey{
    89  		Name:      Name,
    90  		Namespace: NameSpace,
    91  	}, bdc)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return bdc, nil
    96  }
    97  
    98  // GetBlockDevice with Name in Namespace
    99  func (c K8sClient) GetBlockDevice(Name, Namesapce string) (*apis.BlockDevice, error) {
   100  	bd := apis.BlockDevice{}
   101  	err := c.RunTimeClient.Get(context.TODO(), client.ObjectKey{
   102  		Name:      Name,
   103  		Namespace: Namesapce,
   104  	}, &bd)
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  	return &bd, nil
   109  }
   110  
   111  // ListBlockDeviceClaims returns list of BlockDeviceClaims in the cluster
   112  func (c K8sClient) ListBlockDeviceClaims() (*apis.BlockDeviceClaimList, error) {
   113  	bdcList := &apis.BlockDeviceClaimList{
   114  		TypeMeta: metav1.TypeMeta{
   115  			Kind:       "BlockDeviceClaim",
   116  			APIVersion: "openebs.io/v1alpha1",
   117  		},
   118  	}
   119  	err := c.RunTimeClient.List(context.TODO(), bdcList)
   120  	if err != nil {
   121  		return nil, fmt.Errorf("cannot list block device claims. Error :%v", err)
   122  	}
   123  	return bdcList, nil
   124  }
   125  
   126  // RestartPod the given pod
   127  func (c K8sClient) RestartPod(name string) error {
   128  	pods, err := c.ListPodStatus()
   129  	if err != nil {
   130  		return nil
   131  	}
   132  	for pod := range pods {
   133  		if strings.Contains(pod, name) {
   134  			return c.ClientSet.CoreV1().Pods(Namespace).Delete(context.Background(), pod, metav1.DeleteOptions{})
   135  		}
   136  	}
   137  	return fmt.Errorf("could not find given pod")
   138  }
   139  
   140  // NewBDC creates a sample device claim which can be used for
   141  // claiming a block device.
   142  func NewBDC(bdcName string) *apis.BlockDeviceClaim {
   143  	bdcResources := apis.DeviceClaimResources{
   144  		Requests: make(map[corev1.ResourceName]resource.Quantity),
   145  	}
   146  	bdcSpec := apis.DeviceClaimSpec{
   147  		Resources: bdcResources,
   148  	}
   149  	bdc := &apis.BlockDeviceClaim{
   150  		TypeMeta: metav1.TypeMeta{
   151  			Kind:       "BlockDeviceClaim",
   152  			APIVersion: "openebs.io/v1alpha1",
   153  		},
   154  		ObjectMeta: metav1.ObjectMeta{
   155  			Labels: make(map[string]string),
   156  			Name:   bdcName,
   157  		},
   158  		Spec: bdcSpec,
   159  	}
   160  	return bdc
   161  }
   162  
   163  // CreateNamespace creates a namespace
   164  func (c K8sClient) CreateNamespace(namespace corev1.Namespace) error {
   165  	err := c.RunTimeClient.Create(context.Background(), &namespace)
   166  	return err
   167  }
   168  
   169  // DeleteNamespace deletes a namespace
   170  func (c K8sClient) DeleteNamespace(namespace corev1.Namespace) error {
   171  	err := c.RunTimeClient.Delete(context.Background(), &namespace)
   172  	return err
   173  }
   174  
   175  // CreateConfigMap creates a config map
   176  func (c K8sClient) CreateConfigMap(configMap corev1.ConfigMap) error {
   177  	err := c.RunTimeClient.Create(context.Background(), &configMap)
   178  	return err
   179  }
   180  
   181  // DeleteConfigMap deletes the config map
   182  func (c K8sClient) DeleteConfigMap(configMap corev1.ConfigMap) error {
   183  	err := c.RunTimeClient.Delete(context.Background(), &configMap)
   184  	return err
   185  }
   186  
   187  // CreateServiceAccount creates a service account
   188  func (c K8sClient) CreateServiceAccount(serviceAccount corev1.ServiceAccount) error {
   189  	err := c.RunTimeClient.Create(context.Background(), &serviceAccount)
   190  	return err
   191  }
   192  
   193  // DeleteServiceAccount deletes the service account
   194  func (c K8sClient) DeleteServiceAccount(serviceAccount corev1.ServiceAccount) error {
   195  	err := c.RunTimeClient.Delete(context.Background(), &serviceAccount)
   196  	return err
   197  }
   198  
   199  // CreateClusterRole creates a cluster role
   200  func (c K8sClient) CreateClusterRole(clusterRole rbacv1.ClusterRole) error {
   201  	err := c.RunTimeClient.Create(context.Background(), &clusterRole)
   202  	return err
   203  }
   204  
   205  // DeleteClusterRole deletes the cluster role
   206  func (c K8sClient) DeleteClusterRole(clusterRole rbacv1.ClusterRole) error {
   207  	err := c.RunTimeClient.Delete(context.Background(), &clusterRole)
   208  	return err
   209  }
   210  
   211  // CreateClusterRoleBinding creates a rolebinding
   212  func (c K8sClient) CreateClusterRoleBinding(clusterRoleBinding rbacv1.ClusterRoleBinding) error {
   213  	err := c.RunTimeClient.Create(context.Background(), &clusterRoleBinding)
   214  	return err
   215  }
   216  
   217  // DeleteClusterRoleBinding deletes the role binding
   218  func (c K8sClient) DeleteClusterRoleBinding(clusterrolebinding rbacv1.ClusterRoleBinding) error {
   219  	err := c.RunTimeClient.Delete(context.Background(), &clusterrolebinding)
   220  	return err
   221  }
   222  
   223  // CreateCustomResourceDefinition creates a CRD
   224  func (c K8sClient) CreateCustomResourceDefinition(customResourceDefinition apiextensionsV1.CustomResourceDefinition) error {
   225  	_, err := c.APIextClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), &customResourceDefinition, metav1.CreateOptions{})
   226  	return err
   227  }
   228  
   229  // DeleteCustomResourceDefinition deletes the CRD
   230  func (c K8sClient) DeleteCustomResourceDefinition(customResourceDefinition apiextensionsV1.CustomResourceDefinition) error {
   231  	err := c.APIextClient.ApiextensionsV1().CustomResourceDefinitions().Delete(context.Background(), customResourceDefinition.Name, metav1.DeleteOptions{})
   232  	return err
   233  }
   234  
   235  // CreateDaemonSet creates a Daemonset
   236  func (c K8sClient) CreateDaemonSet(daemonset appsv1.DaemonSet) error {
   237  	daemonset.Namespace = Namespace
   238  	err := c.RunTimeClient.Create(context.Background(), &daemonset)
   239  	return err
   240  }
   241  
   242  // DeleteDaemonSet deletes the Daemonset
   243  func (c K8sClient) DeleteDaemonSet(daemonset appsv1.DaemonSet) error {
   244  	daemonset.Namespace = Namespace
   245  	err := c.RunTimeClient.Delete(context.Background(), &daemonset, client.PropagationPolicy(metav1.DeletePropagationForeground))
   246  	return err
   247  }
   248  
   249  // CreateDeployment creates a deployment
   250  func (c K8sClient) CreateDeployment(deployment appsv1.Deployment) error {
   251  	deployment.Namespace = Namespace
   252  	err := c.RunTimeClient.Create(context.Background(), &deployment)
   253  	return err
   254  }
   255  
   256  // DeleteDeployment deletes a deployment
   257  func (c K8sClient) DeleteDeployment(deployment appsv1.Deployment) error {
   258  	deployment.Namespace = Namespace
   259  	err := c.RunTimeClient.Delete(context.Background(), &deployment)
   260  	return err
   261  }
   262  
   263  // CreateBlockDeviceClaim creates a BDC
   264  func (c K8sClient) CreateBlockDeviceClaim(claim *apis.BlockDeviceClaim) error {
   265  	err := c.RunTimeClient.Create(context.Background(), claim)
   266  	return err
   267  }
   268  
   269  // UpdateBlockDeviceClaim updates the BDC
   270  func (c K8sClient) UpdateBlockDeviceClaim(claim *apis.BlockDeviceClaim) error {
   271  	err := c.RunTimeClient.Update(context.Background(), claim)
   272  	return err
   273  }
   274  
   275  // DeleteBlockDeviceClaim deletes a BDC
   276  func (c K8sClient) DeleteBlockDeviceClaim(claim *apis.BlockDeviceClaim) error {
   277  	err := c.RunTimeClient.Delete(context.Background(), claim)
   278  	return err
   279  }