Skip to content

Commit abf58af

Browse files
authored
Merge pull request #1251 from saschagrunert/gcp-precheck
Move command pre-checks into dedicated packages
2 parents a8fd0e2 + a0835ed commit abf58af

File tree

11 files changed

+114
-54
lines changed

11 files changed

+114
-54
lines changed

BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ filegroup(
3535
"//cmd/release-notes:all-srcs",
3636
"//lib:all-srcs",
3737
"//pkg/command:all-srcs",
38-
"//pkg/gcp/auth:all-srcs",
39-
"//pkg/gcp/build:all-srcs",
38+
"//pkg/gcp:all-srcs",
4039
"//pkg/git:all-srcs",
4140
"//pkg/github:all-srcs",
4241
"//pkg/http:all-srcs",

cmd/krel/cmd/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go_library(
1616
visibility = ["//visibility:public"],
1717
deps = [
1818
"//pkg/command:go_default_library",
19+
"//pkg/gcp:go_default_library",
1920
"//pkg/gcp/auth:go_default_library",
2021
"//pkg/gcp/build:go_default_library",
2122
"//pkg/git:go_default_library",

cmd/krel/cmd/gcbmgr.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/sirupsen/logrus"
2727
"github.com/spf13/cobra"
2828

29-
"k8s.io/release/pkg/command"
29+
"k8s.io/release/pkg/gcp"
3030
"k8s.io/release/pkg/gcp/auth"
3131
"k8s.io/release/pkg/gcp/build"
3232
"k8s.io/release/pkg/git"
@@ -64,18 +64,6 @@ type Version interface {
6464
var (
6565
gcbmgrOpts = &GcbmgrOptions{}
6666
buildOpts = &build.Options{}
67-
68-
requiredPackages = []string{
69-
// "bsdmainutils",
70-
}
71-
72-
// TODO: Do we really need this if we use the Google Cloud SDK instead?
73-
requiredCommands = []string{
74-
"gcloud",
75-
"git",
76-
"gsutil",
77-
"jq",
78-
}
7967
)
8068

8169
// gcbmgrCmd is a krel subcommand which invokes RunGcbmgr()
@@ -168,24 +156,14 @@ func init() {
168156
// RunGcbmgr is the function invoked by 'krel gcbmgr', responsible for
169157
// submitting release jobs to GCB
170158
func RunGcbmgr(opts *GcbmgrOptions) error {
171-
logrus.Info("Checking for required packages")
172-
ok, err := util.PackagesAvailable(requiredPackages...)
173-
if err != nil {
174-
return errors.Wrap(err, "unable to verify if packages are available")
175-
}
176-
if !ok {
177-
return errors.New("packages required to run gcbmgr are not present")
178-
}
179-
180-
logrus.Info("Checking for required commands")
181-
if cmdAvailable := command.Available(requiredCommands...); !cmdAvailable {
182-
return errors.New("binaries required to run gcbmgr are not present")
183-
}
184-
185159
toolOrg := release.GetToolOrg()
186160
toolRepo := release.GetToolRepo()
187161
toolBranch := release.GetToolBranch()
188162

163+
if err := gcp.PreCheck(); err != nil {
164+
return errors.Wrap(err, "pre-checking for GCP package")
165+
}
166+
189167
if err := opts.Repo.Open(); err != nil {
190168
return errors.Wrap(err, "open release repo")
191169
}

pkg/gcp/BUILD.bazel

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["gcp.go"],
6+
importpath = "k8s.io/release/pkg/gcp",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//pkg/command:go_default_library",
10+
"@com_github_pkg_errors//:go_default_library",
11+
],
12+
)
13+
14+
filegroup(
15+
name = "package-srcs",
16+
srcs = glob(["**"]),
17+
tags = ["automanaged"],
18+
visibility = ["//visibility:private"],
19+
)
20+
21+
filegroup(
22+
name = "all-srcs",
23+
srcs = [
24+
":package-srcs",
25+
"//pkg/gcp/auth:all-srcs",
26+
"//pkg/gcp/build:all-srcs",
27+
],
28+
tags = ["automanaged"],
29+
visibility = ["//visibility:public"],
30+
)

pkg/gcp/auth/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go_library(
77
visibility = ["//visibility:public"],
88
deps = [
99
"//pkg/command:go_default_library",
10+
"//pkg/gcp:go_default_library",
1011
"@com_github_pkg_errors//:go_default_library",
1112
],
1213
)

pkg/gcp/auth/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import (
2222
"github.com/pkg/errors"
2323

2424
"k8s.io/release/pkg/command"
25+
"k8s.io/release/pkg/gcp"
2526
)
2627

2728
func GetCurrentGCPUser() (string, error) {
2829
authListStatus, authListErr := command.New(
29-
"gcloud",
30+
gcp.GCloudExecutable,
3031
"auth",
3132
"list",
3233
"--filter=status:ACTIVE",

pkg/gcp/build/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go_library(
77
visibility = ["//visibility:public"],
88
deps = [
99
"//pkg/command:go_default_library",
10+
"//pkg/gcp:go_default_library",
1011
"//pkg/release:go_default_library",
1112
"@com_github_google_uuid//:go_default_library",
1213
"@com_github_olekukonko_tablewriter//:go_default_library",

pkg/gcp/build/build.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"google.golang.org/api/option"
3636

3737
"k8s.io/release/pkg/command"
38+
"k8s.io/release/pkg/gcp"
3839
"k8s.io/release/pkg/release"
3940
"sigs.k8s.io/yaml"
4041
)
@@ -138,7 +139,7 @@ func (o *Options) uploadBuildDir(targetBucket string) (string, error) {
138139

139140
logrus.Infof("Creating source tarball at %s...", name)
140141
tarCmdErr := command.Execute(
141-
"tar",
142+
gcp.TarExecutable,
142143
"--exclude",
143144
".git",
144145
"-czf",
@@ -153,7 +154,7 @@ func (o *Options) uploadBuildDir(targetBucket string) (string, error) {
153154
uploaded := fmt.Sprintf("%s/%s.tgz", targetBucket, u.String())
154155
logrus.Infof("Uploading %s to %s...", name, uploaded)
155156
cpErr := command.Execute(
156-
"gsutil",
157+
gcp.GSUtilExecutable,
157158
"cp",
158159
name,
159160
uploaded,
@@ -235,7 +236,7 @@ func RunSingleJob(o *Options, jobName, uploaded, version string, subs map[string
235236
args = append(args, diskSizeArg)
236237
}
237238

238-
cmd := command.New("gcloud", args...)
239+
cmd := command.New(gcp.GCloudExecutable, args...)
239240

240241
if o.LogDir != "" {
241242
p := path.Join(o.LogDir, strings.ReplaceAll(jobName, "/", "-")+".log")

pkg/gcp/gcp.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package gcp
18+
19+
import (
20+
"github.com/pkg/errors"
21+
"k8s.io/release/pkg/command"
22+
)
23+
24+
const (
25+
GCloudExecutable = "tar"
26+
GSUtilExecutable = "gsutil"
27+
TarExecutable = "gcloud"
28+
)
29+
30+
// PreCheck checks if all requirements are fulfilled to run this package and
31+
// all sub-packages
32+
func PreCheck() error {
33+
for _, e := range []string{
34+
GCloudExecutable,
35+
GSUtilExecutable,
36+
TarExecutable,
37+
} {
38+
if !command.Available(e) {
39+
return errors.Errorf(
40+
"%s executable is not available in $PATH", e,
41+
)
42+
}
43+
}
44+
45+
return nil
46+
}

pkg/git/git.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,6 @@ func CloneOrOpenGitHubRepo(repoPath, owner, repo string, useSSH bool) (*Repo, er
223223
// The function returns the repository if cloning or updating of the repository
224224
// was successful, otherwise an error.
225225
func CloneOrOpenRepo(repoPath, repoURL string, useSSH bool) (*Repo, error) {
226-
// We still need the plain git executable for some methods
227-
if !command.Available(gitExecutable) {
228-
return nil, errors.New("git is needed to support all repository features")
229-
}
230-
231226
logrus.Debugf("Using repository url %q", repoURL)
232227
targetDir := ""
233228
if repoPath != "" {
@@ -265,47 +260,45 @@ func CloneOrOpenRepo(repoPath, repoURL string, useSSH bool) (*Repo, error) {
265260
// updateRepo tries to open the provided repoPath and fetches the latest
266261
// changes from the configured remote location
267262
func updateRepo(repoPath string) (*Repo, error) {
268-
r, err := git.PlainOpen(repoPath)
263+
r, err := OpenRepo(repoPath)
269264
if err != nil {
270-
return nil, errors.Wrap(err, "unable to open repo")
265+
return nil, err
271266
}
272267

273268
// Update the repo
274269
if err := command.NewWithWorkDir(
275-
repoPath, gitExecutable, "pull", "--rebase",
270+
r.Dir(), gitExecutable, "pull", "--rebase",
276271
).RunSilentSuccess(); err != nil {
277272
return nil, errors.Wrap(err, "unable to pull from remote")
278273
}
279274

280-
worktree, err := r.Worktree()
281-
if err != nil {
282-
return nil, errors.Wrap(err, "unable to get repository worktree")
283-
}
284-
return &Repo{
285-
inner: r,
286-
worktree: worktree,
287-
dir: repoPath,
288-
}, nil
275+
return r, nil
289276
}
290277

291278
// OpenRepo tries to open the provided repoPath
292279
func OpenRepo(repoPath string) (*Repo, error) {
280+
if !command.Available(gitExecutable) {
281+
return nil, errors.Errorf(
282+
"%s executable is not available in $PATH", gitExecutable,
283+
)
284+
}
285+
293286
r, err := git.PlainOpenWithOptions(
294287
repoPath, &git.PlainOpenOptions{DetectDotGit: true},
295288
)
296289
if err != nil {
297-
return nil, err
290+
return nil, errors.Wrap(err, "opening repo")
298291
}
299292

300293
worktree, err := r.Worktree()
301294
if err != nil {
302-
return nil, err
295+
return nil, errors.Wrap(err, "getting repository worktree")
303296
}
304297

305298
return &Repo{
306299
inner: r,
307300
worktree: worktree,
308-
dir: repoPath,
301+
dir: worktree.Filesystem.Root(),
309302
}, nil
310303
}
311304

0 commit comments

Comments
 (0)