Skip to content
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

feat: inline Environment #403

Merged
merged 16 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
2 changes: 1 addition & 1 deletion cmd/tk/jsonnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func evalCmd() *cli.Command {
jsonnetOpts.EvalPattern = *evalPattern
raw, err := tanka.Eval(args[0], jsonnetOpts)

if err != nil {
if raw == nil && err != nil {
return err
}

Expand Down
13 changes: 13 additions & 0 deletions cmd/tk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/go-clix/cli"

"github.com/grafana/tanka/pkg/jsonnet"
"github.com/grafana/tanka/pkg/jsonnet/jpath"
"github.com/grafana/tanka/pkg/spec"
"github.com/grafana/tanka/pkg/spec/v1alpha1"
Expand Down Expand Up @@ -77,6 +78,18 @@ func setupConfiguration(baseDir string) *v1alpha1.Config {
if verbose {
fmt.Print(err)
}
// no spec.json is found, try parsing main.jsonnet
case spec.ErrNoSpec:
config, err := tanka.EvalEnvs(baseDir, jsonnet.Opts{})
if err != nil {
switch err.(type) {
case tanka.ErrNoEnv:
return nil
default:
log.Fatalf("Reading main.jsonnet: %s", err)
}
}
return config
// some other error
default:
log.Fatalf("Reading spec.json: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/tk/other.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func findBaseDirs() (dirs []string) {
}

if err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
requiredFiles := []string{"main.jsonnet", "spec.json"}
requiredFiles := []string{"main.jsonnet"}
for _, name := range requiredFiles {
if _, err := os.Stat(filepath.Join(path, name)); err != nil {
// missing file, not a valid environment directory
Expand Down
5 changes: 4 additions & 1 deletion pkg/spec/depreciations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
func TestDeprecated(t *testing.T) {
data := []byte(`
{
"metadata": {
"name": "test"
},
"spec": {
"namespace": "new"
},
Expand All @@ -23,7 +26,7 @@ func TestDeprecated(t *testing.T) {
}
`)

got, err := Parse(data, "test")
got, err := Parse(data)
require.Equal(t, ErrDeprecated{
{old: "server", new: "spec.apiServer"},
{old: "team", new: "metadata.labels.team"},
Expand Down
14 changes: 8 additions & 6 deletions pkg/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,22 @@ func ParseDir(baseDir, name string) (*v1alpha1.Config, error) {
return nil, err
}

return Parse(data, name)
c, err := Parse(data)
if c != nil {
// set the name field
c.Metadata.Name = name
}

return c, err
}

// Parse parses the json `data` into a `v1alpha1.Config` object.
// `name` is the name of the environment
func Parse(data []byte, name string) (*v1alpha1.Config, error) {
func Parse(data []byte) (*v1alpha1.Config, error) {
config := v1alpha1.New()
if err := json.Unmarshal(data, config); err != nil {
return nil, errors.Wrap(err, "parsing spec.json")
}

// set the name field
config.Metadata.Name = name
Copy link
Member

Choose a reason for hiding this comment

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

If we no longer set the name ourselves, we should enforce some name is set

Copy link
Member Author

Choose a reason for hiding this comment

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

I've covered for this in 4ee8de7


if err := handleDeprecated(config, data); err != nil {
return config, err
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/spec/v1alpha1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ func New() *Config {
// Config holds the configuration variables for config version v1alpha1
// ApiVersion and Kind are currently unused, this may change in the future.
type Config struct {
Duologic marked this conversation as resolved.
Show resolved Hide resolved
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata Metadata `json:"metadata"`
Spec Spec `json:"spec"`
Data interface{} `json:"data"`
sh0rez marked this conversation as resolved.
Show resolved Hide resolved
}

// Metadata is meant for humans and not parsed
Expand Down
22 changes: 22 additions & 0 deletions pkg/tanka/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tanka

import "fmt"

// ErrNoEnv means that the given jsonnet has no Environment object
// This must not be fatal, some operations work without
type ErrNoEnv struct {
path string
}

func (e ErrNoEnv) Error() string {
return fmt.Sprintf("unable to find an Environment in '%s'", e.path)
}

// ErrMultipleEnvs means that the given jsonnet has multiple Environment objects
type ErrMultipleEnvs struct {
Copy link
Member

Choose a reason for hiding this comment

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

Could this error message include the names of the environments? Makes it far easier to debug then

Copy link
Member Author

Choose a reason for hiding this comment

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

path string
}

func (e ErrMultipleEnvs) Error() string {
return fmt.Sprintf("found multiple Environments in '%s'", e.path)
}
Loading