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 Workspace Variable Set Support #497

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

sheneska
Copy link
Contributor

@sheneska sheneska commented Oct 15, 2024

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.

  • If the variable set is global, we can assume it is already applied to the workspace.
  • If the variable set is not global, then it can be applied to a specific workspace.

Usage Example

References

Community Note

  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.
  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request.
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment.

@sheneska sheneska requested a review from a team as a code owner October 15, 2024 20:58
@sheneska sheneska marked this pull request as draft October 15, 2024 20:58
@github-actions github-actions bot added documentation Improvements or additions to documentation api golang size/XL labels Oct 15, 2024
@arybolovlev arybolovlev added this to the v2.8.0 milestone Oct 24, 2024
@github-actions github-actions bot added size/XS and removed size/XL labels Oct 29, 2024
@sheneska sheneska force-pushed the add-support-workspace-varset branch from 901e005 to dfa1ba6 Compare October 29, 2024 15:57
@github-actions github-actions bot added the test label Nov 4, 2024
@sheneska sheneska marked this pull request as ready for review November 4, 2024 21:29
Copy link
Contributor

@arybolovlev arybolovlev left a 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! :)

api/v1alpha2/workspace_types.go Outdated Show resolved Hide resolved
api/v1alpha2/workspace_types.go Show resolved Hide resolved
api/v1alpha2/workspace_types.go Outdated Show resolved Hide resolved
api/v1alpha2/workspace_types.go Outdated Show resolved Hide resolved
api/v1alpha2/workspace_types.go Outdated Show resolved Hide resolved
api/v1alpha2/variablesets_validation_test.go Outdated Show resolved Hide resolved
controllers/workspace_controller.go Outdated Show resolved Hide resolved
controllers/workspace_controller.go Outdated Show resolved Hide resolved
controllers/workspace_controller.go Outdated Show resolved Hide resolved
controllers/workspace_controller.go Outdated Show resolved Hide resolved
@arybolovlev arybolovlev marked this pull request as draft November 13, 2024 19:16
@arybolovlev
Copy link
Contributor

@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.

@github-actions github-actions bot added size/XL and removed size/L labels Nov 20, 2024
Copy link
Contributor

@arybolovlev arybolovlev left a 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.

controllers/workspace_controller.go Outdated Show resolved Hide resolved
controllers/workspace_controller.go Outdated Show resolved Hide resolved
docs/examples/workspace-variable-sets.yaml Outdated Show resolved Hide resolved
api/v1alpha2/workspace_validation.go Outdated Show resolved Hide resolved
api/v1alpha2/workspace_validation_test.go Show resolved Hide resolved
api/v1alpha2/workspace_validation_test.go 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)
Copy link
Contributor

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.

Copy link
Contributor

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() {
Copy link
Contributor

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>

Copy link
Contributor

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.

Copy link
Contributor

@arybolovlev arybolovlev left a 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() {
Copy link
Contributor

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)
Copy link
Contributor

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.

controllers/workspace_controller_variable_sets.go Outdated Show resolved Hide resolved
controllers/workspace_controller_variable_sets.go Outdated Show resolved Hide resolved

}

func (r *WorkspaceReconciler) reconcileVariableSets(ctx context.Context, w *workspaceInstance, workspace *tfc.Workspace) error {
Copy link
Contributor

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:

  1. It only reconcile the first variable set in the list. See my comment below.
  2. 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).

Copy link
Contributor Author

@sheneska sheneska left a 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{
Copy link
Contributor Author

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]
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants