Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: support multiple values for --experimental
  • Loading branch information
johnstcn committed Jan 18, 2023
commit 76c7b914ede984b464464ef898cb18e68c448786
39 changes: 31 additions & 8 deletions cli/deployment/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,11 @@ func newConfig() *codersdk.DeploymentConfig {
Default: 512,
},
},
Experimental: &codersdk.DeploymentConfigField[bool]{
Name: "Experimental",
Usage: "Enable experimental features. Experimental features are not ready for production.",
Flag: "experimental",
Experimental: &codersdk.DeploymentConfigField[codersdk.Experiments]{
Name: "Experimental",
Usage: "Enable one or more experiments. These are not ready for production. Separate multiple experiments with commas, or enter '*' to opt-in to all available experiments.",
Flag: "experimental",
Default: []string{},
},
UpdateCheck: &codersdk.DeploymentConfigField[bool]{
Name: "Update Check",
Expand Down Expand Up @@ -557,15 +558,37 @@ func setConfig(prefix string, vip *viper.Viper, target interface{}) {
// with a comma, but Viper only supports with a space. This
// is a small hack around it!
rawSlice := reflect.ValueOf(vip.GetStringSlice(prefix)).Interface()
slice, ok := rawSlice.([]string)
stringSlice, ok := rawSlice.([]string)
if !ok {
panic(fmt.Sprintf("string slice is of type %T", rawSlice))
}
value := make([]string, 0, len(slice))
for _, entry := range slice {
value := make([]string, 0, len(stringSlice))
for _, entry := range stringSlice {
value = append(value, strings.Split(entry, ",")...)
}
val.FieldByName("Value").Set(reflect.ValueOf(value))
case codersdk.Experiments:
// As []string above, but we support setting wildcard values
// '*' or 'true' to enable experiments listed in codersdk.ExperimentsAll.
// Experiments not listed in codersdk.ExperimentsAll must be enabled
// explicitly.
vip.MustBindEnv(prefix, env)
rawSlice := reflect.ValueOf(vip.GetStringSlice(prefix)).Interface()
stringSlice, ok := rawSlice.([]string)
if !ok {
panic(fmt.Sprintf("string slice is of type %T", rawSlice))
}
value := make([]string, 0, len(stringSlice))
for _, entry := range stringSlice {
for _, val := range strings.Split(entry, ",") {
if val == "*" || val == "true" {
value = append(value, codersdk.ExperimentsAll...)
} else {
value = append(value, val)
}
}
}
val.FieldByName("Value").Set(reflect.ValueOf(codersdk.Experiments(value)))
case []codersdk.GitAuthConfig:
// Do not bind to CODER_GITAUTH, instead bind to CODER_GITAUTH_0_*, etc.
values := readSliceFromViper[codersdk.GitAuthConfig](vip, prefix, value)
Expand Down Expand Up @@ -743,7 +766,7 @@ func setFlags(prefix string, flagset *pflag.FlagSet, vip *viper.Viper, target in
_ = flagset.IntP(flg, shorthand, vip.GetInt(prefix), usage)
case time.Duration:
_ = flagset.DurationP(flg, shorthand, vip.GetDuration(prefix), usage)
case []string:
case []string, codersdk.Experiments:
_ = flagset.StringSliceP(flg, shorthand, vip.GetStringSlice(prefix), usage)
case []codersdk.GitAuthConfig:
// Ignore this one!
Expand Down
17 changes: 17 additions & 0 deletions cli/deployment/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ func TestConfig(t *testing.T) {
require.Equal(t, config.Prometheus.Enable.Value, true)
require.Equal(t, config.Prometheus.Address.Value, config.Prometheus.Address.Default)
},
}, {
Name: "Experimental - no features",
Env: map[string]string{
"CODER_EXPERIMENTAL": "",
},
Valid: func(config *codersdk.DeploymentConfig) {
require.Empty(t, config.Experimental.Value)
},
}, {
Name: "Experimental - multiple features",
Env: map[string]string{
"CODER_EXPERIMENTAL": "foo,bar",
},
Valid: func(config *codersdk.DeploymentConfig) {
expected := []string{"foo", "bar"}
require.ElementsMatch(t, expected, config.Experimental.Value)
},
}} {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ Flags:
Consumes
$CODER_DERP_SERVER_STUN_ADDRESSES
(default [stun.l.google.com:19302])
--experimental Enable experimental features.
Experimental features are not ready for
production.
--experimental strings Enable one or more experiments. These are
not ready for production. Separate
multiple experiments with commas, or
enter '*' to opt-in to all available
experiments.
Consumes $CODER_EXPERIMENTAL
-h, --help help for server
--http-address string HTTP bind address of the server. Unset to
Expand Down
64 changes: 63 additions & 1 deletion coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 59 additions & 1 deletion coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ func New(options *Options) *API {
r.Post("/csp/reports", api.logReportCSPViolations)

r.Get("/buildinfo", buildInfo)
r.Get("/experiments", api.handleExperimentsGet)
r.Get("/updatecheck", api.updateCheck)
r.Route("/config", func(r chi.Router) {
r.Use(apiKeyMiddleware)
Expand Down
1 change: 1 addition & 0 deletions coderd/coderdtest/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func AGPLRoutes(a *AuthTester) (map[string]string, map[string]RouteCheck) {
"GET:/healthz": {NoAuthorize: true},
"GET:/api/v2": {NoAuthorize: true},
"GET:/api/v2/buildinfo": {NoAuthorize: true},
"GET:/api/v2/experiments": {NoAuthorize: true},
"GET:/api/v2/updatecheck": {NoAuthorize: true},
"GET:/api/v2/users/first": {NoAuthorize: true},
"POST:/api/v2/users/first": {NoAuthorize: true},
Expand Down
1 change: 0 additions & 1 deletion coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ type Options struct {
AppHostname string
AWSCertificates awsidentity.Certificates
Authorizer rbac.Authorizer
Experimental bool
AzureCertificates x509.VerifyOptions
GithubOAuth2Config *coderd.GithubOAuth2Config
RealIPConfig *httpmw.RealIPConfig
Expand Down
18 changes: 18 additions & 0 deletions coderd/experiments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package coderd

import (
"net/http"

"github.com/coder/coder/coderd/httpapi"
)

// @Summary Get experiments
// @ID get-experiments
// @Produce json
// @Tags General
// @Success 200 {array} string
// @Router /experiments [get]
func (api *API) handleExperimentsGet(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
httpapi.Write(ctx, rw, http.StatusOK, api.DeploymentConfig.Experimental.Value)
}
Loading