-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from warjiang/feature/job-api
feat: add api for job resource
- Loading branch information
Showing
8 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package deployment | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/karmada-io/dashboard/cmd/api/app/router" | ||
"github.com/karmada-io/dashboard/cmd/api/app/types/common" | ||
"github.com/karmada-io/dashboard/pkg/client" | ||
"github.com/karmada-io/dashboard/pkg/resource/event" | ||
"github.com/karmada-io/dashboard/pkg/resource/job" | ||
) | ||
|
||
func handleGetJob(c *gin.Context) { | ||
namespace := common.ParseNamespacePathParameter(c) | ||
dataSelect := common.ParseDataSelectPathParameter(c) | ||
k8sClient := client.InClusterClientForKarmadaApiServer() | ||
result, err := job.GetJobList(k8sClient, namespace, dataSelect) | ||
if err != nil { | ||
common.Fail(c, err) | ||
return | ||
} | ||
common.Success(c, result) | ||
} | ||
|
||
func handleGetJobDetail(c *gin.Context) { | ||
namespace := c.Param("namespace") | ||
name := c.Param("statefulset") | ||
k8sClient := client.InClusterClientForKarmadaApiServer() | ||
result, err := job.GetJobDetail(k8sClient, namespace, name) | ||
if err != nil { | ||
common.Fail(c, err) | ||
return | ||
} | ||
common.Success(c, result) | ||
} | ||
|
||
func handleGetJobEvents(c *gin.Context) { | ||
namespace := c.Param("namespace") | ||
name := c.Param("statefulset") | ||
k8sClient := client.InClusterClientForKarmadaApiServer() | ||
dataSelect := common.ParseDataSelectPathParameter(c) | ||
result, err := event.GetResourceEvents(k8sClient, dataSelect, namespace, name) | ||
if err != nil { | ||
common.Fail(c, err) | ||
return | ||
} | ||
common.Success(c, result) | ||
} | ||
func init() { | ||
r := router.V1() | ||
r.GET("/job", handleGetJob) | ||
r.GET("/job/:namespace", handleGetJob) | ||
r.GET("/job/:namespace/:statefulset", handleGetJobDetail) | ||
r.GET("/job/:namespace/:statefulset/event", handleGetJobEvents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package job | ||
|
||
import ( | ||
"github.com/karmada-io/dashboard/pkg/dataselect" | ||
"github.com/karmada-io/dashboard/pkg/resource/common" | ||
batch "k8s.io/api/batch/v1" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
// The code below allows to perform complex data section on []batch.Job | ||
|
||
type JobCell batch.Job | ||
|
||
func (self JobCell) 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 []batch.Job) []dataselect.DataCell { | ||
cells := make([]dataselect.DataCell, len(std)) | ||
for i := range std { | ||
cells[i] = JobCell(std[i]) | ||
} | ||
return cells | ||
} | ||
|
||
func FromCells(cells []dataselect.DataCell) []batch.Job { | ||
std := make([]batch.Job, len(cells)) | ||
for i := range std { | ||
std[i] = batch.Job(cells[i].(JobCell)) | ||
} | ||
return std | ||
} | ||
|
||
func getStatus(list *batch.JobList, pods []v1.Pod) common.ResourceStatus { | ||
info := common.ResourceStatus{} | ||
if list == nil { | ||
return info | ||
} | ||
|
||
for _, job := range list.Items { | ||
matchingPods := common.FilterPodsForJob(job, pods) | ||
podInfo := common.GetPodInfo(job.Status.Active, job.Spec.Completions, matchingPods) | ||
jobStatus := getJobStatus(&job) | ||
|
||
if jobStatus.Status == JobStatusFailed { | ||
info.Failed++ | ||
} else if jobStatus.Status == JobStatusComplete { | ||
info.Succeeded++ | ||
} else if podInfo.Running > 0 { | ||
info.Running++ | ||
} else { | ||
info.Pending++ | ||
} | ||
} | ||
|
||
return info | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package job | ||
|
||
import ( | ||
"context" | ||
"github.com/karmada-io/dashboard/pkg/common/errors" | ||
"github.com/karmada-io/dashboard/pkg/resource/common" | ||
|
||
batch "k8s.io/api/batch/v1" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8sClient "k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// JobDetail is a presentation layer view of Kubernetes Job resource. | ||
type JobDetail struct { | ||
// Extends list item structure. | ||
Job `json:",inline"` | ||
|
||
// Completions specifies the desired number of successfully finished pods the job should be run with. | ||
Completions *int32 `json:"completions"` | ||
|
||
// List of non-critical errors, that occurred during resource retrieval. | ||
Errors []error `json:"errors"` | ||
} | ||
|
||
// GetJobDetail gets job details. | ||
func GetJobDetail(client k8sClient.Interface, namespace, name string) (*JobDetail, error) { | ||
jobData, err := client.BatchV1().Jobs(namespace).Get(context.TODO(), name, metaV1.GetOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
podInfo, err := getJobPodInfo(client, jobData) | ||
nonCriticalErrors, criticalError := errors.ExtractErrors(err) | ||
if criticalError != nil { | ||
return nil, criticalError | ||
} | ||
|
||
job := toJobDetail(jobData, *podInfo, nonCriticalErrors) | ||
return &job, nil | ||
} | ||
|
||
func toJobDetail(job *batch.Job, podInfo common.PodInfo, nonCriticalErrors []error) JobDetail { | ||
return JobDetail{ | ||
Job: toJob(job, &podInfo), | ||
Completions: job.Spec.Completions, | ||
Errors: nonCriticalErrors, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package job | ||
|
||
import ( | ||
"github.com/karmada-io/dashboard/pkg/dataselect" | ||
"github.com/karmada-io/dashboard/pkg/resource/common" | ||
"github.com/karmada-io/dashboard/pkg/resource/event" | ||
client "k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// GetJobEvents gets events associated to job. | ||
func GetJobEvents(client client.Interface, dsQuery *dataselect.DataSelectQuery, namespace, name string) ( | ||
*common.EventList, error) { | ||
|
||
jobEvents, err := event.GetEvents(client, namespace, name) | ||
if err != nil { | ||
return event.EmptyEventList, err | ||
} | ||
|
||
events := event.CreateEventList(jobEvents, dsQuery) | ||
return &events, nil | ||
} |
Oops, something went wrong.