Skip to content

Commit

Permalink
Merge pull request #149 from maysunfaisal/finetune-read-k8s-1
Browse files Browse the repository at this point in the history
Filesystem is not required when reading from URL and Data
  • Loading branch information
maysunfaisal authored Oct 17, 2022
2 parents 8b432ca + a05b6f3 commit 4aa3581
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
9 changes: 7 additions & 2 deletions pkg/devfile/parser/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package parser

import (
"bytes"
"fmt"
"io"

"github.com/devfile/library/pkg/util"
Expand Down Expand Up @@ -53,8 +54,9 @@ type KubernetesResources struct {
// ReadKubernetesYaml reads a yaml Kubernetes file from either the Path, URL or Data provided.
// It returns all the parsed Kubernetes objects as an array of interface.
// Consumers interested in the Kubernetes resources are expected to Unmarshal
// it to the struct of the respective Kubernetes resource.
func ReadKubernetesYaml(src YamlSrc, fs afero.Afero) ([]interface{}, error) {
// it to the struct of the respective Kubernetes resource. If a Path is being passed,
// provide a filesystem, otherwise nil can be passed in
func ReadKubernetesYaml(src YamlSrc, fs *afero.Afero) ([]interface{}, error) {

var data []byte
var err error
Expand All @@ -65,6 +67,9 @@ func ReadKubernetesYaml(src YamlSrc, fs afero.Afero) ([]interface{}, error) {
return nil, errors.Wrapf(err, "failed to download file %q", src.URL)
}
} else if src.Path != "" {
if fs == nil {
return nil, fmt.Errorf("cannot read from %s because fs passed in was nil", src.Path)
}
data, err = fs.ReadFile(src.Path)
if err != nil {
return nil, errors.Wrapf(err, "failed to read yaml from path %q", src.Path)
Expand Down
22 changes: 15 additions & 7 deletions pkg/devfile/parser/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
tests := []struct {
name string
src YamlSrc
fs afero.Afero
fs *afero.Afero
wantErr bool
wantDeploymentNames []string
wantServiceNames []string
Expand All @@ -84,7 +84,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
src: YamlSrc{
URL: "http://" + serverIP,
},
fs: fs,
fs: nil,
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
wantServiceNames: []string{"service-sample", "service-sample-2"},
wantRouteNames: []string{"route-sample", "route-sample-2"},
Expand All @@ -96,19 +96,27 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
src: YamlSrc{
Path: "../../../tests/yamls/resources.yaml",
},
fs: fs,
fs: &fs,
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
wantServiceNames: []string{"service-sample", "service-sample-2"},
wantRouteNames: []string{"route-sample", "route-sample-2"},
wantIngressNames: []string{"ingress-sample", "ingress-sample-2"},
wantOtherNames: []string{"pvc-sample", "pvc-sample-2"},
},
{
name: "Read the YAML from the Path with no fs passed",
src: YamlSrc{
Path: "../../../tests/yamls/resources.yaml",
},
fs: nil,
wantErr: true,
},
{
name: "Read the YAML from the Data",
src: YamlSrc{
Data: data,
},
fs: fs,
fs: nil,
wantDeploymentNames: []string{"deploy-sample", "deploy-sample-2"},
wantServiceNames: []string{"service-sample", "service-sample-2"},
wantRouteNames: []string{"route-sample", "route-sample-2"},
Expand All @@ -120,23 +128,23 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
src: YamlSrc{
URL: "http://badurl",
},
fs: fs,
fs: nil,
wantErr: true,
},
{
name: "Bad Path",
src: YamlSrc{
Path: "$%^&",
},
fs: fs,
fs: &fs,
wantErr: true,
},
{
name: "Bad Data",
src: YamlSrc{
Data: badData,
},
fs: fs,
fs: nil,
wantErr: true,
},
}
Expand Down

0 comments on commit 4aa3581

Please sign in to comment.