Skip to content

Commit

Permalink
Add job suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 authored and c-bata committed Dec 10, 2018
1 parent 1f71e74 commit b3066c2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
9 changes: 9 additions & 0 deletions kube/completer.go
Expand Up @@ -91,6 +91,7 @@ func argumentsCompleter(args []string) []prompt.Suggest {
{Text: "horizontalpodautoscalers"},
{Text: "ingresses"},
{Text: "jobs"},
{Text: "cronjobs"},
{Text: "limitranges"},
{Text: "namespaces"},
{Text: "networkpolicies"},
Expand Down Expand Up @@ -176,6 +177,8 @@ func argumentsCompleter(args []string) []prompt.Suggest {
return prompt.FilterContains(getServiceAccountSuggestions(), third, true)
case "svc", "services":
return prompt.FilterContains(getServiceSuggestions(), third, true)
case "job", "jobs":
return prompt.FilterContains(getJobSuggestions(), third, true)
}
}
case "describe":
Expand Down Expand Up @@ -227,6 +230,8 @@ func argumentsCompleter(args []string) []prompt.Suggest {
return prompt.FilterContains(getServiceAccountSuggestions(), third, true)
case "svc", "services":
return prompt.FilterContains(getServiceSuggestions(), third, true)
case "job", "jobs":
return prompt.FilterContains(getJobSuggestions(), third, true)
}
}
case "create":
Expand Down Expand Up @@ -291,6 +296,8 @@ func argumentsCompleter(args []string) []prompt.Suggest {
return prompt.FilterContains(getServiceAccountSuggestions(), third, true)
case "svc", "services":
return prompt.FilterContains(getServiceSuggestions(), third, true)
case "job", "jobs":
return prompt.FilterContains(getJobSuggestions(), third, true)
}
}
case "edit":
Expand Down Expand Up @@ -341,6 +348,8 @@ func argumentsCompleter(args []string) []prompt.Suggest {
return prompt.FilterContains(getServiceAccountSuggestions(), third, true)
case "svc", "services":
return prompt.FilterContains(getServiceSuggestions(), third, true)
case "job", "jobs":
return prompt.FilterContains(getJobSuggestions(), third, true)
}
}

Expand Down
44 changes: 43 additions & 1 deletion kube/resource.go
Expand Up @@ -9,11 +9,13 @@ import (
"sync/atomic"
"time"

"github.com/c-bata/go-prompt"
prompt "github.com/c-bata/go-prompt"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/api"
batch_v1 "k8s.io/client-go/pkg/apis/batch/v1"
)

const thresholdFetchInterval = 10 * time.Second
Expand All @@ -29,6 +31,7 @@ var resourceTypes = []prompt.Suggest{
{Text: "horizontalpodautoscalers"},
{Text: "ingresses"},
{Text: "jobs"},
{Text: "cronjobs"},
{Text: "limitranges"},
{Text: "namespaces"},
{Text: "networkpolicies"},
Expand Down Expand Up @@ -87,6 +90,7 @@ func init() {
resourceQuotaList = new(sync.Map)
serviceAccountList = new(sync.Map)
serviceList = new(sync.Map)
jobList = new(sync.Map)
}

/* LastFetchedAt */
Expand Down Expand Up @@ -960,3 +964,41 @@ func getServiceSuggestions() []prompt.Suggest {
}
return s
}

/* Job */

var (
jobList *sync.Map
)

func fetchJobs(namespace string) {
key := "job_" + namespace
if !shouldFetch(key) {
return
}
updateLastFetchedAt(key)

l, _ := getClient().BatchV1().Jobs(namespace).List(metav1.ListOptions{})
jobList.Store(namespace, l)
}

func getJobSuggestions() []prompt.Suggest {
namespace := api.NamespaceAll
go fetchJobs(namespace)
x, ok := jobList.Load(namespace)
if !ok {
return []prompt.Suggest{}
}
l, ok := x.(*batch_v1.JobList)
if !ok || len(l.Items) == 0 {
return []prompt.Suggest{}
}
s := make([]prompt.Suggest, len(l.Items))
for i := range l.Items {
s[i] = prompt.Suggest{
Text: l.Items[i].Name,
Description: l.Items[i].Status.StartTime.String(),
}
}
return s
}

0 comments on commit b3066c2

Please sign in to comment.