mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-24 11:28:40 +08:00
【修改】k8s 配置
This commit is contained in:
94
apps/devops/services/k8s/pv/pv.go
Normal file
94
apps/devops/services/k8s/pv/pv.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package pv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"pandax/base/global"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"pandax/apps/devops/entity/k8s"
|
||||
k8scommon "pandax/apps/devops/services/k8s/common"
|
||||
"pandax/apps/devops/services/k8s/dataselect"
|
||||
)
|
||||
|
||||
// PersistentVolumeList contains a list of Persistent Volumes in the cluster.
|
||||
type PersistentVolumeList struct {
|
||||
ListMeta k8s.ListMeta `json:"listMeta"`
|
||||
Items []PersistentVolume `json:"items"`
|
||||
}
|
||||
|
||||
// PersistentVolume provides the simplified presentation layer view of Kubernetes Persistent Volume resource.
|
||||
type PersistentVolume struct {
|
||||
ObjectMeta k8s.ObjectMeta `json:"objectMeta"`
|
||||
TypeMeta k8s.TypeMeta `json:"typeMeta"`
|
||||
Capacity v1.ResourceList `json:"capacity"`
|
||||
AccessModes []v1.PersistentVolumeAccessMode `json:"accessModes"`
|
||||
ReclaimPolicy v1.PersistentVolumeReclaimPolicy `json:"reclaimPolicy"`
|
||||
StorageClass string `json:"storageClass"`
|
||||
MountOptions []string `json:"mountOptions"`
|
||||
Status v1.PersistentVolumePhase `json:"status"`
|
||||
Claim string `json:"claim"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
// GetPersistentVolumeList returns a list of all Persistent Volumes in the cluster.
|
||||
func GetPersistentVolumeList(client kubernetes.Interface, dsQuery *dataselect.DataSelectQuery) (*PersistentVolumeList, error) {
|
||||
global.Log.Info("Getting list persistent volumes")
|
||||
channels := &k8scommon.ResourceChannels{
|
||||
PersistentVolumeList: k8scommon.GetPersistentVolumeListChannel(client, 1),
|
||||
}
|
||||
|
||||
return GetPersistentVolumeListFromChannels(channels, dsQuery)
|
||||
}
|
||||
|
||||
// GetPersistentVolumeListFromChannels returns a list of all Persistent Volumes in the cluster
|
||||
// reading required resource list once from the channels.
|
||||
func GetPersistentVolumeListFromChannels(channels *k8scommon.ResourceChannels, dsQuery *dataselect.DataSelectQuery) (*PersistentVolumeList, error) {
|
||||
persistentVolumes := <-channels.PersistentVolumeList.List
|
||||
err := <-channels.PersistentVolumeList.Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return toPersistentVolumeList(persistentVolumes.Items, dsQuery), nil
|
||||
}
|
||||
|
||||
func toPersistentVolumeList(persistentVolumes []v1.PersistentVolume, dsQuery *dataselect.DataSelectQuery) *PersistentVolumeList {
|
||||
|
||||
result := &PersistentVolumeList{
|
||||
Items: make([]PersistentVolume, 0),
|
||||
ListMeta: k8s.ListMeta{TotalItems: len(persistentVolumes)},
|
||||
}
|
||||
|
||||
pvCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(persistentVolumes), dsQuery)
|
||||
persistentVolumes = fromCells(pvCells)
|
||||
result.ListMeta = k8s.ListMeta{TotalItems: filteredTotal}
|
||||
|
||||
for _, item := range persistentVolumes {
|
||||
result.Items = append(result.Items, toPersistentVolume(item))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func toPersistentVolume(pv v1.PersistentVolume) PersistentVolume {
|
||||
return PersistentVolume{
|
||||
ObjectMeta: k8s.NewObjectMeta(pv.ObjectMeta),
|
||||
TypeMeta: k8s.NewTypeMeta(k8s.ResourceKindPersistentVolume),
|
||||
Capacity: pv.Spec.Capacity,
|
||||
AccessModes: pv.Spec.AccessModes,
|
||||
ReclaimPolicy: pv.Spec.PersistentVolumeReclaimPolicy,
|
||||
StorageClass: pv.Spec.StorageClassName,
|
||||
MountOptions: pv.Spec.MountOptions,
|
||||
Status: pv.Status.Phase,
|
||||
Claim: getPersistentVolumeClaim(&pv),
|
||||
Reason: pv.Status.Reason,
|
||||
}
|
||||
}
|
||||
|
||||
func DeletePersistentVolume(client *kubernetes.Clientset, name string) (err error) {
|
||||
global.Log.Info(fmt.Sprintf("Start deleting persistent volume, name: %v", name))
|
||||
return client.CoreV1().PersistentVolumes().Delete(context.TODO(), name, metav1.DeleteOptions{})
|
||||
}
|
||||
94
apps/devops/services/k8s/pv/pv_common.go
Normal file
94
apps/devops/services/k8s/pv/pv_common.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package pv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
client "k8s.io/client-go/kubernetes"
|
||||
k8scommon "pandax/apps/devops/services/k8s/common"
|
||||
"pandax/apps/devops/services/k8s/dataselect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GetStorageClassPersistentVolumes gets persistentvolumes that are associated with this storageclass.
|
||||
func GetStorageClassPersistentVolumes(client client.Interface, storageClassName string,
|
||||
dsQuery *dataselect.DataSelectQuery) (*PersistentVolumeList, error) {
|
||||
|
||||
storageClass, err := client.StorageV1().StorageClasses().Get(context.TODO(), storageClassName, metaV1.GetOptions{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
channels := &k8scommon.ResourceChannels{
|
||||
PersistentVolumeList: k8scommon.GetPersistentVolumeListChannel(
|
||||
client, 1),
|
||||
}
|
||||
|
||||
persistentVolumeList := <-channels.PersistentVolumeList.List
|
||||
|
||||
err = <-channels.PersistentVolumeList.Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
storagePersistentVolumes := make([]v1.PersistentVolume, 0)
|
||||
for _, pv := range persistentVolumeList.Items {
|
||||
if strings.Compare(pv.Spec.StorageClassName, storageClass.Name) == 0 {
|
||||
storagePersistentVolumes = append(storagePersistentVolumes, pv)
|
||||
}
|
||||
}
|
||||
|
||||
global.Log.Info(fmt.Sprintf("Found %d persistentvolumes related to %s storageclass", len(storagePersistentVolumes), storageClassName))
|
||||
|
||||
return toPersistentVolumeList(storagePersistentVolumes, dsQuery), nil
|
||||
}
|
||||
|
||||
// getPersistentVolumeClaim returns Persistent Volume claim using "namespace/claim" format.
|
||||
func getPersistentVolumeClaim(pv *v1.PersistentVolume) string {
|
||||
var claim string
|
||||
|
||||
if pv.Spec.ClaimRef != nil {
|
||||
claim = pv.Spec.ClaimRef.Namespace + "/" + pv.Spec.ClaimRef.Name
|
||||
}
|
||||
|
||||
return claim
|
||||
}
|
||||
|
||||
// PersistentVolumeCell allows to perform complex data section on []api.PersistentVolume.
|
||||
type PersistentVolumeCell v1.PersistentVolume
|
||||
|
||||
// GetProperty allows to perform complex data section on PersistentVolumeCell.
|
||||
func (self PersistentVolumeCell) GetProperty(name dataselect.PropertyName) dataselect.ComparableValue {
|
||||
switch name {
|
||||
case dataselect.NameProperty:
|
||||
return dataselect.StdComparableString(self.ObjectMeta.Name)
|
||||
case dataselect.CreationTimestampProperty:
|
||||
return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time)
|
||||
case dataselect.NamespaceProperty:
|
||||
return dataselect.StdComparableString(self.ObjectMeta.Namespace)
|
||||
default:
|
||||
// if name is not supported then just return a constant dummy value, sort will have no effect.
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// toCells converts []api.PersistentVolume to []dataselect.DataCell.
|
||||
func toCells(std []v1.PersistentVolume) []dataselect.DataCell {
|
||||
cells := make([]dataselect.DataCell, len(std))
|
||||
for i := range std {
|
||||
cells[i] = PersistentVolumeCell(std[i])
|
||||
}
|
||||
return cells
|
||||
}
|
||||
|
||||
// fromCells converts cells []dataselect.DataCell to []api.PersistentVolume.
|
||||
func fromCells(cells []dataselect.DataCell) []v1.PersistentVolume {
|
||||
std := make([]v1.PersistentVolume, len(cells))
|
||||
for i := range std {
|
||||
std[i] = v1.PersistentVolume(cells[i].(PersistentVolumeCell))
|
||||
}
|
||||
return std
|
||||
}
|
||||
39
apps/devops/services/k8s/pv/pv_detail.go
Normal file
39
apps/devops/services/k8s/pv/pv_detail.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package pv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
client "k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
// PersistentVolumeDetail provides the presentation layer view of Kubernetes Persistent Volume resource.
|
||||
type PersistentVolumeDetail struct {
|
||||
// Extends list item structure.
|
||||
PersistentVolume `json:",inline"`
|
||||
|
||||
Message string `json:"message"`
|
||||
PersistentVolumeSource v1.PersistentVolumeSource `json:"persistentVolumeSource"`
|
||||
}
|
||||
|
||||
// GetPersistentVolumeDetail returns detailed information about a persistent volume
|
||||
func GetPersistentVolumeDetail(client client.Interface, name string) (*PersistentVolumeDetail, error) {
|
||||
global.Log.Info(fmt.Sprintf("Getting details of %s persistent volume", name))
|
||||
|
||||
rawPersistentVolume, err := client.CoreV1().PersistentVolumes().Get(context.TODO(), name, metaV1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return getPersistentVolumeDetail(*rawPersistentVolume), nil
|
||||
}
|
||||
|
||||
func getPersistentVolumeDetail(pv v1.PersistentVolume) *PersistentVolumeDetail {
|
||||
return &PersistentVolumeDetail{
|
||||
PersistentVolume: toPersistentVolume(pv),
|
||||
Message: pv.Status.Message,
|
||||
PersistentVolumeSource: pv.Spec.PersistentVolumeSource,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user