Skip to content

Commit

Permalink
fix: correct namespace when using a file (grafana#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
Duologic authored Jan 8, 2021
1 parent 22de89f commit 037f0eb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
32 changes: 23 additions & 9 deletions pkg/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/pkg/errors"

"github.com/grafana/tanka/pkg/jsonnet/jpath"
"github.com/grafana/tanka/pkg/spec/v1alpha1"
)

Expand All @@ -20,30 +21,43 @@ 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, path string) (*v1alpha1.Environment, error) {
fi, err := os.Stat(baseDir)
func ParseDir(path string) (*v1alpha1.Environment, error) {
root, base, err := jpath.Dirs(path)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, errors.New("baseDir is not an directory")

// name of the environment: relative path from rootDir
name, err := filepath.Rel(root, base)
if err != nil {
return nil, err
}

file, err := jpath.Entrypoint(path)
if err != nil {
return nil, err
}

namespace, err := filepath.Rel(root, file)
if err != nil {
return nil, err
}

data, err := ioutil.ReadFile(filepath.Join(baseDir, Specfile))
data, err := ioutil.ReadFile(filepath.Join(base, Specfile))
if err != nil {
if os.IsNotExist(err) {
c := v1alpha1.New()
c.Metadata.Name = path // legacy behavior
c.Metadata.Namespace = path
c.Metadata.Name = name // legacy behavior
c.Metadata.Namespace = namespace
return c, ErrNoSpec{path}
}
return nil, err
}

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

return c, err
Expand Down
11 changes: 8 additions & 3 deletions pkg/tanka/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ func (i *InlineLoader) Load(path string, opts JsonnetOpts) (*v1alpha1.Environmen
return nil, fmt.Errorf("Found no environments in '%s'", path)
}

root, base, err := jpath.Dirs(path)
root, err := jpath.FindRoot(path)
if err != nil {
return nil, err
}

name, err := filepath.Rel(root, base)
file, err := jpath.Entrypoint(path)
if err != nil {
return nil, err
}

namespace, err := filepath.Rel(root, file)
if err != nil {
return nil, err
}
Expand All @@ -61,7 +66,7 @@ func (i *InlineLoader) Load(path string, opts JsonnetOpts) (*v1alpha1.Environmen
return nil, err
}

env, err := spec.Parse(envData, name)
env, err := spec.Parse(envData, namespace)
if err != nil {
return nil, err
}
Expand Down
36 changes: 33 additions & 3 deletions pkg/tanka/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLoad(t *testing.T) {
Kind: v1alpha1.New().Kind,
Metadata: v1alpha1.Metadata{
Name: "cases/withspecjson",
Namespace: "cases/withspecjson",
Namespace: "cases/withspecjson/main.jsonnet",
Labels: v1alpha1.New().Metadata.Labels,
},
Spec: v1alpha1.Spec{
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestLoad(t *testing.T) {
Kind: v1alpha1.New().Kind,
Metadata: v1alpha1.Metadata{
Name: "cases/withspecjson",
Namespace: "cases/withspecjson",
Namespace: "cases/withspecjson/main.jsonnet",
Labels: v1alpha1.New().Metadata.Labels,
},
Spec: v1alpha1.Spec{
Expand All @@ -79,6 +79,36 @@ func TestLoad(t *testing.T) {

{
name: "inline",
baseDir: "./testdata/cases/withenv/",
expected: manifest.List{{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "config",
"namespace": "withenv",
},
}},
env: &v1alpha1.Environment{
APIVersion: v1alpha1.New().APIVersion,
Kind: v1alpha1.New().Kind,
Metadata: v1alpha1.Metadata{
Name: "withenv",
Namespace: "cases/withenv/main.jsonnet",
Labels: v1alpha1.New().Metadata.Labels,
},
Spec: v1alpha1.Spec{
APIServer: "https://localhost",
Namespace: "withenv",
},
Data: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{"name": "config", "namespace": "withenv"},
},
},
},
{
name: "inline-filename",
baseDir: "./testdata/cases/withenv/main.jsonnet",
expected: manifest.List{{
"apiVersion": "v1",
Expand All @@ -93,7 +123,7 @@ func TestLoad(t *testing.T) {
Kind: v1alpha1.New().Kind,
Metadata: v1alpha1.Metadata{
Name: "withenv",
Namespace: "cases/withenv",
Namespace: "cases/withenv/main.jsonnet",
Labels: v1alpha1.New().Metadata.Labels,
},
Spec: v1alpha1.Spec{
Expand Down
19 changes: 3 additions & 16 deletions pkg/tanka/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package tanka
import (
"encoding/json"
"log"
"path/filepath"

"github.com/grafana/tanka/pkg/jsonnet/jpath"
"github.com/grafana/tanka/pkg/spec"
"github.com/grafana/tanka/pkg/spec/v1alpha1"
)
Expand Down Expand Up @@ -33,12 +31,7 @@ func (s StaticLoader) Load(path string, opts JsonnetOpts) (*v1alpha1.Environment
}

func (s StaticLoader) Peek(path string, opts JsonnetOpts) (*v1alpha1.Environment, error) {
root, base, err := jpath.Dirs(path)
if err != nil {
return nil, err
}

config, err := parseStaticSpec(root, base)
config, err := parseStaticSpec(path)
if err != nil {
return nil, err
}
Expand All @@ -48,14 +41,8 @@ func (s StaticLoader) Peek(path string, opts JsonnetOpts) (*v1alpha1.Environment

// parseStaticSpec parses the `spec.json` of the environment and returns a
// *kubernetes.Kubernetes from it
func parseStaticSpec(root, base string) (*v1alpha1.Environment, error) {
// name of the environment: relative path from rootDir
name, err := filepath.Rel(root, base)
if err != nil {
return nil, err
}

env, err := spec.ParseDir(base, name)
func parseStaticSpec(path string) (*v1alpha1.Environment, error) {
env, err := spec.ParseDir(path)
if err != nil {
switch err.(type) {
// the config includes deprecated fields
Expand Down

0 comments on commit 037f0eb

Please sign in to comment.