-
Notifications
You must be signed in to change notification settings - Fork 6
/
findadhocjob.go
54 lines (45 loc) · 1.36 KB
/
findadhocjob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package client
import (
"encoding/json"
"fmt"
"github.com/wish/ctl/cmd/util/config"
"github.com/wish/ctl/pkg/client/types"
)
// FindAdhocJob loops through all valid contexts and returns the first active job found
func (c *Client) FindAdhocJob(appName string, options ListOptions) (*types.JobDiscovery, error) {
// Get all kubernetes contexts from config file
config, err := config.GetCtlExt()
if err != nil {
return nil, err
}
for ctx := range config {
if rawruns, ok := config[ctx]["_run"]; ok {
runs := make(map[string]types.RunDetails)
err := json.Unmarshal([]byte(rawruns), &runs)
if err != nil {
continue
}
// Check if the app name exists in the raw runs
if run, ok := runs[appName]; ok {
if run.Active {
// Extract manifest json as struct to parse
var manifestData types.ManifestDetails
err = json.Unmarshal([]byte(run.Manifest), &manifestData)
if err != nil {
return nil, fmt.Errorf("Error parsing manifestJson: %s", err)
}
// Check if a job is already running
jobs, err := c.ListJobs(ctx, manifestData.Metadata.Namespace, options)
if err != nil {
return nil, fmt.Errorf("Failed to search for existing job: %s", err)
}
// Return the first job since we limit adhoc pods to one
if len(jobs) > 0 {
return &jobs[0], nil
}
}
}
}
}
return nil, nil
}