From b5871d7a538a8e5b1830545dbe02a9db1fbd896b Mon Sep 17 00:00:00 2001 From: jackgr Date: Wed, 6 Apr 2016 17:25:39 -0700 Subject: [PATCH 1/2] Make env var parsing more robust --- pkg/util/kubernetes.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/util/kubernetes.go b/pkg/util/kubernetes.go index df3f5785c..a3adf62d5 100644 --- a/pkg/util/kubernetes.go +++ b/pkg/util/kubernetes.go @@ -20,6 +20,7 @@ import ( "fmt" "log" "net" + "net/url" "os" "strings" ) @@ -77,7 +78,13 @@ func GetServiceURL(serviceName, servicePort, serviceURL string) (string, error) varName := strings.ToUpper(varBase) + "_PORT" serviceURL := os.Getenv(varName) if serviceURL != "" { - return strings.Replace(serviceURL, "tcp", "http", 1), nil + u, err := url.Parse(serviceURL) + if err != nil || u.Path != "" || u.Scheme != "tcp" { + return "", fmt.Errorf("malformed value: %s for envinronment variable: %s", serviceURL, varName) + } + + u.Scheme = "http" + return u.String(), nil } if servicePort != "" { From 84dc6c9ef7f6d1b772d12be30b3c5056186bd61a Mon Sep 17 00:00:00 2001 From: jackgr Date: Wed, 6 Apr 2016 19:37:21 -0700 Subject: [PATCH 2/2] Load resources from non template charts --- cmd/manager/manager/expander.go | 41 +++++++++++++++++++++++++-------- pkg/util/kubernetesutil.go | 12 +++++++--- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/cmd/manager/manager/expander.go b/cmd/manager/manager/expander.go index 5f76618e1..4f4c081b4 100644 --- a/cmd/manager/manager/expander.go +++ b/cmd/manager/manager/expander.go @@ -90,14 +90,15 @@ func (e *expander) expandConfiguration(conf *common.Configuration) (*ExpandedCon } defer cbr.Close() + + // Load the charts contents into strings that we can pass to exapnsion + content, err := cbr.LoadContent() + if err != nil { + return nil, err + } + expander := cbr.Chartfile().Expander if expander != nil && expander.Name != "" { - // Load the charts contents into strings that we can pass to exapnsion - content, err := cbr.LoadContent() - if err != nil { - return nil, err - } - // Build a request to the expansion service and call it to do the expansion svcReq := &expansion.ServiceRequest{ ChartInvocation: resource, @@ -120,8 +121,28 @@ func (e *expander) expandConfiguration(conf *common.Configuration) (*ExpandedCon // This was not a primitive resource, so add its properties to the layout // Then add the all of the layout resources returned by the recursion to the layout - layout.Properties = resource.Properties layout.Resources = expConf.Layout.Resources + layout.Properties = resource.Properties + } else { + // Raise an error if a non template chart supplies properties + if resource.Properties != nil { + return nil, fmt.Errorf("properties provided for non template chart %s", resource.Type) + } + + additions = []*common.Resource{} + for _, member := range content.Members { + segments := strings.Split(member.Path, "/") + if len(segments) > 1 && segments[0] == "templates" { + if strings.HasSuffix(member.Path, "yaml") || strings.HasSuffix(member.Path, "json") { + resource, err := util.ParseKubernetesObject(member.Content) + if err != nil { + return nil, err + } + + resources = append(resources, resource) + } + } + } } } @@ -130,10 +151,12 @@ func (e *expander) expandConfiguration(conf *common.Configuration) (*ExpandedCon } // All done with this level, so return the expanded configuration - return &ExpandedConfiguration{ + result := &ExpandedConfiguration{ Config: &common.Configuration{Resources: resources}, Layout: &common.Layout{Resources: layouts}, - }, nil + } + + return result, nil } func (e *expander) callService(svcName string, svcReq *expansion.ServiceRequest) (*common.Configuration, error) { diff --git a/pkg/util/kubernetesutil.go b/pkg/util/kubernetesutil.go index b1323a20d..a34fea60a 100644 --- a/pkg/util/kubernetesutil.go +++ b/pkg/util/kubernetesutil.go @@ -28,18 +28,24 @@ import ( func ParseKubernetesObject(object []byte) (*common.Resource, error) { o := &KubernetesObject{} if err := yaml.Unmarshal(object, &o); err != nil { - return nil, fmt.Errorf("cannot unmarshal native kubernetes object (%#v)", err) + return nil, fmt.Errorf("cannot unmarshal native kubernetes object (%s): %s", object, err) } // Ok, it appears to be a valid object, create a Resource out of it. r := &common.Resource{} - r.Name = getRandomName(o.Metadata["name"].(string)) + md, ok := o.Metadata["name"].(string) + if !ok { + return nil, fmt.Errorf("cannot parse native kubernetes object (%s)", object) + } + + r.Name = getRandomName(md) r.Type = o.Kind r.Properties = make(map[string]interface{}) if err := yaml.Unmarshal(object, &r.Properties); err != nil { - return nil, fmt.Errorf("cannot unmarshal native kubernetes object (%#v)", err) + return nil, fmt.Errorf("cannot unmarshal native kubernetes object (%s): %s", object, err) } + return r, nil }