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
10 changes: 10 additions & 0 deletions pkg/controller.v1beta1/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,14 @@ var (
DefaultKatibDBManagerServiceIP = env.GetEnvOrDefault(DefaultKatibDBManagerServiceIPEnvName, "katib-db-manager")
// DefaultKatibDBManagerServicePort is the default Port of Katib DB Manager
DefaultKatibDBManagerServicePort = env.GetEnvOrDefault(DefaultKatibDBManagerServicePortEnvName, "6789")

// List of all valid keys of trial metadata for substitution in Trial template
TrialTemplateMetaKeys = []string{
TrialTemplateMetaKeyOfName,
TrialTemplateMetaKeyOfNamespace,
TrialTemplateMetaKeyOfKind,
TrialTemplateMetaKeyOfAPIVersion,
TrialTemplateMetaKeyOfAnnotations,
TrialTemplateMetaKeyOfLabels,
}
)
18 changes: 16 additions & 2 deletions pkg/webhook/v1beta1/experiment/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,13 @@ func (g *DefaultValidator) validateTrialTemplate(instance *experimentsv1beta1.Ex

// Check if parameter reference exist in experiment parameters
if len(experimentParameterNames) > 0 {
if _, ok := experimentParameterNames[parameter.Reference]; !ok {
return fmt.Errorf("parameter reference %v does not exist in spec.parameters: %v", parameter.Reference, instance.Spec.Parameters)
// 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])) {
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.

I think contains(consts.TrialTemplateMetaKeys, match[1]) returns false when parameter.Reference is Anotations or Labels.
WDYT @alexeygorobets ?

- name: trialLabelCustom
description: Trial's job label with custom value
reference: ${trialSpec.Labels[custom-key]}
- name: trialAnnotationCustom
description: Trial's job annotation with custom value
reference: ${trialSpec.Annotations[custom-key]}

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 @@ -481,3 +486,12 @@ func (g *DefaultValidator) validateMetricsCollector(inst *experimentsv1beta1.Exp

return nil
}

func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
12 changes: 12 additions & 0 deletions pkg/webhook/v1beta1/experiment/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ spec:
validTemplate4 := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(validJobStr, nil)
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)

missedParameterTemplate := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(missedParameterJobStr, nil)
oddParameterTemplate := p.EXPECT().GetTrialTemplate(gomock.Any()).Return(oddParameterJobStr, nil)
Expand All @@ -431,6 +432,7 @@ spec:
validTemplate4,
validTemplate5,
validTemplate6,
validTemplate7,
missedParameterTemplate,
oddParameterTemplate,
invalidParameterTemplate,
Expand Down Expand Up @@ -570,6 +572,16 @@ spec:
Err: false,
testDescription: "Trial template contains Trial parameters when spec.parameters is empty",
},
// Trial template contains Trial metadata parameter substitution
{
Instance: func() *experimentsv1beta1.Experiment {
i := newFakeInstance()
i.Spec.TrialTemplate.TrialParameters[1].Reference = "${trialSpec.Name}"
return i
}(),
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!

// Trial Template doesn't contain parameter from trialParameters
// missedParameterTemplate case
{
Expand Down