Unverified Commit 2f7c4aca authored by bryanl's avatar bryanl
Browse files

make kubernetes version available to charts


Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent 5ab07b01
......@@ -138,7 +138,7 @@ func evaluateMain(a app.App, envName, snippet, components, paramsStr string, opt
libPath,
)
helmRenderer := helm.NewRenderer(a)
helmRenderer := helm.NewRenderer(a, envName)
vm.AddFunctions(helmRenderer.JsonnetNativeFunc())
// Re-vendor versioned packages, such that import paths will remain path-agnostic.
......
......@@ -17,12 +17,7 @@ package helm
import (
"path/filepath"
<<<<<<< HEAD
"strings"
=======
>>>>>>> 4417ebd4... update version sorting
"github.com/blang/semver"
"github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/util/version"
"github.com/pkg/errors"
......@@ -51,7 +46,6 @@ func LatestChartVersion(a app.App, repoName, chartName string) (string, error) {
}
versions = append(versions, v)
}
if len(versions) == 0 {
......
......@@ -27,6 +27,7 @@ import (
ksstrings "github.com/ksonnet/ksonnet/pkg/util/strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/version"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/engine"
"k8s.io/helm/pkg/proto/hapi/chart"
......@@ -34,16 +35,27 @@ import (
// Renderer renders helm charts.
type Renderer struct {
app app.App
app app.App
envName string
}
// NewRenderer creates an instance of Renderer.
func NewRenderer(a app.App) *Renderer {
func NewRenderer(a app.App, envName string) *Renderer {
return &Renderer{
app: a,
app: a,
envName: envName,
}
}
func (r *Renderer) k8sVersion() (string, error) {
env, err := r.app.Environment(r.envName)
if err != nil {
return "", errors.Wrapf(err, "retrieving environment %q", r.envName)
}
return strings.TrimPrefix(env.KubernetesVersion, "v"), nil
}
// JsonnetNativeFunc is a jsonnet native function that renders helm charts.
func (r *Renderer) JsonnetNativeFunc() *jsonnet.NativeFunction {
fn := func(input []interface{}) (interface{}, error) {
......@@ -167,7 +179,19 @@ func (r *Renderer) renderWithHelm(componentName, raw, chartPath string) (map[str
options := chartutil.ReleaseOptions{
Name: componentName,
}
vals, err := chartutil.ToRenderValuesCaps(c, config, options, nil)
kubeVersion, err := r.k8sVersion()
if err != nil {
return nil, errors.Wrap(err, "setting Kubernetes version for Helm")
}
caps := &chartutil.Capabilities{
KubeVersion: &version.Info{
GitVersion: kubeVersion,
},
}
vals, err := chartutil.ToRenderValuesCaps(c, config, options, caps)
if err != nil {
return nil, err
}
......
......@@ -21,6 +21,7 @@ import (
"path/filepath"
"testing"
"github.com/ksonnet/ksonnet/pkg/app"
amocks "github.com/ksonnet/ksonnet/pkg/app/mocks"
"github.com/ksonnet/ksonnet/pkg/util/jsonnet"
"github.com/ksonnet/ksonnet/pkg/util/test"
......@@ -61,7 +62,12 @@ func TestRenderer_Render(t *testing.T) {
test.WithAppFs(t, tmpDir, fs, func(a *amocks.App, fs afero.Fs) {
test.StageDir(t, fs, "redis", filepath.Join(a.Root(), "vendor", "helm-stable", "redis"))
r := NewRenderer(a)
envConfig := &app.EnvironmentConfig{
KubernetesVersion: "v1.10.3",
}
a.On("Environment", "default").Return(envConfig, nil)
r := NewRenderer(a, "default")
values := map[string]interface{}{}
......@@ -107,7 +113,12 @@ func TestRenderer_JsonnetNativeFunc(t *testing.T) {
test.WithAppFs(t, tmpDir, fs, func(a *amocks.App, fs afero.Fs) {
test.StageDir(t, fs, "redis", filepath.Join(a.Root(), "vendor", "helm-stable", "redis"))
r := NewRenderer(a)
envConfig := &app.EnvironmentConfig{
KubernetesVersion: "v1.10.3",
}
a.On("Environment", "default").Return(envConfig, nil)
r := NewRenderer(a, "default")
vm := jsonnet.NewVM()
vm.AddFunctions(r.JsonnetNativeFunc())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment