Skip to content

Commit

Permalink
Migrate promotion CLI tools to k/release
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <saugustus@vmware.com>
  • Loading branch information
justaugustus committed Oct 21, 2020
1 parent 51c5fed commit 61f4ca3
Show file tree
Hide file tree
Showing 31 changed files with 1,843 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cmd/cip-mm/README.md
@@ -0,0 +1,40 @@
# cip-mm

This tool **m**odifies promoter **m**anifests. For now it dumps some filtered
subset of a staging GCR and merges those contents back into a given promoter
manifest.

## Examples

- Add all images with a matching digest from staging repo
`gcr.io/k8s-staging-artifact-promoter` to a manifest, using the name and tags
already existing in the staging repo:

```
cip-mm \
--base_dir=$HOME/go/src/github.com/kubernetes/k8s.io/k8s.gcr.io \
--staging_repo=gcr.io/k8s-staging-artifact-promoter \
--filter_digest=sha256:7594278deaf6eeaa35caedec81796d103e3c83a26d7beab091a5d25a9ba6aa16
```

- Add a single image named "foo" and tagged "1.0" from staging repo
`gcr.io/k8s-staging-artifact-promoter` to a manifest:

```
cip-mm \
--base_dir=$HOME/go/src/github.com/kubernetes/k8s.io/k8s.gcr.io \
--staging_repo=gcr.io/k8s-staging-artifact-promoter \
--filter_image=cip \
--filter_tag=1.0
```

- Add all images tagged `1.0` from staging repo
`gcr.io/k8s-staging-artifact-promoter` to a manifest:

