...

Source file src/github.com/redhat-developer/odo/pkg/kclient/volumes.go

Documentation: github.com/redhat-developer/odo/pkg/kclient

     1  package kclient
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"fmt"
     8  
     9  	"github.com/devfile/library/v2/pkg/devfile/generator"
    10  	corev1 "k8s.io/api/core/v1"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"k8s.io/apimachinery/pkg/types"
    13  )
    14  
    15  // constants for volumes
    16  const (
    17  	PersistentVolumeClaimKind       = "PersistentVolumeClaim"
    18  	PersistentVolumeClaimAPIVersion = "v1"
    19  )
    20  
    21  // CreatePVC creates a PVC resource in the cluster with the given name, size and labels
    22  func (c *Client) CreatePVC(pvc corev1.PersistentVolumeClaim) (*corev1.PersistentVolumeClaim, error) {
    23  	createdPvc, err := c.KubeClient.CoreV1().PersistentVolumeClaims(c.Namespace).Create(context.TODO(), &pvc, metav1.CreateOptions{FieldManager: FieldManager})
    24  	if err != nil {
    25  		return nil, fmt.Errorf("unable to create PVC: %w", err)
    26  	}
    27  	return createdPvc, nil
    28  }
    29  
    30  // DeletePVC deletes the required PVC resource from the cluster
    31  func (c *Client) DeletePVC(pvcName string) error {
    32  	return c.KubeClient.CoreV1().PersistentVolumeClaims(c.Namespace).Delete(context.TODO(), pvcName, metav1.DeleteOptions{})
    33  }
    34  
    35  // ListPVCs returns the PVCs based on the given selector
    36  func (c *Client) ListPVCs(selector string) ([]corev1.PersistentVolumeClaim, error) {
    37  	pvcList, err := c.KubeClient.CoreV1().PersistentVolumeClaims(c.Namespace).List(context.TODO(), metav1.ListOptions{
    38  		LabelSelector: selector,
    39  	})
    40  	if err != nil {
    41  		return nil, fmt.Errorf("unable to get PVCs for selector: %v: %w", selector, err)
    42  	}
    43  
    44  	return pvcList.Items, nil
    45  }
    46  
    47  // ListPVCNames returns the PVC names for the given selector
    48  func (c *Client) ListPVCNames(selector string) ([]string, error) {
    49  	pvcs, err := c.ListPVCs(selector)
    50  	if err != nil {
    51  		return nil, fmt.Errorf("unable to get PVCs from selector: %w", err)
    52  	}
    53  
    54  	var names []string
    55  	for _, pvc := range pvcs {
    56  		names = append(names, pvc.Name)
    57  	}
    58  
    59  	return names, nil
    60  }
    61  
    62  // GetPVCFromName returns the PVC of the given name
    63  func (c *Client) GetPVCFromName(pvcName string) (*corev1.PersistentVolumeClaim, error) {
    64  	return c.KubeClient.CoreV1().PersistentVolumeClaims(c.Namespace).Get(context.TODO(), pvcName, metav1.GetOptions{})
    65  }
    66  
    67  // UpdatePVCLabels updates the given PVC with the given labels
    68  func (c *Client) UpdatePVCLabels(pvc *corev1.PersistentVolumeClaim, labels map[string]string) error {
    69  	pvc.Labels = labels
    70  	_, err := c.KubeClient.CoreV1().PersistentVolumeClaims(c.Namespace).Update(context.TODO(), pvc, metav1.UpdateOptions{FieldManager: FieldManager})
    71  	if err != nil {
    72  		return fmt.Errorf("unable to remove storage label from PVC: %w", err)
    73  	}
    74  	return nil
    75  }
    76  
    77  // UpdateStorageOwnerReference updates the given storage with the given owner references
    78  func (c *Client) UpdateStorageOwnerReference(pvc *corev1.PersistentVolumeClaim, ownerReference ...metav1.OwnerReference) error {
    79  	if len(ownerReference) <= 0 {
    80  		return errors.New("owner references are empty")
    81  	}
    82  
    83  	updatedPVC := generator.GetPVC(generator.PVCParams{
    84  		ObjectMeta: pvc.ObjectMeta,
    85  		TypeMeta:   pvc.TypeMeta,
    86  	})
    87  
    88  	updatedPVC.OwnerReferences = ownerReference
    89  	updatedPVC.Spec = pvc.Spec
    90  
    91  	if c.IsSSASupported() {
    92  		updatedPVC.APIVersion, updatedPVC.Kind = corev1.SchemeGroupVersion.WithKind("PersistentVolumeClaim").ToAPIVersionAndKind()
    93  		updatedPVC.SetManagedFields(nil)
    94  		updatedPVC.SetResourceVersion("")
    95  		data, err := json.Marshal(updatedPVC)
    96  		if err != nil {
    97  			return fmt.Errorf("unable to marshal deployment: %w", err)
    98  		}
    99  		_, err = c.KubeClient.CoreV1().PersistentVolumeClaims(c.Namespace).Patch(context.TODO(), updatedPVC.Name, types.ApplyPatchType, data, metav1.PatchOptions{FieldManager: FieldManager, Force: Bool(true)})
   100  		if err != nil {
   101  			return err
   102  		}
   103  	} else {
   104  		_, err := c.KubeClient.CoreV1().PersistentVolumeClaims(c.Namespace).Update(context.TODO(), updatedPVC, metav1.UpdateOptions{FieldManager: FieldManager})
   105  		if err != nil {
   106  			return err
   107  		}
   108  	}
   109  	return nil
   110  }
   111  

View as plain text