mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
feat: Remove magical parameters from being injected (#371)
* ci: Update DataDog GitHub branch to fallback to GITHUB_REF This was detecting branches, but not our "main" branch before. Hopefully this fixes it! * Add basic Terraform Provider * Rename post files to upload * Add tests for resources * Skip instance identity test * Add tests for ensuring agent get's passed through properly * Fix linting errors * Add echo path * Fix agent authentication * fix: Convert all jobs to use a common resource and agent type This enables a consistent API for project import and provisioned resources. * Add "coder_workspace" data source * feat: Remove magical parameters from being injected This is a much cleaner abstraction. Explicitly declaring the user parameters for each provisioner makes for significantly simpler testing.
This commit is contained in:
@ -33,10 +33,15 @@ func Root() *cobra.Command {
|
||||
Use: "coderd",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
logger := slog.Make(sloghuman.Sink(os.Stderr))
|
||||
accessURL := &url.URL{
|
||||
Scheme: "http",
|
||||
Host: address,
|
||||
}
|
||||
handler, closeCoderd := coderd.New(&coderd.Options{
|
||||
Logger: logger,
|
||||
Database: databasefake.New(),
|
||||
Pubsub: database.NewPubsubInMemory(),
|
||||
AccessURL: accessURL,
|
||||
Logger: logger,
|
||||
Database: databasefake.New(),
|
||||
Pubsub: database.NewPubsubInMemory(),
|
||||
})
|
||||
|
||||
listener, err := net.Listen("tcp", address)
|
||||
@ -45,10 +50,7 @@ func Root() *cobra.Command {
|
||||
}
|
||||
defer listener.Close()
|
||||
|
||||
client := codersdk.New(&url.URL{
|
||||
Scheme: "http",
|
||||
Host: address,
|
||||
})
|
||||
client := codersdk.New(accessURL)
|
||||
daemonClose, err := newProvisionerDaemon(cmd.Context(), client, logger)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("create provisioner daemon: %w", err)
|
||||
|
@ -2,6 +2,7 @@ package coderd
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
@ -16,9 +17,10 @@ import (
|
||||
|
||||
// Options are requires parameters for Coder to start.
|
||||
type Options struct {
|
||||
Logger slog.Logger
|
||||
Database database.Store
|
||||
Pubsub database.Pubsub
|
||||
AccessURL *url.URL
|
||||
Logger slog.Logger
|
||||
Database database.Store
|
||||
Pubsub database.Pubsub
|
||||
|
||||
GoogleTokenValidator *idtoken.Validator
|
||||
}
|
||||
|
@ -80,14 +80,7 @@ func New(t *testing.T, options *Options) *codersdk.Client {
|
||||
})
|
||||
}
|
||||
|
||||
handler, closeWait := coderd.New(&coderd.Options{
|
||||
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
|
||||
Database: db,
|
||||
Pubsub: pubsub,
|
||||
|
||||
GoogleTokenValidator: options.GoogleTokenValidator,
|
||||
})
|
||||
srv := httptest.NewUnstartedServer(handler)
|
||||
srv := httptest.NewUnstartedServer(nil)
|
||||
srv.Config.BaseContext = func(_ net.Listener) context.Context {
|
||||
ctx, cancelFunc := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancelFunc)
|
||||
@ -96,6 +89,16 @@ func New(t *testing.T, options *Options) *codersdk.Client {
|
||||
srv.Start()
|
||||
serverURL, err := url.Parse(srv.URL)
|
||||
require.NoError(t, err)
|
||||
var closeWait func()
|
||||
// We set the handler after server creation for the access URL.
|
||||
srv.Config.Handler, closeWait = coderd.New(&coderd.Options{
|
||||
AccessURL: serverURL,
|
||||
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
|
||||
Database: db,
|
||||
Pubsub: pubsub,
|
||||
|
||||
GoogleTokenValidator: options.GoogleTokenValidator,
|
||||
})
|
||||
t.Cleanup(func() {
|
||||
srv.Close()
|
||||
closeWait()
|
||||
|
@ -11,11 +11,6 @@ import (
|
||||
"github.com/coder/coder/database"
|
||||
)
|
||||
|
||||
const (
|
||||
CoderUsername = "coder_username"
|
||||
CoderWorkspaceTransition = "coder_workspace_transition"
|
||||
)
|
||||
|
||||
// ComputeScope targets identifiers to pull parameters from.
|
||||
type ComputeScope struct {
|
||||
ProjectImportJobID uuid.UUID
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
@ -90,6 +91,7 @@ func (api *api) provisionerDaemonsServe(rw http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
mux := drpcmux.New()
|
||||
err = proto.DRPCRegisterProvisionerDaemon(mux, &provisionerdServer{
|
||||
AccessURL: api.AccessURL,
|
||||
ID: daemon.ID,
|
||||
Database: api.Database,
|
||||
Pubsub: api.Pubsub,
|
||||
@ -117,6 +119,7 @@ type workspaceProvisionJob struct {
|
||||
|
||||
// Implementation of the provisioner daemon protobuf server.
|
||||
type provisionerdServer struct {
|
||||
AccessURL *url.URL
|
||||
ID uuid.UUID
|
||||
Logger slog.Logger
|
||||
Provisioners []database.ProvisionerType
|
||||
@ -228,11 +231,10 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty
|
||||
}
|
||||
protoParameters = append(protoParameters, converted)
|
||||
}
|
||||
protoParameters = append(protoParameters, &sdkproto.ParameterValue{
|
||||
DestinationScheme: sdkproto.ParameterDestination_PROVISIONER_VARIABLE,
|
||||
Name: parameter.CoderWorkspaceTransition,
|
||||
Value: string(workspaceHistory.Transition),
|
||||
})
|
||||
transition, err := convertWorkspaceTransition(workspaceHistory.Transition)
|
||||
if err != nil {
|
||||
return nil, failJob(fmt.Sprint("convert workspace transition: %w", err))
|
||||
}
|
||||
|
||||
protoJob.Type = &proto.AcquiredJob_WorkspaceProvision_{
|
||||
WorkspaceProvision: &proto.AcquiredJob_WorkspaceProvision{
|
||||
@ -240,11 +242,19 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty
|
||||
WorkspaceName: workspace.Name,
|
||||
State: workspaceHistory.ProvisionerState,
|
||||
ParameterValues: protoParameters,
|
||||
Metadata: &sdkproto.Provision_Metadata{
|
||||
CoderUrl: server.AccessURL.String(),
|
||||
WorkspaceTransition: transition,
|
||||
},
|
||||
},
|
||||
}
|
||||
case database.ProvisionerJobTypeProjectVersionImport:
|
||||
protoJob.Type = &proto.AcquiredJob_ProjectImport_{
|
||||
ProjectImport: &proto.AcquiredJob_ProjectImport{},
|
||||
ProjectImport: &proto.AcquiredJob_ProjectImport{
|
||||
Metadata: &sdkproto.Provision_Metadata{
|
||||
CoderUrl: server.AccessURL.String(),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
switch job.StorageMethod {
|
||||
@ -660,3 +670,14 @@ func convertComputedParameterValue(param parameter.ComputedValue) (*sdkproto.Par
|
||||
Value: param.SourceValue,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func convertWorkspaceTransition(transition database.WorkspaceTransition) (sdkproto.WorkspaceTransition, error) {
|
||||
switch transition {
|
||||
case database.WorkspaceTransitionStart:
|
||||
return sdkproto.WorkspaceTransition_START, nil
|
||||
case database.WorkspaceTransitionStop:
|
||||
return sdkproto.WorkspaceTransition_STOP, nil
|
||||
default:
|
||||
return 0, xerrors.Errorf("unrecognized transition: %q", transition)
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,6 @@ import (
|
||||
|
||||
"github.com/coder/coder/coderd"
|
||||
"github.com/coder/coder/coderd/coderdtest"
|
||||
"github.com/coder/coder/coderd/parameter"
|
||||
"github.com/coder/coder/codersdk"
|
||||
"github.com/coder/coder/database"
|
||||
"github.com/coder/coder/provisioner/echo"
|
||||
@ -180,15 +179,7 @@ func TestProvisionerJobResourcesByID(t *testing.T) {
|
||||
user := coderdtest.CreateInitialUser(t, client)
|
||||
_ = coderdtest.NewProvisionerDaemon(t, client)
|
||||
job := coderdtest.CreateProjectImportJob(t, client, user.Organization, &echo.Responses{
|
||||
Parse: []*proto.Parse_Response{{
|
||||
Type: &proto.Parse_Response_Complete{
|
||||
Complete: &proto.Parse_Complete{
|
||||
ParameterSchemas: []*proto.ParameterSchema{{
|
||||
Name: parameter.CoderWorkspaceTransition,
|
||||
}},
|
||||
},
|
||||
},
|
||||
}},
|
||||
Parse: echo.ParseComplete,
|
||||
Provision: []*proto.Provision_Response{{
|
||||
Type: &proto.Provision_Response_Complete{
|
||||
Complete: &proto.Provision_Complete{
|
||||
|
Reference in New Issue
Block a user