```
cip-mm \
--base_dir=$HOME/go/src/github.com/kubernetes/k8s.io/k8s.gcr.io \
--staging_repo=gcr.io/k8s-staging-artifact-promoter \
--filter_image=cip \
--filter_tag=1.0
```
91 changes: 91 additions & 0 deletions cmd/cip-mm/main.go
@@ -0,0 +1,91 @@
/*
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 main

import (
"context"
"flag"
"fmt"
"os"

"k8s.io/klog"
reg "sigs.k8s.io/k8s-container-image-promoter/pkg/dockerregistry"
)

func main() {
ctx := context.Background()

if err := run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
// nolint[gomnd]
os.Exit(1)
}
}

// nolint[lll]
func run(ctx context.Context) error {
klog.InitFlags(nil)

baseDir := ""
flag.StringVar(
&baseDir,
"base_dir",
baseDir,
"the manifest directory to look at and modify")
stagingRepo := ""
flag.StringVar(
&stagingRepo,
"staging_repo",
stagingRepo,
"the staging repo which we want to read from")
filterImage := ""
flag.StringVar(
&filterImage,
"filter_image",
filterImage,
"filter staging repo by this image name")
filterDigest := ""
flag.StringVar(
&filterDigest,
"filter_digest",
filterDigest,
"filter images by this digest")
filterTag := ""
flag.StringVar(
&filterTag,
"filter_tag",
filterTag,
"filter images by this tag")

flag.Parse()

opt := reg.GrowManifestOptions{}
if err := opt.Populate(
baseDir,
stagingRepo,
filterImage,
filterDigest,
filterTag); err != nil {
return err
}

if err := opt.Validate(); err != nil {
return err
}

return reg.GrowManifest(ctx, opt)
}
41 changes: 41 additions & 0 deletions cmd/promobot-files/README.md
@@ -0,0 +1,41 @@
# File Promoter

The file promoter (aka "promobot-files") copies files from source GCS buckets to
one or more destination buckets, by reading a Manifest file (in YAML).

The Manifest lists the files and their hashes that should be copied from src to
dest.

Example Manifest for files:

```
filestores:
- base: gs://staging/
src: true
- base: gs://prod/subdir
service-account: foo@google-containers.iam.gserviceaccount.com
files:
- name: vegetables/artichoke
sha256: 2d4f26491e0e470236f73a0b8d6828db017eab988cd102fc19afe31f1f56aff7
- name: vegetables/beetroot
sha256: 160b98e27ec99f77efe01e2996fa386f2b2aec552599f8bd861be0a857e7f29f
```

`filestores` is the equivalent of the container manifest `registries`, and lists
the buckets from which the promoter should read or write files. `files` is the
equivalent of `images`, and lists the files that should be promoted.

`filestores` supports `service-account`, and it also supports relative paths -
note that the source files in the example above are in the root of the bucket,
but they are copied into a subdirectory of the target bucket.

`files` is a list of files to be copied. The `name` is appended to the base of
the filestore, and then the files are copied. If the source file does not have
the matching sha256, it will not be copied.

When errors are encountered building the list of files to be copied, no files
will be copied. When errors are encountered while copying files, we will still
attempt to copy remaining files, but the process will report the error.

Currently only Google Cloud Storage (GCS) buckets supported, with a prefix of
`gs://`
69 changes: 69 additions & 0 deletions cmd/promobot-files/main.go
@@ -0,0 +1,69 @@
/*
Copyright 2019 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 main

import (
"context"
"flag"
"fmt"
"os"

"k8s.io/klog"
"sigs.k8s.io/k8s-container-image-promoter/pkg/promobot"
)

func main() {
klog.InitFlags(nil)

var options promobot.PromoteFilesOptions
options.PopulateDefaults()

flag.StringVar(
&options.FilestoresPath,
"filestores",
options.FilestoresPath,
"the manifest of filestores (REQUIRED)")
flag.StringVar(
&options.FilesPath,
"files",
options.FilesPath,
"path to the files manifest (REQUIRED). A directory can be specified.")
flag.BoolVar(
&options.DryRun,
"dry-run",
options.DryRun,
"print what would have happened by running this tool;"+
" do not actually modify any registry")

flag.BoolVar(
&options.UseServiceAccount,
"use-service-account",
options.UseServiceAccount,
"allow service account usage with gcloud calls"+
" (default: false)")

flag.Parse()

ctx := context.Background()
if err := promobot.RunPromoteFiles(ctx, options); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
// nolint[gomnd]
os.Exit(1)
} else {
os.Exit(0)
}
}
10 changes: 10 additions & 0 deletions cmd/promobot-generate-manifest/README.md
@@ -0,0 +1,10 @@
# promobot-generate-manifest

This tool will generate a manifest fragment for uploading a set of
files, located in the specified path.

It takes a single argument `--src`, which is the base of the directory
tree; all files under that directory (recursively) are hashed and
output into the `files` section of a manifest.

The manifest is written to stdout.
90 changes: 90 additions & 0 deletions cmd/promobot-generate-manifest/main.go
@@ -0,0 +1,90 @@
/*
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 main

import (
"context"
"flag"
"fmt"
"os"
"path/filepath"

"golang.org/x/xerrors"

"k8s.io/klog"
"sigs.k8s.io/k8s-container-image-promoter/pkg/promobot"
"sigs.k8s.io/yaml"
)

func main() {
ctx := context.Background()

if err := run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
// nolint[gomnd]
os.Exit(1)
}
}

// nolint[lll]
func run(ctx context.Context) error {
klog.InitFlags(nil)

var opt promobot.GenerateManifestOptions
opt.PopulateDefaults()

src := ""
flag.StringVar(
&src,
"src",
src,
"the base directory to copy from")

flag.StringVar(
&opt.Prefix,
"prefix",
opt.Prefix,
"restrict the exported files; only export those starting with the provided prefix")

flag.Parse()

if src == "" {
return xerrors.New("must specify --src")
}

s, err := filepath.Abs(src)
if err != nil {
return xerrors.Errorf("cannot resolve %q to absolute path: %w", src, err)
}
opt.BaseDir = s

manifest, err := promobot.GenerateManifest(ctx, opt)
if err != nil {
return err
}

manifestYAML, err := yaml.Marshal(manifest)
if err != nil {
return xerrors.Errorf("error serializing manifest: %w", err)
}

if _, err := os.Stdout.Write(manifestYAML); err != nil {
return err
}

return nil
}

0 comments on commit 61f4ca3

Please sign in to comment.