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() {
namespace = e.createNamespace()
io := &initOptions{
context: "gke_bryan-heptio_us-central1-a_dev2",
context: *kubectx,
namespace: namespace,
}
......
......@@ -64,7 +64,7 @@ var _ = Describe("builtin prototypes", func() {
namespace = e.createNamespace()
io := &initOptions{
context: "gke_bryan-heptio_us-central1-a_dev2",
context: "docker-for-desktop",
namespace: namespace,
}
......@@ -92,7 +92,7 @@ var _ = Describe("builtin prototypes", func() {
})
It("validates", func() {
o := a.runKs("validate", "default")
o := a.runKs("validate", "default", "-v")
assertExitStatus(o, 0)
})
......
......@@ -30,7 +30,7 @@ var _ = Describe("ks delete", func() {
namespace = e.createNamespace()
io := &initOptions{
context: "gke_bryan-heptio_us-central1-a_dev2",
context: *kubectx,
namespace: namespace,
}
......
......@@ -42,6 +42,7 @@ import (
)
var kubeconfig = flag.String("kubeconfig", "", "absolute path to kubeconfig file")
var kubectx = flag.String("context", "", "kubernetes context")
type e2e struct {
root string
......
......@@ -33,7 +33,7 @@ var _ = Describe("ks env", func() {
It("adds an environment", func() {
o := a.runKs("env", "add", "prod",
"--server", "http://example.com",
"--namespace", "prod")
"--module", "prod")
assertExitStatus(o, 0)
o = a.envList()
......@@ -45,7 +45,7 @@ var _ = Describe("ks env", func() {
o := a.runKs("env", "add", "prod",
"-o",
"--server", "http://example.com",
"--namespace", "prod")
"--module", "prod")
assertExitStatus(o, 0)
o = a.envList()
......@@ -66,7 +66,7 @@ var _ = Describe("ks env", func() {
o := a.runKs("env", "add", "default",
"-o",
"--server", "http://example.com",
"--namespace", "prod")
"--module", "prod")
assertExitStatus(o, 0)
o = a.runKs("env", "describe", "default")
......@@ -192,7 +192,7 @@ var _ = Describe("ks env", func() {
Context("namespace does not exist", func() {
It("returns an error", func() {
o := a.runKs("env", "targets", "invalid",
"--namespace", "/")
"--module", "/")
assertExitStatus(o, 1)
assertOutput("env/targets/invalid-env.txt", o.stderr)
})
......
local env = std.extVar("__ksonnet/environments");
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;
local containerPort = container.portsType;
local service = k.core.v1.service;
local servicePort = k.core.v1.service.mixin.spec.portsType;
local targetPort = params.containerPort;
local labels = {app: params.name};
local appService = service
.new(
params.name,
labels,
servicePort.new(params.servicePort, targetPort))
.withType(params.type);
local appDeployment = deployment
.new(
params.name,
params.replicas,
container
.new(params.name, params.image)
.withPorts(containerPort.new(targetPort)),
labels);
k.core.v1.list.new([appService, appDeployment])
\ No newline at end of file
[
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": params.name
},
"spec": {
"ports": [
{
"port": params.servicePort,
"targetPort": params.containerPort
}
],
"selector": {
"app": params.name
},
"type": params.type
}
},
{
"apiVersion": "apps/v1beta2",
"kind": "Deployment",
"metadata": {
"name": params.name
},
"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
}
]
}
]
}
}
}
}
]
local env = std.extVar("__ksonnet/environments");
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;
local containerPort = container.portsType;
local service = k.core.v1.service;
local servicePort = k.core.v1.service.mixin.spec.portsType;
local targetPort = params.containerPort;
local labels = {app: params.name};
local appService = service
.new(
params.name,
labels,
servicePort.new(params.servicePort, targetPort))
.withType(params.type);
local appDeployment = deployment
.new(
params.name,
params.replicas,
container
.new(params.name, params.image)
.withPorts(containerPort.new(targetPort)),
labels);
k.core.v1.list.new([appService, appDeployment])
[
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": params.name
},
"spec": {
"ports": [
{
"port": params.servicePort,
"targetPort": params.containerPort
}
],
"selector": {
"app": params.name
},
"type": params.type
}
},
{
"apiVersion": "apps/v1beta2",
"kind": "Deployment",
"metadata": {
"name": params.name
},
"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)
jsonnet version: v0.10.0
client-go version: 1.8
client-go version:
......@@ -42,7 +42,7 @@ type PrototypeDescribe struct {
app app.App
out io.Writer
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
......@@ -67,7 +67,10 @@ func (pd *PrototypeDescribe) Run() error {
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()
if err != nil {
......@@ -97,15 +100,15 @@ func (pd *PrototypeDescribe) Run() error {
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()
if err != nil {
return nil, err
}
var prototypes prototype.SpecificationSchemas
var prototypes prototype.Prototypes
for _, library := range libraries {
d := pkg.Descriptor{
......@@ -124,8 +127,11 @@ func allPrototypes(a app.App, appPrototypes prototypeFn) (prototype.Specificatio
return prototypes, nil
}
func findUniquePrototype(query string, prototypes prototype.SpecificationSchemas) (*prototype.SpecificationSchema, error) {
index := prototype.NewIndex(prototypes)
func findUniquePrototype(query string, prototypes prototype.Prototypes) (*prototype.Prototype, error) {
index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return nil, err
}
sameSuffix, err := index.SearchNames(query, prototype.Suffix)
if err != nil {
......@@ -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{}
for _, proto := range prototypes {
partialMatches = append(partialMatches, proto.Name)
......
......@@ -40,7 +40,7 @@ func RunPrototypeList(m map[string]interface{}) error {
type PrototypeList struct {
app app.App
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
......@@ -68,7 +68,10 @@ func (pl *PrototypeList) Run() error {
return err
}
index := prototype.NewIndex(prototypes)
index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return err
}
prototypes, err = index.List()
if err != nil {
......
......@@ -48,7 +48,7 @@ type PrototypePreview struct {
out io.Writer
query 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
......@@ -78,7 +78,10 @@ func (pp *PrototypePreview) Run() error {
return err
}
index := prototype.NewIndex(prototypes)
index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return err
}
prototypes, err = index.List()
if err != nil {
......@@ -115,7 +118,7 @@ func (pp *PrototypePreview) Run() error {
return nil
}
func bindPrototypeParams(p *prototype.SpecificationSchema) *pflag.FlagSet {
func bindPrototypeParams(p *prototype.Prototype) *pflag.FlagSet {
fs := pflag.NewFlagSet("preview", pflag.ContinueOnError)
for _, param := range p.RequiredParams() {
......@@ -129,7 +132,7 @@ func bindPrototypeParams(p *prototype.SpecificationSchema) *pflag.FlagSet {
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{}
values := map[string]string{}
for _, param := range proto.RequiredParams() {
......@@ -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.
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)
if err != nil {
return "", err
......
......@@ -33,7 +33,7 @@ func TestPrototypePreview(t *testing.T) {
args := []string{
"--name", "myDeployment",
"--image", "nginx",
"--port", "80",
"--containerPort", "80",
}
in := map[string]interface{}{
......
......@@ -42,8 +42,8 @@ type PrototypeSearch struct {
app app.App
query string
out io.Writer
prototypesFn func(app.App, pkg.Descriptor) (prototype.SpecificationSchemas, error)
protoSearchFn func(string, prototype.SpecificationSchemas) (prototype.SpecificationSchemas, error)
prototypesFn func(app.App, pkg.Descriptor) (prototype.Prototypes, error)
protoSearchFn func(string, prototype.Prototypes) (prototype.Prototypes, error)
}
// NewPrototypeSearch creates an instance of PrototypeSearch
......@@ -99,7 +99,10 @@ func (ps *PrototypeSearch) Run() error {
return t.Render()
}
func protoSearch(query string, prototypes prototype.SpecificationSchemas) (prototype.SpecificationSchemas, error) {
index := prototype.NewIndex(prototypes)
func protoSearch(query string, prototypes prototype.Prototypes) (prototype.Prototypes, error) {
index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return nil, err
}
return index.SearchNames(query, prototype.Substring)
}
......@@ -42,10 +42,10 @@ func TestPrototypeSearch(t *testing.T) {
var buf bytes.Buffer
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"}
return prototype.SpecificationSchemas{
return prototype.Prototypes{
{Name: "result1", Template: snippet},
{Name: "result2", Template: snippet},
}, nil
......
......@@ -43,7 +43,7 @@ type PrototypeUse struct {
app app.App
args []string
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)
}
......@@ -74,7 +74,10 @@ func (pl *PrototypeUse) Run() error {
return err
}
index := prototype.NewIndex(prototypes)
index, err := prototype.NewIndex(prototypes, prototype.DefaultBuilder)
if err != nil {
return err
}
prototypes, err = index.List()
if err != nil {
......
......@@ -37,7 +37,7 @@ func TestPrototypeUse(t *testing.T) {
"myDeployment",
"--name", "myDeployment",
"--image", "nginx",
"--port", "80",
"--containerPort", "80",
}
in := map[string]interface{}{
......@@ -56,7 +56,7 @@ func TestPrototypeUse(t *testing.T) {
"name": `"myDeployment"`,
"image": `"nginx"`,
"replicas": "1",
"port": "80",
"containerPort": "80",
}
assert.Equal(t, expectedParams, params)
......
......@@ -2,11 +2,10 @@ PROTOTYPE NAME:
io.ksonnet.pkg.namespace
DESCRIPTION:
A simple namespace. Labels are automatically populated from the name of the
namespace.
A simple namespace. Labels are automatically populated from the name of the namespace.
REQUIRED PARAMETERS:
--name=<name> Name to give the namespace. [type: string]
--name=<name> Name to give the namespace [type: string]
OPTIONAL PARAMETERS:
[none]
......
local env = std.extVar("__ksonnet/environments");
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;
local port = container.portsType;
deployment.new(
params.name,
params.replicas,
container
.new(params.name, params.image)
.withPorts(port.new(params.port)),
{app: params.name})
{
"apiVersion": "apps/v1beta1",
"kind": "Deployment",
"metadata": {
"name": params.name
},
"spec": {
"replicas": params.replicas,
"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 params = std.extVar("__ksonnet/params").components.myDeployment;
local k = import "k.libsonnet";
local deployment = k.apps.v1beta1.deployment;
local container = k.apps.v1beta1.deployment.mixin.spec.template.spec.containersType;
local port = container.portsType;
deployment.new(
params.name,
params.replicas,
container
.new(params.name, params.image)
.withPorts(port.new(params.port)),
{app: params.name})
\ No newline at end of file
{
"apiVersion": "apps/v1beta1",
"kind": "Deployment",
"metadata": {
"name": params.name
},
"spec": {
"replicas": params.replicas,
"template": {
"metadata": {
"labels": {
"app": params.name
}
},
"spec": {
"containers": [
{
"image": params.image,
"name": params.name,
"ports": [
{
"containerPort": params.containerPort
}
]
}
]
}
}
}
}
\ No newline at end of file
......@@ -25,20 +25,20 @@ func Test_prototypeUseCmd(t *testing.T) {
cases := []cmdTestCase{
{
name: "in general",
args: []string{"prototype", "use", "name", "--port", "8080"},
args: []string{"prototype", "use", "name", "--containerPort", "8080"},
action: actionPrototypeUse,
expected: map[string]interface{}{
actions.OptionApp: ka,
actions.OptionArguments: []string{"name", "--port", "8080"},
actions.OptionArguments: []string{"name", "--containerPort", "8080"},
},
},
{
name: "using generate alias",
args: []string{"generate", "name", "--port", "8080"},
args: []string{"generate", "name", "--containerPort", "8080"},
action: actionPrototypeUse,
expected: map[string]interface{}{
actions.OptionApp: ka,
actions.OptionArguments: []string{"name", "--port", "8080"},
actions.OptionArguments: []string{"name", "--containerPort", "8080"},
},
},
}
......