From 136aa06117ec16885dfb56b14b7e6bf3f21c85a9 Mon Sep 17 00:00:00 2001 From: Ruslan Aliev Date: Wed, 13 Oct 2021 13:19:56 -0500 Subject: [PATCH] Improve memory performance of clusterctl KRM Switching components type from []byte to string significantly improves memory effiency of config function. Change-Id: I81ddf469bf5abd214ff3785915d82ab494af6268 Signed-off-by: Ruslan Aliev --- krm-functions/clusterctl/image/main.go | 4 ++-- pkg/api/v1alpha1/clusterctl_types.go | 2 +- pkg/api/v1alpha1/zz_generated.deepcopy.go | 12 ++---------- pkg/phase/executors/clusterctl.go | 8 ++++---- 4 files changed, 9 insertions(+), 17 deletions(-) diff --git a/krm-functions/clusterctl/image/main.go b/krm-functions/clusterctl/image/main.go index af8f47ba8..360d62d8e 100644 --- a/krm-functions/clusterctl/image/main.go +++ b/krm-functions/clusterctl/image/main.go @@ -41,7 +41,7 @@ const ( type ClusterctlOptions struct { CmdOptions []string `json:"cmd-options,omitempty"` Config []byte `json:"config,omitempty"` - Components map[string][]byte `json:"components,omitempty"` + Components map[string]string `json:"components,omitempty"` } // Run prepares config, repo tree and executes clusterctl with appropriate options @@ -65,7 +65,7 @@ func (c *ClusterctlOptions) buildRepoTree() error { return err } } - if err := ioutil.WriteFile(f, component, filePerm); err != nil { + if err := ioutil.WriteFile(f, []byte(component), filePerm); err != nil { return err } } diff --git a/pkg/api/v1alpha1/clusterctl_types.go b/pkg/api/v1alpha1/clusterctl_types.go index 777623502..0e154b161 100644 --- a/pkg/api/v1alpha1/clusterctl_types.go +++ b/pkg/api/v1alpha1/clusterctl_types.go @@ -115,7 +115,7 @@ func DefaultClusterctl() *Clusterctl { type ClusterctlOptions struct { CmdOptions []string `json:"cmd-options,omitempty"` Config []byte `json:"config,omitempty"` - Components map[string][]byte `json:"components,omitempty"` + Components map[string]string `json:"components,omitempty"` } // GetKubeconfigOptions carries all the options to retrieve kubeconfig from parent cluster diff --git a/pkg/api/v1alpha1/zz_generated.deepcopy.go b/pkg/api/v1alpha1/zz_generated.deepcopy.go index db43082ce..23f5ba554 100644 --- a/pkg/api/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/api/v1alpha1/zz_generated.deepcopy.go @@ -511,17 +511,9 @@ func (in *ClusterctlOptions) DeepCopyInto(out *ClusterctlOptions) { } if in.Components != nil { in, out := &in.Components, &out.Components - *out = make(map[string][]byte, len(*in)) + *out = make(map[string]string, len(*in)) for key, val := range *in { - var outVal []byte - if val == nil { - (*out)[key] = nil - } else { - in, out := &val, &outVal - *out = make([]byte, len(*in)) - copy(*out, *in) - } - (*out)[key] = outVal + (*out)[key] = val } } } diff --git a/pkg/phase/executors/clusterctl.go b/pkg/phase/executors/clusterctl.go index ca41e8343..471138168 100644 --- a/pkg/phase/executors/clusterctl.go +++ b/pkg/phase/executors/clusterctl.go @@ -69,7 +69,7 @@ func NewClusterctlExecutor(cfg ifc.ExecutorConfig) (ifc.Executor, error) { return nil, err } cctlOpts := &airshipv1.ClusterctlOptions{ - Components: map[string][]byte{}, + Components: map[string]string{}, } if err := initRepoData(options, cctlOpts, cfg.TargetPath); err != nil { return nil, err @@ -134,7 +134,7 @@ func initRepoData(c *airshipv1.Clusterctl, o *airshipv1.ClusterctlOptions, targe return err } - o.Components[filepath.Join(componentDir, "metadata.yaml")] = metadata + o.Components[filepath.Join(componentDir, "metadata.yaml")] = string(metadata) filteredBundle, err := bundle.SelectBundle(document.NewDeployToK8sSelector()) if err != nil { @@ -146,7 +146,7 @@ func initRepoData(c *airshipv1.Clusterctl, o *airshipv1.ClusterctlOptions, targe return err } prv.URL = filepath.Join(componentDir, fmt.Sprintf("%s-components.yaml", typeMap[prv.Type])) - o.Components[prv.URL] = buffer.Bytes() + o.Components[prv.URL] = string(buffer.Bytes()) } return nil } @@ -330,7 +330,7 @@ func (c *ClusterctlExecutor) Render(w io.Writer, ro ifc.RenderOptions) error { dataAll := &bytes.Buffer{} for path, data := range c.cctlOpts.Components { if strings.Contains(path, "components.yaml") { - dataAll.Write(append(data, []byte("\n---\n")...)) + dataAll.Write(append([]byte(data), []byte("\n---\n")...)) } }