Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SYSENG-1357: generic client default options #361

Merged
3 commits merged into from
May 17, 2024

Conversation

ghost
Copy link

@ghost ghost commented Apr 24, 2024

Description

This PR adds functionality to define default requests options via WithRequestOptions when creating a new generic API client.

Checklist

  • added release notes to Unreleased section in CHANGELOG.md, if user facing change

References

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

@ghost ghost self-assigned this Apr 24, 2024
@ghost ghost force-pushed the SYSENG-1357/generic-client-default-options branch from 9ead8f6 to 3f68833 Compare April 24, 2024 05:54
Copy link

codeclimate bot commented Apr 24, 2024

Code Climate has analyzed commit f5b16ea and detected 0 issues on this pull request.

View more on Code Climate.

pkg/apis/kubernetes/v1/common.go Show resolved Hide resolved
pkg/api/options.go Outdated Show resolved Hide resolved
@ghost ghost force-pushed the SYSENG-1357/generic-client-default-options branch 2 times, most recently from 3a1abc3 to 167a9f9 Compare April 24, 2024 06:19
@ghost ghost marked this pull request as ready for review April 24, 2024 06:23
Copy link
Member

@89Q12 89Q12 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@LittleFox94 LittleFox94 left a comment

Choose a reason for hiding this comment

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

found a few things that would be worth changing, but please also first note what I wrote you in Mattermost

Comment on lines 54 to 87
type AnyOption interface {
ApplyToAny(Options) error
}

type Option interface{}

Copy link
Contributor

Choose a reason for hiding this comment

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

missing doc comments

"go.anx.io/go-anxcloud/pkg/api/types"
)

func EnvironmentProduction(override bool) types.Option {
Copy link
Contributor

Choose a reason for hiding this comment

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

missing doc comment

Copy link
Author

@ghost ghost Apr 30, 2024

Choose a reason for hiding this comment

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

Added the comment (not pushed yet).

But do we need this file and its content at all in this project? We are already defaulting to the prod environment.

Copy link
Contributor

Choose a reason for hiding this comment

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

hm probably not, good point - up to you

Comment on lines 15 to 16
url, err := np.EndpointURL(
types.ContextWithOperation(types.ContextWithOptions(context.TODO(), &types.ListOptions{}), types.OperationList))
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this change necessary?

Copy link
Author

Choose a reason for hiding this comment

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

See comment below

Comment on lines 58 to 59
if options, err := types.OptionsFromContext(ctx); err != nil {
return "", err
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't it just return the default value in this case?

Copy link
Author

Choose a reason for hiding this comment

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

Hmm.. not sure about that. I think Options are always expected to be available on the context when using this helper (GetEnvironmentPathSegment) defaulting to prod (in most cases) on error sounds a bit dangerous.

Copy link
Contributor

Choose a reason for hiding this comment

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

really not sure .. I think the risk to default to prod due to this is pretty low and can be reasonably enough tested with unit tests, but makes the code using this function a lot nicer in exchange

And dealing with the risk is also possible by using tokens that cannot use the wrong service

} else if err != nil {
return "", err
} else if envString, ok := env.(string); !ok {
return "", ErrEnvironmentSegmentNotTypeString
Copy link
Contributor

Choose a reason for hiding this comment

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

if the comment above is accepted, this is the last branch that could return an error - could it be sensible to have a environments variable on types.commonOptions that can only hold strings, removing the need for this function to be able to return an error, which then also helps with the now 5 returns in apis/kubernetes/v1.endpointURL?

Copy link
Author

Choose a reason for hiding this comment

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

With extra methods SetEnvironment & GetEnvironment on types.Options i suppose?

Copy link
Contributor

Choose a reason for hiding this comment

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

something like that, yea

pkg/api/api_implementation.go Outdated Show resolved Hide resolved
@ghost ghost force-pushed the SYSENG-1357/generic-client-default-options branch from b1074c9 to e6d05f2 Compare May 3, 2024 04:52
Comment on lines 92 to 94
for _, requestOpt := range a.requestOptions {
if getOpt, ok := requestOpt.(types.GetOption); ok {
err = errors.Join(err, getOpt.ApplyToGet(&options))
}
}
for _, opt := range opts {
opt.ApplyToGet(&options)
err = errors.Join(err, opt.ApplyToGet(&options))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

this can be prettied up a bit for all the request methods with a small generic helper:

func resolveRequestOptions[T any](commonOptions []types.Option, requestOptions []T) []T {
	return append(filterOptions[T](commonOptions), requestOptions...)
}

func filterOptions[T any](opts []types.Option) []T {
	ret := make([]T, 0, len(opts))
	for _, v := range opts {
		if v, ok := v.(T); ok {
			ret = append(ret, v)
		}
	}

	return ret
}

With that present, the marked code would be replaced with this:

for _, opt := range resolveRequestOptions(a.requestOptions, opts) {
	err = errors.Join(err, opt.ApplyToGet(&options))
}

@@ -73,3 +107,26 @@ func (o *commonOptions) Set(key string, val interface{}, overwrite bool) error {
o.additional[key] = val
return nil
}

func (o commonOptions) GetEnvironment(key string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

no doc comment..

Comment on lines 25 to 27

// ErrEnvironmentSegmentNotTypeString is returned when the environment segment is not of type string
ErrEnvironmentSegmentNotTypeString = errors.New("environment segment is not of type string")
Copy link
Contributor

Choose a reason for hiding this comment

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

not needed anymore, right?

@ghost ghost force-pushed the SYSENG-1357/generic-client-default-options branch 3 times, most recently from d56adca to 9fc6738 Compare May 13, 2024 10:18
@ghost ghost added the integration_tests Used to approve the run of integration tests! label May 13, 2024
@ghost ghost force-pushed the SYSENG-1357/generic-client-default-options branch from 9fc6738 to 512b567 Compare May 13, 2024 10:48
@ghost ghost added integration_tests Used to approve the run of integration tests! and removed integration_tests Used to approve the run of integration tests! labels May 13, 2024
@ghost ghost added integration_tests Used to approve the run of integration tests! and removed integration_tests Used to approve the run of integration tests! labels May 14, 2024
Copy link
Member

@89Q12 89Q12 left a comment

Choose a reason for hiding this comment

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

LGTM, the failing integration tests well meh.
Otherwise doc strings have all been added.

@LittleFox94
Copy link
Contributor

All relevant integration tests went through, I'm ok with merging without all of them being green

@ghost ghost merged commit bf9a6d6 into main May 17, 2024
16 of 17 checks passed
@ghost ghost deleted the SYSENG-1357/generic-client-default-options branch May 17, 2024 09:43
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integration_tests Used to approve the run of integration tests!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants