Skip to content

Commit

Permalink
refactor: replace gopkg.in/yaml.v2 with sigs.k8s.io/yaml
Browse files Browse the repository at this point in the history
k3d-io#1094 (comment)

Signed-off-by: Sunghoon Kang <hoon@akuity.io>
  • Loading branch information
devholic committed Jul 1, 2022
1 parent 2fc798f commit 48221ce
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 66 deletions.
5 changes: 2 additions & 3 deletions cmd/cluster/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ import (

"github.com/docker/go-connections/nat"
"github.com/sirupsen/logrus"
"inet.af/netaddr"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"inet.af/netaddr"
"sigs.k8s.io/yaml"

cliutil "github.com/k3d-io/k3d/v5/cmd/util"
cliconfig "github.com/k3d-io/k3d/v5/cmd/util/config"
Expand Down
7 changes: 4 additions & 3 deletions cmd/config/configMigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import (
"os"
"strings"

"github.com/k3d-io/k3d/v5/pkg/config"
l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"sigs.k8s.io/yaml"

"github.com/k3d-io/k3d/v5/pkg/config"
l "github.com/k3d-io/k3d/v5/pkg/logger"
)

// NewCmdConfigMigrate returns a new cobra command
Expand Down
5 changes: 3 additions & 2 deletions cmd/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ package debug
import (
"fmt"

"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"github.com/k3d-io/k3d/v5/cmd/util"
"github.com/k3d-io/k3d/v5/pkg/client"
l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/k3d-io/k3d/v5/pkg/runtimes"
"github.com/k3d-io/k3d/v5/pkg/types"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

// NewCmdDebug returns a new cobra command
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ import (
"strconv"
"strings"

"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/k3d-io/k3d/v5/cmd/cluster"
cfg "github.com/k3d-io/k3d/v5/cmd/config"
Expand All @@ -46,9 +47,8 @@ import (
cliutil "github.com/k3d-io/k3d/v5/cmd/util"
l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/k3d-io/k3d/v5/pkg/runtimes"
"github.com/k3d-io/k3d/v5/pkg/util"
"github.com/k3d-io/k3d/v5/version"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/writer"
)

// RootFlags describes a struct that holds flags that can be set on root level of the command
Expand Down Expand Up @@ -109,7 +109,7 @@ All Nodes of a k3d cluster are part of the same docker network.`,
if err != nil {
l.Log().Fatalln(err)
}
err = yaml.NewEncoder(os.Stdout).Encode(info)
err = util.NewYAMLEncoder(os.Stdout).Encode(info)
if err != nil {
l.Log().Fatalln(err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/util/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import (
"path/filepath"
"strings"

"github.com/k3d-io/k3d/v5/pkg/config"
l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"sigs.k8s.io/yaml"

"github.com/k3d-io/k3d/v5/pkg/config"
l "github.com/k3d-io/k3d/v5/pkg/logger"
)

func InitViperWithConfigFile(cfgViper *viper.Viper, configFile string) error {
Expand Down
5 changes: 3 additions & 2 deletions cmd/util/listings.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import (
"sort"
"strings"

"github.com/liggitt/tabwriter"
"sigs.k8s.io/yaml"

l "github.com/k3d-io/k3d/v5/pkg/logger"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/liggitt/tabwriter"
"gopkg.in/yaml.v2"
)

type NodePrinter interface {
Expand Down
13 changes: 7 additions & 6 deletions pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ import (

"github.com/docker/go-connections/nat"
"github.com/imdario/mergo"
copystruct "github.com/mitchellh/copystructure"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"sigs.k8s.io/yaml"

"github.com/k3d-io/k3d/v5/pkg/actions"
config "github.com/k3d-io/k3d/v5/pkg/config/v1alpha4"
l "github.com/k3d-io/k3d/v5/pkg/logger"
Expand All @@ -45,10 +50,6 @@ import (
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/k3d-io/k3d/v5/pkg/types/k3s"
"github.com/k3d-io/k3d/v5/pkg/util"
copystruct "github.com/mitchellh/copystructure"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
)

// ClusterRun orchestrates the steps of cluster creation, configuration and starting
Expand Down Expand Up @@ -1057,7 +1058,7 @@ func ClusterStart(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clust
}

var outputBuf bytes.Buffer
outputEncoder := yaml.NewEncoder(&outputBuf)
outputEncoder := util.NewYAMLEncoder(&outputBuf)

for _, d := range split {
var doc map[string]interface{}
Expand All @@ -1074,7 +1075,7 @@ func ClusterStart(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clust
return nil, err
}
}
outputEncoder.Close()
_ = outputEncoder.Close()
return outputBuf.Bytes(), nil
},
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/client/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ import (
"github.com/docker/go-connections/nat"
"github.com/go-test/deep"
"github.com/imdario/mergo"
"github.com/spf13/viper"
"sigs.k8s.io/yaml"

l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/k3d-io/k3d/v5/pkg/runtimes"
"github.com/k3d-io/k3d/v5/pkg/types"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)

var (
Expand Down
13 changes: 6 additions & 7 deletions pkg/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,24 @@ import (
"strings"
"time"

copystruct "github.com/mitchellh/copystructure"
"gopkg.in/yaml.v2"

"github.com/docker/go-connections/nat"
dockerunits "github.com/docker/go-units"
"github.com/imdario/mergo"
copystruct "github.com/mitchellh/copystructure"
"golang.org/x/sync/errgroup"
"sigs.k8s.io/yaml"

"github.com/k3d-io/k3d/v5/pkg/actions"
l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/k3d-io/k3d/v5/pkg/runtimes"
"github.com/k3d-io/k3d/v5/pkg/runtimes/docker"
runtimeTypes "github.com/k3d-io/k3d/v5/pkg/runtimes/types"
"github.com/k3d-io/k3d/v5/version"

runtimeErrors "github.com/k3d-io/k3d/v5/pkg/runtimes/errors"
runtimeTypes "github.com/k3d-io/k3d/v5/pkg/runtimes/types"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/k3d-io/k3d/v5/pkg/types/fixes"
"github.com/k3d-io/k3d/v5/pkg/types/k3s"
"github.com/k3d-io/k3d/v5/pkg/util"
"golang.org/x/sync/errgroup"
"github.com/k3d-io/k3d/v5/version"
)

// NodeAddToCluster adds a node to an existing cluster
Expand Down
5 changes: 3 additions & 2 deletions pkg/client/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ import (
"strings"

"github.com/docker/go-connections/nat"
"github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"

"github.com/k3d-io/k3d/v5/pkg/config/types"
config "github.com/k3d-io/k3d/v5/pkg/config/v1alpha4"
l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/k3d-io/k3d/v5/pkg/runtimes"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/k3d-io/k3d/v5/pkg/util"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)

var (
Expand Down
3 changes: 2 additions & 1 deletion pkg/client/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import (

"github.com/docker/go-connections/nat"
"github.com/imdario/mergo"
"sigs.k8s.io/yaml"

l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/k3d-io/k3d/v5/pkg/runtimes"
"github.com/k3d-io/k3d/v5/pkg/runtimes/docker"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/k3d-io/k3d/v5/pkg/types/k3s"
"github.com/k3d-io/k3d/v5/pkg/types/k8s"
"gopkg.in/yaml.v2"
)

func RegistryRun(ctx context.Context, runtime runtimes.Runtime, reg *k3d.Registry) (*k3d.Node, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ import (
"strings"

"github.com/docker/go-connections/nat"
"inet.af/netaddr"
"sigs.k8s.io/yaml"

cliutil "github.com/k3d-io/k3d/v5/cmd/util" // TODO: move parseapiport to pkg
"github.com/k3d-io/k3d/v5/pkg/client"
conf "github.com/k3d-io/k3d/v5/pkg/config/v1alpha4"
l "github.com/k3d-io/k3d/v5/pkg/logger"
"github.com/k3d-io/k3d/v5/pkg/runtimes"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/k3d-io/k3d/v5/pkg/types/k3s"
"github.com/k3d-io/k3d/v5/pkg/util"
"github.com/k3d-io/k3d/v5/version"
"gopkg.in/yaml.v2"
"inet.af/netaddr"

l "github.com/k3d-io/k3d/v5/pkg/logger"
)

// TransformSimpleToClusterConfig transforms a simple configuration to a full-fledged cluster configuration
Expand Down
27 changes: 0 additions & 27 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ THE SOFTWARE.
package util

import (
"bytes"
"io"
"strings"

"gopkg.in/yaml.v2"
)

func RemoveElementFromStringSlice(slice []string, index int) []string {
Expand All @@ -41,26 +37,3 @@ func ReplaceInAllElements(replacer *strings.Replacer, arr []string) []string {
}
return arr
}

func SplitYAML(resources []byte) ([][]byte, error) {

dec := yaml.NewDecoder(bytes.NewReader(resources))

var res [][]byte
for {
var value interface{}
err := dec.Decode(&value)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
valueBytes, err := yaml.Marshal(value)
if err != nil {
return nil, err
}
res = append(res, valueBytes)
}
return res, nil
}
57 changes: 57 additions & 0 deletions pkg/util/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package util

import (
"bytes"
"io"

goyaml "gopkg.in/yaml.v2"
"sigs.k8s.io/yaml"
)

func SplitYAML(resources []byte) ([][]byte, error) {
dec := goyaml.NewDecoder(bytes.NewReader(resources))

var res [][]byte
for {
var value interface{}
err := dec.Decode(&value)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
valueBytes, err := yaml.Marshal(value)
if err != nil {
return nil, err
}
res = append(res, valueBytes)
}
return res, nil
}

type YAMLEncoder struct {
encoder *goyaml.Encoder
}

func NewYAMLEncoder(w io.Writer) *YAMLEncoder {
return &YAMLEncoder{
encoder: goyaml.NewEncoder(w),
}
}

func (e *YAMLEncoder) Encode(v interface{}) (err error) {
data, err := yaml.Marshal(v)
if err != nil {
return err
}
var doc interface{}
if err := yaml.Unmarshal(data, &doc); err != nil {
return err
}
return e.encoder.Encode(doc)
}

func (e *YAMLEncoder) Close() (err error) {
return e.encoder.Close()
}
Loading

0 comments on commit 48221ce

Please sign in to comment.