【修改】k8s 配置

This commit is contained in:
PandaGoAdmin
2022-01-22 17:07:04 +08:00
parent c6ebe89865
commit 33cc74711d
439 changed files with 9936 additions and 21687 deletions

View File

@@ -0,0 +1,235 @@
package statefulset
import (
"context"
"fmt"
"go.uber.org/zap"
apps "k8s.io/api/apps/v1"
autoscalingv1 "k8s.io/api/autoscaling/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"
)
// StatefulSetList contains a list of Stateful Sets in the cluster.
type StatefulSetList struct {
ListMeta k8s.ListMeta `json:"listMeta"`
Status k8scommon.ResourceStatus `json:"status"`
StatefulSets []StatefulSet `json:"statefulSets"`
}
// StatusInfo is the status information of the statefulSet
type StatusInfo struct {
// replicas is the number of Pods created by the StatefulSet controller.
Replicas int32 `json:"replicas"`
// Number of non-terminated pods that have the desired template spec
Updated int32 `json:"updated"`
// readyReplicas is the number of Pods created by the StatefulSet controller that have a Ready Condition.
ReadyReplicas int32 `json:"readyReplicas,omitempty"`
// currentReplicas is the number of Pods created by the StatefulSet controller from the StatefulSet version
// indicated by currentRevision.
CurrentReplicas int32 `json:"currentReplicas,omitempty"`
// updatedReplicas is the number of Pods created by the StatefulSet controller from the StatefulSet version
// indicated by updateRevision.
UpdatedReplicas int32 `json:"updatedReplicas,omitempty"`
// Total number of available pods (ready for at least minReadySeconds) targeted by this statefulset.
// This is an alpha field and requires enabling StatefulSetMinReadySeconds feature gate.
// Remove omitempty when graduating to beta
// +optional
AvailableReplicas int32 `json:"availableReplicas,omitempty"`
}
// StatefulSet is a presentation layer view of Kubernetes Stateful Set resource.
type StatefulSet struct {
ObjectMeta k8s.ObjectMeta `json:"objectMeta"`
TypeMeta k8s.TypeMeta `json:"typeMeta"`
Pods k8scommon.PodInfo `json:"podInfo"`
// Status information on the statefulSet
StatusInfo `json:"statusInfo"`
// Label selector
Selector *metav1.LabelSelector `json:"selector"`
// The statefulset strategy to use to replace existing pods with new ones.
// Valid options: Recreate, RollingUpdate
Strategy apps.StatefulSetUpdateStrategy `json:"strategy"`
ContainerImages []string `json:"containerImages"`
InitContainerImages []string `json:"initContainerImages"`
}
// GetStatefulSetList returns a list of all Stateful Sets in the cluster.
func GetStatefulSetList(client *kubernetes.Clientset, nsQuery *k8scommon.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*StatefulSetList, error) {
global.Log.Info("Getting list of all pet sets in the cluster")
channels := &k8scommon.ResourceChannels{
StatefulSetList: k8scommon.GetStatefulSetListChannel(client, nsQuery, 1),
PodList: k8scommon.GetPodListChannel(client, nsQuery, 1),
EventList: k8scommon.GetEventListChannel(client, nsQuery, 1),
}
return GetStatefulSetListFromChannels(channels, dsQuery)
}
// GetStatefulSetListFromChannels returns a list of all Stateful Sets in the cluster reading
// required resource list once from the channels.
func GetStatefulSetListFromChannels(channels *k8scommon.ResourceChannels, dsQuery *dataselect.DataSelectQuery) (*StatefulSetList, error) {
statefulSets := <-channels.StatefulSetList.List
err := <-channels.StatefulSetList.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
}
ssList := toStatefulSetList(statefulSets.Items, pods.Items, events.Items, dsQuery)
ssList.Status = getStatus(statefulSets, pods.Items, events.Items)
return ssList, nil
}
func toStatefulSetList(statefulSets []apps.StatefulSet, pods []v1.Pod, events []v1.Event, dsQuery *dataselect.DataSelectQuery) *StatefulSetList {
statefulSetList := &StatefulSetList{
StatefulSets: make([]StatefulSet, 0),
ListMeta: k8s.ListMeta{TotalItems: len(statefulSets)},
}
ssCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(statefulSets), dsQuery)
statefulSets = fromCells(ssCells)
statefulSetList.ListMeta = k8s.ListMeta{TotalItems: filteredTotal}
for _, statefulSet := range statefulSets {
matchingPods := k8scommon.FilterPodsByControllerRef(&statefulSet, pods)
podInfo := k8scommon.GetPodInfo(statefulSet.Status.Replicas, statefulSet.Spec.Replicas, matchingPods)
podInfo.Warnings = event.GetPodsEventWarnings(events, matchingPods)
statefulSetList.StatefulSets = append(statefulSetList.StatefulSets, toStatefulSet(&statefulSet, &podInfo))
}
return statefulSetList
}
func toStatefulSet(statefulSet *apps.StatefulSet, podInfo *k8scommon.PodInfo) StatefulSet {
return StatefulSet{
ObjectMeta: k8s.NewObjectMeta(statefulSet.ObjectMeta),
TypeMeta: k8s.NewTypeMeta(k8s.ResourceKindStatefulSet),
StatusInfo: GetStatusInfo(&statefulSet.Status),
Selector: statefulSet.Spec.Selector,
Strategy: statefulSet.Spec.UpdateStrategy,
ContainerImages: k8scommon.GetContainerImages(&statefulSet.Spec.Template.Spec),
InitContainerImages: k8scommon.GetInitContainerImages(&statefulSet.Spec.Template.Spec),
Pods: *podInfo,
}
}
func DeleteCollectionStatefulSet(client *kubernetes.Clientset, statefulSetList []k8s.StatefulSetData) (err error) {
global.Log.Info("批量删除statefulset开始")
for _, v := range statefulSetList {
global.Log.Info(fmt.Sprintf("delete statefulset%v, ns: %v", v.Name, v.Namespace))
err := client.AppsV1().StatefulSets(v.Namespace).Delete(
context.TODO(),
v.Name,
metav1.DeleteOptions{},
)
if err != nil {
global.Log.Error(err.Error())
return err
}
}
global.Log.Info("删除statefulset已完成")
return nil
}
func DeleteStatefulSet(client *kubernetes.Clientset, ns string, name string) (err error) {
global.Log.Info(fmt.Sprintf("请求删除单个statefulset%v, namespace: %v", name, ns))
return client.AppsV1().StatefulSets(ns).Delete(
context.TODO(),
name,
metav1.DeleteOptions{},
)
}
func RestartStatefulSet(client *kubernetes.Clientset, name string, namespace 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().StatefulSets(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
}
func ScaleStatefulSet(client *kubernetes.Clientset, ns string, name string, scaleNumber int32) (err error) {
global.Log.Info(fmt.Sprintf("start scale of %v statefulset in %v namespace", name, ns))
scaleData, err := client.AppsV1().StatefulSets(ns).GetScale(
context.TODO(),
name,
metav1.GetOptions{},
)
global.Log.Info(fmt.Sprintf("The statefulset has changed from %v to %v", scaleData.Spec.Replicas, scaleNumber))
scale := autoscalingv1.Scale{
TypeMeta: scaleData.TypeMeta,
ObjectMeta: scaleData.ObjectMeta,
Spec: autoscalingv1.ScaleSpec{Replicas: scaleNumber},
Status: scaleData.Status,
}
_, err = client.AppsV1().StatefulSets(ns).UpdateScale(
context.TODO(),
name,
&scale,
metav1.UpdateOptions{},
)
if err != nil {
global.Log.Error("扩缩容出现异常", zap.Any("err: ", err))
return err
}
return nil
}
// GetStatusInfo is used to get the status information from the *apps.StatefulSetStatus
func GetStatusInfo(statefulSetStatus *apps.StatefulSetStatus) StatusInfo {
return StatusInfo{
Replicas: statefulSetStatus.Replicas,
Updated: statefulSetStatus.UpdatedReplicas,
AvailableReplicas: statefulSetStatus.AvailableReplicas,
ReadyReplicas: statefulSetStatus.ReadyReplicas,
CurrentReplicas: statefulSetStatus.CurrentReplicas,
}
}

View File

@@ -0,0 +1,66 @@
package statefulset
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 []apps.StatefulSet
type StatefulSetCell apps.StatefulSet
func (self StatefulSetCell) 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 []apps.StatefulSet) []dataselect.DataCell {
cells := make([]dataselect.DataCell, len(std))
for i := range std {
cells[i] = StatefulSetCell(std[i])
}
return cells
}
func fromCells(cells []dataselect.DataCell) []apps.StatefulSet {
std := make([]apps.StatefulSet, len(cells))
for i := range std {
std[i] = apps.StatefulSet(cells[i].(StatefulSetCell))
}
return std
}
func getStatus(list *apps.StatefulSetList, pods []v1.Pod, events []v1.Event) common.ResourceStatus {
info := common.ResourceStatus{}
if list == nil {
return info
}
for _, ss := range list.Items {
matchingPods := common.FilterPodsByControllerRef(&ss, pods)
podInfo := common.GetPodInfo(ss.Status.Replicas, ss.Spec.Replicas, matchingPods)
warnings := event.GetPodsEventWarnings(events, matchingPods)
if len(warnings) > 0 {
info.Failed++
} else if podInfo.Pending > 0 {
info.Pending++
} else {
info.Running++
}
}
return info
}

