diff options
author | Rajamohan Raj <rajamohan.raj@intel.com> | 2020-09-14 19:47:54 +0000 |
---|---|---|
committer | Ritu Sood <ritu.sood@intel.com> | 2020-09-24 19:03:49 +0000 |
commit | 564929b805c014f5e7ea16b5839ebb621b8c8a57 (patch) | |
tree | 100ca9e65a4e0385ee6d1d13cebc43e25625a084 /src | |
parent | f1a368f93708ee0a27f0740e353408ab0f19e147 (diff) |
Cleanup tmp resources after instantiation
In this patch,we clean up the tmp files
and directories generated during the
process of instantiation.
They include the tmp dirs with
prefix : /tmp/helm-tmpl and /tmp/k8s-ext
Issue-ID: MULTICLOUD-1206
Signed-off-by: Rajamohan Raj <rajamohan.raj@intel.com>
Change-Id: I02d11bb2f8d920e35aae7343f041a53b1cd3f057
Diffstat (limited to 'src')
-rw-r--r-- | src/orchestrator/pkg/module/instantiation.go | 34 | ||||
-rw-r--r-- | src/orchestrator/utils/helm/helm.go | 41 |
2 files changed, 65 insertions, 10 deletions
diff --git a/src/orchestrator/pkg/module/instantiation.go b/src/orchestrator/pkg/module/instantiation.go index de723242..0ae76006 100644 --- a/src/orchestrator/pkg/module/instantiation.go +++ b/src/orchestrator/pkg/module/instantiation.go @@ -20,6 +20,8 @@ import ( "encoding/base64" "encoding/json" "fmt" + "os" + "strings" "time" gpic "github.com/onap/multicloud-k8s/src/orchestrator/pkg/gpic" @@ -97,10 +99,12 @@ func NewInstantiationClient() *InstantiationClient { func (c InstantiationClient) Approve(p string, ca string, v string, di string) error { s, err := NewDeploymentIntentGroupClient().GetDeploymentIntentGroupState(di, p, ca, v) if err != nil { + log.Info("DeploymentIntentGroup has no state info ", log.Fields{"DeploymentIntentGroup: ":di}) return pkgerrors.Wrap(err, "DeploymentIntentGroup has no state info: "+di) } stateVal, err := state.GetCurrentStateFromStateInfo(s) if err != nil { + log.Info("Error getting current state from DeploymentIntentGroup stateInfo", log.Fields{"DeploymentIntentGroup ":di}) return pkgerrors.Errorf("Error getting current state from DeploymentIntentGroup stateInfo: " + di) } switch stateVal { @@ -218,6 +222,30 @@ func GetSortedTemplateForApp(appName, p, ca, v, rName, cp string, overrideValues return sortedTemplates, err } +func calculateDirPath(fp string) string { + sa := strings.Split(fp, "/") + return "/" + sa[1] + "/" + sa[2] + "/" +} + +func cleanTmpfiles(sortedTemplates []helm.KubernetesResourceTemplate) error { + dp := calculateDirPath(sortedTemplates[0].FilePath) + for _, st := range sortedTemplates{ + log.Info("Clean up ::", log.Fields{"file: ": st.FilePath}) + err := os.Remove(st.FilePath) + if err != nil { + log.Error("Error while deleting file", log.Fields{"file: ":st.FilePath}) + return err + } + } + err := os.RemoveAll(dp) + if err != nil { + log.Error("Error while deleting dir", log.Fields{"Dir: ":dp}) + return err + } + log.Info("Clean up temp-dir::", log.Fields{"Dir: ": dp}) + return nil +} + /* Instantiate methods takes in projectName, compositeAppName, compositeAppVersion, DeploymentIntentName. This method is responsible for template resolution, intent @@ -296,6 +324,7 @@ func (c InstantiationClient) Instantiate(p string, ca string, v string, di strin if err != nil { deleteAppContext(context) + log.Error("Unable to get the sorted templates for app", log.Fields{}) return pkgerrors.Wrap(err, "Unable to get the sorted templates for app") } @@ -307,6 +336,8 @@ func (c InstantiationClient) Instantiate(p string, ca string, v string, di strin return pkgerrors.Wrapf(err, "Unable to get the resources for app :: %s", eachApp.Metadata.Name) } + defer cleanTmpfiles(sortedTemplates) + specData, err := NewAppIntentClient().GetAllIntentsByApp(eachApp.Metadata.Name, p, ca, v, gIntent) if err != nil { deleteAppContext(context) @@ -338,6 +369,7 @@ func (c InstantiationClient) Instantiate(p string, ca string, v string, di strin deleteAppContext(context) return pkgerrors.Wrap(err, "Error while verifying resources in app: ") } + } jappOrderInstr, err := json.Marshal(appOrderInstr) if err != nil { @@ -482,7 +514,7 @@ func (c InstantiationClient) Terminate(p string, ca string, v string, di string) } if stateVal != state.StateEnum.Instantiated { - return pkgerrors.Errorf("DeploymentIntentGroup is not instantiated" + di) + return pkgerrors.Errorf("DeploymentIntentGroup is not instantiated :" + di) } currentCtxId := state.GetLastContextIdFromStateInfo(s) diff --git a/src/orchestrator/utils/helm/helm.go b/src/orchestrator/utils/helm/helm.go index 3f8b6e9e..88f79c43 100644 --- a/src/orchestrator/utils/helm/helm.go +++ b/src/orchestrator/utils/helm/helm.go @@ -18,20 +18,24 @@ package helm import ( "bytes" + + log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" + utils "github.com/onap/multicloud-k8s/src/orchestrator/utils" pkgerrors "github.com/pkg/errors" - "fmt" "io/ioutil" - "k8s.io/helm/pkg/strvals" "os" "path/filepath" "regexp" "strings" + "k8s.io/helm/pkg/strvals" + "github.com/ghodss/yaml" + logger "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/serializer/json" "k8s.io/apimachinery/pkg/util/validation" @@ -43,8 +47,6 @@ import ( "k8s.io/helm/pkg/renderutil" "k8s.io/helm/pkg/tiller" "k8s.io/helm/pkg/timeconv" - logger "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" - ) //KubernetesResourceTemplate - Represents the template that is used to create a particular @@ -295,6 +297,20 @@ type Resolver interface { Resolve(appContent, appProfileContent []byte, overrideValuesOfAppStr []string, appName string) ([]KubernetesResourceTemplate, error) } + +func cleanupTempFiles(fp string) error { + sa := strings.Split(fp, "/") + dp:= "/" + sa[1] + "/" + sa[2] + "/" + err := os.RemoveAll(dp) + if err != nil { + log.Error("Error while deleting dir", log.Fields{"Dir: ":dp}) + return err + } + log.Info("Clean up k8s-ext-tmp-dir::", log.Fields{"Dir: ": dp}) + return nil +} + + // Resolve function func (h *TemplateClient) Resolve(appContent []byte, appProfileContent []byte, overrideValuesOfAppStr []string, appName string) ([]KubernetesResourceTemplate, error) { @@ -302,33 +318,40 @@ func (h *TemplateClient) Resolve(appContent []byte, appProfileContent []byte, ov //chartBasePath is the tmp path where the appContent(rawHelmCharts) is extracted. chartBasePath, err := utils.ExtractTarBall(bytes.NewBuffer(appContent)) + defer cleanupTempFiles(chartBasePath) if err != nil { - return sortedTemplates, pkgerrors.Wrap(err, "Extracting appContent") + logger.Error("Error while extracting appContent", logger.Fields{}) + return sortedTemplates, pkgerrors.Wrap(err, "Error while extracting appContent") } logger.Info("The chartBasePath ::", logger.Fields{"chartBasePath":chartBasePath}) //prPath is the tmp path where the appProfileContent is extracted. prPath, err := utils.ExtractTarBall(bytes.NewBuffer(appProfileContent)) + defer cleanupTempFiles(prPath) if err != nil { - return sortedTemplates, pkgerrors.Wrap(err, "Extracting Profile Content") + logger.Error("Error while extracting Profile Content", logger.Fields{}) + return sortedTemplates, pkgerrors.Wrap(err, "Error while extracting Profile Content") } logger.Info("The profile path:: ", logger.Fields{"Profile Path":prPath}) prYamlClient, err := ProcessProfileYaml(prPath, h.manifestName) if err != nil { - return sortedTemplates, pkgerrors.Wrap(err, "Processing Profile Manifest") + logger.Error("Error while processing Profile Manifest", logger.Fields{}) + return sortedTemplates, pkgerrors.Wrap(err, "Error while processing Profile Manifest") } logger.Info("Got the profileYamlClient..", logger.Fields{}) err = prYamlClient.CopyConfigurationOverrides(chartBasePath) if err != nil { - return sortedTemplates, pkgerrors.Wrap(err, "Copying configresources to chart") + logger.Error("Error while copying configresources to chart", logger.Fields{}) + return sortedTemplates, pkgerrors.Wrap(err, "Error while copying configresources to chart") } chartPath := filepath.Join(chartBasePath, appName) sortedTemplates, err = h.GenerateKubernetesArtifacts(chartPath, []string{prYamlClient.GetValues()}, overrideValuesOfAppStr) if err != nil { - return sortedTemplates, pkgerrors.Wrap(err, "Generate final k8s yaml") + logger.Error("Error while generating final k8s yaml", logger.Fields{}) + return sortedTemplates, pkgerrors.Wrap(err, "Error while generating final k8s yaml") } return sortedTemplates, nil } |