-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds in the ability to run individual jobs on demand (#19)
* Adds in the ability to run individual jobs on demand * Supes up the job running process to properly manage successful attaches. * Fixed up hope.yaml job definition to not reference local stuff.
- Loading branch information
Showing
9 changed files
with
290 additions
and
69 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
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,110 @@ | ||
package cmd | ||
|
||
import ( | ||
"io/ioutil" | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
import ( | ||
"github.com/Eagerod/hope/pkg/envsubst" | ||
"github.com/Eagerod/hope/pkg/hope" | ||
) | ||
|
||
var runCmdParameterSlice *[]string | ||
|
||
func initRunCmdFlags() { | ||
runCmdParameterSlice = runCmd.Flags().StringArrayP("param", "p", []string{}, "parameters to populate in the job yaml") | ||
} | ||
|
||
var runCmd = &cobra.Command{ | ||
Use: "run", | ||
Short: "Execute and follow a Kubernetes job", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
jobName := args[0] | ||
|
||
job, err := getJob(jobName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
remainingParams := map[string]bool{} | ||
for _, param := range job.Parameters { | ||
remainingParams[param] = true | ||
} | ||
|
||
params := map[string]string{} | ||
for _, param := range *runCmdParameterSlice { | ||
components := strings.Split(param, "=") | ||
if len(components) != 2 { | ||
return errors.New(fmt.Sprintf("Failed to parse argument: %s", param)) | ||
} | ||
|
||
paramName := components[0] | ||
paramValue := components[1] | ||
|
||
params[paramName] = paramValue | ||
if _, ok := remainingParams[paramName]; !ok { | ||
return errors.New(fmt.Sprintf("Parameter: %s not recognized", paramName)) | ||
} | ||
|
||
remainingParams[paramName] = false | ||
} | ||
|
||
for param, missed := range remainingParams { | ||
if missed { | ||
return errors.New(fmt.Sprintf("Failed to find parameter: %s", param)) | ||
} | ||
} | ||
|
||
// Pull kubeconfig from remote as late as possible to avoid extra | ||
// network time before validation is done. | ||
masters := viper.GetStringSlice("masters") | ||
kubectl, err := getKubectlFromAnyMaster(log.WithFields(log.Fields{}), masters) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer kubectl.Destroy() | ||
|
||
// TODO: Move to pkg | ||
jobContents, err := ioutil.ReadFile(job.File) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
jobText, err := envsubst.GetEnvsubstArgs(params, string(jobContents)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
output, err := hope.KubectlGetCreateStdIn(kubectl, jobText) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Grab the job name from the output | ||
re, err := regexp.Compile("job\\.batch/([^\\s]+)") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
kubeJobNameMatches := re.FindStringSubmatch(output) | ||
if len(kubeJobNameMatches) != 2 { | ||
return errors.New(fmt.Sprintf("Failed to parse job name from output: %s", output)) | ||
} | ||
|
||
kubeJobName := kubeJobNameMatches[1] | ||
|
||
return hope.FollowLogsAndPollUntilJobComplete(log.WithFields(log.Fields{}), kubectl, kubeJobName, 10, 12) | ||
}, | ||
} |
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
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
Oops, something went wrong.