-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Job CLI deploy/list/export/remove #119
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,90 @@ | ||
#!/usr/bin/env bats | ||
|
||
# To run locally: | ||
# export KETCH_EXECUTABLE_PATH=<location of ketch binary> | ||
# assure you have a kubernetes cluster running w/ traefik, cert manager, etc. (see ketch getting started docs) | ||
# assure the ketch cli is compiled (make ketch) | ||
# assure you have bats installed locally (via apt, brew, etc.) | ||
# ./cli_tests/app.sh | ||
|
||
setup() { | ||
if [[ -z "${KETCH_EXECUTABLE_PATH}" ]]; then | ||
KETCH=$(pwd)/bin/ketch | ||
else | ||
KETCH="${KETCH_EXECUTABLE_PATH}" | ||
fi | ||
|
||
JOB_FRAMEWORK="jobframework" | ||
JOB_NAME="sample-job" | ||
} | ||
|
||
teardown() { | ||
rm -f job.yaml | ||
} | ||
|
||
@test "job help" { | ||
result="$($KETCH job --help)" | ||
echo "RECEIVED:" $result | ||
[[ $result =~ "deploy" ]] | ||
[[ $result =~ "list" ]] | ||
[[ $result =~ "export" ]] | ||
[[ $result =~ "remove" ]] | ||
} | ||
|
||
@test "job deploy with yaml file" { | ||
fwresult=$($KETCH framework add "$JOB_FRAMEWORK") | ||
echo "RECEIVED:" $fwresult | ||
[[ $fwresult =~ "Successfully added!" ]] | ||
|
||
cat << EOF > job.yaml | ||
name: "$JOB_NAME" | ||
version: v1 | ||
type: Job | ||
framework: "$JOB_FRAMEWORK" | ||
description: "cli test job" | ||
containers: | ||
- name: pi | ||
image: perl | ||
command: | ||
- "perl" | ||
- "-Mbignum=bpi" | ||
- "-wle" | ||
- "print bpi(2000)" | ||
EOF | ||
result=$($KETCH job deploy job.yaml) | ||
[[ $result =~ "Successfully added!" ]] | ||
|
||
dataRegex="$JOB_NAME[ \t]+v1[ \t]+$JOB_FRAMEWORK[ \t]+cli test job" | ||
result=$($KETCH job list $JOB_NAME) | ||
echo "RECEIVED:" $result | ||
[[ $result =~ $dataRegex ]] | ||
} | ||
|
||
@test "job list" { | ||
result=$($KETCH job list) | ||
headerRegex="NAME[ \t]+VERSION[ \t]+FRAMEWORK[ \t]+DESCRIPTION" | ||
dataRegex="$JOB_NAME[ \t]+v1[ \t]+$JOB_FRAMEWORK[ \t]+cli test job" | ||
echo "RECEIVED:" $result | ||
[[ $result =~ $headerRegex ]] | ||
[[ $result =~ $dataRegex ]] | ||
} | ||
|
||
@test "job export" { | ||
run $KETCH job export "$JOB_NAME" -f job.yaml | ||
result=$(cat job.yaml) | ||
echo "RECEIVED:" $result | ||
[[ $result =~ "name: $JOB_NAME" ]] | ||
[[ $result =~ "type: Job" ]] | ||
[[ $result =~ "framework: $JOB_FRAMEWORK" ]] | ||
} | ||
|
||
@test "job remove" { | ||
result=$($KETCH job remove "$JOB_NAME") | ||
echo "RECEIVED:" $result | ||
[[ $result =~ "Successfully removed!" ]] | ||
|
||
# clean up framework | ||
fwresult=$(echo "ketch-$JOB_FRAMEWORK" | $KETCH framework remove "$JOB_FRAMEWORK") | ||
echo "RECEIVED:" $fwresult | ||
[[ $fwresult =~ "Framework successfully removed!" ]] | ||
} |
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
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,27 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
const jobHelp = ` | ||
Manage jobs. | ||
` | ||
|
||
func newJobCmd(cfg config, out io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "job", | ||
Short: "Manage Jobs", | ||
Long: jobHelp, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return cmd.Usage() | ||
}, | ||
} | ||
cmd.AddCommand(newJobListCmd(cfg, out)) | ||
cmd.AddCommand(newJobDeployCmd(cfg, out)) | ||
cmd.AddCommand(newJobRemoveCmd(cfg, out)) | ||
cmd.AddCommand(newJobExportCmd(cfg, out)) | ||
return cmd | ||
} |
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,98 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
"sigs.k8s.io/yaml" | ||
|
||
ketchv1 "github.com/shipa-corp/ketch/internal/api/v1beta1" | ||
) | ||
|
||
const jobDeployHelp = ` | ||
Deploy a job. | ||
` | ||
|
||
const ( | ||
defaultJobVersion = "v1" | ||
defaultJobParallelism = 1 | ||
defaultJobCompletions = 1 | ||
defaultJobBackoffLimit = 6 | ||
defaultJobRestartPolicy = "Never" | ||
) | ||
|
||
func newJobDeployCmd(cfg config, out io.Writer) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "deploy [FILENAME]", | ||
Short: "Deploy a job.", | ||
Long: jobDeployHelp, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
filename := args[0] | ||
return jobDeploy(cmd.Context(), cfg, filename, out) | ||
}, | ||
} | ||
return cmd | ||
} | ||
|
||
func jobDeploy(ctx context.Context, cfg config, filename string, out io.Writer) error { | ||
b, err := os.ReadFile(filename) | ||
if err != nil { | ||
return err | ||
} | ||
var spec ketchv1.JobSpec | ||
err = yaml.Unmarshal(b, &spec) | ||
if err != nil { | ||
return err | ||
} | ||
setJobSpecDefaults(&spec) | ||
if err = validateJobSpec(&spec); err != nil { | ||
return err | ||
} | ||
|
||
job := &ketchv1.Job{ObjectMeta: metav1.ObjectMeta{Name: spec.Name}} | ||
_, err = controllerutil.CreateOrUpdate(ctx, cfg.Client(), job, func() error { | ||
job.Spec = spec | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintln(out, "Successfully added!") | ||
return nil | ||
} | ||
|
||
// setJobSpecDefaults sets defaults on job.Spec for some unset fields | ||
func setJobSpecDefaults(jobSpec *ketchv1.JobSpec) { | ||
jobSpec.Type = "Job" | ||
if jobSpec.Version == "" { | ||
jobSpec.Version = defaultJobVersion | ||
} | ||
if jobSpec.Parallelism == 0 { | ||
jobSpec.Parallelism = defaultJobParallelism | ||
} | ||
if jobSpec.Completions == 0 && jobSpec.Parallelism > 1 { | ||
jobSpec.Completions = defaultJobCompletions | ||
} | ||
if jobSpec.BackoffLimit == 0 { | ||
jobSpec.BackoffLimit = defaultJobBackoffLimit | ||
} | ||
if jobSpec.Policy.RestartPolicy == "" { | ||
jobSpec.Policy.RestartPolicy = defaultJobRestartPolicy | ||
} | ||
} | ||
|
||
// validateJobSpec assures that required fields are populated. Missing fields will throw errors | ||
// when the custom resource is created, but this is a way to surface errors to user clearly. | ||
func validateJobSpec(jobSpec *ketchv1.JobSpec) error { | ||
if jobSpec.Name == "" || jobSpec.Framework == "" { | ||
return errors.New("job.name and job.framework are required") | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