View File

@@ -0,0 +1,60 @@
package statefulset
import (
"context"
"fmt"
apps "k8s.io/api/apps/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
k8scommon "pandax/apps/devops/services/k8s/common"
"pandax/apps/devops/services/k8s/dataselect"
"pandax/apps/devops/services/k8s/event"
"pandax/apps/devops/services/pkg/k8s/service"
)
// StatefulSetDetail is a presentation layer view of Kubernetes Stateful Set resource. This means it is Stateful
type StatefulSetDetail struct {
// Extends list item structure.
StatefulSet `json:",inline"`
Events *k8scommon.EventList `json:"events"`
PodList *PodList `json:"podList"`
SvcList *service.ServiceList `json:"svcList"`
}
// GetStatefulSetDetail gets Stateful Set details.
func GetStatefulSetDetail(client *kubernetes.Clientset, dsQuery *dataselect.DataSelectQuery, namespace, name string) (*StatefulSetDetail, error) {
global.Log.Info(fmt.Sprintf("Getting details of %s statefulset in %s namespace", name, namespace))
ss, err := client.AppsV1().StatefulSets(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
if err != nil {
return nil, err
}
podInfo, err := getStatefulSetPodInfo(client, ss)
if err != nil {
return nil, err
}
events, err := event.GetResourceEvents(client, dsQuery, namespace, name)
if err != nil {
return nil, err
}
serviceList, _ := service.GetToService(client, namespace, name)
ssDetail := getStatefulSetDetail(ss, podInfo, events, serviceList, client)
return &ssDetail, nil
}
func getStatefulSetDetail(statefulSet *apps.StatefulSet, podInfo *k8scommon.PodInfo,
events *k8scommon.EventList, svc *service.ServiceList, client *kubernetes.Clientset) StatefulSetDetail {
return StatefulSetDetail{
StatefulSet: toStatefulSet(statefulSet, podInfo),
Events: events,
PodList: getStatefulSetToPod(client, statefulSet),
SvcList: svc,
}
}

View File

@@ -0,0 +1,79 @@
package statefulset
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"`
}
// Returns simple info about pods(running, desired, failing, etc.) related to given pet set.
func getStatefulSetPodInfo(client kubernetes.Interface, statefulSet *apps.StatefulSet) (*k8scommon.PodInfo, error) {
podList, err := getRawStatefulSetPods(client, statefulSet.Name, statefulSet.Namespace)
if err != nil {
return nil, err
}
podInfo := k8scommon.GetPodInfo(statefulSet.Status.Replicas, statefulSet.Spec.Replicas, podList)
return &podInfo, nil
}
// getRawStatefulSetPods return array of api pods targeting pet set with given name.
func getRawStatefulSetPods(client kubernetes.Interface, name, namespace string) ([]v1.Pod, error) {
statefulSet, err := client.AppsV1().StatefulSets(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
if err != nil {
return nil, err
}
channels := &k8scommon.ResourceChannels{
PodList: k8scommon.GetPodListChannel(client, k8scommon.NewSameNamespaceQuery(namespace), 1),
}
podList := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return nil, err
}
return k8scommon.FilterPodsByControllerRef(statefulSet, podList.Items), nil
}
func getStatefulSetToPod(client *kubernetes.Clientset, stateful *apps.StatefulSet) (po *PodList) {
selector, err := metaV1.LabelSelectorAsSelector(stateful.Spec.Selector)
if err != nil {
return nil
}
options := metaV1.ListOptions{LabelSelector: selector.String()}
podData, err := client.CoreV1().Pods(stateful.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
}