Skip to content

Commit

Permalink
Inline utils from rules_docker go pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Muller committed Apr 7, 2023
1 parent 75c78d0 commit e4830cd
Show file tree
Hide file tree
Showing 4 changed files with 613 additions and 17 deletions.
9 changes: 7 additions & 2 deletions k8s/go/pkg/resolver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "resolver",
srcs = ["resolver.go"],
srcs = [
"array_flags.go",
"resolver.go",
"stamper.go",
],
importpath = "github.com/bazelbuild/rules_k8s/k8s/go/pkg/resolver",
visibility = ["//visibility:public"],
deps = [
"@com_github_google_go_containerregistry//pkg/authn:go_default_library",
"@com_github_google_go_containerregistry//pkg/name:go_default_library",
"@com_github_google_go_containerregistry//pkg/v1:go_default_library",
"@com_github_google_go_containerregistry//pkg/v1/remote:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@in_gopkg_yaml_v2//:go_default_library",
"@io_bazel_rules_docker//container/go/pkg/compat:go_default_library",
"@io_bazel_rules_docker//container/go/pkg/utils:go_default_library",
],
)

Expand Down
23 changes: 23 additions & 0 deletions k8s/go/pkg/resolver/array_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package resolver

import "fmt"

// ArrayStringFlags are defined for string flags that may have multiple values.
type ArrayStringFlags []string

// Returns the concatenated string representation of the array of flags.
func (f *ArrayStringFlags) String() string {
return fmt.Sprintf("%v", *f)
}

// Get returns an empty interface that may be type-asserted to the underlying
// value of type bool, string, etc.
func (f *ArrayStringFlags) Get() interface{} {
return ""
}

// Set appends value the array of flags.
func (f *ArrayStringFlags) Set(value string) error {
*f = append(*f, value)
return nil
}
30 changes: 15 additions & 15 deletions k8s/go/pkg/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

"github.com/bazelbuild/rules_docker/container/go/pkg/compat"
"github.com/bazelbuild/rules_docker/container/go/pkg/utils"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
Expand All @@ -26,8 +25,8 @@ type Flags struct {
SubstitutionsFile string
AllowUnusedImages bool
NoPush bool
StampInfoFile utils.ArrayStringFlags
ImgSpecs utils.ArrayStringFlags
StampInfoFile ArrayStringFlags
ImgSpecs ArrayStringFlags
}

// Commandline flags
Expand Down Expand Up @@ -97,7 +96,7 @@ func NewResolver(flags *Flags, option ...Option) *Resolver {

// Resolve will parse the files pointed by the flags and return a resolvedTemplate and error as applicable
func (r *Resolver) Resolve() (resolvedTemplate string, err error) {
stamper, err := compat.NewStamper(r.flags.StampInfoFile)
stamper, err := NewStamper(r.flags.StampInfoFile)
if err != nil {
return "", fmt.Errorf("Failed to initialize the stamper: %w", err)
}
Expand Down Expand Up @@ -221,7 +220,7 @@ func parseImageSpec(spec string) (imageSpec, error) {
// parseSubsitutions parses a substitution file, which should be a JSON object
// with strings to search for and values to replace them with. The replacement values
// are stamped using the provided stamper.
func parseSubstitutions(file string, stamper *compat.Stamper) (map[string]string, error) {
func parseSubstitutions(file string, stamper *Stamper) (map[string]string, error) {
b, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("unable to read file: %v", err)
Expand All @@ -245,7 +244,7 @@ func parseSubstitutions(file string, stamper *compat.Stamper) (map[string]string
// registry indicated in the image name. The image name is stamped with the
// given stamper.
// The stamped image name is returned referenced by its sha256 digest.
func (r *Resolver) publishSingle(spec imageSpec, stamper *compat.Stamper) (string, error) {
func (r *Resolver) publishSingle(spec imageSpec, stamper *Stamper) (string, error) {
layers, err := spec.layers()
if err != nil {
return "", fmt.Errorf("unable to convert the layer parts in image spec for %s into a single comma separated argument: %v", spec.name, err)
Expand Down Expand Up @@ -297,10 +296,10 @@ func (r *Resolver) publishSingle(spec imageSpec, stamper *compat.Stamper) (strin
}

// publish publishes the image with the given spec. It returns:
// 1. A map from the unstamped & tagged image name to the stamped image name
// referenced by its sha256 digest.
// 2. A set of unstamped & tagged image names that were pushed to the registry.
func (r *Resolver) publish(spec []imageSpec, stamper *compat.Stamper) (map[string]string, map[string]bool, error) {
// 1. A map from the unstamped & tagged image name to the stamped image name
// referenced by its sha256 digest.
// 2. A set of unstamped & tagged image names that were pushed to the registry.
func (r *Resolver) publish(spec []imageSpec, stamper *Stamper) (map[string]string, map[string]bool, error) {
overrides := make(map[string]string)
unseen := make(map[string]bool)
for _, s := range spec {
Expand Down Expand Up @@ -336,11 +335,12 @@ type yamlResolver struct {
// a tagged image name with an image name referenced by its sha256 digest. If
// the given string doesn't represent a tagged image, it is returned as is.
// The given resolver is also modified:
// 1. If the given string was a tagged image, the resolved image lookup in the
// given resolver is updated to include a mapping from the given string to
// the resolved image name.
// 2. If the given string was a tagged image, the set of unseen images in the
// given resolver is updated to exclude the given string.
// 1. If the given string was a tagged image, the resolved image lookup in the
// given resolver is updated to include a mapping from the given string to
// the resolved image name.
// 2. If the given string was a tagged image, the set of unseen images in the
// given resolver is updated to exclude the given string.
//
// The resolver is best-effort, i.e., if any errors are encountered, the given
// string is returned as is.
func resolveString(r *yamlResolver, s string) (string, error) {
Expand Down
Loading

0 comments on commit e4830cd

Please sign in to comment.