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 468aecb
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 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 := makeRemoteOptions(ctx, obj, keychain, auth, transport)

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

func makeOptions(ctxTimeout context.Context, obj *sourcev1.OCIRepository, opts ...Option) remoteOptions {
func makeRemoteOptions(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 transport != nil {
o.craneOpts = append(o.craneOpts, crane.WithTransport(transport))
o.verifyOpts = append(o.verifyOpts, remote.WithTransport(transport))
}

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))
return o
}

o.verifyOpts = append(o.verifyOpts, remote.WithAuthFromKeychain(keychain))
o.craneOpts = append(o.craneOpts, crane.WithAuthFromKeychain(keychain))

return o
}

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 468aecb

Please sign in to comment.