-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): Loader interface (#459)
Introduces an abstraction over environment loading, for clearly handling inline and static loading
- Loading branch information
Showing
21 changed files
with
426 additions
and
396 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package tanka | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/grafana/tanka/pkg/jsonnet/jpath" | ||
"github.com/grafana/tanka/pkg/kubernetes/manifest" | ||
"github.com/grafana/tanka/pkg/process" | ||
"github.com/grafana/tanka/pkg/spec" | ||
"github.com/grafana/tanka/pkg/spec/v1alpha1" | ||
) | ||
|
||
// InlineLoader loads an environment that is specified inline from within | ||
// Jsonnet. The Jsonnet output is expected to hold a tanka.dev/Environment type, | ||
// Kubernetes resources are expected at the `data` key of this very type | ||
type InlineLoader struct{} | ||
|
||
func (i *InlineLoader) Load(path string, opts JsonnetOpts) (*v1alpha1.Environment, error) { | ||
raw, err := EvalJsonnet(path, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data interface{} | ||
if err := json.Unmarshal([]byte(raw), &data); err != nil { | ||
return nil, err | ||
} | ||
|
||
envs, err := extractEnvs(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(envs) > 1 { | ||
names := make([]string, 0, len(envs)) | ||
for _, e := range envs { | ||
names = append(names, e.Metadata().Name()) | ||
} | ||
return nil, ErrMultipleEnvs{path, names} | ||
} | ||
|
||
if len(envs) == 0 { | ||
return nil, fmt.Errorf("Found no environments in '%s'", path) | ||
} | ||
|
||
root, base, err := jpath.Dirs(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
name, err := filepath.Rel(root, base) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO: Re-serializing the entire env here. This is horribly inefficient | ||
envData, err := json.Marshal(envs[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
env, err := spec.Parse(envData, name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return env, nil | ||
} | ||
|
||
// extractEnvs filters out any Environment manifests | ||
func extractEnvs(data interface{}) (manifest.List, error) { | ||
// Scan for everything that looks like a Kubernetes object | ||
extracted, err := process.Extract(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Unwrap *List types | ||
if err := process.Unwrap(extracted); err != nil { | ||
return nil, err | ||
} | ||
|
||
out := make(manifest.List, 0, len(extracted)) | ||
for _, m := range extracted { | ||
out = append(out, m) | ||
} | ||
|
||
// Extract only object of Kind: Environment | ||
return process.Filter(out, process.MustStrExps("Environment/.*")), nil | ||
} |
Oops, something went wrong.