Skip to content

Commit

Permalink
add metadata.Namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Duologic committed Dec 10, 2020
1 parent c2c5c9f commit 6d50338
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/spec/depreciations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestDeprecated(t *testing.T) {
}
`)

got, err := Parse(data)
got, err := Parse(data, "test")
require.Equal(t, ErrDeprecated{
{old: "server", new: "spec.apiServer"},
{old: "team", new: "metadata.labels.team"},
Expand Down
15 changes: 9 additions & 6 deletions pkg/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Specfile = "spec.json"

// ParseDir parses the given environments `spec.json` into a `v1alpha1.Environment`
// object with the name set to the directories name
func ParseDir(baseDir, name string) (*v1alpha1.Environment, error) {
func ParseDir(baseDir, path string) (*v1alpha1.Environment, error) {
fi, err := os.Stat(baseDir)
if err != nil {
return nil, err
Expand All @@ -33,23 +33,24 @@ func ParseDir(baseDir, name string) (*v1alpha1.Environment, error) {
if err != nil {
if os.IsNotExist(err) {
c := v1alpha1.New()
c.Metadata.Name = name
return c, ErrNoSpec{name}
c.Metadata.Name = path // legacy behavior
c.Metadata.Namespace = path
return c, ErrNoSpec{path}
}
return nil, err
}

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

return c, err
}

// Parse parses the json `data` into a `v1alpha1.Environment` object.
func Parse(data []byte) (*v1alpha1.Environment, error) {
func Parse(data []byte, path string) (*v1alpha1.Environment, error) {
config := v1alpha1.New()
if err := json.Unmarshal(data, config); err != nil {
return nil, errors.Wrap(err, "parsing spec.json")
Expand All @@ -64,6 +65,8 @@ func Parse(data []byte) (*v1alpha1.Environment, error) {
config.Spec.APIServer = "https://" + config.Spec.APIServer
}

config.Metadata.Namespace = path

return config, nil
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/spec/v1alpha1/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ type Environment struct {

// Metadata is meant for humans and not parsed
type Metadata struct {
Name string `json:"name,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
}

// Has and Get make Metadata a simple wrapper for labels.Labels to use our map in their querier
Expand Down
19 changes: 18 additions & 1 deletion pkg/tanka/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ func ParseEnv(path string, opts ParseOpts) (interface{}, *v1alpha1.Environment,
if err != nil {
return nil, nil, err
}
env, err = spec.Parse(marshalled)
relpath, err := relativeEntrypoint(path)
if err != nil {
return nil, nil, err
}
env, err = spec.Parse(marshalled, relpath)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -244,3 +248,16 @@ func extractEnvironments(data interface{}) (manifest.List, error) {
// Extract only object of Kind: Environment
return process.Filter(out, process.MustStrExps("Environment/.*")), nil
}

func relativeEntrypoint(path string) (string, error) {
entrypoint, err := jpath.Entrypoint(path)
if err != nil {
return "", err
}
_, _, rootDir, err := jpath.Resolve(entrypoint)
if err != nil {
return "", errors.Wrap(err, "resolving jpath")
}

return filepath.Rel(rootDir, entrypoint)
}
15 changes: 9 additions & 6 deletions pkg/tanka/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ func TestEvalJsonnet(t *testing.T) {
APIVersion: v1alpha1.New().APIVersion,
Kind: v1alpha1.New().Kind,
Metadata: v1alpha1.Metadata{
Name: "cases/withspecjson",
Labels: v1alpha1.New().Metadata.Labels,
Name: "cases/withspecjson",
Namespace: "cases/withspecjson",
Labels: v1alpha1.New().Metadata.Labels,
},
Spec: v1alpha1.Spec{
APIServer: "https://localhost",
Expand All @@ -64,8 +65,9 @@ func TestEvalJsonnet(t *testing.T) {
APIVersion: v1alpha1.New().APIVersion,
Kind: v1alpha1.New().Kind,
Metadata: v1alpha1.Metadata{
Name: "cases/withspecjson",
Labels: v1alpha1.New().Metadata.Labels,
Name: "cases/withspecjson",
Namespace: "cases/withspecjson",
Labels: v1alpha1.New().Metadata.Labels,
},
Spec: v1alpha1.Spec{
APIServer: "https://localhost",
Expand Down Expand Up @@ -96,8 +98,9 @@ func TestEvalJsonnet(t *testing.T) {
APIVersion: v1alpha1.New().APIVersion,
Kind: v1alpha1.New().Kind,
Metadata: v1alpha1.Metadata{
Name: "withenv",
Labels: v1alpha1.New().Metadata.Labels,
Name: "withenv",
Namespace: "cases/withenv/main.jsonnet",
Labels: v1alpha1.New().Metadata.Labels,
},
Spec: v1alpha1.Spec{
APIServer: "https://localhost",
Expand Down

0 comments on commit 6d50338

Please sign in to comment.