Skip to content
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 3 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ jobs:
- bash cli_tests/install_minikube.sh
- bash cli_tests/install_bats.sh
- bash cli_tests/install_ketch.sh
- bats cli_tests/app.sh
- bats cli_tests/app.sh
- bats cli_tests/job.sh
90 changes: 90 additions & 0 deletions cli_tests/job.sh
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

# 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!" ]]
}
9 changes: 2 additions & 7 deletions cmd/ketch/app_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"fmt"
"io"
"os"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"

"github.com/shipa-corp/ketch/cmd/ketch/output"
ketchv1 "github.com/shipa-corp/ketch/internal/api/v1beta1"
"github.com/shipa-corp/ketch/internal/deploy"
)
Expand Down Expand Up @@ -51,12 +51,7 @@ func exportApp(ctx context.Context, cfg config, options appExportOptions, out io
}
application := deploy.GetApplicationFromKetchApp(app)
if options.filename != "" {
// open file, err if exist, write application
_, err := os.Stat(options.filename)
if !os.IsNotExist(err) {
return errFileExists
}
f, err := os.Create(options.filename)
f, err := output.GetOutputFile(options.filename)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/ketch/auto_completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ func autoCompleteFrameworkNames(cfg config, toComplete ...string) ([]string, cob
func autoCompleteBuilderNames(cfg config, toComplete ...string) ([]string, cobra.ShellCompDirective) {
return builderList.Names(toComplete...), cobra.ShellCompDirectiveNoSpace
}

func autoCompleteJobNames(cfg config, toComplete ...string) ([]string, cobra.ShellCompDirective) {
names, err := jobListNames(cfg, toComplete...)
if err != nil {
return []string{err.Error()}, cobra.ShellCompDirectiveError
}
return names, cobra.ShellCompDirectiveNoSpace
}
3 changes: 3 additions & 0 deletions cmd/ketch/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const (
ErrInvalidAppName cliError = "invalid app name, app name should have at most 40 " +
"characters, containing only lower case letters, numbers or dashes, starting with a letter"

ErrInvalidJobName cliError = "invalid job name, job name should have at most 40 " +
"characters, containing only lower case letters, numbers or dashes, starting with a letter"

ErrNoEntrypointAndCmd cliError = "image doesn't have entrypoint and cmd set"
ErrLogUnknownTimeFormat cliError = "unknown time format"

Expand Down
12 changes: 2 additions & 10 deletions cmd/ketch/framework_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package main

import (
"context"
"errors"
"io"
"os"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"

"github.com/shipa-corp/ketch/cmd/ketch/output"
ketchv1 "github.com/shipa-corp/ketch/internal/api/v1beta1"
)

Expand All @@ -20,8 +19,6 @@ type frameworkExportOptions struct {

const frameworkExportHelp = `Export a framework's configuration file.`

var errFileExists = errors.New("file already exists")

func newFrameworkExportCmd(cfg config, out io.Writer) *cobra.Command {
var options frameworkExportOptions

Expand All @@ -48,12 +45,7 @@ func exportFramework(ctx context.Context, cfg config, options frameworkExportOpt
framework.Spec.Name = framework.Name

if options.filename != "" {
// open file, err if exist, write framework.Spec
_, err = os.Stat(options.filename)
if !os.IsNotExist(err) {
return errFileExists
}
f, err := os.Create(options.filename)
f, err := output.GetOutputFile(options.filename)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/ketch/framework_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"testing"

"github.com/shipa-corp/ketch/cmd/ketch/output"

"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -82,7 +84,7 @@ version: v1
before: func() {
os.WriteFile("test-framework.yaml", []byte("data"), os.ModePerm)
},
err: errFileExists,
err: output.ErrFileExists,
},
}
for _, tt := range tests {
Expand Down
27 changes: 27 additions & 0 deletions cmd/ketch/job.go
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
}
98 changes: 98 additions & 0 deletions cmd/ketch/job_deploy.go
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
}
Loading