Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move to sigs.k8s.io/yaml #1985

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion client/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (k *Kompose) Convert(options ConvertOptions) ([]runtime.Object, error) {
Controller: k.getController(options),
IsReplicaSetFlag: *options.Replicas != 0,
IsDeploymentConfigFlag: k.createDeploymentConfig(options),
YAMLIndent: 2,
WithKomposeAnnotation: *options.WithKomposeAnnotations,
MultipleContainerMode: k.multiContainerMode(options),
ServiceGroupMode: k.serviceGroupMode(options),
Expand Down
3 changes: 0 additions & 3 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ var convertCmd = &cobra.Command{
Controller: strings.ToLower(ConvertController),
IsReplicaSetFlag: cmd.Flags().Lookup("replicas").Changed,
IsDeploymentConfigFlag: cmd.Flags().Lookup("deployment-config").Changed,
YAMLIndent: ConvertYAMLIndent,
Profiles: ConvertProfiles,
WithKomposeAnnotation: WithKomposeAnnotation,
NoInterpolate: NoInterpolate,
Expand Down Expand Up @@ -212,8 +211,6 @@ func init() {
convertCmd.Flags().BoolVar(&ConvertEmptyVols, "emptyvols", false, "Use Empty Volumes. Do not generate PVCs")
convertCmd.Flags().MarkDeprecated("emptyvols", "emptyvols has been marked as deprecated. Use --volumes emptyDir")

convertCmd.Flags().IntVar(&ConvertYAMLIndent, "indent", 2, "Spaces length to indent generated yaml files")

convertCmd.Flags().StringArrayVar(&ConvertProfiles, "profile", []string{}, `Specify the profile to use, can use multiple profiles`)

// In order to 'separate' both OpenShift and Kubernetes only flags. A custom help page is created
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
golang.org/x/tools v0.27.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.5.1
k8s.io/api v0.31.2
k8s.io/apimachinery v0.31.2
sigs.k8s.io/yaml v1.4.0
)

require (
Expand Down Expand Up @@ -81,6 +81,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand Down
2 changes: 0 additions & 2 deletions pkg/kobject/kobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ type ConvertOptions struct {

Server string

YAMLIndent int

WithKomposeAnnotation bool

MultipleContainerMode bool
Expand Down
68 changes: 16 additions & 52 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ import (
deployapi "github.com/openshift/api/apps/v1"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
appsv1 "k8s.io/api/apps/v1"
hpa "k8s.io/api/autoscaling/v2beta2"
api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

yaml "sigs.k8s.io/yaml"
)

// Default values for Horizontal Pod Autoscaler (HPA)
Expand Down Expand Up @@ -227,7 +228,7 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
return err
}

data, err := marshal(versionedObject, opt.GenerateJSON, opt.YAMLIndent)
data, err := marshal(versionedObject, opt.GenerateJSON)
if err != nil {
return fmt.Errorf("error in marshalling the List: %v", err)
}
Expand Down Expand Up @@ -256,7 +257,7 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
if err != nil {
return err
}
data, err := marshal(versionedObject, opt.GenerateJSON, opt.YAMLIndent)
data, err := marshal(versionedObject, opt.GenerateJSON)
if err != nil {
return err
}
Expand Down Expand Up @@ -301,17 +302,20 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
}

// marshal object runtime.Object and return byte array
func marshal(obj runtime.Object, jsonFormat bool, indent int) (data []byte, err error) {
// convert data to yaml or json
if jsonFormat {
data, err = json.MarshalIndent(obj, "", " ")
} else {
data, err = marshalWithIndent(obj, indent)
}
func marshal(obj runtime.Object, jsonFormat bool) ([]byte, error) {
data, err := json.Marshal(obj)
if err != nil {
data = nil
return nil, err
}
return

if !jsonFormat {
data, err = yaml.JSONToYAML(data)
if err != nil {
return nil, err
}
}

return data, nil
}

// remove empty map[string]interface{} strings from the object
Expand Down Expand Up @@ -354,46 +358,6 @@ func removeEmptyInterfaces(obj interface{}) interface{} {
}
}

// Convert JSON to YAML.
func jsonToYaml(j []byte, spaces int) ([]byte, error) {
// Convert the JSON to an object.
var jsonObj interface{}
// We are using yaml.Unmarshal here (instead of json.Unmarshal) because the
// Go JSON library doesn't try to pick the right number type (int, float,
// etc.) when unmarshling to interface{}, it just picks float64
// universally. go-yaml does go through the effort of picking the right
// number type, so we can preserve number type throughout this process.
err := yaml.Unmarshal(j, &jsonObj)
if err != nil {
return nil, err
}
jsonObj = removeEmptyInterfaces(jsonObj)
var b bytes.Buffer
encoder := yaml.NewEncoder(&b)
encoder.SetIndent(spaces)
if err := encoder.Encode(jsonObj); err != nil {
return nil, err
}
return b.Bytes(), nil

// Marshal this object into YAML.
// return yaml.Marshal(jsonObj)
}

func marshalWithIndent(o interface{}, indent int) ([]byte, error) {
j, err := json.Marshal(o)
if err != nil {
return nil, fmt.Errorf("error marshaling into JSON: %s", err.Error())
}

y, err := jsonToYaml(j, indent)
if err != nil {
return nil, fmt.Errorf("error converting JSON to YAML: %s", err.Error())
}

return y, nil
}

// Convert object to versioned object
// if groupVersion is empty (metav1.GroupVersion{}), use version from original object (obj)
func convertToVersion(obj runtime.Object) (runtime.Object, error) {
Expand Down
Loading