【修改】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,117 @@
package cronjob
import (
"context"
"fmt"
"pandax/base/global"
"k8s.io/api/batch/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
client "k8s.io/client-go/kubernetes"
"pandax/apps/devops/entity/k8s"
k8scommon "pandax/apps/devops/services/k8s/common"
"pandax/apps/devops/services/k8s/dataselect"
)
// CronJobList contains a list of CronJobs in the cluster.
type CronJobList struct {
ListMeta k8s.ListMeta `json:"listMeta"`
Items []CronJob `json:"items"`
// Basic information about resources status on the list.
Status k8scommon.ResourceStatus `json:"status"`
}
// CronJob is a presentation layer view of Kubernetes Cron Job resource.
type CronJob struct {
ObjectMeta k8s.ObjectMeta `json:"objectMeta"`
TypeMeta k8s.TypeMeta `json:"typeMeta"`
Schedule string `json:"schedule"`
Suspend *bool `json:"suspend"`
Active int `json:"active"`
LastSchedule *metav1.Time `json:"lastSchedule"`
// ContainerImages holds a list of the CronJob images.
ContainerImages []string `json:"containerImages"`
}
// GetCronJobList returns a list of all CronJobs in the cluster.
func GetCronJobList(client client.Interface, nsQuery *k8scommon.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*CronJobList, error) {
global.Log.Info("Getting list of all cron jobs in the cluster")
channels := &k8scommon.ResourceChannels{
CronJobList: k8scommon.GetCronJobListChannel(client, nsQuery, 1),
}
return GetCronJobListFromChannels(channels, dsQuery)
}
// GetCronJobListFromChannels returns a list of all CronJobs in the cluster reading required resource
// list once from the channels.
func GetCronJobListFromChannels(channels *k8scommon.ResourceChannels, dsQuery *dataselect.DataSelectQuery) (*CronJobList, error) {
cronJobs := <-channels.CronJobList.List
err := <-channels.CronJobList.Error
if err != nil {
return nil, err
}
cronJobList := toCronJobList(cronJobs.Items, dsQuery)
cronJobList.Status = getStatus(cronJobs)
return cronJobList, nil
}
func toCronJobList(cronJobs []v1beta1.CronJob, dsQuery *dataselect.DataSelectQuery) *CronJobList {
list := &CronJobList{
Items: make([]CronJob, 0),
ListMeta: k8s.ListMeta{TotalItems: len(cronJobs)},
}
cronJobCells, filteredTotal := dataselect.GenericDataSelectWithFilter(ToCells(cronJobs), dsQuery)
cronJobs = FromCells(cronJobCells)
list.ListMeta = k8s.ListMeta{TotalItems: filteredTotal}
for _, cronJob := range cronJobs {
list.Items = append(list.Items, toCronJob(&cronJob))
}
return list
}
func toCronJob(cj *v1beta1.CronJob) CronJob {
return CronJob{
ObjectMeta: k8s.NewObjectMeta(cj.ObjectMeta),
TypeMeta: k8s.NewTypeMeta(k8s.ResourceKindCronJob),
Schedule: cj.Spec.Schedule,
Suspend: cj.Spec.Suspend,
Active: len(cj.Status.Active),
LastSchedule: cj.Status.LastScheduleTime,
ContainerImages: getContainerImages(cj),
}
}
func DeleteCronJob(client *client.Clientset, namespace, name string) (err error) {
// for k8s version < 1.21.0, use batch/v1beta1
// if you use BatchV1 Will report an error: the server could not find the requested resource
// BatchV1beta1
return client.BatchV1beta1().CronJobs(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{})
}
func DeleteCollectionCronJob(client *client.Clientset, jobList []k8s.JobData) (err error) {
global.Log.Info("批量删除cronjob开始")
for _, v := range jobList {
global.Log.Info(fmt.Sprintf("delete cronjob%v, ns: %v", v.Name, v.Namespace))
err := client.BatchV1beta1().CronJobs(v.Namespace).Delete(
context.TODO(),
v.Name,
metav1.DeleteOptions{},
)
if err != nil {
global.Log.Error(err.Error())
return err
}
}
global.Log.Info("删除cronjob已完成")
return nil
}

View File

