06-PVC操作

@

1. 获取PVC列表

获取指定namespace下pvc列表

import (
	"context"
	coreV1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
)
func GetPVCList(clientSet *kubernetes.Clientset, namespaceName string) (pvcList *coreV1.PersistentVolumeClaimList, err error) {
	pvcList, err = clientSet.CoreV1().PersistentVolumeClaims(namespaceName).List(context.TODO(), metaV1.ListOptions{})
	if err != nil {
		return nil, err
	}
	return pvcList, err
}

2. 获取指定PVC

import (
	"context"
	coreV1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
)

func GetPVC(clientSet *kubernetes.Clientset, namespaceName string, PVCName string) (pvcInfo *coreV1.PersistentVolumeClaim, err error) {
	pvcInfo, err = clientSet.CoreV1().PersistentVolumeClaims(namespaceName).Get(context.TODO(), PVCName, metaV1.GetOptions{})
	if err != nil {
		return nil, err
	}
	return pvcInfo, err
}

3. 创建PVC

import (
	"context"
	coreV1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
)

type CreatePVCRequest struct {
	NamespaceName    string                              `json:"namespace_name"`
	PvcName          string                              `json:"pvc_name"`
	AccessModes      []coreV1.PersistentVolumeAccessMode `json:"access_modes"`
	StorageSize      int64                               `json:"storage_size"`
	StorageClassName string                              `json:"storage_class_name"`
}

func CreatePVC(clientSet *kubernetes.Clientset, request CreatePVCRequest) (pvcInfo *coreV1.PersistentVolumeClaim, err error) {

	pvcSrc := new(coreV1.PersistentVolumeClaim)
	pvcSrc.ObjectMeta.Name = request.PvcName
	pvcSrc.Spec.AccessModes = request.AccessModes

	//设置存储大小
	var resourceQuantity resource.Quantity
	resourceQuantity.Set(request.StorageSize * 1000 * 1000 * 1000)
	pvcSrc.Spec.Resources.Requests = coreV1.ResourceList{
		"storage": resourceQuantity,
	}

	//使用存储卷名字
	if len(request.StorageClassName) != 0 {
		pvcSrc.Spec.StorageClassName = &request.StorageClassName
	}

	//创建pvc
	pvcInfo, err = clientSet.CoreV1().PersistentVolumeClaims(request.NamespaceName).Create(context.TODO(), pvcSrc, metaV1.CreateOptions{})
	if err != nil {
		return nil, err
	}
	return pvcInfo, err
}

4. 删除指定PVC

import (
	"context"
	coreV1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
)

type DeletePVCRequest struct {
	NamespaceName string `json:"namespace_name"`
	PvcName       string `json:"pvc_name"`
}

func DeletePvc(clientSet *kubernetes.Clientset, request DeletePVCRequest) (err error) {
	err = clientSet.CoreV1().PersistentVolumeClaims(request.NamespaceName).Delete(context.TODO(), request.PvcName, metaV1.DeleteOptions{})
	if err != nil {
		return err
	}
	return nil
}

5. Deployment使用pvc

另一篇有说明: 《Go语言开发k8s(client-go)-03-Deployment挂载configmap、pvc、host path》


posted on 2023-05-23 08:56  运维开发玄德公  阅读(58)  评论(0编辑  收藏  举报  来源

导航