Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#39 from chizhg/refactor-kubetest2-gke
Browse files Browse the repository at this point in the history
Refactor kubetest2 gke deployer into different source files
  • Loading branch information
k8s-ci-robot authored Aug 13, 2020
2 parents 7839214 + 516c7c2 commit e647faf
Show file tree
Hide file tree
Showing 8 changed files with 626 additions and 502 deletions.
36 changes: 36 additions & 0 deletions kubetest2-gke/deployer/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package deployer

import (
"fmt"

"sigs.k8s.io/kubetest2/pkg/build"
)

func (d *deployer) Build() error {
if err := build.Build(); err != nil {
return err
}

if d.stageLocation != "" {
if err := build.Stage(d.stageLocation); err != nil {
return fmt.Errorf("error staging build: %v", err)
}
}
return nil
}
16 changes: 16 additions & 0 deletions kubetest2-gke/deployer/commandutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,19 @@ func home(parts ...string) string {
p := append([]string{os.Getenv("HOME")}, parts...)
return filepath.Join(p...)
}

func (d *deployer) getClusterCredentials(project, cluster string) error {
// Get gcloud to create the file.
loc, err := d.location()
if err != nil {
return err
}

if err := runWithOutput(exec.Command("gcloud",
d.containerArgs("clusters", "get-credentials", cluster, "--project="+project, loc)...),
); err != nil {
return fmt.Errorf("error executing get-credentials: %v", err)
}

return nil
}
25 changes: 25 additions & 0 deletions kubetest2-gke/deployer/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package deployer

import (
"fmt"
realexec "os/exec"
"strconv"
"strings"
"time"

"k8s.io/klog"

"sigs.k8s.io/kubetest2/pkg/boskos"
"sigs.k8s.io/kubetest2/pkg/exec"
)

const (
Expand Down Expand Up @@ -110,3 +112,26 @@ func buildProjectClustersLayout(projects, clusters []string, projectClustersLayo
}
return nil
}

func (d *deployer) containerArgs(args ...string) []string {
return append(append([]string{}, "container"), args...)
}

func runWithNoOutput(cmd exec.Cmd) error {
exec.NoOutput(cmd)
return cmd.Run()
}

func runWithOutput(cmd exec.Cmd) error {
exec.InheritOutput(cmd)
return cmd.Run()
}

// execError returns a string format of err including stderr if the
// err is an ExitError, useful for errors from e.g. exec.Cmd.Output().
func execError(err error) string {
if ee, ok := err.(*realexec.ExitError); ok {
return fmt.Sprintf("%v (output: %q)", err, string(ee.Stderr))
}
return err.Error()
}
Loading

0 comments on commit e647faf

Please sign in to comment.