@@ -0,0 +1,69 @@
package cronjob
import (
batchv1beta1 "k8s.io/api/batch/v1beta1"
"pandax/apps/devops/services/k8s/common"
"pandax/apps/devops/services/k8s/dataselect"
)
// The code below allows to perform complex data section on []batch.CronJob
type CronJobCell batchv1beta1.CronJob
func (self CronJobCell) 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 []batchv1beta1.CronJob) []dataselect.DataCell {
cells := make([]dataselect.DataCell, len(std))
for i := range std {
cells[i] = CronJobCell(std[i])
}
return cells
}
func FromCells(cells []dataselect.DataCell) []batchv1beta1.CronJob {
std := make([]batchv1beta1.CronJob, len(cells))
for i := range std {
std[i] = batchv1beta1.CronJob(cells[i].(CronJobCell))
}
return std
}
func getStatus(list *batchv1beta1.CronJobList) common.ResourceStatus {
info := common.ResourceStatus{}
if list == nil {
return info
}
for _, cronJob := range list.Items {
if cronJob.Spec.Suspend != nil && !(*cronJob.Spec.Suspend) {
info.Running++
} else {
info.Failed++
}
}
return info
}
func getContainerImages(cronJob *batchv1beta1.CronJob) []string {
podSpec := cronJob.Spec.JobTemplate.Spec.Template.Spec
result := make([]string, 0)
for _, container := range podSpec.Containers {
result = append(result, container.Image)
}
return result
}

View File

@@ -0,0 +1,45 @@
package cronjob
import (
"context"
"encoding/json"
"fmt"
batch2 "k8s.io/api/batch/v1beta1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// CronJobDetail contains Cron Job details.
type CronJobDetail struct {
// Extends list item structure.
CronJob `json:",inline"`
ConcurrencyPolicy string `json:"concurrencyPolicy"`
StartingDeadLineSeconds *int64 `json:"startingDeadlineSeconds"`
JobList *JobList `json:"jobList"`
}
// GetCronJobDetail gets Cron Job details.
func GetCronJobDetail(client *kubernetes.Clientset, namespace, name string) (*CronJobDetail, error) {
rawObject, err := client.BatchV1beta1().CronJobs(namespace).Get(context.TODO(), name, metaV1.GetOptions{})
if err != nil {
return nil, err
}
j, _ := json.Marshal(rawObject)
fmt.Printf("cronJob: %s\n", j)
cj := toCronJobDetail(rawObject, client, name)
return &cj, nil
}
func toCronJobDetail(cj *batch2.CronJob, client *kubernetes.Clientset, name string) CronJobDetail {
return CronJobDetail{
CronJob: toCronJob(cj),
ConcurrencyPolicy: string(cj.Spec.ConcurrencyPolicy),
StartingDeadLineSeconds: cj.Spec.StartingDeadlineSeconds,
JobList: getJobList(client, cj, name),
}
}

View File

@@ -0,0 +1,59 @@
package cronjob
import (
"context"
"go.uber.org/zap"
batch "k8s.io/api/batch/v1"
batch2 "k8s.io/api/batch/v1beta1"
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/pkg/k8s/job"
"strings"
)
type JobList struct {
ListMeta k8s.ListMeta `json:"listMeta"`
// Basic information about resources status on the list.
Status k8scommon.ResourceStatus `json:"status"`
// Unordered list of Pods.
Jobs []job.Job `json:"jobs"`
}
func getJobList(client *kubernetes.Clientset, cj *batch2.CronJob, name string) (jo *JobList) {
jobData, err := client.BatchV1().Jobs(cj.Namespace).List(context.TODO(), metaV1.ListOptions{})
if err != nil {
global.Log.Error("Get a job list exception from the cronjob", zap.Any("err", err))
}
jobList := JobList{
Jobs: make([]job.Job, 0),
}
jobList.ListMeta = k8s.ListMeta{TotalItems: len(jobData.Items)}
for _, j := range jobData.Items {
if strings.Contains(j.Name, name) {
jobList.Jobs = append(jobList.Jobs, toJob(&j))
jobList.ListMeta = k8s.ListMeta{
TotalItems: len(jobList.Jobs),
}
}
}
return &jobList
}
func toJob(j *batch.Job) job.Job {
return job.Job{
ObjectMeta: k8s.NewObjectMeta(j.ObjectMeta),
TypeMeta: k8s.NewTypeMeta(k8s.ResourceKindJob),
ContainerImages: k8scommon.GetContainerImages(&j.Spec.Template.Spec),
InitContainerImages: k8scommon.GetInitContainerImages(&j.Spec.Template.Spec),
JobStatus: job.GetJobStatus(j),
PodStatus: job.GetPodStatus(j),
Parallelism: j.Spec.Parallelism,
}
}