Skip to content

Commit

Permalink
gh2gcs: Enable uploads to GCS
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <saugustus@vmware.com>
  • Loading branch information
justaugustus committed May 25, 2020
1 parent 15c5b4f commit f36b04a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 11 deletions.
2 changes: 2 additions & 0 deletions cmd/gh2gcs/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ go_library(
importpath = "k8s.io/release/cmd/gh2gcs/cmd",
visibility = ["//visibility:public"],
deps = [
"//pkg/gcp:go_default_library",
"//pkg/gh2gcs:go_default_library",
"//pkg/github:go_default_library",
"//pkg/log:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_spf13_cobra//:go_default_library",
],
Expand Down
16 changes: 12 additions & 4 deletions cmd/gh2gcs/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package cmd
import (
"io/ioutil"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"k8s.io/release/pkg/gcp"
"k8s.io/release/pkg/gh2gcs"
"k8s.io/release/pkg/github"
"k8s.io/release/pkg/log"
Expand Down Expand Up @@ -136,6 +138,10 @@ func initLogging(*cobra.Command, []string) error {
}

func run(opts *options) error {
if err := gcp.PreCheck(); err != nil {
return errors.Wrap(err, "pre-checking for GCP package usage")
}

if opts.outputDir == "" {
tmpDir, err := ioutil.TempDir("", "gh2gcs")
if err != nil {
Expand All @@ -151,9 +157,11 @@ func run(opts *options) error {
// TODO: Support downloading releases via yaml config
uploadConfig := &gh2gcs.Config{}
releaseConfig := &gh2gcs.ReleaseConfig{
Org: opts.org,
Name: opts.repo,
Tags: []string{},
Org: opts.org,
Repo: opts.repo,
Tags: []string{},
GCSBucket: opts.bucket,
ReleaseDir: opts.releaseDir,
}

if opts.tag != "" {
Expand All @@ -174,7 +182,7 @@ func run(opts *options) error {
return err
}

if err := gh2gcs.UploadToGCS(&rc, gh, opts.outputDir); err != nil {
if err := gh2gcs.Upload(&rc, gh, opts.outputDir); err != nil {
return err
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/gcp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ filegroup(
":package-srcs",
"//pkg/gcp/auth:all-srcs",
"//pkg/gcp/build:all-srcs",
"//pkg/gcp/gcs:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
Expand Down
27 changes: 27 additions & 0 deletions pkg/gcp/gcs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["gcs.go"],
importpath = "k8s.io/release/pkg/gcp/gcs",
visibility = ["//visibility:public"],
deps = [
"//pkg/command:go_default_library",
"//pkg/gcp:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
73 changes: 73 additions & 0 deletions pkg/gcp/gcs/gcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
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 gcs

import (
"strings"

"github.com/sirupsen/logrus"
"k8s.io/release/pkg/command"
"k8s.io/release/pkg/gcp"
)

var (
gcsPrefix = "gs://"
concurrentFlag = "-m"
recursiveFlag = "-r"
)

func CopyToGCS(src, gcsPath string, recursive, concurrent bool) error {
gcsPath = normalizeGCSPath(gcsPath)

logrus.Infof("Copying %s to GCS (%s)", src, gcsPath)
return bucketCopy(src, gcsPath, recursive, concurrent)
}

func CopyToLocal(gcsPath, dst string, recursive, concurrent bool) error {
gcsPath = normalizeGCSPath(gcsPath)

logrus.Infof("Copying GCS (%s) to %s", gcsPath, dst)
return bucketCopy(gcsPath, dst, recursive, concurrent)
}

func bucketCopy(src, dst string, recursive, concurrent bool) error {
args := []string{}

if concurrent {
args = append(args, concurrentFlag)
}

args = append(args, "cp")
if recursive {
args = append(args, recursiveFlag)
}
args = append(args, src, dst)

cpErr := command.Execute(gcp.GSUtilExecutable, args...)
if cpErr != nil {
return cpErr
}

return nil
}

func normalizeGCSPath(gcsPath string) string {
gcsPath = strings.TrimPrefix(gcsPath, gcsPrefix)
gcsPath = gcsPrefix + gcsPath

return gcsPath
}
2 changes: 1 addition & 1 deletion pkg/gh2gcs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ go_library(
importpath = "k8s.io/release/pkg/gh2gcs",
visibility = ["//visibility:public"],
deps = [
"//pkg/gcp/gcs:go_default_library",
"//pkg/github:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

Expand Down
22 changes: 16 additions & 6 deletions pkg/gh2gcs/gh2gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ limitations under the License.
package gh2gcs

import (
"github.com/sirupsen/logrus"
"path/filepath"

"k8s.io/release/pkg/gcp/gcs"
"k8s.io/release/pkg/github"
)

Expand All @@ -28,23 +29,32 @@ type Config struct {

type ReleaseConfig struct {
Org string
Name string
Repo string
Tags []string
GCSBucket string
ReleaseDir string
}

func DownloadReleases(releaseCfg *ReleaseConfig, ghClient *github.GitHub, outputDir string) error {
tags := releaseCfg.Tags
if err := ghClient.DownloadReleaseAssets(releaseCfg.Org, releaseCfg.Name, tags, outputDir); err != nil {
if err := ghClient.DownloadReleaseAssets(releaseCfg.Org, releaseCfg.Repo, tags, outputDir); err != nil {
return err
}

return nil
}

// TODO: Add GCS upload logic
func UploadToGCS(releaseCfg *ReleaseConfig, ghClient *github.GitHub, outputDir string) error {
logrus.Info("Uploading to GCS...")
func Upload(releaseCfg *ReleaseConfig, ghClient *github.GitHub, outputDir string) error {
uploadBase := filepath.Join(outputDir, releaseCfg.Org, releaseCfg.Repo)
gcsPath := filepath.Join(releaseCfg.GCSBucket, releaseCfg.ReleaseDir)

tags := releaseCfg.Tags
for _, tag := range tags {
srcDir := filepath.Join(uploadBase, tag)
if err := gcs.CopyToGCS(srcDir, gcsPath, true, true); err != nil {
return err
}
}

return nil
}

0 comments on commit f36b04a

Please sign in to comment.