mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 10:58:35 +08:00
【修改】k8s 配置
This commit is contained in:
92
apps/devops/services/k8s/pvc/pvc.go
Normal file
92
apps/devops/services/k8s/pvc/pvc.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package pvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// PersistentVolumeClaimList contains a list of Persistent Volume Claims in the cluster.
|
||||
type PersistentVolumeClaimList struct {
|
||||
ListMeta k8s.ListMeta `json:"listMeta"`
|
||||
|
||||
// Unordered list of persistent volume claims
|
||||
Items []PersistentVolumeClaim `json:"items"`
|
||||
}
|
||||
|
||||
// PersistentVolumeClaim provides the simplified presentation layer view of Kubernetes Persistent Volume Claim resource.
|
||||
type PersistentVolumeClaim struct {
|
||||
ObjectMeta k8s.ObjectMeta `json:"objectMeta"`
|
||||
TypeMeta k8s.TypeMeta `json:"typeMeta"`
|
||||
Status string `json:"status"`
|
||||
Volume string `json:"volume"`
|
||||
Capacity v1.ResourceList `json:"capacity"`
|
||||
AccessModes []v1.PersistentVolumeAccessMode `json:"accessModes"`
|
||||
StorageClass *string `json:"storageClass"`
|
||||
}
|
||||
|
||||
// GetPersistentVolumeClaimList returns a list of all Persistent Volume Claims in the cluster.
|
||||
func GetPersistentVolumeClaimList(client kubernetes.Interface, nsQuery *k8scommon.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*PersistentVolumeClaimList, error) {
|
||||
|
||||
global.Log.Info("Getting list persistent volumes claims")
|
||||
channels := &k8scommon.ResourceChannels{
|
||||
PersistentVolumeClaimList: k8scommon.GetPersistentVolumeClaimListChannel(client, nsQuery, 1),
|
||||
}
|
||||
|
||||
return GetPersistentVolumeClaimListFromChannels(channels, nsQuery, dsQuery)
|
||||
}
|
||||
|
||||
// GetPersistentVolumeClaimListFromChannels returns a list of all Persistent Volume Claims in the cluster
|
||||
// reading required resource list once from the channels.
|
||||
func GetPersistentVolumeClaimListFromChannels(channels *k8scommon.ResourceChannels, nsQuery *k8scommon.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*PersistentVolumeClaimList, error) {
|
||||
|
||||
persistentVolumeClaims := <-channels.PersistentVolumeClaimList.List
|
||||
err := <-channels.PersistentVolumeClaimList.Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return toPersistentVolumeClaimList(persistentVolumeClaims.Items, dsQuery), nil
|
||||
}
|
||||
|
||||
func toPersistentVolumeClaim(pvc v1.PersistentVolumeClaim) PersistentVolumeClaim {
|
||||
|
||||
return PersistentVolumeClaim{
|
||||
ObjectMeta: k8s.NewObjectMeta(pvc.ObjectMeta),
|
||||
TypeMeta: k8s.NewTypeMeta(k8s.ResourceKindPersistentVolumeClaim),
|
||||
Status: string(pvc.Status.Phase),
|
||||
Volume: pvc.Spec.VolumeName,
|
||||
Capacity: pvc.Status.Capacity,
|
||||
AccessModes: pvc.Spec.AccessModes,
|
||||
StorageClass: pvc.Spec.StorageClassName,
|
||||
}
|
||||
}
|
||||
|
||||
func toPersistentVolumeClaimList(persistentVolumeClaims []v1.PersistentVolumeClaim, dsQuery *dataselect.DataSelectQuery) *PersistentVolumeClaimList {
|
||||
|
||||
result := &PersistentVolumeClaimList{
|
||||
Items: make([]PersistentVolumeClaim, 0),
|
||||
ListMeta: k8s.ListMeta{TotalItems: len(persistentVolumeClaims)},
|
||||
}
|
||||
|
||||
pvcCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(persistentVolumeClaims), dsQuery)
|
||||
persistentVolumeClaims = fromCells(pvcCells)
|
||||
result.ListMeta = k8s.ListMeta{TotalItems: filteredTotal}
|
||||
|
||||
for _, item := range persistentVolumeClaims {
|
||||
result.Items = append(result.Items, toPersistentVolumeClaim(item))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func DeletePersistentVolumeClaim(client *kubernetes.Clientset, namespace, name string) (err error) {
|
||||
global.Log.Info(fmt.Sprintf("Start deleting persistent volumes claims, namespace: %v, name: %v", namespace, name))
|
||||
return client.CoreV1().PersistentVolumeClaims(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
|
||||
}
|
||||
99
apps/devops/services/k8s/pvc/pvc_common.go
Normal file
99
apps/devops/services/k8s/pvc/pvc_common.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package pvc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
api "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"
|
||||
)
|
||||
|
||||
// The code below allows to perform complex data section on []api.PersistentVolumeClaim
|
||||
|
||||
type PersistentVolumeClaimCell api.PersistentVolumeClaim
|
||||
|
||||
// GetPodPersistentVolumeClaims gets persistentvolumeclaims that are associated with this pod.
|
||||
func GetPodPersistentVolumeClaims(client client.Interface, namespace string, podName string, dsQuery *dataselect.DataSelectQuery) (*PersistentVolumeClaimList, error) {
|
||||
|
||||
pod, err := client.CoreV1().Pods(namespace).Get(context.TODO(), podName, metaV1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
claimNames := make([]string, 0)
|
||||
if pod.Spec.Volumes != nil && len(pod.Spec.Volumes) > 0 {
|
||||
for _, v := range pod.Spec.Volumes {
|
||||
persistentVolumeClaim := v.PersistentVolumeClaim
|
||||
if persistentVolumeClaim != nil {
|
||||
claimNames = append(claimNames, persistentVolumeClaim.ClaimName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(claimNames) > 0 {
|
||||
channels := &k8scommon.ResourceChannels{
|
||||
PersistentVolumeClaimList: k8scommon.GetPersistentVolumeClaimListChannel(
|
||||
client, k8scommon.NewSameNamespaceQuery(namespace), 1),
|
||||
}
|
||||
|
||||
persistentVolumeClaimList := <-channels.PersistentVolumeClaimList.List
|
||||
|
||||
err = <-channels.PersistentVolumeClaimList.Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
podPersistentVolumeClaims := make([]api.PersistentVolumeClaim, 0)
|
||||
for _, pvc := range persistentVolumeClaimList.Items {
|
||||
for _, claimName := range claimNames {
|
||||
if strings.Compare(claimName, pvc.Name) == 0 {
|
||||
podPersistentVolumeClaims = append(podPersistentVolumeClaims, pvc)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
global.Log.Info(fmt.Sprintf("Found %d persistentvolumeclaims related to %s pod", len(podPersistentVolumeClaims), podName))
|
||||
|
||||
return toPersistentVolumeClaimList(podPersistentVolumeClaims, dsQuery), nil
|
||||
}
|
||||
|
||||
global.Log.Warn(fmt.Sprintf("No persistentvolumeclaims found related to %s pod", podName))
|
||||
|
||||
// No ClaimNames found in Pod details, return empty response.
|
||||
return &PersistentVolumeClaimList{}, nil
|
||||
}
|
||||
|
||||
func (self PersistentVolumeClaimCell) 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
|
||||
}
|
||||
}
|
||||
|
||||
func toCells(std []api.PersistentVolumeClaim) []dataselect.DataCell {
|
||||
cells := make([]dataselect.DataCell, len(std))
|
||||
for i := range std {
|
||||
cells[i] = PersistentVolumeClaimCell(std[i])
|
||||
}
|
||||
return cells
|
||||
}
|
||||
|
||||
func fromCells(cells []dataselect.DataCell) []api.PersistentVolumeClaim {
|
||||
std := make([]api.PersistentVolumeClaim, len(cells))
|
||||
for i := range std {
|
||||
std[i] = api.PersistentVolumeClaim(cells[i].(PersistentVolumeClaimCell))
|
||||
}
|
||||
return std
|
||||
}
|
||||
35
apps/devops/services/k8s/pvc/pvc_detail.go
Normal file
35
apps/devops/services/k8s/pvc/pvc_detail.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package pvc
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// PersistentVolumeClaimDetail provides the presentation layer view of Kubernetes Persistent Volume Claim resource.
|
||||
type PersistentVolumeClaimDetail struct {
|
||||
// Extends list item structure.
|
||||
PersistentVolumeClaim `json:",inline"`
|
||||
}
|
||||
|
||||
// GetPersistentVolumeClaimDetail returns detailed information about a persistent volume claim
|
||||
func GetPersistentVolumeClaimDetail(client kubernetes.Interface, namespace string, name string) (*PersistentVolumeClaimDetail, error) {
|
||||
global.Log.Info(fmt.Sprintf("Getting details of %s persistent volume claim", name))
|
||||
|
||||
pvc, err := client.CoreV1().PersistentVolumeClaims(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return getPersistentVolumeClaimDetail(*pvc), nil
|
||||
}
|
||||
|
||||
func getPersistentVolumeClaimDetail(pvc v1.PersistentVolumeClaim) *PersistentVolumeClaimDetail {
|
||||
return &PersistentVolumeClaimDetail{
|
||||
PersistentVolumeClaim: toPersistentVolumeClaim(pvc),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user