- Notifications
You must be signed in to change notification settings - Fork923
feat: implement dynamic parameter validation#18482
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
Emyrk wants to merge2 commits intostevenmasley/parameters_to_previewChoose a base branch fromstevenmasley/dynamic_validate
base:stevenmasley/parameters_to_preview
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+437 −131
Draft
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletioncli/server.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletionscoderd/autobuild/lifecycle_executor.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionscoderd/coderdtest/coderdtest.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionscoderd/dynamicparameters/render.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletionscoderd/dynamicparameters/render_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dynamicparameters | ||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
) | ||
func TestProvisionerVersionSupportsDynamicParameters(t *testing.T) { | ||
t.Parallel() | ||
for v, dyn := range map[string]bool{ | ||
"": false, | ||
"na": false, | ||
"0.0": false, | ||
"0.10": false, | ||
"1.4": false, | ||
"1.5": false, | ||
"1.6": true, | ||
"1.7": true, | ||
"1.8": true, | ||
"2.0": true, | ||
"2.17": true, | ||
"4.0": true, | ||
} { | ||
t.Run(v, func(t *testing.T) { | ||
t.Parallel() | ||
does := ProvisionerVersionSupportsDynamicParameters(v) | ||
require.Equal(t, dyn, does) | ||
}) | ||
} | ||
} |
162 changes: 162 additions & 0 deletionscoderd/dynamicparameters/resolver.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package dynamicparameters | ||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/google/uuid" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/util/slice" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
type ParameterResolver struct { | ||
renderer Renderer | ||
firstBuild bool | ||
presetValues []database.TemplateVersionPresetParameter | ||
previousValues []database.WorkspaceBuildParameter | ||
buildValues []database.WorkspaceBuildParameter | ||
} | ||
type parameterValueSource int | ||
const ( | ||
sourcePrevious parameterValueSource = iota | ||
sourceBuild | ||
sourcePreset | ||
) | ||
type parameterValue struct { | ||
Value string | ||
Source parameterValueSource | ||
} | ||
func ResolveParameters( | ||
ctx context.Context, | ||
ownerID uuid.UUID, | ||
renderer Renderer, | ||
firstBuild bool, | ||
previousValues []database.WorkspaceBuildParameter, | ||
buildValues []codersdk.WorkspaceBuildParameter, | ||
presetValues []database.TemplateVersionPresetParameter, | ||
) (map[string]string, hcl.Diagnostics) { | ||
previousValuesMap := slice.ToMap(previousValues, func(p database.WorkspaceBuildParameter) (string, string) { | ||
return p.Value, p.Value | ||
}) | ||
// Start with previous | ||
values := parameterValueMap(slice.ToMap(previousValues, func(p database.WorkspaceBuildParameter) (string, parameterValue) { | ||
return p.Name, parameterValue{Source: sourcePrevious, Value: p.Value} | ||
})) | ||
// Add build values | ||
for _, buildValue := range buildValues { | ||
if _, ok := values[buildValue.Name]; !ok { | ||
values[buildValue.Name] = parameterValue{Source: sourceBuild, Value: buildValue.Value} | ||
} | ||
} | ||
// Add preset values | ||
for _, preset := range presetValues { | ||
if _, ok := values[preset.Name]; !ok { | ||
values[preset.Name] = parameterValue{Source: sourcePreset, Value: preset.Value} | ||
} | ||
} | ||
originalValues := make(map[string]parameterValue, len(values)) | ||
for name, value := range values { | ||
// Store the original values for later use. | ||
originalValues[name] = value | ||
} | ||
// Render the parameters using the values that were supplied to the previous build. | ||
// | ||
// This is how the form should look to the user on their workspace settings page. | ||
// This is the original form truth that our validations should be based on going forward. | ||
output, diags := renderer.Render(ctx, ownerID, values.ValuesMap()) | ||
if diags.HasErrors() { | ||
// Top level diagnostics should break the build. Previous values (and new) should | ||
// always be valid. If there is a case where this is not true, then this has to | ||
// be changed to allow the build to continue with a different set of values. | ||
return nil, diags | ||
} | ||
// The user's input now needs to be validated against the parameters. | ||
// Mutability & Ephemeral parameters depend on sequential workspace builds. | ||
// | ||
// To enforce these, the user's input values are trimmed based on the | ||
// mutability and ephemeral parameters defined in the template version. | ||
// The output parameters | ||
for _, parameter := range output.Parameters { | ||
// Ephemeral parameters should not be taken from the previous build. | ||
// Remove their values from the input if they are sourced from the previous build. | ||
if parameter.Ephemeral { | ||
v := values[parameter.Name] | ||
if v.Source == sourcePrevious { | ||
delete(values, parameter.Name) | ||
} | ||
} | ||
// Immutable parameters should also not be allowed to be changed from | ||
// the previous build. Remove any values taken from the preset or | ||
// new build params. This forces the value to be the same as it was before. | ||
if !firstBuild && !parameter.Mutable { | ||
delete(values, parameter.Name) | ||
prev, ok := previousValuesMap[parameter.Name] | ||
if ok { | ||
values[parameter.Name] = parameterValue{ | ||
Value: prev, | ||
Source: sourcePrevious, | ||
} | ||
} | ||
} | ||
} | ||
// This is the final set of values that will be used. Any errors at this stage | ||
// are fatal. Additional validation for immutability has to be done manually. | ||
output, diags = renderer.Render(ctx, ownerID, values.ValuesMap()) | ||
if diags.HasErrors() { | ||
return nil, diags | ||
} | ||
for _, parameter := range output.Parameters { | ||
if !firstBuild && !parameter.Mutable { | ||
if parameter.Value.AsString() != originalValues[parameter.Name].Value { | ||
var src *hcl.Range | ||
if parameter.Source != nil { | ||
src = ¶meter.Source.HCLBlock().TypeRange | ||
} | ||
// An immutable parameter was changed, which is not allowed. | ||
// Add the failed diagnostic to the output. | ||
diags = diags.Append(&hcl.Diagnostic{ | ||
Severity: 0, | ||
Summary: "Immutable parameter changed", | ||
Detail: fmt.Sprintf("Parameter %q is not mutable, so it can't be updated after creating a workspace.", parameter.Name), | ||
Subject: src, | ||
}) | ||
} | ||
} | ||
} | ||
// TODO: Validate all parameter values. | ||
// Return the values to be saved for the build. | ||
// TODO: The previous code always returned parameter names and values, even if they were not set | ||
// by the user. So this should loop over the parameters and return all of them. | ||
// This catches things like if a default value changes, we keep the old value. | ||
return values.ValuesMap(), diags | ||
} | ||
type parameterValueMap map[string]parameterValue | ||
func (p parameterValueMap) ValuesMap() map[string]string { | ||
values := make(map[string]string, len(p)) | ||
for name, paramValue := range p { | ||
values[name] = paramValue.Value | ||
} | ||
return values | ||
} |
10 changes: 10 additions & 0 deletionscoderd/util/slice/slice.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscoderd/workspacebuilds.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletionscoderd/workspaces.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.