Skip to content

Commit

Permalink
refactor makeOptions
Browse files Browse the repository at this point in the history
Reduce complexity by replacing the functional options with a flat out
conditional logic in makeOptions.

Signed-off-by: Soule BA <[email protected]>
  • Loading branch information
souleb committed Sep 29, 2022
1 parent 6c590b6 commit a4f5fb4
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions controllers/ocirepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (r *OCIRepositoryReconciler) reconcileSource(ctx context.Context, obj *sour
return sreconcile.ResultEmpty, e
}

opts := makeOptions(ctx, obj, withTransport(transport), withKeychainOrAuth(keychain, auth))
opts := makeOptions(ctx, obj, keychain, auth, transport)

// Determine which artifact revision to pull
url, err := r.getArtifactURL(obj, opts.craneOpts)
Expand Down Expand Up @@ -1174,14 +1174,26 @@ func craneOptions(ctx context.Context, insecure bool) []crane.Option {
return options
}

func makeOptions(ctxTimeout context.Context, obj *sourcev1.OCIRepository, opts ...Option) remoteOptions {
func makeOptions(ctxTimeout context.Context, obj *sourcev1.OCIRepository, keychain authn.Keychain, auth authn.Authenticator,
transport http.RoundTripper) remoteOptions {
o := remoteOptions{
craneOpts: craneOptions(ctxTimeout, obj.Spec.Insecure),
verifyOpts: []remote.Option{},
}

for _, opt := range opts {
opt(&o)
if auth != nil {
// auth take precedence over keychain here as we expect the caller to set
// the auth only if it is required.
o.verifyOpts = append(o.verifyOpts, remote.WithAuth(auth))
o.craneOpts = append(o.craneOpts, crane.WithAuth(auth))
} else {
o.verifyOpts = append(o.verifyOpts, remote.WithAuthFromKeychain(keychain))
o.craneOpts = append(o.craneOpts, crane.WithAuthFromKeychain(keychain))
}

if transport != nil {
o.craneOpts = append(o.craneOpts, crane.WithTransport(transport))
o.verifyOpts = append(o.verifyOpts, remote.WithTransport(transport))
}

return o
Expand All @@ -1191,28 +1203,3 @@ type remoteOptions struct {
craneOpts []crane.Option
verifyOpts []remote.Option
}

type Option func(*remoteOptions)

func withKeychainOrAuth(keychain authn.Keychain, auth authn.Authenticator) Option {
return func(o *remoteOptions) {
if auth != nil {
// auth take precedence over keychain here as we expect the caller to set
// the auth only if it is required.
o.verifyOpts = append(o.verifyOpts, remote.WithAuth(auth))
o.craneOpts = append(o.craneOpts, crane.WithAuth(auth))
} else {
o.verifyOpts = append(o.verifyOpts, remote.WithAuthFromKeychain(keychain))
o.craneOpts = append(o.craneOpts, crane.WithAuthFromKeychain(keychain))
}
}
}

func withTransport(transport http.RoundTripper) Option {
return func(o *remoteOptions) {
if transport != nil {
o.craneOpts = append(o.craneOpts, crane.WithTransport(transport))
o.verifyOpts = append(o.verifyOpts, remote.WithTransport(transport))
}
}
}

0 comments on commit a4f5fb4

Please sign in to comment.