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

🐛 Fix panic when executionMode is set to agent but agentPool is not set #242

Merged
merged 3 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
BUG FIXES:
* `AgentPool`: fix an issue when `plan_queued` and `apply_queued` statuses do not trigger agent scaling. [[GH-215](https://github.com/hashicorp/terraform-cloud-operator/pull/215)]
* `Helm Chart`: fix an issue with the Deployment template in the Helm chart where `name` in path `spec.template.spec.containers[0]` was duplicated. [[GH-216](https://github.com/hashicorp/terraform-cloud-operator/pull/216)]
* `Workspace`: fix an issue when the Operator panics when `spec.executionMode` is configured as `agent` but `spec.agentPool` is not set which is mandatory in this case. [[GH-242](https://github.com/hashicorp/terraform-cloud-operator/pull/242)]

ENHANCEMENT:
* `Operator`: Add the ability to skip TLS certificate validation for communication between the Operator and the TFC/E endpoint. A new environment variable `TFC_TLS_SKIP_VERIFY` should be set to `true` to skip the validation. Default: `false`. [[GH-222](https://github.com/hashicorp/terraform-cloud-operator/pull/222)]
Expand Down
18 changes: 17 additions & 1 deletion api/v1alpha2/workspace_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func (w *Workspace) ValidateSpec() error {
var allErrs field.ErrorList

allErrs = append(allErrs, w.validateSpecAgentPool()...)
allErrs = append(allErrs, w.validateSpecExecutionMode()...)
allErrs = append(allErrs, w.validateSpecNotifications()...)
allErrs = append(allErrs, w.validateSpecRemoteStateSharing()...)
allErrs = append(allErrs, w.validateSpecRunTasks()...)
Expand Down Expand Up @@ -62,6 +63,22 @@ func (w *Workspace) validateSpecAgentPool() field.ErrorList {
return allErrs
}

func (w *Workspace) validateSpecExecutionMode() field.ErrorList {
allErrs := field.ErrorList{}
spec := w.Spec.ExecutionMode

f := field.NewPath("spec").Child("executionMode")

if spec == "agent" && w.Spec.AgentPool == nil {
allErrs = append(allErrs, field.Required(
f,
"'spec.agentPool' must be set when 'spec.executionMode' is set to 'agent'"),
)
}

return allErrs
}

func (w *Workspace) validateSpecNotifications() field.ErrorList {
allErrs := field.ErrorList{}
spec := w.Spec.Notifications
Expand Down Expand Up @@ -417,6 +434,5 @@ func (w *Workspace) validateSpecSSHKey() field.ErrorList {
// + EnvironmentVariables names duplicate: spec.environmentVariables[].name
// + TerraformVariables names duplicate: spec.terraformVariables[].name
// + Tags duplicate: spec.tags[]
// + AgentPool must be set when ExecutionMode = 'agent': spec.agentPool <- spec.executionMode['agent']
//
// + Invalid CR cannot be deleted until it is fixed -- need to discuss if we want to do something about it
45 changes: 45 additions & 0 deletions api/v1alpha2/workspace_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,51 @@ func TestValidateWorkspaceSpecAgentPool(t *testing.T) {
}
}

func TestValidateWorkspaceSpecExecutionMode(t *testing.T) {
successCases := map[string]Workspace{
"AgentWithAgentPoolWithID": {
Spec: WorkspaceSpec{
ExecutionMode: "agent",
AgentPool: &WorkspaceAgentPool{
ID: "this",
},
},
},
"AgentWithAgentPoolWithName": {
Spec: WorkspaceSpec{
ExecutionMode: "agent",
AgentPool: &WorkspaceAgentPool{
Name: "this",
},
},
},
}

for n, c := range successCases {
t.Run(n, func(t *testing.T) {
if errs := c.validateSpecExecutionMode(); len(errs) != 0 {
t.Errorf("Unexpected validation errors: %v", errs)
}
})
}

errorCases := map[string]Workspace{
"AgentWithoutAgentPool": {
Spec: WorkspaceSpec{
ExecutionMode: "agent",
},
},
}

for n, c := range errorCases {
t.Run(n, func(t *testing.T) {
if errs := c.validateSpecExecutionMode(); len(errs) == 0 {
t.Error("Unexpected failure, at least one error is expected")
}
})
}
}

func TestValidateWorkspaceSpecNotifications(t *testing.T) {
token := "token"
url := "https://example.com"
Expand Down
4 changes: 4 additions & 0 deletions controllers/workspace_controller_agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (r *WorkspaceReconciler) getAgentPoolIDByName(ctx context.Context, w *works
func (r *WorkspaceReconciler) getAgentPoolID(ctx context.Context, w *workspaceInstance) (string, error) {
specAgentPool := w.instance.Spec.AgentPool

if specAgentPool == nil {
return "", fmt.Errorf("'spec.agentPool' is not set")
}

if specAgentPool.Name != "" {
w.log.Info("Reconcile Agent Pool", "msg", "getting agent pool ID by name")
return r.getAgentPoolIDByName(ctx, w)
Expand Down
Loading