diff options
author | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-03-11 16:32:20 -0700 |
---|---|---|
committer | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-03-11 16:53:12 -0700 |
commit | f70a3d22ce65e9c17a99fc921d350fbcbe146332 (patch) | |
tree | 83da1f4911ca11fa410af01701e5cc3539256f10 /src/k8splugin/internal/rb/definition.go | |
parent | 49a48a37a76201839c9507a970d639a318ba9d3f (diff) |
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 <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src/k8splugin/internal/rb/definition.go')
-rw-r--r-- | src/k8splugin/internal/rb/definition.go | 43 |
1 files changed, 40 insertions, 3 deletions
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) |