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

Enhances how we're handling sourcing #162

Merged
merged 2 commits into from
Feb 13, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions pkg/config/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,24 @@ func (m *Manifest) SliceElem(index int, val reflect.Value) error {
return nil
}

// There are two cases possible in the YAML. The first is when the element in the slice is a string. That might look like:
// install:
// - description: "Install Hello World"
// exec:
// command: bash
// arguments:
// - -c
// - "source: bundle.parameters.command"
Copy link
Member

Choose a reason for hiding this comment

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

I don't want to make this harder, because I agree that we need to rethink how we do sourcing anyway. But when I look at the two here,

  • "source: bundle.parameters.command"
  • source: bundle.parameters.command

My reaction is that the first one shouldn't even be parsed by porter and should be ignored really. It's only the second that matches how source works everywhere else.

Does that simplify this PR? If so, can we nix the first case "source as a string"? If it makes it harder, then leave it as it. 😀

// The second case is when the declaration interpreted to represent a Map. This is the case when there are no quotes:
// install:
// - description: "Install Hello World"
// exec:
// command: bash
// arguments:
// - -c
// - source: bundle.parameters.command
// Branching logic below first checks the easy case (a string) then falls through into the case where we have a map with a single key.
// This is fairly similar to how we process map elements, but the replacement is done differently.
v, ok := val.Interface().(string)
if ok {
//if the array entry is a string that matches source:...., we should replace it
Expand All @@ -559,6 +577,34 @@ func (m *Manifest) SliceElem(index int, val reflect.Value) error {
}
val.Set(reflect.ValueOf(r))
}
} else {
v := val
if val.Kind() == reflect.Interface {
val = val.Elem()
}
if kind := val.Kind(); kind == reflect.Map {
if len(val.MapKeys()) == 1 {
sk := val.MapKeys()[0]
if sk.Kind() == reflect.Interface {
sk = sk.Elem()
}
//if the key is a string, and the string is source, then we should try
Copy link
Member

Choose a reason for hiding this comment

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

Like if we stop supporting - "source: bundle...." does this entire block go away?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the opposite. This got added to support the other case where there are no quotes.

This is the part that handles the quotes:

v, ok := val.Interface().(string)
	if ok {
		//if the array entry is a string that matches source:...., we should replace it
		re := regexp.MustCompile("source:\\s?(.*)")
		matches := re.FindStringSubmatch(v)
		if len(matches) > 0 {
			source := matches[1]
			r, err := m.resolveValue(source)
			if err != nil {
				return errors.Wrap(err, "unable to source value")
			}
			val.Set(reflect.ValueOf(r))
		}
	}

Copy link
Member

Choose a reason for hiding this comment

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

Oh noes, I've been found out. I have a really hard time reading this reflection code.

I'm ok merging this as-is. Up to you if you think we can just remove the "source: bundle...." case and make things simpler or not.

//and replace this
if sk.Kind() == reflect.String && sk.String() == "source" {
kv := val.MapIndex(sk)
if kv.Kind() == reflect.Interface {
kv = kv.Elem()
value := kv.String()
replacement, err := m.resolveValue(value)
if err != nil {
errors.Wrap(err, fmt.Sprintf("unable to resolve value for key: %s", value))
}
v.Set(reflect.ValueOf(replacement))
}
}
}
} // it's not a map, we shouldn't try and process it.

}
return nil
}
Expand Down
24 changes: 24 additions & 0 deletions pkg/config/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,30 @@ func TestResolveArray(t *testing.T) {
assert.Equal(t, "Ralpha", args[0])
}

func TestResolveSliceWithAMap(t *testing.T) {
c := NewTestConfig(t)
c.SetupPorterHome()

c.TestContext.AddTestFile("testdata/slice-test.yaml", Name)

require.NoError(t, c.LoadManifest())

installStep := c.Manifest.Install[0]

os.Setenv("COMMAND", "echo hello world")
err := c.Manifest.ResolveStep(installStep)
assert.NoError(t, err)

assert.NotNil(t, installStep.Data)
t.Logf("install data %v", installStep.Data)
exec := installStep.Data["exec"].(map[interface{}]interface{})
assert.NotNil(t, exec)
args := exec["arguments"].([]interface{})
assert.Len(t, args, 2)
assert.Equal(t, "echo hello world", args[1])
assert.NotNil(t, args)
}

func TestDependency_Validate_NameRequired(t *testing.T) {
c := NewTestConfig(t)
c.SetupPorterHome()
Expand Down
29 changes: 29 additions & 0 deletions pkg/config/testdata/slice-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mixins:
- exec

name: HELLO
version: 0.1.0
description: "An example Porter configuration"
invocationImage: jeremyrickard/porter-hello:latest

parameters:
- name: command
type: string
default: "echo Hello World"

install:
- description: "Install Hello World"
exec:
command: bash
arguments:
- -c
- source: bundle.parameters.command
Copy link
Member

Choose a reason for hiding this comment

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

This looks natural to me, based on how we do source elsewhere.

Ok I've said it 3 ways. I'll stop now. 😇

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, to support this form, the change i added is needed. Both this and "source:...." are valid YAML, so I think we should support both for right now.

We should probably fix this in general though..but also:

arguments: 
   - asdadadad
   - {bundle.params.blah}

will probably have similar stupid YAML processing outcomes for example, that block above gets turned into:

args: 
  - blah
  - 
    bundle.parameters.blah: ~

by a validator, which is even worse. I think in that case it might require quotes to make things simpler.

Copy link
Member

Choose a reason for hiding this comment

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

rawr 🦁 Ok I see why this is hard.

I say ship it and then reasses later.



uninstall:
- description: "Uninstall Hello World"
exec:
command: bash
arguments:
- -c
- echo Goodbye World