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

Implementing Apply, Delete and DeleteByName in Kube HTTP Client #610

Merged
merged 44 commits into from Aug 31, 2021

Conversation

luizbafilho
Copy link
Contributor

@luizbafilho luizbafilho commented Aug 16, 2021

What changed?

Why?

  • Avoid using kubectl binary

How did you test it?

Release notes

Documentation Changes

@luizbafilho luizbafilho changed the title Kube apply Implementing Apply, Delete and DeleteByName in Kube HTTP Client Aug 23, 2021
@luizbafilho luizbafilho marked this pull request as ready for review August 24, 2021 18:26
Copy link
Contributor

@jpellizzari jpellizzari left a comment

Choose a reason for hiding this comment

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

Tough to review this one. This level of complexity is why I didn't implement the .Apply() method in the first place I guess.

I would feel better if there were some test coverage for these changes in the kubehttp_test.go file.

cmd/wego/gitops/cmd.go Show resolved Hide resolved
pkg/gitproviders/provider_test.go Outdated Show resolved Hide resolved
pkg/kube/kube.go Outdated Show resolved Hide resolved
pkg/kube/kubehttp.go Show resolved Hide resolved
if err != nil {
return nil, fmt.Errorf("failed to initialize discovery client: %s", err)
}
mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(dc))
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a memcached instance running somewhere? Or is this only an in-memory cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That in-memory cache only I believe

@luizbafilho
Copy link
Contributor Author

Tough to review this one. This level of complexity is why I didn't implement the .Apply() method in the first place I guess.

I would feel better if there were some test coverage for these changes in the kubehttp_test.go file.

indeed, that ":8080" I reported happened while implementing those tests.

@jpellizzari
Copy link
Contributor

jpellizzari commented Aug 24, 2021

Tough to review this one. This level of complexity is why I didn't implement the .Apply() method in the first place I guess.
I would feel better if there were some test coverage for these changes in the kubehttp_test.go file.

indeed, that ":8080" I reported happened while implementing those tests.

Might help, it has a teardown func:

func StartK8sTestEnvironment() (client.Client, func(), error) {

pkg/kube/kube.go Outdated Show resolved Hide resolved
pkg/kube/kubehttp.go Outdated Show resolved Hide resolved
pkg/kube/kubehttp.go Outdated Show resolved Hide resolved
pkg/gitproviders/provider_test.go Show resolved Hide resolved
pkg/gitproviders/provider_test.go Outdated Show resolved Hide resolved

func (c *KubeHTTP) getResourceInterface(manifest []byte) (dynamic.ResourceInterface, string, []byte, error) {
obj := &unstructured.Unstructured{}
_, gvk, err := decUnstructured.Decode([]byte(manifest), nil, obj)
Copy link
Contributor

Choose a reason for hiding this comment

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

Major: It's already a []byte. Why create a new one?

pkg/kube/kubehttp.go Outdated Show resolved Hide resolved
pkg/kube/kubehttp.go Outdated Show resolved Hide resolved
pkg/kube/kubehttp.go Outdated Show resolved Hide resolved
Expect(kustObj.Name).To(Equal(name))
})

It("Delete", func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Major: I'll make this comment here and at the top of the PR. I would really like to see more thorough testing on apply, delete, delete by name. This is a super crucial object and I think it should be locked down tight.

Some other tests I would like to see are:

  • Better error handling (i.e. test the error path and confirm those areas are covered)
  • Test namespace scoped objects and non-namespace scoped objects. The current tests just test namespace resources.
  • Malformed manifests being passed into the functions

An example, is what happens to Apply when a different namespace is passed in as the parameter and the manifest parameter has a different namespace.

I know as a team we haven't come together yet on what are expectations for unit testing. Happy to discuss this further face-to-face or pair program to get some extra tests down for these functions.

Copy link
Contributor

@JamWils JamWils 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 for addressing so many comments.

default:
return kube.GVRKustomization
return schema.GroupVersionResource{}, fmt.Errorf("no matching schema.GroupVersionResource to the ResourceKind: %s", string(rk))
Copy link
Contributor

@JamWils JamWils Aug 31, 2021

Choose a reason for hiding this comment

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

FYI: I think it's good that you are passing the resourceKind that caused the error. Another approach could have been a custom error:

type ConversionError struct {
    kind string
}

func (e *ConversionError) Error() string {
    return fmt.Sprintf("no matching schema.GroupVersionResource to the ResourceKind: %s", string(e.kind))
}

The effect of this is a more clear way to check for the error consistently with errors.As(err, &e) and not having to worry about the ResourceKind changing on you. However, this is excessive considering you aren't explicitly checking this error and handling it in a special way.

Thanks for addressing my main concern of odd side effects.

@luizbafilho luizbafilho merged commit 87c4fcd into main Aug 31, 2021
@luizbafilho luizbafilho deleted the 546-kube-apply branch August 31, 2021 20:02
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.

Make lint fails Implement Apply, Delete and DeleteByName in kubehttp
3 participants