Skip to content

Commit af8f733

Browse files
clamoriniere1Asdminonne
authored andcommitted
Add DaemonSetJob support in kubectl plugin (AmadeusITGroup#91)
Issue: AmadeusITGroup#71
1 parent 45aa4bf commit af8f733

File tree

19 files changed

+1308
-0
lines changed

19 files changed

+1308
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/beorn7/perks v0.0.0-20160229213445-3ac7bf7a47d1 // indirect
99
github.com/davecgh/go-spew v0.0.0-20170626231645-782f4967f2dc // indirect
1010
github.com/dgrijalva/jwt-go v0.0.0-20160705203006-01aeca54ebda // indirect
11+
github.com/dustin/go-humanize v1.0.0
1112
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633 // indirect
1213
github.com/emicklei/go-restful-swagger12 v0.0.0-20170208215640-dcef7f557305 // indirect
1314
github.com/fsnotify/fsnotify v1.4.7 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/davecgh/go-spew v0.0.0-20170626231645-782f4967f2dc h1:0A0n6a0Y3vW5kto
1212
github.com/davecgh/go-spew v0.0.0-20170626231645-782f4967f2dc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1313
github.com/dgrijalva/jwt-go v0.0.0-20160705203006-01aeca54ebda h1:NyywMz59neOoVRFDz+ccfKWxn784fiHMDnZSy6T+JXY=
1414
github.com/dgrijalva/jwt-go v0.0.0-20160705203006-01aeca54ebda/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
15+
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
16+
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
1517
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633 h1:H2pdYOb3KQ1/YsqVWoWNLQO+fusocsw354rqGTZtAgw=
1618
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
1719
github.com/emicklei/go-restful-swagger12 v0.0.0-20170208215640-dcef7f557305 h1:2vAWk0wMCWb/pYiyat2rRZp5I5ZM+efPlagySNZ3JeM=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package daemonsetjob
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
"github.com/dustin/go-humanize"
9+
"github.com/spf13/cobra"
10+
11+
kapiv1 "k8s.io/api/core/v1"
12+
13+
apierrors "k8s.io/apimachinery/pkg/api/errors"
14+
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
16+
kclient "k8s.io/client-go/kubernetes"
17+
18+
"github.com/amadeusitgroup/workflow-controller/kubectl-plugin/utils"
19+
api "github.com/amadeusitgroup/workflow-controller/pkg/api/daemonsetjob/v1"
20+
wfversioned "github.com/amadeusitgroup/workflow-controller/pkg/client/clientset/versioned"
21+
)
22+
23+
var kubeClient *kclient.Clientset
24+
var workflowClient wfversioned.Interface
25+
26+
// NewCmd returns WorkflowCmd instance
27+
func NewCmd(kc *kclient.Clientset, wfc wfversioned.Interface) *cobra.Command {
28+
kubeClient = kc
29+
workflowClient = wfc
30+
31+
return &cobra.Command{
32+
Use: "daemonsetjob",
33+
Short: "daemonsetjob shows daemonsetjob custom resources",
34+
Long: `daemonsetjob shows daemonsetjob custom resources`,
35+
RunE: func(cmd *cobra.Command, args []string) error {
36+
return processGetDaemonSetJob(cmd, args)
37+
},
38+
}
39+
}
40+
41+
func processGetDaemonSetJob(cmdC *cobra.Command, args []string) error {
42+
var err error
43+
namespace := os.Getenv("KUBECTL_PLUGINS_CURRENT_NAMESPACE")
44+
45+
daemonSetJobName := ""
46+
if len(args) > 0 {
47+
daemonSetJobName = args[0]
48+
}
49+
50+
dsjs := &api.DaemonSetJobList{}
51+
if daemonSetJobName == "" {
52+
dsjs, err = workflowClient.DaemonsetjobV1().DaemonSetJobs(namespace).List(meta_v1.ListOptions{})
53+
if err != nil {
54+
return fmt.Errorf("unable to list Workflows err:%v", err)
55+
}
56+
} else {
57+
dsj, err := workflowClient.DaemonsetjobV1().DaemonSetJobs(namespace).Get(daemonSetJobName, meta_v1.GetOptions{})
58+
if err == nil && dsj != nil {
59+
dsjs.Items = append(dsjs.Items, *dsj)
60+
}
61+
if err != nil && !apierrors.IsNotFound(err) {
62+
return fmt.Errorf("unable to get Workflow err:%v", err)
63+
}
64+
}
65+
66+
utils.GenerateTable([]string{"Namespace", "Name", "Status", "Active", "Succeeded", "Failed", "Node Selector", "Age"}, computeTableData(kubeClient, dsjs))
67+
return nil
68+
}
69+
70+
func hasStatus(dsj *api.DaemonSetJob, conditionType api.DaemonSetJobConditionType, status kapiv1.ConditionStatus) bool {
71+
for _, cond := range dsj.Status.Conditions {
72+
if cond.Type == conditionType && cond.Status == status {
73+
return true
74+
}
75+
}
76+
return false
77+
}
78+
79+
func buildDaemonSetJobStatus(dsj *api.DaemonSetJob) string {
80+
status := "Created"
81+
if dsj.Status.StartTime != nil {
82+
status = "Running"
83+
}
84+
if hasStatus(dsj, api.DaemonSetJobComplete, kapiv1.ConditionTrue) {
85+
status = string(api.DaemonSetJobComplete)
86+
} else if hasStatus(dsj, api.DaemonSetJobFailed, kapiv1.ConditionTrue) {
87+
status = string(api.DaemonSetJobFailed)
88+
}
89+
90+
return status
91+
}
92+
93+
func computeTableData(kubeClient *kclient.Clientset, dsjs *api.DaemonSetJobList) [][]string {
94+
data := [][]string{}
95+
96+
for _, dsj := range dsjs.Items {
97+
status := buildDaemonSetJobStatus(&dsj)
98+
active := fmt.Sprintf("%d", dsj.Status.Active)
99+
succeeded := fmt.Sprintf("%d", dsj.Status.Succeeded)
100+
failed := fmt.Sprintf("%d", dsj.Status.Failed)
101+
nodeSelector := "<none>"
102+
if dsj.Spec.NodeSelector != nil {
103+
nodeSelector = dsj.Spec.NodeSelector.String()
104+
}
105+
age := translateTimestampSince(dsj.CreationTimestamp)
106+
107+
data = append(data, []string{dsj.Namespace, dsj.Name, status, active, succeeded, failed, nodeSelector, age})
108+
}
109+
110+
return data
111+
}
112+
113+
// translateTimestampSince returns the elapsed time since timestamp in
114+
// human-readable approximation.
115+
func translateTimestampSince(timestamp meta_v1.Time) string {
116+
if timestamp.IsZero() {
117+
return "<unknown>"
118+
}
119+
120+
return humanize.RelTime(timestamp.Time, time.Now(), "", "")
121+
}

kubectl-plugin/cmd/root.go

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
3232

3333
"github.com/amadeusitgroup/workflow-controller/kubectl-plugin/cmd/cronworkflow"
34+
"github.com/amadeusitgroup/workflow-controller/kubectl-plugin/cmd/daemonsetjob"
3435
"github.com/amadeusitgroup/workflow-controller/kubectl-plugin/cmd/workflow"
3536
wfclient "github.com/amadeusitgroup/workflow-controller/pkg/client"
3637
wfversioned "github.com/amadeusitgroup/workflow-controller/pkg/client/clientset/versioned"
@@ -76,6 +77,7 @@ func init() {
7677
// Register sub commands
7778
RootCmd.AddCommand(workflow.NewCmd(kubeClient, workflowClient))
7879
RootCmd.AddCommand(cronworkflow.NewCmd(kubeClient, workflowClient))
80+
RootCmd.AddCommand(daemonsetjob.NewCmd(kubeClient, workflowClient))
7981
}
8082

8183
// initConfig reads in config file and ENV variables if set.

vendor/github.com/dustin/go-humanize/.travis.yml

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/dustin/go-humanize/LICENSE

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/dustin/go-humanize/README.markdown

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/dustin/go-humanize/big.go

+31
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)