From 580cdc46fc7a197f31d8a3eff3d686df56e0d2fd Mon Sep 17 00:00:00 2001 From: ying-jeanne <74549700+ying-jeanne@users.noreply.github.com> Date: Tue, 28 Sep 2021 17:09:09 +0200 Subject: [PATCH] [Chore] update to cue v0.4.0 (#39641) * change global flag to flagset * update pr with comments * replace flag.args by flagset * fix build * migrate the schema package to use cue 4.0 * fix the load package --- go.mod | 4 +++- go.sum | 7 ++++-- pkg/cmd/grafana-server/commands/cli.go | 31 +++++++++++++++----------- pkg/schema/load/common.go | 4 ++-- pkg/schema/load/dashboard.go | 18 +++++++-------- pkg/schema/load/generic.go | 16 +++++++------ pkg/schema/load/load_test.go | 14 ++++++------ pkg/schema/load/panel.go | 16 ++++++------- pkg/schema/schema.go | 31 +++++++++++++------------- pkg/schema/schema_test.go | 18 +++++++-------- 10 files changed, 85 insertions(+), 74 deletions(-) diff --git a/go.mod b/go.mod index ab9cb2af023..286b4cab158 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ replace k8s.io/client-go => k8s.io/client-go v0.22.1 require ( cloud.google.com/go/storage v1.14.0 - cuelang.org/go v0.3.2 + cuelang.org/go v0.4.0 github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0 github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.10.0 github.com/BurntSushi/toml v0.3.1 @@ -164,6 +164,7 @@ require ( github.com/gogo/googleapis v1.4.1 // indirect github.com/gogo/status v1.0.3 // indirect github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect + github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/gomodule/redigo v2.0.0+incompatible // indirect @@ -210,6 +211,7 @@ require ( github.com/prometheus/exporter-toolkit v0.6.1 // indirect github.com/prometheus/node_exporter v1.0.0-rc.0.0.20200428091818-01054558c289 // indirect github.com/prometheus/procfs v0.6.0 // indirect + github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc // indirect github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be // indirect github.com/rs/cors v1.8.0 // indirect github.com/russross/blackfriday/v2 v2.0.1 // indirect diff --git a/go.sum b/go.sum index ebecf886343..93a58a90afd 100644 --- a/go.sum +++ b/go.sum @@ -58,8 +58,8 @@ code.cloudfoundry.org/clock v1.0.0/go.mod h1:QD9Lzhd/ux6eNQVUDVRJX/RKTigpewimNYB collectd.org v0.3.0/go.mod h1:A/8DzQBkF6abtvrT2j/AU/4tiBgJWYyh0y/oB/4MlWE= contrib.go.opencensus.io/exporter/ocagent v0.6.0/go.mod h1:zmKjrJcdo0aYcVS7bmEeSEBLPA9YJp5bjrofdU3pIXs= contrib.go.opencensus.io/exporter/prometheus v0.3.0/go.mod h1:rpCPVQKhiyH8oomWgm34ZmgIdZa8OVYO5WAIygPbBBE= -cuelang.org/go v0.3.2 h1:/Am5yFDwqnaEi+g942OPM1M4/qtfVSm49wtkQbeh5Z4= -cuelang.org/go v0.3.2/go.mod h1:jvMO35Q4D2D3m2ujAmKESICaYkjMbu5+D+2zIGuWTpQ= +cuelang.org/go v0.4.0 h1:GLJblw6m2WGGCA3k1v6Wbk9gTOt2qto48ahO2MmSd6I= +cuelang.org/go v0.4.0/go.mod h1:tz/edkPi+T37AZcb5GlPY+WJkL6KiDlDVupKwL3vvjs= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8= @@ -1007,6 +1007,7 @@ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZ github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= github.com/golang/geo v0.0.0-20190916061304-5b978397cfec/go.mod h1:QZ0nwyI2jOfgRAoBvP+ab5aRr7c9x7lhGEJrKvBwjWI= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -2026,6 +2027,8 @@ github.com/prometheus/prometheus v1.8.2-0.20210915140241-bd217c58a735/go.mod h1: github.com/prometheus/statsd_exporter v0.20.0/go.mod h1:YL3FWCG8JBBtaUSxAg4Gz2ZYu22bS84XM89ZQXXTWmQ= github.com/prometheus/statsd_exporter v0.21.0/go.mod h1:rbT83sZq2V+p73lHhPZfMc3MLCHmSHelCh9hSGYNLTQ= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc h1:gSVONBi2HWMFXCa9jFdYvYk7IwW/mTLxWOF7rXS4LO0= +github.com/protocolbuffers/txtpbfmt v0.0.0-20201118171849-f6a6b3f636fc/go.mod h1:KbKfKPy2I6ecOIGA9apfheFv14+P3RSmmQvshofQyMY= github.com/rafaeljusto/redigomock v0.0.0-20190202135759-257e089e14a1/go.mod h1:JaY6n2sDr+z2WTsXkOmNRUfDy6FN0L6Nk7x06ndm4tY= github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ= github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q= diff --git a/pkg/cmd/grafana-server/commands/cli.go b/pkg/cmd/grafana-server/commands/cli.go index 620e2f86c4e..4252c239a6f 100644 --- a/pkg/cmd/grafana-server/commands/cli.go +++ b/pkg/cmd/grafana-server/commands/cli.go @@ -39,27 +39,32 @@ type exitWithCode struct { code int } +var serverFs = flag.NewFlagSet("server", flag.ContinueOnError) + func (e exitWithCode) Error() string { return e.reason } func RunServer(opt ServerOptions) int { var ( - configFile = flag.String("config", "", "path to config file") - homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory") - pidFile = flag.String("pidfile", "", "path to pid file") - packaging = flag.String("packaging", "unknown", "describes the way Grafana was installed") + configFile = serverFs.String("config", "", "path to config file") + homePath = serverFs.String("homepath", "", "path to grafana install/home path, defaults to working directory") + pidFile = serverFs.String("pidfile", "", "path to pid file") + packaging = serverFs.String("packaging", "unknown", "describes the way Grafana was installed") - v = flag.Bool("v", false, "prints current version and exits") - vv = flag.Bool("vv", false, "prints current version, all dependencies and exits") - profile = flag.Bool("profile", false, "Turn on pprof profiling") - profileAddr = flag.String("profile-addr", "localhost", "Define custom address for profiling") - profilePort = flag.Uint64("profile-port", 6060, "Define custom port for profiling") - tracing = flag.Bool("tracing", false, "Turn on tracing") - tracingFile = flag.String("tracing-file", "trace.out", "Define tracing output file") + v = serverFs.Bool("v", false, "prints current version and exits") + vv = serverFs.Bool("vv", false, "prints current version, all dependencies and exits") + profile = serverFs.Bool("profile", false, "Turn on pprof profiling") + profileAddr = serverFs.String("profile-addr", "localhost", "Define custom address for profiling") + profilePort = serverFs.Uint64("profile-port", 6060, "Define custom port for profiling") + tracing = serverFs.Bool("tracing", false, "Turn on tracing") + tracingFile = serverFs.String("tracing-file", "trace.out", "Define tracing output file") ) - flag.Parse() + if err := serverFs.Parse(os.Args[1:]); err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + return 1 + } if *v || *vv { fmt.Printf("Version %s (commit: %s, branch: %s)\n", opt.Version, opt.Commit, opt.BuildBranch) @@ -161,7 +166,7 @@ func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnos } s, err := server.Initialize(setting.CommandLineArgs{ - Config: configFile, HomePath: homePath, Args: flag.Args(), + Config: configFile, HomePath: homePath, Args: serverFs.Args(), }, server.Options{ PidFile: pidFile, Version: opt.Version, Commit: opt.Commit, BuildBranch: opt.BuildBranch, }, api.ServerOptions{}) diff --git a/pkg/schema/load/common.go b/pkg/schema/load/common.go index 33cb4c74d83..df8cd164052 100644 --- a/pkg/schema/load/common.go +++ b/pkg/schema/load/common.go @@ -6,12 +6,12 @@ import ( "io/fs" "path/filepath" - "cuelang.org/go/cue" + "cuelang.org/go/cue/cuecontext" "cuelang.org/go/cue/load" "github.com/grafana/grafana" ) -var rt = &cue.Runtime{} +var ctx = cuecontext.New() // Families can have variants, where more typing information narrows the // possible values for certain keys in schemas. These are a meta-property diff --git a/pkg/schema/load/dashboard.go b/pkg/schema/load/dashboard.go index 71aceb9cacf..b3335cc2312 100644 --- a/pkg/schema/load/dashboard.go +++ b/pkg/schema/load/dashboard.go @@ -56,15 +56,15 @@ func baseDashboardFamily(p BaseLoadPaths) (cue.Value, error) { Module: "github.com/grafana/grafana", Dir: filepath.Join(prefix, dashboardDir), } - inst, err := rt.Build(load.Instances(nil, cfg)[0]) - if err != nil { - cueError := schema.WrapCUEError(err) - if err != nil { + inst := ctx.BuildInstance(load.Instances(nil, cfg)[0]) + if inst.Err() != nil { + cueError := schema.WrapCUEError(inst.Err()) + if inst.Err() != nil { return cue.Value{}, cueError } } - famval := inst.Value().LookupPath(cue.MakePath(cue.Str("Family"))) + famval := inst.LookupPath(cue.MakePath(cue.Str("Family"))) if !famval.Exists() { return cue.Value{}, errors.New("dashboard schema family did not exist at expected path in expected file") } @@ -148,11 +148,11 @@ func (cds *compositeDashboardSchema) Validate(r schema.Resource) error { if name == "" { name = "resource" } - rv, err := rt.Compile(name, r.Value) - if err != nil { - return err + rv := ctx.CompileString(r.Value.(string), cue.Filename(name)) + if rv.Err() != nil { + return rv.Err() } - return cds.actual.Unify(rv.Value()).Validate(cue.Concrete(true)) + return cds.actual.Unify(rv).Validate(cue.Concrete(true)) } // CUE returns the cue.Value representing the actual schema. diff --git a/pkg/schema/load/generic.go b/pkg/schema/load/generic.go index 12323126656..d50daed1513 100644 --- a/pkg/schema/load/generic.go +++ b/pkg/schema/load/generic.go @@ -12,11 +12,11 @@ import ( // definitions on which all Grafana scuemata rely. // // TODO probably cache this or something -func getBaseScuemata(p BaseLoadPaths) (*cue.Instance, error) { +func getBaseScuemata(p BaseLoadPaths) (cue.Value, error) { overlay := make(map[string]load.Source) if err := toOverlay(filepath.Join(prefix, "grafana"), p.BaseCueFS, overlay); err != nil { - return nil, err + return cue.Value{}, err } cfg := &load.Config{ @@ -37,10 +37,12 @@ func getBaseScuemata(p BaseLoadPaths) (*cue.Instance, error) { // this "/". Dir: prefix, } - return rt.Build(load.Instances([]string{ + + res := ctx.BuildInstance(load.Instances([]string{ filepath.Join(prefix, "grafana", "cue", "scuemata", "scuemata.cue"), filepath.Join(prefix, "grafana", "cue", "scuemata", "panel-plugin.cue"), }, cfg)[0]) + return res, res.Err() } func buildGenericScuemata(famval cue.Value) (schema.VersionedCueSchema, error) { @@ -106,11 +108,11 @@ func (gvs *genericVersionedSchema) Validate(r schema.Resource) error { if name == "" { name = "resource" } - rv, err := rt.Compile(name, r.Value) - if err != nil { - return err + rv := ctx.CompileString(r.Value.(string), cue.Filename(name)) + if rv.Err() != nil { + return rv.Err() } - return gvs.actual.Unify(rv.Value()).Validate(cue.Concrete(true)) + return gvs.actual.Unify(rv).Validate(cue.Concrete(true)) } // CUE returns the cue.Value representing the actual schema. diff --git a/pkg/schema/load/load_test.go b/pkg/schema/load/load_test.go index 3f450f58517..5c61878c812 100644 --- a/pkg/schema/load/load_test.go +++ b/pkg/schema/load/load_test.go @@ -91,7 +91,7 @@ func TestDevenvDashboardValidity(t *testing.T) { } t.Run(filepath.Base(path), func(t *testing.T) { - err := sch.Validate(schema.Resource{Value: byt, Name: path}) + err := sch.Validate(schema.Resource{Value: string(byt), Name: path}) if err != nil { // Testify trims errors to short length. We want the full text errstr := errors.Details(err, nil) @@ -191,16 +191,16 @@ func TestAllPluginsInDist(t *testing.T) { Dir: filepath.Join(prefix, dashboardDir, "dist"), Package: "dist", } - inst, err := rt.Build(load.Instances(nil, cfg)[0]) - require.NoError(t, err) + inst := ctx.BuildInstance(load.Instances(nil, cfg)[0]) + require.NoError(t, inst.Err()) - dinst, err := rt.Compile("str", ` + dinst := ctx.CompileString(` Family: compose: Panel: {} typs: [for typ, _ in Family.compose.Panel {typ}] - `) - require.NoError(t, err) + `, cue.Filename("str")) + require.NoError(t, dinst.Err()) - typs := dinst.Value().Unify(inst.Value()).LookupPath(cue.MakePath(cue.Str("typs"))) + typs := dinst.Unify(inst).LookupPath(cue.MakePath(cue.Str("typs"))) j, err := cuejson.Marshal(typs) require.NoError(t, err) diff --git a/pkg/schema/load/panel.go b/pkg/schema/load/panel.go index 0a176cd33e7..50cf002a86d 100644 --- a/pkg/schema/load/panel.go +++ b/pkg/schema/load/panel.go @@ -20,7 +20,7 @@ import ( func mapPanelModel(id string, vcs schema.VersionedCueSchema) cue.Value { maj, min := vcs.Version() // Ignore err return, this can't fail to compile - inter, _ := rt.Compile(fmt.Sprintf("%s-glue-panelComposition", id), fmt.Sprintf(` + inter := ctx.CompileString(fmt.Sprintf(` in: { type: %q v: { @@ -39,10 +39,10 @@ func mapPanelModel(id string, vcs schema.VersionedCueSchema) cue.Value { fieldConfig: defaults: custom: in.model.PanelFieldConfig } } - `, id, maj, min)) + `, id, maj, min), cue.Filename(fmt.Sprintf("%s-glue-panelComposition", id))) // TODO validate, especially with #PanelModel - return inter.Value().FillPath(cue.MakePath(cue.Str("in"), cue.Str("model")), vcs.CUE()).LookupPath(cue.MakePath(cue.Str(("result")))) + return inter.FillPath(cue.MakePath(cue.Str("in"), cue.Str("model")), vcs.CUE()).LookupPath(cue.MakePath(cue.Str(("result")))) } func loadPanelScuemata(p BaseLoadPaths) (map[string]cue.Value, error) { @@ -60,7 +60,7 @@ func loadPanelScuemata(p BaseLoadPaths) (map[string]cue.Value, error) { return nil, err } - pmf := base.Value().LookupPath(cue.MakePath(cue.Def("#PanelFamily"))) + pmf := base.LookupPath(cue.MakePath(cue.Def("#PanelFamily"))) if !pmf.Exists() { return nil, errors.New("could not locate #PanelFamily definition") } @@ -108,13 +108,13 @@ func loadPanelScuemata(p BaseLoadPaths) (map[string]cue.Value, error) { } li := load.Instances([]string{filepath.Join("/", dpath, "models.cue")}, cfg) - imod, err := rt.Build(li[0]) - if err != nil { - return err + imod := ctx.BuildInstance(li[0]) + if imod.Err() != nil { + return imod.Err() } // Get the Family declaration in the models.cue file... - pmod := imod.Value().LookupPath(cue.MakePath(cue.Str("Panel"))) + pmod := imod.LookupPath(cue.MakePath(cue.Str("Panel"))) if !pmod.Exists() { return fmt.Errorf("%s does not contain a declaration of its models at path 'Family'", path) } diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index d845db35c10..99d8313465f 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -8,11 +8,12 @@ import ( "strings" "cuelang.org/go/cue" + "cuelang.org/go/cue/cuecontext" errs "cuelang.org/go/cue/errors" cuejson "cuelang.org/go/pkg/encoding/json" ) -var rt = &cue.Runtime{} +var ctx = cuecontext.New() // CueError wraps Errors caused by malformed cue files. type CueError struct { @@ -281,11 +282,11 @@ func ApplyDefaults(r Resource, scue cue.Value) (Resource, error) { if name == "" { name = "resource" } - rv, err := rt.Compile(name, r.Value) - if err != nil { - return r, err + rv := ctx.CompileString(r.Value.(string), cue.Filename(name)) + if rv.Err() != nil { + return r, rv.Err() } - rvUnified := rv.Value().Unify(scue) + rvUnified := rv.Unify(scue) re, err := convertCUEValueToString(rvUnified) if err != nil { return r, err @@ -314,11 +315,11 @@ func TrimDefaults(r Resource, scue cue.Value) (Resource, error) { if name == "" { name = "resource" } - rvInstance, err := rt.Compile(name, r.Value) - if err != nil { - return r, err + rvInstance := ctx.CompileString(r.Value.(string), cue.Filename(name)) + if rvInstance.Err() != nil { + return r, rvInstance.Err() } - rv, _, err := removeDefaultHelper(scue, rvInstance.Value()) + rv, _, err := removeDefaultHelper(scue, rvInstance) if err != nil { return r, err } @@ -337,9 +338,9 @@ func isCueValueEqual(inputdef cue.Value, input cue.Value) bool { func removeDefaultHelper(inputdef cue.Value, input cue.Value) (cue.Value, bool, error) { // To include all optional fields, we need to use inputdef for iteration, // since the lookuppath with optional field doesn't work very well - rvInstance, err := rt.Compile("helper", []byte{}) - if err != nil { - return input, false, err + rvInstance := ctx.CompileString("", cue.Filename("helper")) + if rvInstance.Err() != nil { + return input, false, rvInstance.Err() } rv := rvInstance.Value() @@ -404,11 +405,11 @@ func removeDefaultHelper(inputdef cue.Value, input cue.Value) (cue.Value, bool, } } iterlistContent := fmt.Sprintf("[%s]", strings.Join(iterlist, ",")) - liInstance, err := rt.Compile("resource", []byte(iterlistContent)) - if err != nil { + liInstance := ctx.CompileString(iterlistContent, cue.Filename("resource")) + if liInstance.Err() != nil { return rv, false, err } - return liInstance.Value(), false, nil + return liInstance, false, nil default: if isCueValueEqual(inputdef, input) { return input, true, nil diff --git a/pkg/schema/schema_test.go b/pkg/schema/schema_test.go index 178bbb1e784..b5a3bf4e645 100644 --- a/pkg/schema/schema_test.go +++ b/pkg/schema/schema_test.go @@ -30,13 +30,12 @@ func TestGenerate(t *testing.T) { for _, c := range cases { t.Run(c.Name+" apply default value", func(t *testing.T) { - var r cue.Runtime - scmInstance, err := r.Compile(c.Name+".cue", c.CUE) - if err != nil { - t.Fatal(err) + scmInstance := ctx.CompileString(c.CUE, cue.Filename(c.Name+".cue")) + if scmInstance.Err() != nil { + t.Fatal(scmInstance.Err()) } inputResource := Resource{Value: c.Trimmed} - out, err := ApplyDefaults(inputResource, scmInstance.Value()) + out, err := ApplyDefaults(inputResource, scmInstance) if err != nil { t.Fatal(err) } @@ -50,13 +49,12 @@ func TestGenerate(t *testing.T) { for _, c := range cases { t.Run(c.Name+" trim default value", func(t *testing.T) { - var r cue.Runtime - scmInstance, err := r.Compile(c.Name+".cue", c.CUE) - if err != nil { - t.Fatal(err) + scmInstance := ctx.CompileString(c.CUE, cue.Filename(c.Name+".cue")) + if scmInstance.Err() != nil { + t.Fatal(scmInstance.Err()) } inputResource := Resource{Value: c.Full} - out, err := TrimDefaults(inputResource, scmInstance.Value()) + out, err := TrimDefaults(inputResource, scmInstance) if err != nil { t.Fatal(err) }