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: Make the out flag print to one file using seperator #1541

Merged
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
44 changes: 14 additions & 30 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,6 @@ func getDirName(opt kobject.ConvertOptions) string {
return dirName
}

func objectToRaw(object runtime.Object) runtime.RawExtension {
r := runtime.RawExtension{
Object: object,
}

bytes, _ := json.Marshal(object)
r.Raw = bytes

return r
}

// PrintList will take the data converted and decide on the commandline attributes given
func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
var f *os.File
Expand Down Expand Up @@ -192,36 +181,31 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
}

var files []string

// if asked to print to stdout or to put in single file
// we will create a list
if opt.ToStdout || f != nil {
list := &api.List{}
// convert objects to versioned and add them to list
if opt.GenerateJSON {
return fmt.Errorf("cannot convert to one file while specifying a json output file or stdout option")
}
for _, object := range objects {
versionedObject, err := convertToVersion(object)
if err != nil {
return err
}

list.Items = append(list.Items, objectToRaw(versionedObject))
}
// version list itself
list.Kind = "List"
list.APIVersion = "v1"
convertedList, err := convertToVersion(list)
if err != nil {
return err
}
data, err := marshal(convertedList, opt.GenerateJSON, opt.YAMLIndent)
if err != nil {
return fmt.Errorf("error in marshalling the List: %v", err)
}
printVal, err := transformer.Print("", dirName, "", data, opt.ToStdout, opt.GenerateJSON, f, opt.Provider)
if err != nil {
return errors.Wrap(err, "transformer.Print failed")
data, err := marshal(versionedObject, opt.GenerateJSON, opt.YAMLIndent)
if err != nil {
return fmt.Errorf("error in marshalling the List: %v", err)
}
// this part add --- which unifies the file
data = []byte(fmt.Sprintf("---\n%s", data))
printVal, err := transformer.Print("", dirName, "", data, opt.ToStdout, opt.GenerateJSON, f, opt.Provider)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good, but you need to add comments "this part add --- which unifies the file", etc.

if err != nil {
return errors.Wrap(err, "transformer to print to one single file failed")
}
files = append(files, printVal)
}
files = append(files, printVal)
} else {
finalDirName := dirName
if opt.CreateChart {
Expand Down
14 changes: 6 additions & 8 deletions script/test/cmd/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,11 @@ function convert::match_output() {
convert::run_cmd $cmd
exit_status=$?
if [ $exit_status -ne 0 ]; then FAIL_MSGS=$FAIL_MSGS"exit status: $exit_status\n"; return $exit_status; fi

match=$(jq --argfile a $TEMP_STDOUT --argfile b $expected_output -n 'def post_recurse(f): def r: (f | select(. != null) | r), .; r; def post_recurse: post_recurse(.[]?); ($a | (post_recurse | arrays) |= sort) as $a | ($b | (post_recurse | arrays) |= sort) as $b | $a == $b')
$cmd > /tmp/test.json
diff /tmp/test.json $expected_output > /tmp/diff
rm /tmp/test.json
if [ "$match" = true ]; then SUCCESS_MSGS=$SUCCESS_MSGS"converted output matches\n"; return 0;
else FAIL_MSGS=$FAIL_MSGS"converted output does not match\n"; cat /tmp/diff; rm /tmp/diff; return 1; fi
match=$(diff <(yq -P 'sort_keys(....)' $expected_output) <(yq -P 'sort_keys(....)' $TEMP_STDOUT))
echo "$match" > /tmp/diff
if [ "$match" == "" ]; then SUCCESS_MSGS=$SUCCESS_MSGS"converted output matches\n"; return 0;
else FAIL_MSGS=$FAIL_MSGS"converted output does not match\n"; cat /tmp/diff; rm /tmp/diff; return 1;
fi
}
readonly -f convert::match_output

Expand Down Expand Up @@ -221,4 +219,4 @@ function convert::check_artifacts_generated() {
convert::teardown
return $exit_status
}
readonly -f convert::check_artifacts_generated
readonly -f convert::check_artifacts_generated
141 changes: 73 additions & 68 deletions script/test/cmd/tests_new.sh

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.service.type: headless
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
spec:
clusterIP: None
ports:
- name: headless
port: 55555
targetPort: 0
selector:
io.kompose.service: redis
status:
loadBalancer: {}

---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
name: web
spec:
ports:
- name: "5000"
port: 5000
targetPort: 5000
selector:
io.kompose.service: web
status:
loadBalancer: {}

---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.service.type: headless
creationTimestamp: null
labels:
io.kompose.service: redis
name: redis
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: redis
strategy: {}
template:
metadata:
annotations:
kompose.service.type: headless
creationTimestamp: null
labels:
io.kompose.service: redis
spec:
containers:
- image: redis
name: redis
resources: {}
restartPolicy: Always
status: {}

---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
name: web
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: web
strategy:
type: Recreate
template:
metadata:
creationTimestamp: null
labels:
io.kompose.service: web
spec:
containers:
- args:
- python
- app.py
image: flask_web
name: web
ports:
- containerPort: 5000
resources: {}
volumeMounts:
- mountPath: /code
name: code-volume
restartPolicy: Always
volumes:
- emptyDir: {}
name: code-volume
status: {}
Loading