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

Don't check if trial's metadata is in spec.parameters #1848

Merged
merged 6 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
24 changes: 20 additions & 4 deletions pkg/webhook/v1beta1/experiment/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,7 @@ func (g *DefaultValidator) validateTrialTemplate(instance *experimentsv1beta1.Ex

// Check if parameter reference exist in experiment parameters
if len(experimentParameterNames) > 0 {
// Check if parameter is trial metadata
regex := regexp.MustCompile(consts.TrialTemplateMetaReplaceFormatRegex)
match := regex.FindStringSubmatch(parameter.Reference)
if !(len(match) > 0 && contains(consts.TrialTemplateMetaKeys, match[1])) {
if !isMetaKey(parameter.Reference) {
if _, ok := experimentParameterNames[parameter.Reference]; !ok {
return fmt.Errorf("parameter reference %v does not exist in spec.parameters: %v", parameter.Reference, instance.Spec.Parameters)
}
Expand Down Expand Up @@ -487,6 +484,25 @@ func (g *DefaultValidator) validateMetricsCollector(inst *experimentsv1beta1.Exp
return nil
}

func isMetaKey(parameter string) bool {
// Check if parameter is trial metadata reference as ${trailSpec.Name}, ${trialSpec.Labels[label]}, etc. used for substitution
match := regexp.MustCompile(consts.TrialTemplateMetaReplaceFormatRegex).FindStringSubmatch(parameter)
isMeta := false
if len(match) > 0 {
matchedKey := match[1]
if contains(consts.TrialTemplateMetaKeys, matchedKey) {
isMeta = true
} else {
// Check if it's Labels[label] or Annotations[annotation]
subMatch := regexp.MustCompile(consts.TrialTemplateMetaParseFormatRegex).FindStringSubmatch(matchedKey)
if len(subMatch) == 3 && contains(consts.TrialTemplateMetaKeys, subMatch[1]) {
isMeta = true
}
}
}
return isMeta
}

func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
Expand Down
22 changes: 22 additions & 0 deletions pkg/webhook/v1beta1/experiment/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ spec:
validTemplate5 := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(validJobStr, nil)
validTemplate6 := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(validJobStr, nil)
validTemplate7 := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(validJobStr, nil)
validTemplate8 := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(validJobStr, nil)
validTemplate9 := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(validJobStr, nil)

missedParameterTemplate := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(missedParameterJobStr, nil)
oddParameterTemplate := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(oddParameterJobStr, nil)
Expand All @@ -433,6 +435,8 @@ spec:
validTemplate5,
validTemplate6,
validTemplate7,
validTemplate8,
validTemplate9,
missedParameterTemplate,
oddParameterTemplate,
invalidParameterTemplate,
Expand Down Expand Up @@ -582,6 +586,24 @@ spec:
Err: false,
testDescription: "Trial template contains Trial metadata reference as parameter",
},
Copy link
Member

@tenzen-y tenzen-y Apr 14, 2022

Choose a reason for hiding this comment

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

Could you also add a test case for Labels or Annotations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi @tenzen-y , I've added handing of those cases and tests, thanks!

{
Instance: func() *experimentsv1beta1.Experiment {
i := newFakeInstance()
i.Spec.TrialTemplate.TrialParameters[1].Reference = "${trialSpec.Annotations[test-annotation]}"
return i
}(),
Err: false,
testDescription: "Trial template contains Trial annotation reference as parameter",
},
{
Instance: func() *experimentsv1beta1.Experiment {
i := newFakeInstance()
i.Spec.TrialTemplate.TrialParameters[1].Reference = "${trialSpec.Labels[test-label]}"
return i
}(),
Err: false,
testDescription: "Trial template contains Trial's label reference as parameter",
},
// Trial Template doesn't contain parameter from trialParameters
// missedParameterTemplate case
{
Expand Down