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

Add condition "nodes" that triggers a task everytime a Consul node is updated #358

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

remilapeyre
Copy link

No description provided.

@remilapeyre remilapeyre requested a review from a team August 17, 2021 20:42
@lornasong lornasong added the enhancement New feature or request label Aug 18, 2021
Copy link
Contributor

@findkim findkim left a comment

Choose a reason for hiding this comment

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

Hi @remilapeyre. We are excited about your contribution! @lornasong has caught me up to speed with communication between you two. We rotate community duties and so happy to jump in here and update you on where we are:

After reviewing the changes, the approach and proposed feature look good. It’s great to see how the nodes feature you have added extends our new construct of conditions quite well.

There are a few items that would get this PR and feature ready.

  • Can you write up a PR summary to include: a high level summary of the feature + use case, and a code block with a config example setting a task with the node condition?
  • Can you add an integration test and an end-to-end test for the node condition template? Here are some examples that might help with that: catalog-services integration, services-regex e2e

We are planning to release 0.3.0 soon, and so your feature could make it into the next release! It shouldn't be too long after 0.3.0 🙂


import "fmt"

const nodesConditionTypes = "nodes"
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you mind adding a compile check for NodesConditionConfig to satisfy the interface ConditionConfig below the const? It helps us keep all of the condition types standardized.

var _ ConditionConfig = (*NodesConditionConfig)(nil)

type NodesConditionConfig struct {
Datacenter *string `mapstructure:"datacenter"`

services []string
Copy link
Contributor

Choose a reason for hiding this comment

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

I support the idea that the nodes condition doesn't allow setting the services in the task config until we know of a clearer use-case. Though I don't see how this list of services here would be set for this validation during config loading.

If that's the case, can you remove this and add the validation within the TaskConfig.Validate()? There are other cases where the condition specs are checked.

}

var o NodesConditionConfig
o.Datacenter = n.Datacenter
Copy link
Contributor

Choose a reason for hiding this comment

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

minor nit to avoid copying the pointer here to avoid unintended changes between copies and merges.

Suggested change
o.Datacenter = n.Datacenter
o.Datacenter = StringCopy(n.Datacenter)

&NodesConditionConfig{Datacenter: String("same")},
&NodesConditionConfig{Datacenter: String("different")},
&NodesConditionConfig{Datacenter: String("different")},
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding a couple of test cases for completion

Suggested change
},
},
{
"datacenter_empty_one",
& NodesConditionConfig{Datacenter: String("same")},
& NodesConditionConfig{},
& NodesConditionConfig{Datacenter: String("same")},
},
{
"datacenter_empty_two",
& NodesConditionConfig{},
& NodesConditionConfig{Datacenter: String("same")},
& NodesConditionConfig{Datacenter: String("same")},
},

const nodesConditionTypes = "nodes"

type NodesConditionConfig struct {
Datacenter *string `mapstructure:"datacenter"`
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that the query parameter Filter is available and supported in the node template, it would be a good feature to expose through the configuration to be used.

I like the omission of the node-meta parameter since it's marked as deprecated for the /v1/catalog/nodes endpoint

}

const nodesRegexTmpl = `
nodes = [
Copy link
Contributor

Choose a reason for hiding this comment

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

The attributes in the variable object look good 👍

We have a larger design choice for CTS to generate input variables as maps instead of lists for the purpose of modules to easily consume it with the Terraform for_each syntax. Would you mind changing it from a list to map?

As for the key of the map, we could use id.node.datacenter for uniqueness. Similar to the ID of the var.services input variable

"github.com/pkg/errors"
)

func nodesFunc(recall hcat.Recaller) interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

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

Although these functions are not public, can you add code comments for them since they contain a lot of broader context that is not immediately noticeable.

You can follow the general format found here

// catalogServicesRegistrationFunc returns information on registered Consul
// services. It queries the Catalog List Services API and supports the query
// parameters dc, ns, and node-meta. It also adds an additional layer of
// custom functionality on the API response:
// - Adds regex filtering on service name option e.g. "regexp=api"
//
// Endpoint: /v1/catalog/services
// Template: {{ catalogServicesRegistration <filter options> ... }}

@findkim
Copy link
Contributor

findkim commented Aug 24, 2021

Adding one more comment that is not an ask.

The content added in templates/tftmpl/tmplfunc/nodes.go and its test file would be great to move to the hashicorp/hcat library. It's where general Consul API handling is done for templates. The library will be also adopted by Consul-Template, and I can imagine that others would benefit from your addition for nodes and filtering support!

@mkam has some hcat work inflight too, so that's why it's not an ask for you to move it there. We can handle that and updating the CTS dependency on the library afterwards.

@mkam
Copy link
Contributor

mkam commented Oct 4, 2021

Hi @remilapeyre, just for awareness, we did a fairly major refactoring of our conditions code (see #385), so your PR is out of date with how conditions are structured now. Please let us know how you'd like to proceed with your changes!

@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants