Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: Allow provisioner jobs to be disconnected from projects #194

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

Merged
merged 12 commits into from
Feb 8, 2022
Prev Previous commit
Next Next commit
Allow variables to be injected that are not defined by the schema
  • Loading branch information
kylecarbs committed Feb 8, 2022
commit 78055e402a60437c867f205118ad48bf170f46cd
46 changes: 20 additions & 26 deletions coderd/projectparameter/projectparameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// Scope targets identifiers to pull parameters from.
type Scope struct {
ImportJobID uuid.UUID
OrganizationID sql.NullString
OrganizationID string
ProjectID uuid.NullUUID
UserID sql.NullString
WorkspaceID uuid.NullUUID
Expand All @@ -42,12 +42,10 @@ func Compute(ctx context.Context, db database.Store, scope Scope, additional ...
parameterSchemasByName: map[string]database.ParameterSchema{},
}

// All parameters for the project version!
// All parameters for the import job ID!
parameterSchemas, err := db.GetParameterSchemasByJobID(ctx, scope.ImportJobID)
if errors.Is(err, sql.ErrNoRows) {
// This occurs when the provided import job has
// defined no parameters, so we have nothing to compute!
return []Value{}, nil
err = nil
}
if err != nil {
return nil, xerrors.Errorf("get project parameters: %w", err)
Expand All @@ -56,15 +54,13 @@ func Compute(ctx context.Context, db database.Store, scope Scope, additional ...
compute.parameterSchemasByName[projectVersionParameter.Name] = projectVersionParameter
}

if scope.OrganizationID.Valid {
// Organization parameters come first!
err = compute.injectScope(ctx, database.GetParameterValuesByScopeParams{
Scope: database.ParameterScopeOrganization,
ScopeID: scope.OrganizationID.String,
})
if err != nil {
return nil, err
}
// Organization parameters come first!
err = compute.injectScope(ctx, database.GetParameterValuesByScopeParams{
Scope: database.ParameterScopeOrganization,
ScopeID: scope.OrganizationID,
})
if err != nil {
return nil, err
}

// Default project parameter values come second!
Expand Down Expand Up @@ -182,18 +178,16 @@ func (c *compute) injectScope(ctx context.Context, scopeParams database.GetParam

func (c *compute) injectSingle(scopedParameter database.ParameterValue) error {
parameterSchema, hasParameterSchema := c.parameterSchemasByName[scopedParameter.Name]
if !hasParameterSchema {
if hasParameterSchema {
// Don't inject parameters that aren't defined by the project.
return nil
}

_, hasExistingParameter := c.computedParameterByName[scopedParameter.Name]
if hasExistingParameter {
// If a parameter already exists, check if this variable can override it.
// Injection hierarchy is the responsibility of the caller. This check ensures
// project parameters cannot be overridden if already set.
if !parameterSchema.AllowOverrideSource && scopedParameter.Scope != database.ParameterScopeProject {
return nil
_, hasExistingParameter := c.computedParameterByName[scopedParameter.Name]
if hasExistingParameter {
// If a parameter already exists, check if this variable can override it.
// Injection hierarchy is the responsibility of the caller. This check ensures
// project parameters cannot be overridden if already set.
if !parameterSchema.AllowOverrideSource && scopedParameter.Scope != database.ParameterScopeProject {
return nil
}
}
}

Expand All @@ -204,7 +198,7 @@ func (c *compute) injectSingle(scopedParameter database.ParameterValue) error {

switch scopedParameter.SourceScheme {
case database.ParameterSourceSchemeData:
c.computedParameterByName[parameterSchema.Name] = Value{
c.computedParameterByName[scopedParameter.Name] = Value{
Proto: &proto.ParameterValue{
DestinationScheme: destinationScheme,
Name: scopedParameter.SourceValue,
Expand Down
9 changes: 3 additions & 6 deletions coderd/projectparameter/projectparameter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ func TestCompute(t *testing.T) {
t.Parallel()
generateScope := func() projectparameter.Scope {
return projectparameter.Scope{
ImportJobID: uuid.New(),
OrganizationID: sql.NullString{
String: uuid.New().String(),
Valid: true,
},
ImportJobID: uuid.New(),
OrganizationID: uuid.NewString(),
ProjectID: uuid.NullUUID{
UUID: uuid.New(),
Valid: true,
Expand Down Expand Up @@ -124,7 +121,7 @@ func TestCompute(t *testing.T) {
ID: uuid.New(),
Name: parameter.Name,
Scope: database.ParameterScopeOrganization,
ScopeID: scope.OrganizationID.String,
ScopeID: scope.OrganizationID,
SourceScheme: database.ParameterSourceSchemeData,
SourceValue: "nop",
DestinationScheme: database.ParameterDestinationSchemeEnvironmentVariable,
Expand Down
7 changes: 2 additions & 5 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,8 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty

// Compute parameters for the workspace to consume.
parameters, err := projectparameter.Compute(ctx, server.Database, projectparameter.Scope{
ImportJobID: job.ID,
OrganizationID: sql.NullString{
String: input.OrganizationID,
Valid: input.OrganizationID != "",
},
ImportJobID: job.ID,
OrganizationID: input.OrganizationID,
ProjectID: uuid.NullUUID{
UUID: input.ProjectID,
Valid: input.ProjectID.String() != uuid.Nil.String(),
Expand Down