Unverified Commit 3294f0f7 authored by bryanl's avatar bryanl
Browse files

System prototypes should not use ksonnet-lib



* converted prototypes defined in code to jsonnet
* created abstraction to allow for future prototype formats
Signed-off-by: default avatarbryanl <bryanliles@gmail.com>
parent 8eafea56
...@@ -30,7 +30,7 @@ var _ = Describe("ks apply", func() { ...@@ -30,7 +30,7 @@ var _ = Describe("ks apply", func() {
namespace = e.createNamespace() namespace = e.createNamespace()
io := &initOptions{ io := &initOptions{
context: "gke_bryan-heptio_us-central1-a_dev2", context: *kubectx,
namespace: namespace, namespace: namespace,
} }
......
...@@ -64,7 +64,7 @@ var _ = Describe("builtin prototypes", func() { ...@@ -64,7 +64,7 @@ var _ = Describe("builtin prototypes", func() {
namespace = e.createNamespace() namespace = e.createNamespace()
io := &initOptions{ io := &initOptions{
context: "gke_bryan-heptio_us-central1-a_dev2", context: "docker-for-desktop",
namespace: namespace, namespace: namespace,
} }
...@@ -92,7 +92,7 @@ var _ = Describe("builtin prototypes", func() { ...@@ -92,7 +92,7 @@ var _ = Describe("builtin prototypes", func() {
}) })
It("validates", func() { It("validates", func() {
o := a.runKs("validate", "default") o := a.runKs("validate", "default", "-v")
assertExitStatus(o, 0) assertExitStatus(o, 0)
}) })
......
...@@ -30,7 +30,7 @@ var _ = Describe("ks delete", func() { ...@@ -30,7 +30,7 @@ var _ = Describe("ks delete", func() {
namespace = e.createNamespace() namespace = e.createNamespace()
io := &initOptions{ io := &initOptions{
context: "gke_bryan-heptio_us-central1-a_dev2", context: *kubectx,
namespace: namespace, namespace: namespace,
} }
......
...@@ -42,6 +42,7 @@ import ( ...@@ -42,6 +42,7 @@ import (
) )
var kubeconfig = flag.String("kubeconfig", "", "absolute path to kubeconfig file") var kubeconfig = flag.String("kubeconfig", "", "absolute path to kubeconfig file")
var kubectx = flag.String("context", "", "kubernetes context")
type e2e struct { type e2e struct {
root string root string
......
...@@ -33,7 +33,7 @@ var _ = Describe("ks env", func() { ...@@ -33,7 +33,7 @@ var _ = Describe("ks env", func() {
It("adds an environment", func() { It("adds an environment", func() {
o := a.runKs("env", "add", "prod", o := a.runKs("env", "add", "prod",
"--server", "http://example.com", "--server", "http://example.com",
"--namespace", "prod") "--module", "prod")
assertExitStatus(o, 0) assertExitStatus(o, 0)
o = a.envList() o = a.envList()
...@@ -45,7 +45,7 @@ var _ = Describe("ks env", func() { ...@@ -45,7 +45,7 @@ var _ = Describe("ks env", func() {
o := a.runKs("env", "add", "prod", o := a.runKs("env", "add", "prod",
"-o", "-o",
"--server", "http://example.com", "--server", "http://example.com",
"--namespace", "prod") "--module", "prod")
assertExitStatus(o, 0) assertExitStatus(o, 0)
o = a.envList() o = a.envList()
...@@ -66,7 +66,7 @@ var _ = Describe("ks env", func() { ...@@ -66,7 +66,7 @@ var _ = Describe("ks env", func() {
o := a.runKs("env", "add", "default", o := a.runKs("env", "add", "default",
"-o", "-o",
"--server", "http://example.com", "--server", "http://example.com",
"--namespace", "prod") "--module", "prod")
assertExitStatus(o, 0) assertExitStatus(o, 0)
o = a.runKs("env", "describe", "default") o = a.runKs("env", "describe", "default")
...@@ -192,7 +192,7 @@ var _ = Describe("ks env", func() { ...@@ -192,7 +192,7 @@ var _ = Describe("ks env", func() {
Context("namespace does not exist", func() { Context("namespace does not exist", func() {
It("returns an error", func() { It("returns an error", func() {
o := a.runKs("env", "targets", "invalid", o := a.runKs("env", "targets", "invalid",
"--namespace", "/") "--module", "/")
assertExitStatus(o, 1) assertExitStatus(o, 1)
assertOutput("env/targets/invalid-env.txt", o.stderr) assertOutput("env/targets/invalid-env.txt", o.stderr)
}) })
......
local env = std.extVar("__ksonnet/environments"); local env = std.extVar("__ksonnet/environments");
local params = std.extVar("__ksonnet/params").components["guestbook-ui"]; local params = std.extVar("__ksonnet/params").components["guestbook-ui"];
local k = import "k.libsonnet"; [
local deployment = k.apps.v1beta1.deployment; {
local container = k.apps.v1beta1.deployment.mixin.spec.template.spec.containersType; "apiVersion": "v1",
local containerPort = container.portsType; "kind": "Service",
local service = k.core.v1.service; "metadata": {
local servicePort = k.core.v1.service.mixin.spec.portsType; "name": params.name
},
local targetPort = params.containerPort; "spec": {
local labels = {app: params.name}; "ports": [
{
local appService = service "port": params.servicePort,
.new( "targetPort": params.containerPort
params.name, }
labels, ],
servicePort.new(params.servicePort, targetPort)) "selector": {
.withType(params.type); "app": params.name
},
local appDeployment = deployment "type": params.type
.new( }
params.name, },
params.replicas, {
container "apiVersion": "apps/v1beta2",
.new(params.name, params.image) "kind": "Deployment",
.withPorts(containerPort.new(targetPort)), "metadata": {
labels); "name": params.name
},
k.core.v1.list.new([appService, appDeployment]) "spec": {
\ No newline at end of file "replicas": params.replicas,
"selector": {
"matchLabels": {
"app": params.name
},
},
"template": {
"metadata": {
"labels": {
"app": params.name
}
},
"spec": {
"containers": [
{
"image": params.image,
"name": params.name,
"ports": [
{
"containerPort": params.containerPort
}
]
}
]
}
}
}
}
]
local env = std.extVar("__ksonnet/environments"); local env = std.extVar("__ksonnet/environments");
local params = std.extVar("__ksonnet/params").components.preview; local params = std.extVar("__ksonnet/params").components.preview;
local k = import "k.libsonnet"; [
local deployment = k.apps.v1beta1.deployment; {
local container = k.apps.v1beta1.deployment.mixin.spec.template.spec.containersType; "apiVersion": "v1",
local containerPort = container.portsType; "kind": "Service",
local service = k.core.v1.service; "metadata": {
local servicePort = k.core.v1.service.mixin.spec.portsType; "name": params.name
},
local targetPort = params.containerPort; "spec": {
local labels = {app: params.name}; "ports": [
{
local appService = service "port": params.servicePort,
.new( "targetPort": params.containerPort
params.name, }
labels, ],
servicePort.new(params.servicePort, targetPort)) "selector": {
.withType(params.type); "app": params.name
},
local appDeployment = deployment "type": params.type
.new( }
params.name, },
params.replicas, {
container "apiVersion": "apps/v1beta2",
.new(params.name, params.image) "kind": "Deployment",
.withPorts(containerPort.new(targetPort)), "metadata": {
labels); "name": params.name
},
k.core.v1.list.new([appService, appDeployment]) "spec": {
"replicas": params.replicas,
"selector": {
"matchLabels": {
"app": params.name
},
},
"template": {
"metadata": {
"labels": {
"app": params.name
}
},
"spec": {
"containers": [
{
"image": params.image,
"name": params.name,
"ports": [
{
"containerPort": params.containerPort
}
]
}
]
}
}
}
}
]
\ No newline at end of file
ksonnet version: (dev build) ksonnet version: (dev build)
jsonnet version: v0.10.0 jsonnet version: v0.10.0
client-go version: 1.8 client-go version:
...@@ -42,7 +42,7 @@ type PrototypeDescribe struct { ...@@ -42,7 +42,7 @@ type PrototypeDescribe struct {
app app.App app app.App
out io.Writer out io.Writer
query string query string
appPrototypesFn func(app.App, pkg.Descriptor) (prototype.SpecificationSchemas, error) appPrototypesFn func(app.App, pkg.Descriptor) (prototype.Prototypes, error)
} }
// NewPrototypeDescribe creates an instance of PrototypeDescribe // NewPrototypeDescribe creates an instance of PrototypeDescribe
...@@ -67,7 +67,10 @@ func (pd *PrototypeDescribe) Run() error { ...@@ -67,7 +67,10 @@ func (pd *PrototypeDescribe) Run() error {
return err return err
} }
index := prototype.NewIndex(prototypes) index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return errors.Wrap(err, "create prototype index")
}
prototypes, err = index.List() prototypes, err = index.List()
if err != nil { if err != nil {
...@@ -97,15 +100,15 @@ func (pd *PrototypeDescribe) Run() error { ...@@ -97,15 +100,15 @@ func (pd *PrototypeDescribe) Run() error {
return nil return nil
} }
type prototypeFn func(app.App, pkg.Descriptor) (prototype.SpecificationSchemas, error) type prototypeFn func(app.App, pkg.Descriptor) (prototype.Prototypes, error)
func allPrototypes(a app.App, appPrototypes prototypeFn) (prototype.SpecificationSchemas, error) { func allPrototypes(a app.App, appPrototypes prototypeFn) (prototype.Prototypes, error) {
libraries, err := a.Libraries() libraries, err := a.Libraries()
if err != nil { if err != nil {
return nil, err return nil, err
} }
var prototypes prototype.SpecificationSchemas var prototypes prototype.Prototypes
for _, library := range libraries { for _, library := range libraries {
d := pkg.Descriptor{ d := pkg.Descriptor{
...@@ -124,8 +127,11 @@ func allPrototypes(a app.App, appPrototypes prototypeFn) (prototype.Specificatio ...@@ -124,8 +127,11 @@ func allPrototypes(a app.App, appPrototypes prototypeFn) (prototype.Specificatio
return prototypes, nil return prototypes, nil
} }
func findUniquePrototype(query string, prototypes prototype.SpecificationSchemas) (*prototype.SpecificationSchema, error) { func findUniquePrototype(query string, prototypes prototype.Prototypes) (*prototype.Prototype, error) {
index := prototype.NewIndex(prototypes) index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return nil, err
}
sameSuffix, err := index.SearchNames(query, prototype.Suffix) sameSuffix, err := index.SearchNames(query, prototype.Suffix)
if err != nil { if err != nil {
...@@ -152,7 +158,7 @@ func findUniquePrototype(query string, prototypes prototype.SpecificationSchemas ...@@ -152,7 +158,7 @@ func findUniquePrototype(query string, prototypes prototype.SpecificationSchemas
} }
} }
func specNames(prototypes []*prototype.SpecificationSchema) []string { func specNames(prototypes []*prototype.Prototype) []string {
partialMatches := []string{} partialMatches := []string{}
for _, proto := range prototypes { for _, proto := range prototypes {
partialMatches = append(partialMatches, proto.Name) partialMatches = append(partialMatches, proto.Name)
......
...@@ -40,7 +40,7 @@ func RunPrototypeList(m map[string]interface{}) error { ...@@ -40,7 +40,7 @@ func RunPrototypeList(m map[string]interface{}) error {
type PrototypeList struct { type PrototypeList struct {
app app.App app app.App
out io.Writer out io.Writer
prototypesFn func(app.App, pkg.Descriptor) (prototype.SpecificationSchemas, error) prototypesFn func(app.App, pkg.Descriptor) (prototype.Prototypes, error)
} }
// NewPrototypeList creates an instance of PrototypeList // NewPrototypeList creates an instance of PrototypeList
...@@ -68,7 +68,10 @@ func (pl *PrototypeList) Run() error { ...@@ -68,7 +68,10 @@ func (pl *PrototypeList) Run() error {
return err return err
} }
index := prototype.NewIndex(prototypes) index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return err
}
prototypes, err = index.List() prototypes, err = index.List()
if err != nil { if err != nil {
......
...@@ -48,7 +48,7 @@ type PrototypePreview struct { ...@@ -48,7 +48,7 @@ type PrototypePreview struct {
out io.Writer out io.Writer
query string query string
args []string args []string
appPrototypesFn func(app.App, pkg.Descriptor) (prototype.SpecificationSchemas, error) appPrototypesFn func(app.App, pkg.Descriptor) (prototype.Prototypes, error)
} }
// NewPrototypePreview creates an instance of PrototypePreview // NewPrototypePreview creates an instance of PrototypePreview
...@@ -78,7 +78,10 @@ func (pp *PrototypePreview) Run() error { ...@@ -78,7 +78,10 @@ func (pp *PrototypePreview) Run() error {
return err return err
} }
index := prototype.NewIndex(prototypes) index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return err
}
prototypes, err = index.List() prototypes, err = index.List()
if err != nil { if err != nil {
...@@ -115,7 +118,7 @@ func (pp *PrototypePreview) Run() error { ...@@ -115,7 +118,7 @@ func (pp *PrototypePreview) Run() error {
return nil return nil
} }
func bindPrototypeParams(p *prototype.SpecificationSchema) *pflag.FlagSet { func bindPrototypeParams(p *prototype.Prototype) *pflag.FlagSet {
fs := pflag.NewFlagSet("preview", pflag.ContinueOnError) fs := pflag.NewFlagSet("preview", pflag.ContinueOnError)
for _, param := range p.RequiredParams() { for _, param := range p.RequiredParams() {
...@@ -129,7 +132,7 @@ func bindPrototypeParams(p *prototype.SpecificationSchema) *pflag.FlagSet { ...@@ -129,7 +132,7 @@ func bindPrototypeParams(p *prototype.SpecificationSchema) *pflag.FlagSet {
return fs return fs
} }
func getParameters(proto *prototype.SpecificationSchema, flags *pflag.FlagSet) (map[string]string, error) { func getParameters(proto *prototype.Prototype, flags *pflag.FlagSet) (map[string]string, error) {
missingRequired := prototype.ParamSchemas{} missingRequired := prototype.ParamSchemas{}
values := map[string]string{} values := map[string]string{}
for _, param := range proto.RequiredParams() { for _, param := range proto.RequiredParams() {
...@@ -172,7 +175,7 @@ func getParameters(proto *prototype.SpecificationSchema, flags *pflag.FlagSet) ( ...@@ -172,7 +175,7 @@ func getParameters(proto *prototype.SpecificationSchema, flags *pflag.FlagSet) (
} }
// TODO: this doesn't belong here. Needs to be closer to where other jsonnet processing happens. // TODO: this doesn't belong here. Needs to be closer to where other jsonnet processing happens.
func expandPrototype(proto *prototype.SpecificationSchema, templateType prototype.TemplateType, params map[string]string, componentName string) (string, error) { func expandPrototype(proto *prototype.Prototype, templateType prototype.TemplateType, params map[string]string, componentName string) (string, error) {
template, err := proto.Template.Body(templateType) template, err := proto.Template.Body(templateType)
if err != nil { if err != nil {
return "", err return "", err
......
...@@ -33,7 +33,7 @@ func TestPrototypePreview(t *testing.T) { ...@@ -33,7 +33,7 @@ func TestPrototypePreview(t *testing.T) {
args := []string{ args := []string{
"--name", "myDeployment", "--name", "myDeployment",
"--image", "nginx", "--image", "nginx",
"--port", "80", "--containerPort", "80",
} }
in := map[string]interface{}{ in := map[string]interface{}{
......
...@@ -42,8 +42,8 @@ type PrototypeSearch struct { ...@@ -42,8 +42,8 @@ type PrototypeSearch struct {
app app.App app app.App
query string query string
out io.Writer out io.Writer
prototypesFn func(app.App, pkg.Descriptor) (prototype.SpecificationSchemas, error) prototypesFn func(app.App, pkg.Descriptor) (prototype.Prototypes, error)
protoSearchFn func(string, prototype.SpecificationSchemas) (prototype.SpecificationSchemas, error) protoSearchFn func(string, prototype.Prototypes) (prototype.Prototypes, error)
} }
// NewPrototypeSearch creates an instance of PrototypeSearch // NewPrototypeSearch creates an instance of PrototypeSearch
...@@ -99,7 +99,10 @@ func (ps *PrototypeSearch) Run() error { ...@@ -99,7 +99,10 @@ func (ps *PrototypeSearch) Run() error {
return t.Render() return t.Render()
} }
func protoSearch(query string, prototypes prototype.SpecificationSchemas) (prototype.SpecificationSchemas, error) { func protoSearch(query string, prototypes prototype.Prototypes) (prototype.Prototypes, error) {
index := prototype.NewIndex(prototypes) index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return nil, err
}
return index.SearchNames(query, prototype.Substring) return index.SearchNames(query, prototype.Substring)
} }
...@@ -42,10 +42,10 @@ func TestPrototypeSearch(t *testing.T) { ...@@ -42,10 +42,10 @@ func TestPrototypeSearch(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
a.out = &buf a.out = &buf
a.protoSearchFn = func(string, prototype.SpecificationSchemas) (prototype.SpecificationSchemas, error) { a.protoSearchFn = func(string, prototype.Prototypes) (prototype.Prototypes, error) {
snippet := prototype.SnippetSchema{ShortDescription: "description"} snippet := prototype.SnippetSchema{ShortDescription: "description"}
return prototype.SpecificationSchemas{ return prototype.Prototypes{
{Name: "result1", Template: snippet}, {Name: "result1", Template: snippet},
{Name: "result2", Template: snippet}, {Name: "result2", Template: snippet},
}, nil }, nil
......
...@@ -43,7 +43,7 @@ type PrototypeUse struct { ...@@ -43,7 +43,7 @@ type PrototypeUse struct {
app app.App app app.App
args []string args []string
out io.Writer out io.Writer
prototypesFn func(app.App, pkg.Descriptor) (prototype.SpecificationSchemas, error) prototypesFn func(app.App, pkg.Descriptor) (prototype.Prototypes, error)
createComponentFn func(app.App, string, string, param.Params, prototype.TemplateType) (string, error) createComponentFn func(app.App, string, string, param.Params, prototype.TemplateType) (string, error)
} }
...@@ -74,7 +74,10 @@ func (pl *PrototypeUse) Run() error { ...@@ -74,7 +74,10 @@ func (pl *PrototypeUse) Run() error {
return err return err
} }
index := prototype.NewIndex(prototypes) index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return err
}
prototypes, err = index.List() prototypes, err = index.List()
if err != nil { if err != nil {
......
...@@ -37,7 +37,7 @@ func TestPrototypeUse(t *testing.T) { ...@@ -37,7 +37,7 @@ func TestPrototypeUse(t *testing.T) {
"myDeployment", "myDeployment",
"--name", "myDeployment", "--name", "myDeployment",
"--image", "nginx", "--image", "nginx",
"--port", "80", "--containerPort", "80",
} }
in := map[string]interface{}{ in := map[string]interface{}{
...@@ -53,10 +53,10 @@ func TestPrototypeUse(t *testing.T) { ...@@ -53,10 +53,10 @@ func TestPrototypeUse(t *testing.T) {
assertOutput(t, "prototype/use/text.txt", text) assertOutput(t, "prototype/use/text.txt", text)
expectedParams := param.Params{