Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Commit

Permalink
Avoid redundant conversions by passing an io.ReadCloser to manifest.W…
Browse files Browse the repository at this point in the history
…ithNamespace

Signed-off-by: Dennis Marttinen <dennis@weave.works>
  • Loading branch information
twelho committed Aug 3, 2020
1 parent 9ba0b10 commit faf7480
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 28 deletions.
3 changes: 2 additions & 1 deletion pkg/addons/addon.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ghodss/yaml"
"github.com/google/go-jsonnet"
log "github.com/sirupsen/logrus"
"github.com/weaveworks/libgitops/pkg/serializer"
"github.com/weaveworks/wksctl/pkg/addons/assets"
"github.com/weaveworks/wksctl/pkg/qjson"
"github.com/weaveworks/wksctl/pkg/registry"
Expand Down Expand Up @@ -341,7 +342,7 @@ func (a *Addon) buildJsonnet(config BuildOptions) ([]string, error) {
return nil, err
}
// The WithNamespace function supports either JSON or YAML
content, err = manifest.WithNamespace(string(data), "wkp-addons")
content, err = manifest.WithNamespace(serializer.FromBytes(data), "wkp-addons")
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/wksprovider/machine/os/os.go
Expand Up @@ -686,7 +686,7 @@ func getConfigMapManifest(configDir, mapName, namespace string) ([]byte, error)
if err != nil {
return nil, err
}
content, err := manifest.WithNamespace(string(bytes), namespace)
content, err := manifest.WithNamespace(serializer.FromBytes(bytes), namespace)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func capiControllerManifest(controller ControllerParams, namespace, configDir st
if err != nil {
return nil, err
}
content, err := manifest.WithNamespace(string(manifestbytes), namespace)
content, err := manifest.WithNamespace(serializer.FromBytes(manifestbytes), namespace)
return content, err
}

Expand Down Expand Up @@ -1072,7 +1072,7 @@ func wksControllerManifest(controller ControllerParams, namespace, configDir str
if err != nil {
return nil, err
}
content, err := manifest.WithNamespace(string(manifestbytes), namespace)
content, err := manifest.WithNamespace(serializer.FromBytes(manifestbytes), namespace)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1346,7 +1346,7 @@ func buildAddon(addonDefn existinginfrav1.Addon, imageRepository string, Cluster
// The build puts files in a temp dir we read them into []byte and return those
// so we can cleanup the temp files
for _, m := range manifests {
content, err := manifest.WithNamespace(m, namespace)
content, err := manifest.WithNamespace(serializer.FromFile(m), namespace)
if err != nil {
return nil, err
}
Expand All @@ -1369,7 +1369,7 @@ func processDeps(deps []string, manifests [][]byte, namespace string) ([][]byte,
if err != nil {
logger.Warnf("Failed to load addon dependency - %v", err)
}
content, err := manifest.WithNamespace(string(contents), namespace)
content, err := manifest.WithNamespace(serializer.FromBytes(contents), namespace)
if err != nil {
logger.Warnf("Failed to set namespace for manifest:\n%s\n", content)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/plan/resource/kubeadm_init.go
Expand Up @@ -186,7 +186,7 @@ func (ki *KubeadmInit) updateManifestNamespace(fileName, namespace string) ([]by
if err != nil {
return nil, errors.Wrap(err, "Failed to open manifest")
}
c, err := manifest.WithNamespace(string(content), namespace)
c, err := manifest.WithNamespace(serializer.FromBytes(content), namespace)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/plan/resource/kubectl_apply.go
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/weaveworks/libgitops/pkg/serializer"
"github.com/weaveworks/wksctl/pkg/apis/wksprovider/machine/scripts"
"github.com/weaveworks/wksctl/pkg/plan"
"github.com/weaveworks/wksctl/pkg/utilities/manifest"
Expand Down Expand Up @@ -113,7 +114,7 @@ func (ka *KubectlApply) Apply(runner plan.Runner, diff plan.Diff) (bool, error)
}

if str(ka.Namespace) != "" {
content, err := manifest.WithNamespace(string(c), str(ka.Namespace))
content, err := manifest.WithNamespace(serializer.FromBytes(c), str(ka.Namespace))
if err != nil {
return false, err
}
Expand Down
20 changes: 1 addition & 19 deletions pkg/utilities/manifest/manifest.go
Expand Up @@ -3,8 +3,6 @@ package manifest
import (
"bytes"
"io"
"os"
"strings"

"github.com/weaveworks/libgitops/pkg/serializer"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
Expand All @@ -20,15 +18,7 @@ const (

var DefaultAddonNamespaces = map[string]string{"weave-net": "kube-system"}

func WithNamespace(fileOrString, namespace string) ([]byte, error) {
// Set up the readcloser from either a file, or from the given string
var rc io.ReadCloser
if isFile(fileOrString) {
rc = serializer.FromFile(fileOrString)
} else {
rc = serializer.FromBytes([]byte(fileOrString))
}

func WithNamespace(rc io.ReadCloser, namespace string) ([]byte, error) {
// Create a FrameReader and FrameWriter, using YAML document separators
// The FrameWriter will write into buf
fr := serializer.NewYAMLFrameReader(rc)
Expand Down Expand Up @@ -140,11 +130,3 @@ func visitElementsForPath(obj *kyaml.RNode, fn func(node *kyaml.RNode) error, pa
func setNamespaceFilter(ns string) kyaml.FieldSetter {
return kyaml.SetField("namespace", kyaml.NewScalarRNode(ns))
}

func isFile(fileOrString string) bool {
_, err := os.Stat(fileOrString)
if err != nil && (os.IsNotExist(err) || strings.Contains(err.Error(), "file name too long")) {
return false
}
return true
}
3 changes: 2 additions & 1 deletion pkg/utilities/manifest/manifest_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/weaveworks/libgitops/pkg/serializer"
)

const (
Expand Down Expand Up @@ -253,7 +254,7 @@ func TestManifestWithNamespace(t *testing.T) {

defer os.Remove(fname)

updated, err := WithNamespace(fname, newNamespace)
updated, err := WithNamespace(serializer.FromFile(fname), newNamespace)
assert.NoError(t, err)
assert.NotEqual(t, tt.content, string(updated))
assert.Contains(t, string(updated), newNamespace)
Expand Down

0 comments on commit faf7480

Please sign in to comment.