Skip to content

Commit cbc2c66

Browse files
committed
fix: stable naming of the environment
Before, the name of the internally used Environment type was different depending on the argument passed on the command line, even though it resulted in the same environment after all (relative vs absolute paths, etc.) This fixes the issue by using the `jpath` package to compute baseDir and rootDir, using the relative path from rootDir to baseDir as the name. This results in stable names, regardless which directory representation is used.
1 parent 856eb81 commit cbc2c66

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

cmd/tk/main.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"path/filepath"
89

910
"github.com/posener/complete"
1011
"github.com/spf13/cobra"
1112
"github.com/spf13/viper"
1213
"golang.org/x/crypto/ssh/terminal"
1314

1415
"github.com/grafana/tanka/pkg/cli/cmp"
16+
"github.com/grafana/tanka/pkg/jsonnet/jpath"
1517
"github.com/grafana/tanka/pkg/spec"
1618
"github.com/grafana/tanka/pkg/spec/v1alpha1"
1719
)
@@ -86,7 +88,15 @@ func main() {
8688
}
8789

8890
func setupConfiguration(baseDir string) *v1alpha1.Config {
89-
config, err := spec.ParseDir(baseDir)
91+
_, baseDir, rootDir, err := jpath.Resolve(baseDir)
92+
if err != nil {
93+
log.Fatalln("Resolving jpath:", err)
94+
}
95+
96+
// name of the environment: relative path from rootDir
97+
name, _ := filepath.Rel(rootDir, baseDir)
98+
99+
config, err := spec.ParseDir(baseDir, name)
90100
if err != nil {
91101
switch err.(type) {
92102
// just run fine without config. Provider features won't work (apply, show, diff)

pkg/jsonnet/jpath/jpath.go

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func (e ErrorFileNotFound) Error() string {
3232
// This results in predictable imports, as it doesn't matter whether the user called
3333
// called the command further down tree or not. A little bit like git.
3434
func Resolve(workdir string) (path []string, base, root string, err error) {
35+
workdir, err = filepath.Abs(workdir)
36+
if err != nil {
37+
return nil, "", "", err
38+
}
39+
3540
root, err = FindParentFile("jsonnetfile.json", workdir, "/")
3641
if err != nil {
3742
if _, ok := err.(ErrorFileNotFound); ok {

pkg/spec/spec.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package spec
33
import (
44
"bytes"
55
"os"
6-
"path/filepath"
76

87
"github.com/pkg/errors"
98
"github.com/spf13/viper"
@@ -32,7 +31,7 @@ func Parse(data []byte, name string) (*v1alpha1.Config, error) {
3231

3332
// ParseDir parses the given environments `spec.json` into a `v1alpha1.Config`
3433
// object with the name set to the directories name
35-
func ParseDir(baseDir string) (*v1alpha1.Config, error) {
34+
func ParseDir(baseDir, name string) (*v1alpha1.Config, error) {
3635
fi, err := os.Stat(baseDir)
3736
if err != nil {
3837
return nil, err
@@ -49,7 +48,7 @@ func ParseDir(baseDir string) (*v1alpha1.Config, error) {
4948
return nil, err
5049
}
5150

52-
return parse(v, filepath.Base(baseDir))
51+
return parse(v, name)
5352
}
5453

5554
// parse accepts a viper.Viper already loaded with the actual config and

pkg/tanka/parse.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ func (p *ParseResult) newKube() (*kubernetes.Kubernetes, error) {
3535
// parse loads the `spec.json`, evaluates the jsonnet and returns both, the
3636
// kubernetes object and the reconciled manifests
3737
func parse(baseDir string, opts *options) (*ParseResult, error) {
38-
env, err := parseEnv(baseDir, opts)
38+
_, baseDir, rootDir, err := jpath.Resolve(baseDir)
39+
if err != nil {
40+
return nil, errors.Wrap(err, "resolving jpath")
41+
}
42+
43+
env, err := parseEnv(baseDir, rootDir, opts)
3944
if err != nil {
4045
return nil, err
4146
}
@@ -45,7 +50,7 @@ func parse(baseDir string, opts *options) (*ParseResult, error) {
4550
return nil, errors.Wrap(err, "evaluating jsonnet")
4651
}
4752

48-
rec, err := kubernetes.Reconcile(raw, env.Spec, opts.targets)
53+
rec, err := kubernetes.Reconcile(raw, *env, opts.targets)
4954
if err != nil {
5055
return nil, errors.Wrap(err, "reconciling")
5156
}
@@ -58,8 +63,11 @@ func parse(baseDir string, opts *options) (*ParseResult, error) {
5863

5964
// parseEnv parses the `spec.json` of the environment and returns a
6065
// *kubernetes.Kubernetes from it
61-
func parseEnv(baseDir string, opts *options) (*v1alpha1.Config, error) {
62-
config, err := spec.ParseDir(baseDir)
66+
func parseEnv(baseDir, rootDir string, opts *options) (*v1alpha1.Config, error) {
67+
// name of the environment: relative path from rootDir
68+
name, _ := filepath.Rel(rootDir, baseDir)
69+
70+
config, err := spec.ParseDir(baseDir, name)
6371
if err != nil {
6472
switch err.(type) {
6573
// config is missing
@@ -82,15 +90,7 @@ func parseEnv(baseDir string, opts *options) (*v1alpha1.Config, error) {
8290

8391
// eval evaluates the jsonnet environment at the given directory starting with
8492
// `main.jsonnet`
85-
func eval(path string) (map[string]interface{}, error) {
86-
workdir, err := filepath.Abs(path)
87-
if err != nil {
88-
return nil, err
89-
}
90-
_, baseDir, _, err := jpath.Resolve(workdir)
91-
if err != nil {
92-
return nil, errors.Wrap(err, "resolving jpath")
93-
}
93+
func eval(baseDir string) (map[string]interface{}, error) {
9494
raw, err := jsonnet.EvaluateFile(filepath.Join(baseDir, "main.jsonnet"))
9595
if err != nil {
9696
return nil, err

0 commit comments

Comments
 (0)