From f70a3d22ce65e9c17a99fc921d350fbcbe146332 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Mon, 11 Mar 2019 16:32:20 -0700 Subject: Detect chart name ChartName should not be mandatory field If not provided it will be detected by inspection of the tar.gz archive. Issue-ID: MULTICLOUD-525 Change-Id: Idaf9672f2cbbb882d78b1987467472ce73c651da Signed-off-by: Kiran Kamineni --- src/k8splugin/internal/rb/definition.go | 43 ++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) (limited to 'src/k8splugin/internal/rb/definition.go') diff --git a/src/k8splugin/internal/rb/definition.go b/src/k8splugin/internal/rb/definition.go index 19844990..4eaa9578 100644 --- a/src/k8splugin/internal/rb/definition.go +++ b/src/k8splugin/internal/rb/definition.go @@ -19,8 +19,12 @@ package rb import ( "bytes" "encoding/base64" - "k8splugin/internal/db" + "io/ioutil" "log" + "os" + "path/filepath" + + "k8splugin/internal/db" uuid "github.com/hashicorp/go-uuid" pkgerrors "github.com/pkg/errors" @@ -142,8 +146,8 @@ func (v *DefinitionClient) Delete(id string) error { // Upload the contents of resource bundle into database func (v *DefinitionClient) Upload(id string, inp []byte) error { - //ignore the returned data here - _, err := v.Get(id) + //Check if definition metadata exists + def, err := v.Get(id) if err != nil { return pkgerrors.Errorf("Invalid Definition ID provided: %s", err.Error()) } @@ -153,6 +157,39 @@ func (v *DefinitionClient) Upload(id string, inp []byte) error { return pkgerrors.Errorf("Error in file format: %s", err.Error()) } + //Detect chart name from data if it was not provided originally + if def.ChartName == "" { + path, err := ExtractTarBall(bytes.NewBuffer(inp)) + if err != nil { + return pkgerrors.Wrap(err, "Detecting chart name") + } + + finfo, err := ioutil.ReadDir(path) + if err != nil { + return pkgerrors.Wrap(err, "Detecting chart name") + } + + //Store the first directory with Chart.yaml found as the chart name + for _, f := range finfo { + if f.IsDir() { + //Check if Chart.yaml exists + if _, err = os.Stat(filepath.Join(path, f.Name(), "Chart.yaml")); err == nil { + def.ChartName = f.Name() + break + } + } + } + + if def.ChartName == "" { + return pkgerrors.New("Unable to detect chart name") + } + + _, err = v.Create(def) + if err != nil { + return pkgerrors.Wrap(err, "Storing updated chart metadata") + } + } + //Encode given byte stream to text for storage encodedStr := base64.StdEncoding.EncodeToString(inp) err = db.DBconn.Create(v.storeName, id, v.tagContent, encodedStr) -- cgit 1.2.3-korg