mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 10:58:35 +08:00
【修改】k8s 配置
This commit is contained in:
190
apps/devops/services/k8s/daemonset/daemonset.go
Normal file
190
apps/devops/services/k8s/daemonset/daemonset.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package daemonset
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap"
|
||||
apps "k8s.io/api/apps/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"pandax/apps/devops/entity/k8s"
|
||||
k8scommon "pandax/apps/devops/services/k8s/common"
|
||||
"pandax/apps/devops/services/k8s/dataselect"
|
||||
"pandax/apps/devops/services/k8s/event"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DaemonSetList contains a list of Daemon Sets in the cluster.
|
||||
type DaemonSetList struct {
|
||||
ListMeta k8s.ListMeta `json:"listMeta"`
|
||||
DaemonSets []DaemonSet `json:"daemonSets"`
|
||||
Status k8scommon.ResourceStatus `json:"status"`
|
||||
|
||||
// List of non-critical errors, that occurred during resource retrieval.
|
||||
Errors []error `json:"errors"`
|
||||
}
|
||||
|
||||
// DaemonSet plus zero or more Kubernetes services that target the Daemon Set.
|
||||
type DaemonSet struct {
|
||||
ObjectMeta k8s.ObjectMeta `json:"objectMeta"`
|
||||
TypeMeta k8s.TypeMeta `json:"typeMeta"`
|
||||
Pods k8scommon.PodInfo `json:"podInfo"`
|
||||
Strategy apps.DaemonSetUpdateStrategy `json:"strategy"`
|
||||
// Status information on the statefulSet
|
||||
StatusInfo `json:"statusInfo"`
|
||||
ContainerImages []string `json:"containerImages"`
|
||||
InitContainerImages []string `json:"initContainerImages"`
|
||||
}
|
||||
|
||||
// StatusInfo is the status information of the daemonset
|
||||
type StatusInfo struct {
|
||||
|
||||
// readyReplicas is the number of Pods created by the DaemonSet controller that have a Ready Condition.
|
||||
Ready int32 `json:"ready,omitempty"`
|
||||
|
||||
// currentReplicas is the number of Pods created by the DaemonSet controller from the DaemonSet version
|
||||
// indicated by currentRevision.
|
||||
Current int32 `json:"current,omitempty"`
|
||||
|
||||
// updatedReplicas is the number of Pods created by the DaemonSet controller from the DaemonSet version
|
||||
// indicated by updateRevision.
|
||||
Updated int32 `json:"updated,omitempty"`
|
||||
// Total number of available pods (ready for at least minReadySeconds) targeted by this daemonset.
|
||||
// This is an alpha field and requires enabling DaemonSetMinReadySeconds feature gate.
|
||||
// Remove omitempty when graduating to beta
|
||||
// +optional
|
||||
Available int32 `json:"available,omitempty"`
|
||||
|
||||
Unavailable int32 `json:"unavailable"`
|
||||
}
|
||||
|
||||
func GetDaemonSetList(client *kubernetes.Clientset, nsQuery *k8scommon.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*DaemonSetList, error) {
|
||||
channels := &k8scommon.ResourceChannels{
|
||||
DaemonSetList: k8scommon.GetDaemonSetListChannel(client, nsQuery, 1),
|
||||
ServiceList: k8scommon.GetServiceListChannel(client, nsQuery, 1),
|
||||
PodList: k8scommon.GetPodListChannel(client, nsQuery, 1),
|
||||
EventList: k8scommon.GetEventListChannel(client, nsQuery, 1),
|
||||
}
|
||||
|
||||
return GetDaemonSetListFromChannels(channels, dsQuery)
|
||||
}
|
||||
|
||||
// GetDaemonSetListFromChannels returns a list of all Daemon Set in the cluster
|
||||
// reading required resource list once from the channels.
|
||||
func GetDaemonSetListFromChannels(channels *k8scommon.ResourceChannels, dsQuery *dataselect.DataSelectQuery) (*DaemonSetList, error) {
|
||||
|
||||
daemonSets := <-channels.DaemonSetList.List
|
||||
err := <-channels.DaemonSetList.Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pods := <-channels.PodList.List
|
||||
err = <-channels.PodList.Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
events := <-channels.EventList.List
|
||||
err = <-channels.EventList.Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dsList := toDaemonSetList(daemonSets.Items, pods.Items, events.Items, dsQuery)
|
||||
dsList.Status = getStatus(daemonSets, pods.Items, events.Items)
|
||||
return dsList, nil
|
||||
}
|
||||
|
||||
func toDaemonSetList(daemonSets []apps.DaemonSet, pods []v1.Pod, events []v1.Event, dsQuery *dataselect.DataSelectQuery) *DaemonSetList {
|
||||
|
||||
daemonSetList := &DaemonSetList{
|
||||
DaemonSets: make([]DaemonSet, 0),
|
||||
ListMeta: k8s.ListMeta{TotalItems: len(daemonSets)},
|
||||
}
|
||||
|
||||
dsCells, filteredTotal := dataselect.GenericDataSelectWithFilter(ToCells(daemonSets), dsQuery)
|
||||
daemonSets = FromCells(dsCells)
|
||||
daemonSetList.ListMeta = k8s.ListMeta{TotalItems: filteredTotal}
|
||||
|
||||
for _, daemonSet := range daemonSets {
|
||||
daemonSetList.DaemonSets = append(daemonSetList.DaemonSets, toDaemonSet(daemonSet, pods, events))
|
||||
}
|
||||
|
||||
return daemonSetList
|
||||
}
|
||||
|
||||
func toDaemonSet(daemonSet apps.DaemonSet, pods []v1.Pod, events []v1.Event) DaemonSet {
|
||||
matchingPods := k8scommon.FilterPodsByControllerRef(&daemonSet, pods)
|
||||
podInfo := k8scommon.GetPodInfo(daemonSet.Status.CurrentNumberScheduled, &daemonSet.Status.DesiredNumberScheduled, matchingPods)
|
||||
podInfo.Warnings = event.GetPodsEventWarnings(events, matchingPods)
|
||||
return DaemonSet{
|
||||
ObjectMeta: k8s.NewObjectMeta(daemonSet.ObjectMeta),
|
||||
TypeMeta: k8s.NewTypeMeta(k8s.ResourceKindDaemonSet),
|
||||
Pods: podInfo,
|
||||
Strategy: daemonSet.Spec.UpdateStrategy,
|
||||
StatusInfo: GetStatusInfo(&daemonSet.Status),
|
||||
ContainerImages: k8scommon.GetContainerImages(&daemonSet.Spec.Template.Spec),
|
||||
InitContainerImages: k8scommon.GetInitContainerImages(&daemonSet.Spec.Template.Spec),
|
||||
}
|
||||
}
|
||||
|
||||
// GetStatusInfo is used to get the status information from the *apps.DaemonSetStatus
|
||||
func GetStatusInfo(daemonSetStatus *apps.DaemonSetStatus) StatusInfo {
|
||||
return StatusInfo{
|
||||
Updated: daemonSetStatus.UpdatedNumberScheduled,
|
||||
Available: daemonSetStatus.NumberAvailable,
|
||||
Ready: daemonSetStatus.NumberReady,
|
||||
Current: daemonSetStatus.CurrentNumberScheduled,
|
||||
Unavailable: daemonSetStatus.NumberUnavailable,
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteCollectionDaemonSet(client *kubernetes.Clientset, daemonSetList []k8s.DaemonSetData) (err error) {
|
||||
global.Log.Info("批量删除daemonset开始")
|
||||
for _, v := range daemonSetList {
|
||||
global.Log.Info(fmt.Sprintf("delete statefulset:%v, ns: %v", v.Name, v.Namespace))
|
||||
err := client.AppsV1().DaemonSets(v.Namespace).Delete(
|
||||
context.TODO(),
|
||||
v.Name,
|
||||
metav1.DeleteOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
global.Log.Error(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
global.Log.Info("删除daemonset已完成")
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteDaemonSet(client *kubernetes.Clientset, namespace, name string) (err error) {
|
||||
global.Log.Info(fmt.Sprintf("请求删除单个daemonset:%v, namespace: %v", name, namespace))
|
||||
return client.AppsV1().DaemonSets(namespace).Delete(
|
||||
context.TODO(),
|
||||
name,
|
||||
metav1.DeleteOptions{},
|
||||
)
|
||||
}
|
||||
|
||||
func RestartDaemonSet(client *kubernetes.Clientset, namespace, name string) (err error) {
|
||||
global.Log.Info(fmt.Sprintf("下发应用重启指令, 名称空间:%v, 守护进程集:%v", namespace, name))
|
||||
data := fmt.Sprintf(`{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":"%s"}}}}}`, time.Now().String())
|
||||
_, err = client.AppsV1().DaemonSets(namespace).Patch(
|
||||
context.Background(),
|
||||
name,
|
||||
types.StrategicMergePatchType,
|
||||
[]byte(data),
|
||||
metav1.PatchOptions{
|
||||
FieldManager: "kubectl-rollout",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
global.Log.Error("应用重启失败", zap.Any("err: ", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
67
apps/devops/services/k8s/daemonset/daemonset_common.go
Normal file
67
apps/devops/services/k8s/daemonset/daemonset_common.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package daemonset
|
||||
|
||||
import (
|
||||
apps "k8s.io/api/apps/v1"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"pandax/apps/devops/services/k8s/common"
|
||||
"pandax/apps/devops/services/k8s/dataselect"
|
||||
"pandax/apps/devops/services/k8s/event"
|
||||
)
|
||||
|
||||
// The code below allows to perform complex data section on Daemon Set
|
||||
|
||||
type DaemonSetCell apps.DaemonSet
|
||||
|
||||
func (self DaemonSetCell) 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 getStatus(list *apps.DaemonSetList, pods []v1.Pod, events []v1.Event) common.ResourceStatus {
|
||||
info := common.ResourceStatus{}
|
||||
if list == nil {
|
||||
return info
|
||||
}
|
||||
|
||||
for _, daemonSet := range list.Items {
|
||||
matchingPods := common.FilterPodsByControllerRef(&daemonSet, pods)
|
||||
podInfo := common.GetPodInfo(daemonSet.Status.CurrentNumberScheduled,
|
||||
&daemonSet.Status.DesiredNumberScheduled, matchingPods)
|
||||
warnings := event.GetPodsEventWarnings(events, matchingPods)
|
||||
|
||||
if len(warnings) > 0 {
|
||||
info.Failed++
|
||||
} else if podInfo.Pending > 0 {
|
||||
info.Pending++
|
||||
} else {
|
||||
info.Running++
|
||||
}
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
func ToCells(std []apps.DaemonSet) []dataselect.DataCell {
|
||||
cells := make([]dataselect.DataCell, len(std))
|
||||
for i := range std {
|
||||
cells[i] = DaemonSetCell(std[i])
|
||||
}
|
||||
return cells
|
||||
}
|
||||
|
||||
func FromCells(cells []dataselect.DataCell) []apps.DaemonSet {
|
||||
std := make([]apps.DaemonSet, len(cells))
|
||||
for i := range std {
|
||||
std[i] = apps.DaemonSet(cells[i].(DaemonSetCell))
|
||||
}
|
||||
return std
|
||||
}
|
||||
56
apps/devops/services/k8s/daemonset/daemonset_detail.go
Normal file
56
apps/devops/services/k8s/daemonset/daemonset_detail.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package daemonset
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
k8scommon "pandax/apps/devops/services/k8s/common"
|
||||
"pandax/apps/devops/services/pkg/k8s/service"
|
||||
)
|
||||
|
||||
// DaemonSetDetail represents detailed information about a Daemon Set.
|
||||
type DaemonSetDetail struct {
|
||||
// Extends list item structure.
|
||||
DaemonSet `json:",inline"`
|
||||
|
||||
LabelSelector *v1.LabelSelector `json:"labelSelector,omitempty"`
|
||||
|
||||
PodList *PodList `json:"podList"`
|
||||
|
||||
SvcList *service.ServiceList `json:"svcList"`
|
||||
}
|
||||
|
||||
// GetDaemonSetDetail Returns detailed information about the given daemon set in the given namespace.
|
||||
func GetDaemonSetDetail(client *kubernetes.Clientset, namespace, name string) (*DaemonSetDetail, error) {
|
||||
|
||||
global.Log.Info(fmt.Sprintf("Getting details of %s daemon set in %s namespace", name, namespace))
|
||||
daemonSet, err := client.AppsV1().DaemonSets(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
channels := &k8scommon.ResourceChannels{
|
||||
EventList: k8scommon.GetEventListChannel(client, k8scommon.NewSameNamespaceQuery(namespace), 1),
|
||||
PodList: k8scommon.GetPodListChannel(client, k8scommon.NewSameNamespaceQuery(namespace), 1),
|
||||
}
|
||||
|
||||
eventList := <-channels.EventList.List
|
||||
if err := <-channels.EventList.Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
podList := <-channels.PodList.List
|
||||
if err := <-channels.PodList.Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serviceList, _ := service.GetToService(client, namespace, name)
|
||||
return &DaemonSetDetail{
|
||||
DaemonSet: toDaemonSet(*daemonSet, podList.Items, eventList.Items),
|
||||
LabelSelector: daemonSet.Spec.Selector,
|
||||
PodList: getDaemonSetToPod(client, *daemonSet),
|
||||
SvcList: serviceList,
|
||||
}, nil
|
||||
}
|
||||
49
apps/devops/services/k8s/daemonset/daemonset_pods.go
Normal file
49
apps/devops/services/k8s/daemonset/daemonset_pods.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package daemonset
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
apps "k8s.io/api/apps/v1"
|
||||
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/event"
|
||||
"pandax/apps/devops/services/k8s/pods"
|
||||
)
|
||||
|
||||
type PodList struct {
|
||||
ListMeta k8s.ListMeta `json:"listMeta"`
|
||||
|
||||
// Basic information about resources status on the list.
|
||||
Status k8scommon.ResourceStatus `json:"status"`
|
||||
|
||||
// Unordered list of Pods.
|
||||
Pods []pods.Pod `json:"pods"`
|
||||
}
|
||||
|
||||
func getDaemonSetToPod(client *kubernetes.Clientset, daemonSet apps.DaemonSet) (po *PodList) {
|
||||
|
||||
selector, err := metaV1.LabelSelectorAsSelector(daemonSet.Spec.Selector)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
options := metaV1.ListOptions{LabelSelector: selector.String()}
|
||||
|
||||
podData, err := client.CoreV1().Pods(daemonSet.Namespace).List(context.TODO(), options)
|
||||
if err != nil {
|
||||
global.Log.Error("Get a pod exception from the statefulSet", zap.Any("err", err))
|
||||
}
|
||||
podList := PodList{
|
||||
Pods: make([]pods.Pod, 0),
|
||||
}
|
||||
podList.ListMeta = k8s.ListMeta{TotalItems: len(podData.Items)}
|
||||
for _, pod := range podData.Items {
|
||||
warnings := event.GetPodsEventWarnings(nil, []v1.Pod{pod})
|
||||
podDetail := pods.ToPod(&pod, warnings)
|
||||
podList.Pods = append(podList.Pods, podDetail)
|
||||
}
|
||||
return &podList
|
||||
}
|
||||
Reference in New Issue
Block a user