Skip to content

Commit

Permalink
k8s-tester: clean up
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <[email protected]>
  • Loading branch information
gyuho committed May 27, 2021
1 parent ccb747d commit f3a8a40
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 70 deletions.
3 changes: 3 additions & 0 deletions k8s-tester/fluent-bit/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (ts *tester) Apply() error {
if ok := ts.runPrompt("apply"); !ok {
return errors.New("cancelled")
}

if err := client.CreateNamespace(ts.cfg.Logger, ts.cli, ts.cfg.Namespace); err != nil {
return err
}
Expand Down Expand Up @@ -89,6 +90,7 @@ func (ts *tester) Apply() error {
if err := ts.testLogsWithinNamespace(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -194,6 +196,7 @@ func (ts *tester) Delete() error {
if len(errs) > 0 {
return errors.New(strings.Join(errs, ", "))
}

return nil
}

Expand Down
106 changes: 61 additions & 45 deletions k8s-tester/jobs-echo/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,9 @@ func (ts *tester) Apply() (err error) {
return errors.New("cancelled")
}

// check ECR permission
// ref. https://github.com/aws/aws-k8s-tester/blob/v1.5.9/eks/jobs-echo/jobs-echo.go#L75-L90
bsyImg := jobBusyboxImageName
if ts.cfg.RepositoryBusyboxAccountID != "" &&
ts.cfg.RepositoryBusyboxRegion != "" &&
ts.cfg.RepositoryBusyboxName != "" &&
ts.cfg.RepositoryBusyboxImageTag != "" &&
ts.cfg.ECRAPI != nil {
bsyImg, _, err = aws_v1_ecr.Check(
ts.cfg.Logger,
ts.cfg.ECRAPI,
ts.cfg.RepositoryBusyboxPartition,
ts.cfg.RepositoryBusyboxAccountID,
ts.cfg.RepositoryBusyboxRegion,
ts.cfg.RepositoryBusyboxName,
ts.cfg.RepositoryBusyboxImageTag,
)
if err != nil {
return err
}
bsyImg, err := ts.checkECRImage()
if err != nil {
return err
}

if err := client.CreateNamespace(ts.cfg.Logger, ts.cli, ts.cfg.Namespace); err != nil {
Expand All @@ -136,33 +119,9 @@ func (ts *tester) Apply() (err error) {
return err
}

timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.Completes)
if timeout > 3*time.Hour {
timeout = 3 * time.Hour
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []core_v1.Pod
_, pods, err = client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.LogWriter,
ts.cfg.Stopc,
ts.cli,
time.Minute,
5*time.Second,
ts.cfg.Namespace,
jobName,
int(ts.cfg.Completes),
)
cancel()
if err != nil {
if err := ts.checkJob(); err != nil {
return err
}
fmt.Fprintf(ts.cfg.LogWriter, "\n")
for _, item := range pods {
fmt.Fprintf(ts.cfg.LogWriter, "Job Pod %q: %q\n", item.Name, item.Status.Phase)
}
fmt.Fprintf(ts.cfg.LogWriter, "\n")

return nil
}
Expand Down Expand Up @@ -241,6 +200,31 @@ const (
jobBusyboxImageName = "busybox"
)

func (ts *tester) checkECRImage() (busyboxImg string, err error) {
// check ECR permission
// ref. https://github.com/aws/aws-k8s-tester/blob/v1.5.9/eks/jobs-echo/jobs-echo.go#L75-L90
busyboxImg = jobBusyboxImageName
if ts.cfg.RepositoryBusyboxAccountID != "" &&
ts.cfg.RepositoryBusyboxRegion != "" &&
ts.cfg.RepositoryBusyboxName != "" &&
ts.cfg.RepositoryBusyboxImageTag != "" &&
ts.cfg.ECRAPI != nil {
busyboxImg, _, err = aws_v1_ecr.Check(
ts.cfg.Logger,
ts.cfg.ECRAPI,
ts.cfg.RepositoryBusyboxPartition,
ts.cfg.RepositoryBusyboxAccountID,
ts.cfg.RepositoryBusyboxRegion,
ts.cfg.RepositoryBusyboxName,
ts.cfg.RepositoryBusyboxImageTag,
)
if err != nil {
return "", err
}
}
return busyboxImg, nil
}

func (ts *tester) createObject(busyboxImg string) (batch_v1.Job, string, error) {
podSpec := core_v1.PodTemplateSpec{
Spec: core_v1.PodSpec{
Expand Down Expand Up @@ -313,3 +297,35 @@ func (ts *tester) createJob(busyboxImg string) (err error) {
ts.cfg.Logger.Info("created a Job object")
return nil
}

func (ts *tester) checkJob() error {
timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.Completes)
if timeout > 3*time.Hour {
timeout = 3 * time.Hour
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []core_v1.Pod
_, pods, err := client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.LogWriter,
ts.cfg.Stopc,
ts.cli,
time.Minute,
5*time.Second,
ts.cfg.Namespace,
jobName,
int(ts.cfg.Completes),
)
cancel()
if err != nil {
return err
}

fmt.Fprintf(ts.cfg.LogWriter, "\n")
for _, item := range pods {
fmt.Fprintf(ts.cfg.LogWriter, "Job Pod %q: %q\n", item.Name, item.Status.Phase)
}
fmt.Fprintf(ts.cfg.LogWriter, "\n")
return nil
}
58 changes: 33 additions & 25 deletions k8s-tester/jobs-pi/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,9 @@ func (ts *tester) Apply() error {
return err
}

timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.Completes)
if timeout > 3*time.Hour {
timeout = 3 * time.Hour
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []core_v1.Pod
_, pods, err := client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.LogWriter,
ts.cfg.Stopc,
ts.cli,
time.Minute,
5*time.Second,
ts.cfg.Namespace,
jobName,
int(ts.cfg.Completes),
)
cancel()
if err != nil {
if err := ts.checkJob(); err != nil {
return err
}
fmt.Fprintf(ts.cfg.LogWriter, "\n")
for _, item := range pods {
fmt.Fprintf(ts.cfg.LogWriter, "Job Pod %q: %q\n", item.Name, item.Status.Phase)
}
fmt.Fprintf(ts.cfg.LogWriter, "\n")

return nil
}
Expand Down Expand Up @@ -257,3 +233,35 @@ func (ts *tester) createJob() (err error) {
ts.cfg.Logger.Info("created a Job object")
return nil
}

func (ts *tester) checkJob() error {
timeout := 5*time.Minute + 5*time.Minute*time.Duration(ts.cfg.Completes)
if timeout > 3*time.Hour {
timeout = 3 * time.Hour
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var pods []core_v1.Pod
_, pods, err := client.WaitForJobCompletes(
ctx,
ts.cfg.Logger,
ts.cfg.LogWriter,
ts.cfg.Stopc,
ts.cli,
time.Minute,
5*time.Second,
ts.cfg.Namespace,
jobName,
int(ts.cfg.Completes),
)
cancel()
if err != nil {
return err
}

fmt.Fprintf(ts.cfg.LogWriter, "\n")
for _, item := range pods {
fmt.Fprintf(ts.cfg.LogWriter, "Job Pod %q: %q\n", item.Name, item.Status.Phase)
}
fmt.Fprintf(ts.cfg.LogWriter, "\n")
return nil
}
3 changes: 3 additions & 0 deletions k8s-tester/nlb-hello-world/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,23 @@ func (ts *tester) Apply() error {
if err := ts.createDeployment(); err != nil {
return err
}

if err := ts.checkDeployment(); err != nil {
return err
}

if err := ts.createService(); err != nil {
return err
}

waitDur := 3 * time.Minute
ts.cfg.Logger.Info("waiting for NLB hello-world Service", zap.Duration("wait", waitDur))
select {
case <-ts.cfg.Stopc:
return errors.New("NLB hello-world Service apply aborted")
case <-time.After(waitDur):
}

if err := ts.checkService(); err != nil {
return err
}
Expand Down

0 comments on commit f3a8a40

Please sign in to comment.