-
Notifications
You must be signed in to change notification settings - Fork 32
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 Workspace Variable Set Support #497
base: main
Are you sure you want to change the base?
Conversation
901e005
to
dfa1ba6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have left a few comments, mostly cosmetic changes to align with the patterns that we have in place.
It looks like you are on the right track with this addition. 👍🏻
I would suggest adding a usage(bare-minimum) example of a YAML manifest to the PR description. It will help you and everyone else who reviews this PR to understand how it should look from the end user's perspective.
Also, consider doing changes one by one when you work on the controller logic. For instance, once you prototyped CR and are done with API, add logic that will allow you to refer to a variable set by ID only. Make sure you cover all scenarios here, such as but not limited to: creating a new CR with VS, adding VS to an existing CR, deleting VS from CR, updating VS reference or adding a new one, etc. One this all done and works, it would be easier for you to add VS reference by name.
One more thing, please take a look at how files are organized and either move your logic to a new file now or work on it within workspace_controller.go
and move it later.
All in all, good! :)
@sheneska, as we discussed, please make sure you have updated the FAQ to explain variable sets support logic. It is a bit tricky addition and might cause some unexpected results to the one who is not aware of the implementation details. But this is something you can add at the end. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for addressing my previous comments. 👍🏻 I have left a few more.
charts/hcp-terraform-operator/crds/app.terraform.io_workspaces.yaml
Outdated
Show resolved
Hide resolved
workspace = fmt.Sprintf("kubernetes-operator-%v", randomNumber()) | ||
variableSetName = fmt.Sprintf("variable-set-%v", randomNumber()) | ||
variableSetName2 = fmt.Sprintf("%v-2", variableSetName) | ||
variableSetID = createTestVariableSet(variableSetName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please validate this function. This set of tests fails due to an error returned by HCP Terraform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still an issue with the latest commit.
appv1alpha2 "github.com/hashicorp/hcp-terraform-operator/api/v1alpha2" | ||
) | ||
|
||
var _ = Describe("Workspace controller", Ordered, func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All in all, it's great that you've added tests here. However, they are failing due to the lack of connection between workspace reconciliation and the new logic you're working on. Please refer to my comment in the controllers/workspace_controller_variable_sets.go
file.
You can run tests locally and focus only on a specific one by passing an addition argument:
$ make test TESTARGS='-ginkgo.focus "VariableSet"'
You need to export the following environment variables to make them run:
$ export TFC_TOKEN=<TFC_API_TOKEN>
$ export TFC_ORG=<TFC_ORG_NAME>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still an issue with the latest commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks better now. 👍🏻 I have left a few comments regarding the reconciliation logic. Please, ensure you have fixed tests before marking the comment as resolved.
appv1alpha2 "github.com/hashicorp/hcp-terraform-operator/api/v1alpha2" | ||
) | ||
|
||
var _ = Describe("Workspace controller", Ordered, func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still an issue with the latest commit.
workspace = fmt.Sprintf("kubernetes-operator-%v", randomNumber()) | ||
variableSetName = fmt.Sprintf("variable-set-%v", randomNumber()) | ||
variableSetName2 = fmt.Sprintf("%v-2", variableSetName) | ||
variableSetID = createTestVariableSet(variableSetName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still an issue with the latest commit.
|
||
} | ||
|
||
func (r *WorkspaceReconciler) reconcileVariableSets(ctx context.Context, w *workspaceInstance, workspace *tfc.Workspace) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the logic in this method:
- It only reconcile the first variable set in the list. See my comment below.
- It does API calls before they are needed.
When everything is synchronized, and during every sync period, when the operator runs a reconciliation loop, we should make as few API calls as possible. The ideal case is either 0 or 1 API call. 0 is when spec.variableSets
is empty(this one you have handled), and 1 is when actual and desired states match. With the current logic, you do 2 or 3 API calls(depending on how a variable set is referred to, by name or ID).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a self review - need to optimize code
} | ||
|
||
// List all variable sets once, a single API call | ||
listOpts := &tfc.VariableSetListOptions{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Page size = 100, what happens if there are >100?
for loop to iterate thru all pages to ensure we access everything...
|
||
for _, variableSetID := range variableSetIDs { | ||
// Check if the variable set exists in the actual state | ||
variableSet, found := variableSetsByID[variableSetID] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is variableSet & variableSetsByID ?
} | ||
|
||
// Searching by name instead | ||
for _, vs := range variableSetList.Items { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to be optimized...
extra looping
} | ||
|
||
// Update page number to fetch the next set of results | ||
listOpts.PageNumber = variableSetList.NextPage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider the for loop for paginating thru all var sets,
restructuring these lines (fr line 33)
func (w *workspaceInstance) getVariableSetIDs(ctx context.Context) ([]string, error) { | ||
variableSets := w.instance.Spec.VariableSets | ||
|
||
if variableSets == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will always error, users also have the ability to not add variable sets
consider this....
} | ||
} | ||
|
||
if len(variableSetIDs) == 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error message needs to be more explicit
1st check if varset was found or not...move it up
Description
This PR extends the Workspace Controller by adding support for variable sets. A new field
spec.variablesets.[id | name]
allows assigning a variable set to a workspace.Related to #207.
How does this work?
This functionality allows users to apply existing variable sets to existing workspaces. If the variable set has been set to global, we can assume it is applied to all workspaces. The scope cannot be changed in this functionality.
Usage Example
References
Community Note