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

Endpoints controller with tests #455

Merged
merged 14 commits into from Mar 24, 2021

Conversation

ndhanushkodi
Copy link
Contributor

@ndhanushkodi ndhanushkodi commented Mar 15, 2021

The endpoints controller and test code is ready for review.

The only things expected to change at this point are code and tests in handler (to remove always setting the service-name annotation) and inject-connect (to get the command unit tests to pass since the manager takes a kubeconfig that's hard to fake out).

Changes in this PR:

  • supports creating, updating and deleting endpoints
    • with different consul service name (using the service-name pod annotation)
  • updating endpoints supports adding and removing addresses from the endpoints object.

Todos:

  • testing with new connect-init command
  • testing end to end

Future PR:

  • delete default annotation for service name (to not cause rebase issues with webhook refactor PR lined up after this one)
  • inject-connect tests-- the webhook PR has a fix for one of these tests, and the other test is signal handling that would likely be refactored out. There's a TODO for this in the code.
  • add metrics config to proxy registration (this is going to a future PR so it won't conflict with the upcoming webhook refactor PR)
  • deleting old service registration

Co-authored-by: Iryna Shustava iryna@hashicorp.com
Co-authored-by: Kyle Schochenmaier kyle.schochenmaier@hashicorp.com

How I've tested this PR:

  • Unit tests for endpoints controller

How I expect reviewers to test this PR:

Checklist:

  • Tests added
  • CHANGELOG entry added (HashiCorp engineers only, community PRs should not add a changelog entry)

@ndhanushkodi ndhanushkodi marked this pull request as draft March 15, 2021 16:24
@ndhanushkodi ndhanushkodi changed the title WIP Endpoints controller with tests Endpoints controller with tests Mar 18, 2021
@ndhanushkodi ndhanushkodi marked this pull request as ready for review March 18, 2021 17:12
@ndhanushkodi ndhanushkodi requested review from a team, ishustava and thisisnotashwin and removed request for a team March 18, 2021 17:12
@ndhanushkodi ndhanushkodi force-pushed the basic-endpoints-controller-and-tests branch from 9390339 to affaf94 Compare March 18, 2021 18:51
Copy link
Contributor

@thisisnotashwin thisisnotashwin left a comment

Choose a reason for hiding this comment

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

This looks really good so far Nitya!! I do want to give it one more pass cuz it is complicated (not the code, mostly just the number of things it has to manage)!!

subcommand/inject-connect/command.go Show resolved Hide resolved
subcommand/inject-connect/command.go Outdated Show resolved Hide resolved
subcommand/inject-connect/command.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
return ctrl.Result{}, err
}

r.Log.Info("retrieved service from kube", "serviceEndpoints", serviceEndpoints)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The whole serviceEndpoints object shouldn't be logged here, just the name, same for other logs. Make sure to decide what should be Debug vs Info. Should info logs log every reconcile or only debug?

return ctrl.Result{}, err
}

r.Log.Info("retrieved service from kube", "serviceEndpoints", serviceEndpoints)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it to "retrieved Kubernetes Endpoints" since it's the endpoints object

@ndhanushkodi ndhanushkodi force-pushed the basic-endpoints-controller-and-tests branch from 10900d1 to 752eca4 Compare March 22, 2021 05:11
@ndhanushkodi ndhanushkodi added the theme/tproxy Items related to transparent proxy label Mar 22, 2021
ndhanushkodi and others added 12 commits March 22, 2021 15:26
* supports using serviceName annotation (different k8s service name)
* supports using servicePort annotation
* supports creating endpoints (with serviceName annotation)
* supports updating endpoints (adding and removing addresses)
* supports deleting endpoints (with different k8s service name)

todo:
* tags, meta, and upstreams from annotations + tests
* cleaning up and refactoring code to run the endpoints controller with
manager
* deleting old service registration
* testing with new connect-init command
* testing end to end

Co-authored-by: Iryna Shustava <iryna@hashicorp.com>
Co-authored-by: Kyle Schochenmaier <kyle.schochenmaier@hashicorp.com>
deregistration to use catalog api rather than agent api
* they needed the auth plugin import: _ "k8s.io/client-go/plugin/pkg/client/auth"
* kubernetes/client-go#242
Todos left:
* remove service name default annotation
* make inject-connect unit tests pass without a kubeconfig
* end to end testing
method.

This way, additional edge case logic can be tested separately, rather
than in TestReconcileCreateEndpoint.
This will be a part of a future PR that enables endpoints-controller and
removes the old method of service registration.
@ndhanushkodi ndhanushkodi force-pushed the basic-endpoints-controller-and-tests branch from 752eca4 to 715854a Compare March 22, 2021 22:27
@hashicorp hashicorp deleted a comment from thisisnotashwin Mar 22, 2021
Copy link
Member

