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 the invalid kube yaml causing panic issue #171

Merged
merged 3 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions pkg/devfile/parser/reader.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022 Red Hat, Inc.
// Copyright 2022-2023 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -121,7 +121,12 @@ func ParseKubernetesYaml(values []interface{}) (KubernetesResources, error) {
return KubernetesResources{}, err
}

kubernetesMap := value.(map[string]interface{})
// kubernetesMap := value.(map[string]interface{})
Copy link
Member

Choose a reason for hiding this comment

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

comment

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated

var kubernetesMap map[string]interface{}
err = k8yaml.Unmarshal(byteData, &kubernetesMap)
if err != nil {
return KubernetesResources{}, err
}
kind := kubernetesMap["kind"]

switch kind {
Expand Down
67 changes: 43 additions & 24 deletions pkg/devfile/parser/reader_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2022 Red Hat, Inc.
// Copyright 2022-2023 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,7 +72,9 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
name string
src YamlSrc
fs *afero.Afero
testParseYamlOnly bool
Copy link
Member

Choose a reason for hiding this comment

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

where are we setting this? We are using it but i dont see it being set a value

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if tt.testParseYamlOnly {

wantErr bool
wantParserErr bool
wantDeploymentNames []string
wantServiceNames []string
wantRouteNames []string
Expand Down Expand Up @@ -111,6 +113,14 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
fs: nil,
wantErr: true,
},
{
name: "Bad Path",
src: YamlSrc{
Path: "$%^&",
},
fs: &fs,
wantErr: true,
},
{
name: "Read the YAML from the Data",
src: YamlSrc{
Expand Down Expand Up @@ -147,45 +157,54 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
fs: nil,
wantErr: true,
},
{
name: "Invalid kube yaml Data",
src: YamlSrc{
Data: []byte("invalidyaml"),
},
fs: nil,
wantParserErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
values, err := ReadKubernetesYaml(tt.src, tt.fs)
if (err != nil) != tt.wantErr {
t.Errorf("unexpected error: %v", err)
t.Errorf("unexpected error: %v, wantErr: %v", err, tt.wantErr)
return
}

for _, value := range values {
kubernetesMap := value.(map[string]interface{})

kind := kubernetesMap["kind"]
metadataMap := kubernetesMap["metadata"].(map[string]interface{})
name := metadataMap["name"]

switch kind {
case "Deployment":
assert.Contains(t, tt.wantDeploymentNames, name)
case "Service":
assert.Contains(t, tt.wantServiceNames, name)
case "Route":
assert.Contains(t, tt.wantRouteNames, name)
case "Ingress":
assert.Contains(t, tt.wantIngressNames, name)
default:
assert.Contains(t, tt.wantOtherNames, name)
if tt.testParseYamlOnly {
for _, value := range values {
kubernetesMap := value.(map[string]interface{})

kind := kubernetesMap["kind"]
metadataMap := kubernetesMap["metadata"].(map[string]interface{})
name := metadataMap["name"]

switch kind {
case "Deployment":
assert.Contains(t, tt.wantDeploymentNames, name)
case "Service":
assert.Contains(t, tt.wantServiceNames, name)
case "Route":
assert.Contains(t, tt.wantRouteNames, name)
case "Ingress":
assert.Contains(t, tt.wantIngressNames, name)
default:
assert.Contains(t, tt.wantOtherNames, name)
}
}
}

if len(values) > 0 {
resources, err := ParseKubernetesYaml(values)
if err != nil {
t.Error(err)
if (err != nil) != tt.wantParserErr {
t.Errorf("unexpected error: %v, wantErr: %v", err, tt.wantParserErr)
return
}

if reflect.DeepEqual(resources, KubernetesResources{}) {
if reflect.DeepEqual(resources, KubernetesResources{}) && !tt.wantParserErr {
t.Error("Kubernetes resources is empty, expected to contain some resources")
} else {
deployments := resources.Deployments
Expand Down