Skip to content

Commit

Permalink
k8s-tester/jobs-pi: initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
  • Loading branch information
gyuho committed May 27, 2021
1 parent 2746e64 commit 5a188f1
Show file tree
Hide file tree
Showing 5 changed files with 1,032 additions and 0 deletions.
139 changes: 139 additions & 0 deletions k8s-tester/jobs-pi/cmd/k8s-tester-jobs-pi/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// k8s-tester-jobs-pi installs Kubernetes Jobs Pi tester.
package main

import (
"fmt"
"os"

"github.com/aws/aws-k8s-tester/client"
jobs_pi "github.com/aws/aws-k8s-tester/k8s-tester/jobs-pi"
"github.com/aws/aws-k8s-tester/utils/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
)

var rootCmd = &cobra.Command{
Use: "k8s-tester-jobs-pi",
Short: "Kubernetes Jobs Pi tester",
SuggestFor: []string{"jobs-pi"},
}

func init() {
cobra.EnablePrefixMatching = true
}

var (
enablePrompt bool
logLevel string
logOutputs []string
namespace string
kubectlPath string
kubeConfigPath string
)

func init() {
rootCmd.PersistentFlags().BoolVar(&enablePrompt, "enable-prompt", true, "'true' to enable prompt mode")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", log.DefaultLogLevel, "Logging level")
rootCmd.PersistentFlags().StringSliceVar(&logOutputs, "log-outputs", []string{"stderr"}, "Additional logger outputs")
rootCmd.PersistentFlags().StringVar(&namespace, "namespace", "test-namespace", "'true' to auto-generate path for create config/cluster, overwrites existing --path value")
rootCmd.PersistentFlags().StringVar(&kubectlPath, "kubectl-path", "", "kubectl path")
rootCmd.PersistentFlags().StringVar(&kubeConfigPath, "kubeconfig-path", "", "KUBECONFIG path")

rootCmd.AddCommand(
newApply(),
newDelete(),
)
}

func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "k8s-tester-jobs-pi failed %v\n", err)
os.Exit(1)
}
os.Exit(0)
}

var (
completes int32
parallels int32
)

func newApply() *cobra.Command {
cmd := &cobra.Command{
Use: "apply",
Short: "Apply tests",
Run: createApplyFunc,
}
cmd.PersistentFlags().Int32Var(&completes, "completes", 10, "desired number of successfully finished pods")
cmd.PersistentFlags().Int32Var(&parallels, "parallels", 10, "maximum desired number of pods the job should run at any given time")
return cmd
}

func createApplyFunc(cmd *cobra.Command, args []string) {
lg, logWriter, _, err := log.NewWithStderrWriter(logLevel, logOutputs)
if err != nil {
panic(err)
}
_ = zap.ReplaceGlobals(lg)

cfg := jobs_pi.Config{
EnablePrompt: enablePrompt,
Logger: lg,
LogWriter: logWriter,
Namespace: namespace,
ClientConfig: &client.Config{
Logger: lg,
KubectlPath: kubectlPath,
KubeConfigPath: kubeConfigPath,
},
Completes: completes,
Parallels: parallels,
}

ts := jobs_pi.New(cfg)
if err := ts.Apply(); err != nil {
fmt.Fprintf(os.Stderr, "failed to apply (%v)\n", err)
os.Exit(1)
}

fmt.Printf("\n*********************************\n")
fmt.Printf("'k8s-tester-jobs-pi apply' success\n")
}

func newDelete() *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete resources",
Run: createDeleteFunc,
}
return cmd
}

func createDeleteFunc(cmd *cobra.Command, args []string) {
lg, logWriter, _, err := log.NewWithStderrWriter(logLevel, logOutputs)
if err != nil {
panic(err)
}
_ = zap.ReplaceGlobals(lg)

cfg := jobs_pi.Config{
EnablePrompt: enablePrompt,
Logger: lg,
LogWriter: logWriter,
Namespace: namespace,
ClientConfig: &client.Config{
Logger: lg,
KubectlPath: kubectlPath,
KubeConfigPath: kubeConfigPath,
},
}

ts := jobs_pi.New(cfg)
if err := ts.Delete(); err != nil {
fmt.Fprintf(os.Stderr, "failed to delete (%v)\n", err)
os.Exit(1)
}

fmt.Printf("\n*********************************\n")
fmt.Printf("'k8s-tester-jobs-pi delete' success\n")
}
24 changes: 24 additions & 0 deletions k8s-tester/jobs-pi/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module github.com/aws/aws-k8s-tester/k8s-tester/jobs-pi

go 1.16

require (
github.com/aws/aws-k8s-tester/client v0.0.0-00010101000000-000000000000
github.com/aws/aws-k8s-tester/k8s-tester/tester v0.0.0-00010101000000-000000000000
github.com/aws/aws-k8s-tester/utils v0.0.0-00010101000000-000000000000
github.com/aws/aws-sdk-go v1.38.21
github.com/dustin/go-humanize v1.0.0
github.com/manifoldco/promptui v0.8.0
github.com/spf13/cobra v1.1.3
go.uber.org/zap v1.17.0
k8s.io/api v0.21.0
k8s.io/apimachinery v0.21.0
k8s.io/client-go v0.21.0
sigs.k8s.io/yaml v1.2.0
)

replace (
github.com/aws/aws-k8s-tester/client => ../../client
github.com/aws/aws-k8s-tester/k8s-tester/tester => ../tester
github.com/aws/aws-k8s-tester/utils => ../../utils
)
Loading

0 comments on commit 5a188f1

Please sign in to comment.