@ishustava ishustava left a comment

Choose a reason for hiding this comment

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

Looking great!! Awesome work thinking through all the test case scenarios!

I've left some comments and questions, let me know what you think!

// "k8s-service-name". So, we query Consul services by "k8s-service-name" metadata, which is only exposed on the agent
// API. Therefore, we need to query all agents who have services matching that metadata, and deregister each service
// instance. When querying by the k8s service name and namespace, the request will return service instances and
// associated proxy service instances.
Copy link
Member

Choose a reason for hiding this comment

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

great job on this func and the comments!! It was very easy to read!

Copy link
Contributor

Choose a reason for hiding this comment

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

HARD AGREE

connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller_test.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller_test.go Show resolved Hide resolved
connect-inject/endpoints_controller_test.go Show resolved Hide resolved
connect-inject/endpoints_controller_test.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@thisisnotashwin thisisnotashwin left a comment

Choose a reason for hiding this comment

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

Ok i gave this a thorough code review!! left a bunch of suggestions. Most of them are random tidy ups and some of them are questions.
I haven't actually run this but im guessing that will be within the scope of the glue PR.
Im ok with this getting merged when the suggestions and open comments have been resolved!!

This is excellent work @ndhanushkodi and the code is really easy to read for the nature of the complexity it is trying to solve!! Love the comments as well!! Amazing job overall!!

connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
// "k8s-service-name". So, we query Consul services by "k8s-service-name" metadata, which is only exposed on the agent
// API. Therefore, we need to query all agents who have services matching that metadata, and deregister each service
// instance. When querying by the k8s service name and namespace, the request will return service instances and
// associated proxy service instances.
Copy link
Contributor

Choose a reason for hiding this comment

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

HARD AGREE

connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
if _, ok := endpointsAddressesMap[serviceRegistration.Address]; !ok {
// If the service address is not in the Endpoints addresses, deregister it.
if err = client.Agent().ServiceDeregister(svcID); err != nil {
r.Log.Error(err, "failed to deregister service instance", "consul-service-id", svcID)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we create a constant for "consul-service-id"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems a little strange to me to make it a const since it's just a key in the log. Basically I am not sure what I would call the const to have it be more readable than the string "consul-service-id".

connect-inject/endpoints_controller.go Outdated Show resolved Hide resolved
subcommand/inject-connect/command.go Outdated Show resolved Hide resolved
Copy link
Member

@ishustava ishustava left a comment

Choose a reason for hiding this comment

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

Looks great!!

Comment on lines +180 to +183
// Get the tags from the deprecated tags annotation and combine.
if raw, ok := pod.Annotations[annotationConnectTags]; ok && raw != "" {
tags = append(tags, strings.Split(raw, ",")...)
}
Copy link
Member

Choose a reason for hiding this comment

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

It looks like the annotation was renamed in #141, and we've kept both names for backward compatibility. Since this is meant to be a refactor I think we can keep it. It seems relatively cheap to maintain.

@ndhanushkodi ndhanushkodi merged commit 281552c into feature-tproxy Mar 24, 2021
@ndhanushkodi ndhanushkodi deleted the basic-endpoints-controller-and-tests branch March 24, 2021 22:01
thisisnotashwin pushed a commit that referenced this pull request Mar 26, 2021
Add basic endpoints controller with tests

* reconciles k8s services with Consul

* supports creating, updating, and deleting endpoints, both with matching Consul and k8s service names, and with different Consul and k8s service names


Co-authored-by: Iryna Shustava <iryna@hashicorp.com>
Co-authored-by: Kyle Schochenmaier <kyle.schochenmaier@hashicorp.com>
thisisnotashwin pushed a commit that referenced this pull request Mar 26, 2021
Add basic endpoints controller with tests

* reconciles k8s services with Consul

* supports creating, updating, and deleting endpoints, both with matching Consul and k8s service names, and with different Consul and k8s service names


Co-authored-by: Iryna Shustava <iryna@hashicorp.com>
Co-authored-by: Kyle Schochenmaier <kyle.schochenmaier@hashicorp.com>
ishustava added a commit that referenced this pull request Apr 14, 2021
Add basic endpoints controller with tests

* reconciles k8s services with Consul

* supports creating, updating, and deleting endpoints, both with matching Consul and k8s service names, and with different Consul and k8s service names


Co-authored-by: Iryna Shustava <iryna@hashicorp.com>
Co-authored-by: Kyle Schochenmaier <kyle.schochenmaier@hashicorp.com>
ndhanushkodi pushed a commit to ndhanushkodi/consul-k8s that referenced this pull request Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme/tproxy Items related to transparent proxy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants