Skip to content

Commit

Permalink
fix: print consistent json/yaml output
Browse files Browse the repository at this point in the history
Currently, some commands (e.g. `k3d cluster list` or `k3d cluster get`)
prints differently per output option (`json` / `yaml`). This behavior
may break interoperability and can make users hard to figure out the
difference between outputs.

This commit fixes this issue by replacing `gopkg.in/yaml.v2` with
`sigs.k8s.io/yaml`, which converts the json-encoded struct to yaml.

Signed-off-by: Sunghoon Kang <hoon@akuity.io>
  • Loading branch information
devholic committed Jul 1, 2022
1 parent 8e7cea6 commit 3099004
Show file tree
Hide file tree
Showing 26 changed files with 482 additions and 386 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
18 changes: 9 additions & 9 deletions cmd/cluster/clusterList.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ import (
"os"
"strings"

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

"github.com/k3d-io/k3d/v5/cmd/util"
k3cluster "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"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/liggitt/tabwriter"
)

// TODO : deal with --all flag to manage differentiate started cluster and stopped cluster like `docker ps` and `docker ps -a`
Expand Down Expand Up @@ -103,11 +103,11 @@ func PrintClusters(clusters []*k3d.Cluster, flags clusterFlags) {
// the output details printed when we dump JSON/YAML
type jsonOutput struct {
k3d.Cluster
ServersRunning int `yaml:"servers_running" json:"serversRunning"`
ServersCount int `yaml:"servers_count" json:"serversCount"`
AgentsRunning int `yaml:"agents_running" json:"agentsRunning"`
AgentsCount int `yaml:"agents_count" json:"agentsCount"`
LoadBalancer bool `yaml:"has_lb,omitempty" json:"hasLoadbalancer,omitempty"`
ServersRunning int `json:"serversRunning"`
ServersCount int `json:"serversCount"`
AgentsRunning int `json:"agentsRunning"`
AgentsCount int `json:"agentsCount"`
LoadBalancer bool `json:"hasLoadbalancer,omitempty"`
}

jsonOutputEntries := []jsonOutput{}
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
17 changes: 9 additions & 8 deletions 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 Expand Up @@ -275,19 +276,19 @@ func RegistryFromNode(node *k3d.Node) (*k3d.Registry, error) {
func RegistryGenerateLocalRegistryHostingConfigMapYAML(ctx context.Context, runtime runtimes.Runtime, registries []*k3d.Registry) ([]byte, error) {

type cmMetadata struct {
Name string `yaml:"name"`
Namespace string `yaml:"namespace"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}

type cmData struct {
RegHostV1 string `yaml:"localRegistryHosting.v1"`
RegHostV1 string `json:"localRegistryHosting.v1"`
}

type configmap struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Metadata cmMetadata `yaml:"metadata"`
Data cmData `yaml:"data"`
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata cmMetadata `json:"metadata"`
Data cmData `json:"data"`
}

if len(registries) > 1 {
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
6 changes: 3 additions & 3 deletions pkg/config/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ package types

// TypeMeta is basically copied from https://github.com/kubernetes/apimachinery/blob/a3b564b22db316a41e94fdcffcf9995424fe924c/pkg/apis/meta/v1/types.go#L36-L56
type TypeMeta struct {
Kind string `mapstructure:"kind,omitempty" yaml:"kind,omitempty" json:"kind,omitempty"`
APIVersion string `mapstructure:"apiVersion,omitempty" yaml:"apiVersion,omitempty" json:"apiVersion,omitempty"`
Kind string `mapstructure:"kind,omitempty" json:"kind,omitempty"`
APIVersion string `mapstructure:"apiVersion,omitempty" json:"apiVersion,omitempty"`
}

// ObjectMeta got its name from the Kubernetes counterpart.
type ObjectMeta struct {
Name string `mapstructure:"name,omitempty" yaml:"name,omitempty" json:"name,omitempty"`
Name string `mapstructure:"name,omitempty" json:"name,omitempty"`
}

// Config interface.
Expand Down
Loading

0 comments on commit 3099004

Please sign in to comment.