Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Kedge will read input from stdin #450

Merged
merged 1 commit into from
Jan 30, 2018
Merged

Conversation

surajnarwade
Copy link
Collaborator

Resolves #183

For example,

$ cat mariadb.yaml | kedge generate -f -
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    app: database
  name: database
...
...

@cdrage
Copy link
Collaborator

cdrage commented Nov 16, 2017

Please write integration / unit tests 👍

pkg/cmd/util.go Outdated
if err != nil {
return nil, errors.Wrapf(err, "can't list *.yml files in %s", path)
return nil, errors.Wrapf(err, "can't get file info about %s", path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

can't retrieve file information about

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

github is messed up here, I didn't change anything

Copy link
Collaborator

Choose a reason for hiding this comment

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

I know, but the correct grammar is can't retrieve file information about %s

Copy link
Member

@surajssd surajssd left a comment

Choose a reason for hiding this comment

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

When working with single file it works fine. Like this one works pretty well:

$ cat db.yaml | kedge generate -f - | grep -B 3 "kind"                                  
---
apiVersion: v1
kind: PersistentVolumeClaim
--
status: {}
---
apiVersion: v1
kind: Service
--
  loadBalancer: {}
---
apiVersion: extensions/v1beta1
kind: Deployment

But when I give multiple files, it does not work

$ cat *.yaml | kedge generate -f - | grep -B 3 "kind" 
---
apiVersion: v1
kind: PersistentVolumeClaim
--
status: {}
---
apiVersion: v1
kind: Service
--
  loadBalancer: {}
---
apiVersion: extensions/v1beta1
kind: Deployment

It is still reading only one file.

The right output for $ cat *.yaml | kedge generate -f - | grep -B 3 "kind" should match following.

$ kedge generate -f . | grep -B 3 "kind" 
---
apiVersion: v1
kind: PersistentVolumeClaim
--
status: {}
---
apiVersion: v1
kind: Service
--
  loadBalancer: {}
---
apiVersion: extensions/v1beta1
kind: Deployment
--
status: {}
---
apiVersion: v1
kind: Service
--
  loadBalancer: {}
---
apiVersion: extensions/v1beta1
kind: Deployment

@surajnarwade
Copy link
Collaborator Author

@surajssd , if you put --- in every file that you are feeding to kedge, it will give you output as you expected

@cdrage
Copy link
Collaborator

cdrage commented Nov 20, 2017

@surajnarwade Yes, but that's not what's intended nor originally used... You will have to update your function in order to facilitate having multiple files.

@kadel
Copy link
Member

kadel commented Nov 21, 2017

@surajssd , if you put --- in every file that you are feeding to kedge, it will give you output as you expected

that is not user friendly, we need another solution

@surajnarwade
Copy link
Collaborator Author

@kadel how it will distinguish between two files since it's stdin ?

@surajnarwade
Copy link
Collaborator Author

@surajssd @kadel @cdrage , I have updated PR as per @surajssd suggestion, can you please have a look

pkg/cmd/util.go Outdated
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {

if strings.Contains(scanner.Text(), "name") && !strings.Contains(scanner.Text(), "-") && !strings.HasPrefix(scanner.Text(), " ") {
Copy link
Member

Choose a reason for hiding this comment

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

hmm, Interesting solution.
Maybe this could be a little bit more elegant if you use regexp instead of 3 tests with string ;-)

We should make sure that there are no unintentional consequences when we are using name for detecting that new file begun. Like for example what happens if name is at the end of the file? That is perfectly valid.

@surajssd
Copy link
Member

I think we have been over this discussion before of "how to make sure the given kedge format is valid one?".

I think what @surajnarwade is saying is correct there is no way to find out the separation of various kedge files, unless user is writing the yaml files in valid way.

So accoding to yaml specification:

YAML uses three dashes (“---”) to separate directives from document content.

source: http://www.yaml.org/spec/1.2/spec.html#id2760395

So it is user's responsibility to delimit multiple files being streamed in via STDIN

@surajnarwade
Copy link
Collaborator Author

@surajssd @kadel , Updated PR, can you please test it again.
@cdrage I didn't find any proper documentation for how to write test for such stdin cases (as in functional/unit/integration test)

@cdrage
Copy link
Collaborator

cdrage commented Nov 23, 2017

@surajnarwade
Copy link
Collaborator Author

surajnarwade commented Nov 28, 2017

@cdrage , I tried that solution but didn't work for me :(
I tried,

    subproc := exec.Command("cat kedge.yml | kedge generate -f -")
    output, _ := subproc.Output()

I am bit confused over it.

@cdrage
Copy link
Collaborator

cdrage commented Nov 28, 2017

@surajnarwade it's not a direct solution, but rather a way to investigate on how to implement it. You would use:

Some pseudocode:

    kedgeFile := `
name: httpd

containers:
- image: centos/httpd

services:
- name: httpd
  type: LoadBalancer
  portMappings: 
    - 8080:80
`
    subproc := exec.Command("cat kedge.yml | ./kedge generate -f -")
    output, _ := subproc.Output()
    if output != kedgeFile {
      return nil
  }

and compare the output(s).

What errors are you getting too?

It'll take some trial-and-error, but I believe it's do-able, let me know if you need any help 👍

@surajnarwade surajnarwade changed the title Kedge will read input from stdin [WIP]Kedge will read input from stdin Dec 11, 2017
@surajnarwade surajnarwade force-pushed the stdin branch 7 times, most recently from fd481a9 to f665e79 Compare December 12, 2017 06:56
@surajnarwade surajnarwade changed the title [WIP]Kedge will read input from stdin Kedge will read input from stdin Dec 12, 2017
@cdrage
Copy link
Collaborator

cdrage commented Dec 13, 2017

New tests LGTM 👍 Code LGTM 👍

Copy link
Collaborator

@cdrage cdrage left a comment

Choose a reason for hiding this comment

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

code lgtm other than small nitpicks

.travis.yml Outdated
@@ -25,6 +25,7 @@ install:
- true

script:
- make bin
Copy link
Collaborator

Choose a reason for hiding this comment

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

why are we adding this? kedge is getting installed anyways within travis 👎

pkg/cmd/util.go Outdated
if err != nil {
return nil, errors.Wrapf(err, "can't list *.yml files in %s", path)
return nil, errors.Wrapf(err, "can't get file info about %s", path)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know, but the correct grammar is can't retrieve file information about %s

@surajnarwade surajnarwade force-pushed the stdin branch 2 times, most recently from 8003138 to 57d4b5f Compare December 14, 2017 08:20
@surajnarwade
Copy link
Collaborator Author

@cdrage all done

return nil, errors.Wrapf(err, "cannot determine the absolute file path of %q", file)

if file == "-" {
data, err = ioutil.ReadAll(os.Stdin)
Copy link
Collaborator

Choose a reason for hiding this comment

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

err is not handled here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added error handling :)

@surajnarwade
Copy link
Collaborator Author

@containscafeine @cdrage @kadel all green

@kadel
Copy link
Member

kadel commented Jan 4, 2018

looks good, but needs rebase

@surajnarwade
Copy link
Collaborator Author

@kadel @cdrage done with rebase , good to go

Copy link
Member

@kadel kadel left a comment

Choose a reason for hiding this comment

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

LGTM, but this definitely needs second review

@surajnarwade
Copy link
Collaborator Author

@cdrage , review needed :)

@cdrage
Copy link
Collaborator

cdrage commented Jan 11, 2018

@surajnarwade code LGTM. one last rebase and let's merge

Resolves kedgeproject#183

For example,

```
$ cat mariadb.yaml | kedge generate -f -
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    app: database
  name: database
...
...
```
@surajnarwade
Copy link
Collaborator Author

@containscafeine final review please and then it's ready to merge

@concaf concaf merged commit b86dfbc into kedgeproject:master Jan 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants