Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Inline utils from rules_docker go pkg #725

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about this one, should it be inlined too? I guess you'll have a later PR to drop the go_repository that fetches rules_docker code. I don't know what is the maintenance story for rules_k8s so maybe we should wait-and-see if we get any responses.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe that one should also be inlined as part of this change. I had held off since that particular import needs to be translated to understand the OCI image format rather than the format rules_docker is outputting today. But that translation could easily be done in a followup PR after inlining in this one

"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