From 2a1b551504815da17c6f4290613c63afe9d3f98c Mon Sep 17 00:00:00 2001 From: Brendan Melville Date: Fri, 6 Nov 2015 16:16:47 -0800 Subject: [PATCH 1/4] Adding support for deploying a type directly from client. --- client/client.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index ffbc2dd32..3a841955e 100644 --- a/client/client.go +++ b/client/client.go @@ -14,7 +14,10 @@ limitations under the License. package main import ( + "github.com/ghodss/yaml" + "github.com/kubernetes/deployment-manager/expandybird/expander" + "github.com/kubernetes/deployment-manager/manager/manager" "bytes" "encoding/json" @@ -26,6 +29,7 @@ import ( "net/http" "net/url" "os" + "strconv" "strings" "time" ) @@ -36,6 +40,11 @@ var ( service = flag.String("service", "http://localhost:8080", "URL for deployment manager") binary = flag.String("binary", "../expandybird/expansion/expansion.py", "Path to template expansion binary") + + properties = flag.String("properties", "", "Properties to use when deploying a type") + repository = flag.String("repository", + "https://raw.githubusercontent.com/kubernetes/deployment-manager/master/examples", + "Root of type repository") ) var usage = func() { @@ -121,7 +130,11 @@ func loadTemplate(name string) *expander.Template { var template *expander.Template var err error if len(args) == 1 { - template, err = expander.NewTemplateFromRootTemplate(args[0]) + if strings.HasSuffix(args[0], ".py") || strings.HasSuffix(args[0], ".jinja") { + template = buildTemplateFromType(name, args[0]) + } else { + template, err = expander.NewTemplateFromRootTemplate(args[0]) + } } else { template, err = expander.NewTemplateFromFileNames(args[0], args[1:]) } @@ -133,9 +146,52 @@ func loadTemplate(name string) *expander.Template { template.Name = name } + log.Printf("%v", template) + return template } +func buildTemplateFromType(name string, typeName string) *expander.Template { + fullType := *repository + "/" + typeName + + props := make(map[string]interface{}) + if *properties != "" { + plist := strings.Split(*properties, ",") + for _, p := range plist { + ppair := strings.Split(p, "=") + if len(ppair) != 2 { + log.Fatalf("--properties must be in the form \"p1=v1,p2=v2,...\": %s", p) + } + + // support ints + // TODO: needs to done to support other types. + i, err := strconv.Atoi(ppair[1]) + if err != nil { + props[ppair[0]] = ppair[1] + } else { + props[ppair[0]] = i + } + } + } + + config := manager.Configuration{Resources: []*manager.Resource{&manager.Resource{ + Name: name, + Type: fullType, + Properties: props, + }}} + + y, err := yaml.Marshal(config) + if err != nil { + log.Fatalf("cannot create configuration for deployment: %v\n", config) + } + + return &expander.Template{ + // Name will be set later. + Content: string(y), + // No imports, as this is a single type from repository. + } +} + func marshalTemplate(template *expander.Template) io.ReadCloser { j, err := json.Marshal(template) if err != nil { From 24e2e49ee2d0088c82351a37837280a78da8234a Mon Sep 17 00:00:00 2001 From: Brendan Melville Date: Fri, 6 Nov 2015 16:29:47 -0800 Subject: [PATCH 2/4] Adding todo for filling in actual URL. --- client/client.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client/client.go b/client/client.go index 3a841955e..779cb238e 100644 --- a/client/client.go +++ b/client/client.go @@ -146,12 +146,11 @@ func loadTemplate(name string) *expander.Template { template.Name = name } - log.Printf("%v", template) - return template } func buildTemplateFromType(name string, typeName string) *expander.Template { + // TODO: Fill this in with an actual URL fetched from github. fullType := *repository + "/" + typeName props := make(map[string]interface{}) From cf1dd10362ded21efa6500b917bab02bb0f58128 Mon Sep 17 00:00:00 2001 From: Brendan Melville Date: Sat, 7 Nov 2015 08:45:18 -0800 Subject: [PATCH 3/4] Integrating with git registry and new registry type syntax. --- client/client.go | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/client/client.go b/client/client.go index 37cc8aa18..ca7726ea1 100644 --- a/client/client.go +++ b/client/client.go @@ -39,14 +39,11 @@ var ( action = flag.String("action", "deploy", "expand | deploy | list | get | delete | update | listtypes | listtypeinstances | types") name = flag.String("name", "", "Name of template or deployment") service = flag.String("service", "http://localhost:8080", "URL for deployment manager") - type_registry = flag.String("type_registry", "kubernetes/deployment-manager", "Type registry [owner/repo], defaults to kubernetes/deployment-manager/") + type_registry = flag.String("type_registry", "kubernetes/deployment-manager", "Type registry [owner/repo], defaults to kubernetes/deployment-manager") binary = flag.String("binary", "../expandybird/expansion/expansion.py", "Path to template expansion binary") properties = flag.String("properties", "", "Properties to use when deploying a type") - repository = flag.String("repository", - "https://raw.githubusercontent.com/kubernetes/deployment-manager/master/examples", - "Root of type repository") ) var usage = func() { @@ -55,13 +52,17 @@ var usage = func() { flag.PrintDefaults() } +func getGitRegistry() *registry.GithubRegistry { + s := strings.Split(*type_registry, "/") + return registry.NewGithubRegistry(s[0], s[1]) +} + func main() { flag.Parse() name := getNameArgument() switch *action { case "types": - s := strings.Split(*type_registry, "/") - git := registry.NewGithubRegistry(s[0], s[1]) + git := getGitRegistry() types, err := git.List() if err != nil { log.Fatalf("Cannot list %v err") @@ -71,7 +72,7 @@ func main() { log.Printf("%s:%s", t.Name, t.Version) downloadURL, err := git.GetURL(t) if err != nil { - log.Printf("Failed to get download URL for %s:%s", t.Name, t.Version) + log.Printf("Failed to get download URL for type %s:%s", t.Name, t.Version) } log.Printf("\tdownload URL: %s", downloadURL) } @@ -149,8 +150,8 @@ func loadTemplate(name string) *expander.Template { var template *expander.Template var err error if len(args) == 1 { - if strings.HasSuffix(args[0], ".py") || strings.HasSuffix(args[0], ".jinja") { - template = buildTemplateFromType(name, args[0]) + if t := getRegistryType(args[0]); t != nil { + template = buildTemplateFromType(name, *t) } else { template, err = expander.NewTemplateFromRootTemplate(args[0]) } @@ -168,9 +169,25 @@ func loadTemplate(name string) *expander.Template { return template } -func buildTemplateFromType(name string, typeName string) *expander.Template { - // TODO: Fill this in with an actual URL fetched from github. - fullType := *repository + "/" + typeName +// TODO: needs better validation that this is actually a registry type. +func getRegistryType(fullType string) *registry.Type { + tList := strings.Split(fullType, ":") + if len(tList) != 2 { + return nil + } + + return ®istry.Type{ + Name: tList[0], + Version: tList[1], + } +} + +func buildTemplateFromType(name string, t registry.Type) *expander.Template { + git := getGitRegistry() + downloadURL, err := git.GetURL(t) + if err != nil { + log.Printf("Failed to get download URL for type %s:%s", t.Name, t.Version) + } props := make(map[string]interface{}) if *properties != "" { @@ -182,7 +199,7 @@ func buildTemplateFromType(name string, typeName string) *expander.Template { } // support ints - // TODO: needs to done to support other types. + // TODO: needs to support other types. i, err := strconv.Atoi(ppair[1]) if err != nil { props[ppair[0]] = ppair[1] @@ -194,7 +211,7 @@ func buildTemplateFromType(name string, typeName string) *expander.Template { config := manager.Configuration{Resources: []*manager.Resource{&manager.Resource{ Name: name, - Type: fullType, + Type: downloadURL, Properties: props, }}} From 9f81a68dbe4cba8a73b56509cfca5c49f3861425 Mon Sep 17 00:00:00 2001 From: Brendan Melville Date: Sat, 7 Nov 2015 09:06:31 -0800 Subject: [PATCH 4/4] Adding validation when parsing registry. --- client/client.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/client/client.go b/client/client.go index ca7726ea1..63cd72800 100644 --- a/client/client.go +++ b/client/client.go @@ -54,6 +54,10 @@ var usage = func() { func getGitRegistry() *registry.GithubRegistry { s := strings.Split(*type_registry, "/") + if len(s) != 2 { + log.Fatalf("invalid type reegistry: %s", type_registry) + } + return registry.NewGithubRegistry(s[0], s[1]) }