Skip to content

fix: use compact indent for sequences #1062

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

Merged
merged 1 commit into from
Mar 14, 2025
Merged
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ require (
golang.org/x/oauth2 v0.27.0
golang.org/x/sync v0.11.0
google.golang.org/grpc v1.70.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.31.0
k8s.io/apimachinery v0.31.0
k8s.io/client-go v0.31.0
sigs.k8s.io/kustomize/api v0.17.2
sigs.k8s.io/kustomize/kyaml v0.17.1
sigs.k8s.io/yaml v1.4.0
)

require (
Expand Down Expand Up @@ -153,6 +153,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.31.2 // indirect
k8s.io/apiserver v0.31.0 // indirect
k8s.io/cli-runtime v0.31.0 // indirect
Expand All @@ -167,7 +168,6 @@ require (
oras.land/oras-go/v2 v2.5.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace (
Expand Down
44 changes: 11 additions & 33 deletions pkg/argocd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"gopkg.in/yaml.v3"
yaml "sigs.k8s.io/yaml/goyaml.v3"
)

// Stores some statistics about the results of a run
Expand Down Expand Up @@ -61,6 +61,8 @@ const (
WriteBackGit WriteBackMethod = 1
)

const defaultIndent = 2

// WriteBackConfig holds information on how to write back the changes to an Application
type WriteBackConfig struct {
Method WriteBackMethod
Expand Down Expand Up @@ -425,6 +427,7 @@ func marshalWithIndent(in interface{}, indent int) (out []byte, err error) {
defer encoder.Close()
// note: yaml.v3 will only respect indents from 1 to 9 inclusive.
encoder.SetIndent(indent)
encoder.CompactSeqIndent()
if err = encoder.Encode(in); err != nil {
return nil, err
}
Expand All @@ -434,30 +437,6 @@ func marshalWithIndent(in interface{}, indent int) (out []byte, err error) {
return b.Bytes(), nil
}

func guessIndent(root *yaml.Node) int {
node := root
if root.Kind == yaml.DocumentNode {
if len(node.Content) == 0 {
return 2
}
node = root.Content[0]
}
// anything other than a map at the root makes guessing difficult
if node.Kind != yaml.MappingNode || len(node.Content) == 0 {
return 2
}
// first level map entries that are themselves mappings or sequences,
// in block style, and are indented, allow guessing the preferred indent.
for i, child := range node.Content {
if i%2 == 1 && child.Column > 1 && child.Column < 10 && child.Style != yaml.FlowStyle {
if child.Kind == yaml.MappingNode || child.Kind == yaml.SequenceNode {
return child.Column - 1
}
}
}
return 2
}

// marshalParamsOverride marshals the parameter overrides of a given application
// into YAML bytes
func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]byte, error) {
Expand All @@ -481,16 +460,16 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
}

if len(originalData) == 0 {
override, err = marshalWithIndent(newParams, 2)
override, err = marshalWithIndent(newParams, defaultIndent)
break
}
err = yaml.Unmarshal(originalData, &params)
if err != nil {
override, err = marshalWithIndent(newParams, 2)
override, err = marshalWithIndent(newParams, defaultIndent)
break
}
mergeKustomizeOverride(&params, &newParams)
override, err = marshalWithIndent(params, 2)
override, err = marshalWithIndent(params, defaultIndent)
case ApplicationTypeHelm:
if appSource.Helm == nil {
return []byte{}, nil
Expand All @@ -504,7 +483,6 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
if err != nil {
return nil, err
}
indent := guessIndent(&helmNewValues)

for _, c := range images {
if c.ImageAlias == "" {
Expand Down Expand Up @@ -546,7 +524,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
}
}

override, err = marshalWithIndent(&helmNewValues, indent)
override, err = marshalWithIndent(&helmNewValues, defaultIndent)
} else {
var params helmOverride
newParams := helmOverride{
Expand All @@ -559,16 +537,16 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
log.WithContext().AddField("application", app).Debugf("values: '%s'", outputParams)

if len(originalData) == 0 {
override, err = marshalWithIndent(newParams, 2)
override, err = marshalWithIndent(newParams, defaultIndent)
break
}
err = yaml.Unmarshal(originalData, &params)
if err != nil {
override, err = marshalWithIndent(newParams, 2)
override, err = marshalWithIndent(newParams, defaultIndent)
break
}
mergeHelmOverride(&params, &newParams)
override, err = marshalWithIndent(params, 2)
override, err = marshalWithIndent(params, defaultIndent)
}
default:
err = fmt.Errorf("unsupported application type")
Expand Down
Loading
Loading