This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
Add ParseTagOption to k8s/go/pkg/resolver #698
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -58,14 +58,41 @@ func RegisterFlags(flagset *flag.FlagSet) *Flags { | |||
return &flags | ||||
} | ||||
|
||||
// Option can be passed to NewResolver to configure the resolver. | ||||
type Option struct { | ||||
apply func(r *Resolver) | ||||
} | ||||
|
||||
// Resolver performs substitutions and resolves/pushes images | ||||
type Resolver struct { | ||||
flags *Flags | ||||
|
||||
// parseTag is called instead of name.NewTag, which allows overriding how image | ||||
// tags are parsed. | ||||
parseTag func(name string, opts ...name.Option) (name.Tag, error) | ||||
} | ||||
|
||||
// ParseTagOption specifies a function to be used instead of name.NewTag to parse image tags. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to add a hook here vs just replacing the resolver binary? Line 284 in 6323c19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is needed to override the name resolution heuristics. Please see the example program below. A custom binary is still needed. However, the binary can reuse all the work done in the resolver library. |
||||
// | ||||
// This option allows specifying different name.Option values in the call to name.Tag. For | ||||
// example, the default logic for detecting whether a registry name is insecure can be | ||||
// overridden. | ||||
func ParseTagOption(f func(name string, opts ...name.Option) (name.Tag, error)) Option { | ||||
return Option{ | ||||
func(r *Resolver) { r.parseTag = f }, | ||||
} | ||||
} | ||||
|
||||
// NewResolver takes some Flags and returns a Resolver | ||||
func NewResolver(flags *Flags) *Resolver { | ||||
return &Resolver{flags: flags} | ||||
func NewResolver(flags *Flags, option ...Option) *Resolver { | ||||
r := &Resolver{ | ||||
flags: flags, | ||||
parseTag: name.NewTag, | ||||
} | ||||
for _, o := range option { | ||||
o.apply(r) | ||||
} | ||||
return r | ||||
} | ||||
|
||||
// Resolve will parse the files pointed by the flags and return a resolvedTemplate and error as applicable | ||||
|
@@ -238,13 +265,13 @@ func (r *Resolver) publishSingle(spec imageSpec, stamper *compat.Stamper) (strin | |||
var ref name.Reference | ||||
if r.flags.ImgChroot != "" { | ||||
n := path.Join(r.flags.ImgChroot, stampedName) | ||||
t, err := name.NewTag(n, name.WeakValidation) | ||||
t, err := r.parseTag(n, name.WeakValidation) | ||||
if err != nil { | ||||
return "", fmt.Errorf("unable to create a docker tag from stamped name %q: %v", n, err) | ||||
} | ||||
ref = t | ||||
} else { | ||||
t, err := name.NewTag(stampedName, name.WeakValidation) | ||||
t, err := r.parseTag(stampedName, name.WeakValidation) | ||||
if err != nil { | ||||
return "", fmt.Errorf("unable to create a docker tag from stamped name %q: %v", stampedName, err) | ||||
} | ||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused about why we're exporting the struct to other packages but not the methods?
Can you show me a concrete use case for how you want to use this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Option
pattern is pretty common in Go to parameterize a function with optional arguments.By using your library within a custom resolver binary, it's possible to replace the heuristics built into the resolver library with custom logic without needing to fork the library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how that is possible when Options struct rather than an interface. No one will be able to provide an alternative implementation of
apply()
(which by being lower case is also inaccessible to other packages)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The resolver library provides the only means of constructing an
Option
, likeParseTagOption
. I don't see a need for any other option types at a this time, so I kept the type private to the package. You can always open it up later.This pattern is used by the cmp library, though it uses an interface with unexported methods rather than a struct. Either works. https://pkg.go.dev/github.com/google/go-cmp/cmp#Option
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of this API design, but I guess we can change it later if necessary.