Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Expose basic cluster information to jsonnet #124

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var deleteCmd = &cobra.Command{
return err
}

objs, err := readObjs(cmd, args)
objs, err := readObjs(cmd, args, c.Discovery)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var diffCmd = &cobra.Command{
return err
}

objs, err := readObjs(cmd, args)
objs, err := readObjs(cmd, args, c.Discovery)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (f *logFormatter) Format(e *log.Entry) ([]byte, error) {

// JsonnetVM constructs a new jsonnet.VM, according to command line
// flags
func JsonnetVM(cmd *cobra.Command) (*jsonnet.VM, error) {
func JsonnetVM(cmd *cobra.Command, disco discovery.DiscoveryInterface) (*jsonnet.VM, error) {
vm := jsonnet.Make()
flags := cmd.Flags()

Expand Down Expand Up @@ -258,7 +258,7 @@ func JsonnetVM(cmd *cobra.Command) (*jsonnet.VM, error) {
if err != nil {
return nil, err
}
utils.RegisterNativeFuncs(vm, resolver)
utils.RegisterNativeFuncs(vm, resolver, disco)

return vm, nil
}
Expand Down Expand Up @@ -317,8 +317,8 @@ func (r *resolverErrorWrapper) Resolve(image *utils.ImageName) error {
return err
}

func readObjs(cmd *cobra.Command, paths []string) ([]*unstructured.Unstructured, error) {
vm, err := JsonnetVM(cmd)
func readObjs(cmd *cobra.Command, paths []string, disco discovery.DiscoveryInterface) ([]*unstructured.Unstructured, error) {
vm, err := JsonnetVM(cmd, disco)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ var showCmd = &cobra.Command{
return err
}

objs, err := readObjs(cmd, args)
_, disco, err := restClientPool(cmd)
if err != nil {
return err
}

objs, err := readObjs(cmd, args, disco)
if err != nil {
return err
}
Expand Down
98 changes: 0 additions & 98 deletions cmd/show_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var updateCmd = &cobra.Command{
return err
}

objs, err := readObjs(cmd, args)
objs, err := readObjs(cmd, args, c.Discovery)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var validateCmd = &cobra.Command{
return err
}

objs, err := readObjs(cmd, args)
objs, err := readObjs(cmd, args, c.Discovery)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions cmd/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,25 @@
package cmd

import (
"bytes"
"regexp"
"testing"
)

func cmdOutput(t *testing.T, args []string) string {
var buf bytes.Buffer
RootCmd.SetOutput(&buf)
defer RootCmd.SetOutput(nil)

t.Log("Running args", args)
RootCmd.SetArgs(args)
if err := RootCmd.Execute(); err != nil {
t.Fatal("command failed:", err)
}

return buf.String()
}

func TestVersion(t *testing.T) {
output := cmdOutput(t, []string{"version"})

Expand Down
189 changes: 189 additions & 0 deletions integration/show_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// +build integration

package integration

import (
"encoding/json"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"

"k8s.io/client-go/discovery"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("show", func() {
const ns = "showtest"
var args []string
var output []byte
var input string
var env []string

BeforeEach(func() {
args = []string{"show", "-vv", "-n", ns}
if *kubeconfig != "" {
args = append(args, "--kubeconfig", *kubeconfig)
}

env = os.Environ()
})

JustBeforeEach(func() {
if input != "" {
tmpdir, err := ioutil.TempDir("", "showtest")
Expect(err).NotTo(HaveOccurred())
fname := filepath.Join(tmpdir, "input.jsonnet")
defer os.Remove(fname)

err = ioutil.WriteFile(fname, []byte(input), 0666)
Expect(err).NotTo(HaveOccurred())

args = append(args, "-f", fname)
}

cmd := exec.Command(*kubecfgBin, args...)
cmd.Env = env
cmd.Stderr = GinkgoWriter

var err error
output, err = cmd.Output()
Expect(err).NotTo(HaveOccurred())
})

Context("with testdata input", func() {
BeforeEach(func() {
args = append(args,
"-J", filepath.FromSlash("../testdata/lib"),
"-f", filepath.FromSlash("../testdata/test.jsonnet"),
"-V", "aVar=aVal",
"-V", "anVar",
"--ext-str-file", "filevar="+filepath.FromSlash("../testdata/extvar.file"),
)

env = append(env, "anVar=aVal2")
})

Context("with -o json", func() {
BeforeEach(func() {
args = append(args, "-o", "json")
})

It("should output expected JSON", func() {
const expected = `
{
"apiVersion": "v0alpha1",
"kind": "TestObject",
"nil": null,
"bool": true,
"number": 42,
"string": "bar",
"notAVal": "aVal",
"notAnotherVal": "aVal2",
"filevar": "foo\n",
"array": ["one", 2, [3]],
"object": {"foo": "bar"}
}
`
Expect(output).To(MatchJSON(expected))
})
})

Context("with -o yaml", func() {
BeforeEach(func() {
args = append(args, "-o", "yaml")
})

It("should output expected YAML", func() {
const expected = `
apiVersion: v0alpha1
kind: TestObject
nil: null
bool: true
number: 42
string: bar
notAVal: aVal
notAnotherVal: aVal2
filevar: "foo\n"
array: ["one", 2, [3]]
object: {"foo": "bar"}
`
Expect(output).To(MatchYAML(expected))
})
})
})

Context("against current cluster", func() {
var disco discovery.DiscoveryInterface
var jsonout interface{}

BeforeEach(func() {
var err error
disco, err = discovery.NewDiscoveryClientForConfig(clusterConfigOrDie())
Expect(err).NotTo(HaveOccurred())

args = append(args,
"-J", filepath.FromSlash("../lib"),
"-o", "json",
)
})

JustBeforeEach(func() {
err := json.Unmarshal(output, &jsonout)
Expect(err).NotTo(HaveOccurred())
})

Context("kubernetesVersion()", func() {
BeforeEach(func() {
input = `
local kubecfg = import "kubecfg.libsonnet";
{
apiVersion: "test",
kind: "Result",
result: kubecfg.kubernetesVersion(),
}`
})
It("should find current cluster version", func() {
info, err := disco.ServerVersion()
Expect(err).NotTo(HaveOccurred())
major, err := strconv.ParseFloat(info.Major, 64)
Expect(err).NotTo(HaveOccurred())
minor, err := strconv.ParseFloat(info.Minor, 64)
Expect(err).NotTo(HaveOccurred())

result := jsonout.(map[string]interface{})["result"]
Expect(result).To(HaveLen(2))
x := result.([]interface{})
Expect(x[0]).To(Equal(major))
Expect(x[1]).To(Equal(minor))
})
})

Context("kubernetesGroupVersionSupported", func() {
BeforeEach(func() {
input = `
local kubecfg = import "kubecfg.libsonnet";
local res = {
[gv]: kubecfg.kubernetesGroupVersionSupported(gv) for gv in
["", "v1", "authentication.k8s.io/v1", "bogus/v42"]
};
{apiVersion: "test", kind: "Result", result: res}
`
})

It("should query for supported GroupVersions", func() {
result := jsonout.(map[string]interface{})["result"]

Expect(result).To(HaveLen(4))
x := result.(map[string]interface{})
Expect(x[""]).To(BeFalse())
Expect(x["v1"]).To(BeTrue())
Expect(x["authentication.k8s.io/v1"]).To(BeTrue())
Expect(x["bogus/v42"]).To(BeFalse())
})
})
})
})
Loading