Skip to content

feat: add YAML support to server #6934

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 42 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
0d08b84
big wip
ammario Mar 30, 2023
fce8e5f
SimpleString isomorphism!!!
ammario Mar 30, 2023
2844497
Support scalars
ammario Mar 30, 2023
4960df8
ComplexObjects work!
ammario Mar 30, 2023
2a2c926
golden files work!
ammario Mar 30, 2023
056437f
Fixup YAML types
ammario Mar 30, 2023
c2428ff
give default value in comment
ammario Mar 30, 2023
8b210dc
Merge remote-tracking branch 'origin/main' into yaml
ammario Mar 30, 2023
26d7f21
Merge remote-tracking branch 'origin/main' into yaml
ammario Mar 31, 2023
38fa539
Add values.YAMLConfigPath
ammario Mar 31, 2023
64b167a
Merge remote-tracking branch 'origin/main' into yaml
ammario Mar 31, 2023
7edea99
Add YAML to core clibase parsing
ammario Mar 31, 2023
3ab35c1
Server Test WIP
ammario Mar 31, 2023
38155da
More WIP
ammario Mar 31, 2023
451c149
Merge remote-tracking branch 'origin/main' into yaml
ammario Apr 1, 2023
c72f67c
hmm
ammario Apr 1, 2023
9d61ca7
Cant find a clean way to do this..
ammario Apr 4, 2023
5b61b7b
Mediocre solution
ammario Apr 4, 2023
4923d92
hmm
ammario Apr 4, 2023
b6f982c
Merge remote-tracking branch 'origin/main' into yaml
ammario Apr 4, 2023
781786b
New, better YAML
ammario Apr 6, 2023
b03999a
clibase passes
ammario Apr 6, 2023
d018838
Fix UnknownOptions errors
ammario Apr 6, 2023
fe93d7f
Work on nil normalization
ammario Apr 6, 2023
0af47bb
Server tests pass!
ammario Apr 6, 2023
4a1df77
Merge remote-tracking branch 'origin/main' into yaml
ammario Apr 6, 2023
64255b3
make gen + self review cleanup
ammario Apr 6, 2023
99d6068
Generate docs
ammario Apr 6, 2023
300484b
make gen
ammario Apr 6, 2023
4cf5a21
Add --debug-options
ammario Apr 6, 2023
abc92d9
Normalize golden files
ammario Apr 6, 2023
a958324
fix log path
ammario Apr 7, 2023
8ead9fc
minor fix
ammario Apr 7, 2023
6eb19f6
Fix mutability bug in PrepareAll
ammario Apr 7, 2023
f77460f
Address review comments
ammario Apr 7, 2023
4e63bd5
Merge remote-tracking branch 'origin/main' into yaml
ammario Apr 7, 2023
2a96c70
Fix windows?
ammario Apr 7, 2023
7ae7cad
Small improvements
ammario Apr 7, 2023
09d5a35
Reduce YAML ident to 2
ammario Apr 7, 2023
d6506c6
Log mystery error
ammario Apr 7, 2023
551e55d
fixup! Log mystery error
ammario Apr 7, 2023
c3f3317
ecdsa
ammario Apr 7, 2023
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
Prev Previous commit
Next Next commit
Work on nil normalization
  • Loading branch information
ammario committed Apr 6, 2023
commit fe93d7f1280da26fa52f7b03541b63387faad6c3
12 changes: 12 additions & 0 deletions cli/clibase/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,15 @@ func (s *OptionSet) SetDefaults() error {
}
return merr.ErrorOrNil()
}

// ByName returns the Option with the given name, or nil if no such option
// exists.
func (s *OptionSet) ByName(name string) *Option {
for i := range *s {
opt := &(*s)[i]
if opt.Name == name {
return opt
}
}
return nil
}
9 changes: 7 additions & 2 deletions cli/clibase/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *OptionSet) MarshalYAML() (any, error) {
defValue = "<unset>"
}
comment := wordwrap.WrapString(
fmt.Sprintf("%s (default: %s)", opt.Description, defValue),
fmt.Sprintf("%s (default: %s, type: %s)", opt.Description, defValue, opt.Value.Type()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be nicer to read as %s\n(default: %s, type: %s) so default is always on the last line. Looking at the golden file it was hard to parse when the default/type randomly wrapped at 80 chars.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big fan of including the description as a comment, and the word wrapping makes it easy to read.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

80,
)
nameNode := yaml.Node{
Expand All @@ -75,7 +75,12 @@ func (s *OptionSet) MarshalYAML() (any, error) {
HeadComment: wordwrap.WrapString(comment, 80),
}
var valueNode yaml.Node
if m, ok := opt.Value.(yaml.Marshaler); ok {
if opt.Value == nil {
valueNode = yaml.Node{
Kind: yaml.ScalarNode,
Value: "null",
}
} else if m, ok := opt.Value.(yaml.Marshaler); ok {
v, err := m.MarshalYAML()
if err != nil {
return nil, xerrors.Errorf(
Expand Down
2 changes: 1 addition & 1 deletion cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ func (r *RootCmd) checkVersions(i *clibase.Invocation, client *codersdk.Client)
func (r *RootCmd) checkWarnings(i *clibase.Invocation, client *codersdk.Client) error {
if r.noFeatureWarning {
return nil
}
}

ctx, cancel := context.WithTimeout(i.Context(), 10*time.Second)
defer cancel()
Expand Down
2 changes: 0 additions & 2 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
cliui.Warnf(inv.Stderr, "YAML support is experimental and offers no compatibility guarantees.")
}

println("HERE1")

// Print deprecation warnings.
for _, opt := range opts {
if opt.UseInstead == nil {
Expand Down
59 changes: 49 additions & 10 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
Expand All @@ -35,6 +36,7 @@ import (
"gopkg.in/yaml.v3"

"github.com/coder/coder/cli"
"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/cli/config"
"github.com/coder/coder/coderd/coderdtest"
Expand Down Expand Up @@ -1441,12 +1443,13 @@ func TestServer(t *testing.T) {
client := codersdk.New(gotURL)

_ = coderdtest.CreateFirstUser(t, client)
wantValues, err := client.DeploymentConfig(ctx)
wantConfig, err := client.DeploymentConfig(ctx)
require.NoError(t, err)
cancel()
w.RequireSuccess()

// Next, we instruct the same server to display config.
// Next, we instruct the same server to display the YAML config
// and then save it.
inv = inv.WithContext(testutil.Context(t, testutil.WaitMedium))
inv.Args = append(args, "--write-config")
fi, err := os.OpenFile(testutil.TempFile(t, "", "coder-config-test-*"), os.O_WRONLY|os.O_CREATE, 0o600)
Expand All @@ -1458,19 +1461,55 @@ func TestServer(t *testing.T) {
err = inv.Run()
require.NoError(t, err)

// Finally, we read the config back in and ensure it matches the
// original.
inv.Args = append(args, "--config="+fi.Name())
// Reset the context.
ctx = testutil.Context(t, testutil.WaitMedium)
// Finally, we restart the server with just the config and no flags
// and ensure that the live configuration is equivalent.
inv, cfg = clitest.New(t, "server", "--config="+fi.Name())
w = clitest.StartWithWaiter(t, inv)
// The same client should work.
gotValues, err := client.DeploymentConfig(ctx)
require.NoError(t, err, "config:\n%s", conf.String())
require.Equal(t, wantValues, gotValues)
client = codersdk.New(waitAccessURL(t, cfg))
_ = coderdtest.CreateFirstUser(t, client)
gotConfig, err := client.DeploymentConfig(ctx)
normalizeNilsInOptionSet(&wantConfig.Options)
normalizeNilsInOptionSet(&gotConfig.Options)
require.NoError(t, err, "config:\n%s\nargs: %+v", conf.String(), inv.Args)
gotConfig.Options.ByName("Config Path").Value.Set("")
require.EqualValues(t, wantConfig.Options, gotConfig.Options)
w.RequireSuccess()
})
})
}

// The YAML process sets slice values to nil if they are empty, leading to
// false negatives when comparing equality.
func normalizeNilsInOptionSet(s *clibase.OptionSet) {
for i := range *s {
opt := &(*s)[i]
if opt.Value == nil {
continue
}

var (
v = reflect.Indirect(reflect.ValueOf(opt.Value))
kind = v.Type().Kind()
)

if opt.YAML == "supportLinks" {
fmt.Printf("supportLinks: %v, %v", kind, v.IsZero())
}

if kind == reflect.Slice && v.Len() > 0 {
continue
}

if kind == reflect.Struct && !v.IsZero() {
continue
}

opt.Value = nil
}
}

func generateTLSCertificate(t testing.TB, commonName ...string) (certPath, keyPath string) {
dir := t.TempDir()

Expand Down Expand Up @@ -1542,7 +1581,7 @@ func TestServerYAMLConfig(t *testing.T) {
require.NoError(t, err)

// Sanity-check that we can read the config back in.
err = opts.UnmarshalYAML(n)
err = opts.UnmarshalYAML(n.(*yaml.Node))
require.NoError(t, err)

wantByt, err := yaml.Marshal(n)
Expand Down
Loading