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

feat: add v1alpha3 api with RuntimeSpec configuration #922

Merged
merged 29 commits into from
Dec 14, 2023

Conversation

ashnamehrotra
Copy link
Contributor

What this PR does / why we need it:
Part 1 of adding RuntimeSpec configuration. Adds v1alpha3 api with RuntimeSpec field and relevant conversion functions.

Which issue(s) this PR fixes (optional, using fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when the PR gets merged):
Fixes #

Special notes for your reviewer:

Signed-off-by: ashnamehrotra <[email protected]>
Signed-off-by: ashnamehrotra <[email protected]>
cfg *v1alpha3.EraserConfig
}

func (m *Manager) Read() (v1alpha3.EraserConfig, error) {
Copy link
Contributor

@pmengelbert pmengelbert Dec 4, 2023

Choose a reason for hiding this comment

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

I believe you can remove these Read and Update methods and the NewManager function. Only the unversioned package needs those. For example, the corresponding functions from the v1alpha2/config package are not actually called anywhere.

I think the functions aren't even needed for the conversion generation either. You can remove these, and the corresponding ones in v1alpha1 and v1alpha2 as well.

return rs, nil
}

func (td *Duration) UnmarshalJSON(b []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd also like to have a unit test to prevent this from regressing. The unmarshaling is important because it catches errors, and we need to have tests to prove it fails when it is supposed to and doesn't when it's not.

// TODO: change this to use unversioned.RuntimeSpec when unversioned is updated
//
//nolint:revive
func autoConvert_unversioned_Runtime_To_v1alpha3_RuntimeSpec(in *unversioned.Runtime, out *RuntimeSpec, _ conversion.Scope) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be named autoConvert..., since it's manually written, right?

Copy link
Contributor

@pmengelbert pmengelbert left a comment

Choose a reason for hiding this comment

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

This PR isn't quite complete. We need to replace the unversioned.EraserConfig with the v1alpha3 version. The unversioned should always be the latest stable version. Since none of our EraserConfig versions are stable, it should always just be the latest.

This will have effects on some of the code here, since it won't be necessary to have conversion functions between v1alpha3 and unversioned. But you will need converters for 1 -> unversioned and 2 -> unversioned.

@pmengelbert
Copy link
Contributor

pmengelbert commented Dec 4, 2023

On second thought, it's best to keep the unversioned api at v1alpha2, and to switch it to v1alpha3 in the future when it's properly wired up.

Signed-off-by: ashnamehrotra <[email protected]>
Signed-off-by: ashnamehrotra <[email protected]>
func ConvertRuntimeToRuntimeSpec(r Runtime) (RuntimeSpec, error) {
var rs RuntimeSpec

switch rt := r; rt {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just switch r here

Signed-off-by: ashnamehrotra <[email protected]>
Signed-off-by: ashnamehrotra <[email protected]>
type (
Duration time.Duration
Runtime string
RuntimeAddress string
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the newtype for RuntimeAddress? Why not just use a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed to string only Runtime has its own type


tests := map[string]testCase{
"ValidContainerd": {
input: []byte(`{"Name": "containerd", "Address": "unix:///run/containerd/containerd.sock"}`),
Copy link
Contributor

Choose a reason for hiding this comment

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

The keys should all be lowercase, i.e. name and not Name. If the test is passing as-is, it is a false positive and we need to find out why.

Copy link
Contributor

Choose a reason for hiding this comment

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

And by keys I mean the keys in the JSON byte string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed keys to lowercase! it says here it accepts case-insensitive match for the keys: https://pkg.go.dev/encoding/json#Unmarshal

@@ -12,12 +12,14 @@ func loadConfig(filename string) (Config, error) {

b, err := os.ReadFile(filename)
if err != nil {
log.Info("LOADCONFIG unable to read file")
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe log.Error here, and remove the LOADCONFIG -- the logger should give us more info on where the error is occurring.

for name, test := range tests {
t.Run(name, func(t *testing.T) {
var rs RuntimeSpec
err := rs.UnmarshalJSON(test.input)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we shouldn't call UnmarshalJSON directly but rather just err := json.Unmarshal(test.input, &rs). That's because json.Unmarshal will ultimately call rs.UnmarshalJSON. That's how it ends up getting used in our code, so that's what we should test.

Copy link
Contributor

@pmengelbert pmengelbert left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@ashnamehrotra ashnamehrotra merged commit afb831b into eraser-dev:main Dec 14, 2023
169 of 170 checks passed
ashnamehrotra added a commit to ashnamehrotra/eraser that referenced this pull request Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants