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(controller): allow to use global params with withParam #2757

Merged
merged 12 commits into from
Apr 23, 2020
Merged

fix(controller): allow to use global params with withParam #2757

merged 12 commits into from
Apr 23, 2020

Conversation

dtaniwaki
Copy link
Member

@dtaniwaki dtaniwaki commented Apr 20, 2020

Checklist:

  • Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this is a chore.

(b)

  • The title of the PR is (a) conventional, (b) states what changed, and (c) suffixes the related issues number. E.g. "fix(controller): Updates such and such. Fixes #1234".
  • I've signed the CLA.
  • I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
  • My builds are green. Try syncing with master if they are not.
  • My organization is added to USERS.md.

@@ -526,7 +526,12 @@ func (woc *wfOperationCtx) resolveDependencyReferences(dagCtx *dagContext, task
return nil, errors.InternalWrapError(err)
}
fstTmpl := fasttemplate.New(string(taskBytes), "{{", "}}")
newTaskStr, err := common.Replace(fstTmpl, scope.replaceMap(), true)

replaceMap := scope.replaceMap()
Copy link
Member

Choose a reason for hiding this comment

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

I think something like

Suggested change
replaceMap := scope.replaceMap()
replaceMap := scope.replaceMap(woc.globalParams)

Would be cleaner.

You could edit the replaceMap function to include this optional parameter and add its contents to the scope if it's not nil

Copy link
Member

Choose a reason for hiding this comment

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

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the method name replaceMap is not intuitive to accept an argument of params extension. Maybe, we can have something like mergeParams utility method in the common package.

Copy link
Member Author

Choose a reason for hiding this comment

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

If we want to introduce mergeParams utility method, we should make another PR to replace other codes meging params.

Copy link
Member

Choose a reason for hiding this comment

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

We could change the name to something like prepareScope, here is an example where I do something similar:

https://github.com/argoproj/argo/blob/7cb2fd17765aad691eda25ca4c5acecb89f84394/workflow/controller/steps.go#L466-L471

This is minor though, I'll leave it up to you.

Copy link
Member Author

Choose a reason for hiding this comment

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

As I found there're many cases of merging params, I defined Params type which has Clone and Merge functions so we can replace those codes with one line.

I also found the order of merging was not correct in my original commit of this PR. scoep.replaceMap() should be merged into globalParmas but not the opposite.

@@ -526,7 +526,12 @@ func (woc *wfOperationCtx) resolveDependencyReferences(dagCtx *dagContext, task
return nil, errors.InternalWrapError(err)
}
fstTmpl := fasttemplate.New(string(taskBytes), "{{", "}}")
newTaskStr, err := common.Replace(fstTmpl, scope.replaceMap(), true)

replaceMap := scope.replaceMap()
Copy link
Member

Choose a reason for hiding this comment

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

We could change the name to something like prepareScope, here is an example where I do something similar:

https://github.com/argoproj/argo/blob/7cb2fd17765aad691eda25ca4c5acecb89f84394/workflow/controller/steps.go#L466-L471

This is minor though, I'll leave it up to you.

@dtaniwaki
Copy link
Member Author

@simster7 Could you review it again?

@@ -0,0 +1,21 @@
package params
Copy link
Member

Choose a reason for hiding this comment

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

This is a Workflow specific util, so it shouldn't be at the top-level util folder. Can you add this to the workflow/common package? You can add a new file there, but it should still be under the common package.

@@ -0,0 +1,21 @@
package params

type Params map[string]string
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
type Params map[string]string
type Parameters map[string]string

Full name is more clear.

@@ -300,19 +301,13 @@ func ProcessArgs(tmpl *wfv1.Template, args wfv1.ArgumentsProvider, globalParams,
}

// SubstituteParams returns a new copy of the template with global, pod, and input parameters substituted
func SubstituteParams(tmpl *wfv1.Template, globalParams, localParams map[string]string) (*wfv1.Template, error) {
func SubstituteParams(tmpl *wfv1.Template, globalParams, localParams params.Params) (*wfv1.Template, error) {
Copy link
Member

Choose a reason for hiding this comment

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

When we move the util file under common, we won't need the import prefix anymore.

params := Params{"foo": "1"}
newParams := params.Clone()
assert.Equal(t, params, newParams)
assert.NotSame(t, &params, &newParams)
Copy link
Member

Choose a reason for hiding this comment

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

Nice, this is important.

return newParams
}

func (ps Params) Clone() Params {
Copy link
Member

Choose a reason for hiding this comment

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

DeepCopy is the name we use for this in Argo.

@@ -14,7 +15,7 @@ type wfScope struct {
}

// replaceMap returns a replacement map of strings intended to be used simple string substitution
func (s *wfScope) replaceMap() map[string]string {
func (s *wfScope) replaceMap() params.Params {
replaceMap := make(map[string]string)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be?

Suggested change
replaceMap := make(map[string]string)
replaceMap := make(params.Params)

@simster7 simster7 assigned simster7 and unassigned sarabala1979 Apr 22, 2020
Copy link
Member

@simster7 simster7 left a comment

Choose a reason for hiding this comment

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

Some more minor comments

@@ -14,8 +15,8 @@ type wfScope struct {
}

// replaceMap returns a replacement map of strings intended to be used simple string substitution
func (s *wfScope) replaceMap() map[string]string {
replaceMap := make(map[string]string)
func (s *wfScope) replaceMap() common.Parameters {
Copy link
Member

Choose a reason for hiding this comment

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

Please rename this function parameters() or getParameters() or something similar

podParams[k] = v
}
func substitutePodParams(pod *apiv1.Pod, globalParams common.Parameters, tmpl *wfv1.Template) (*apiv1.Pod, error) {
podParams := globalParams.Merge()
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be DeepCopy if Merge has not arguments?

Suggested change
podParams := globalParams.Merge()
podParams := globalParams.DeepCopy()

@dtaniwaki
Copy link
Member Author

@simster7 Fixed. Please check it again!

@sonarcloud
Copy link

sonarcloud bot commented Apr 23, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 1 Code Smell

95.2% 95.2% Coverage
0.0% 0.0% Duplication

@simster7 simster7 merged commit e0f2697 into argoproj:master Apr 23, 2020
@simster7
Copy link
Member

Back-ported to 2.7

@dtaniwaki dtaniwaki deleted the fix-global-param-resolution-with-with-param branch April 28, 2020 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants