Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #482 from thestormforge/constant-parameters
Browse files Browse the repository at this point in the history
Expose constant parameters as assignments
  • Loading branch information
jgustie authored Jun 9, 2022
2 parents 8318115 + 9651a3c commit 723cb6f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
28 changes: 28 additions & 0 deletions internal/experiment/trial_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
optimizev1beta2 "github.com/thestormforge/optimize-controller/v2/api/v1beta2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

// PopulateTrialFromTemplate creates a new trial for an experiment
Expand Down Expand Up @@ -59,4 +60,31 @@ func PopulateTrialFromTemplate(exp *optimizev1beta2.Experiment, t *optimizev1bet
if t.Namespace == "" && exp.Spec.NamespaceSelector == nil && exp.Spec.NamespaceTemplate == nil {
t.Namespace = exp.Namespace
}

// Constant parameters are only part of the experiment, so they must be added here
for _, p := range exp.Spec.Parameters {
if v := ParameterConstant(p); v != nil {
t.Spec.Assignments = append(t.Spec.Assignments, optimizev1beta2.Assignment{
Name: p.Name,
Value: *v,
})
}
}
}

// ParameterConstant returns the value of the supplied parameter if it is constant.
// A constant parameter has a domain which includes exactly 1 value (i.e. min==max
// for numeric, or len(values)==1 for categorical); these parameters are not processed
// on the server.
func ParameterConstant(p optimizev1beta2.Parameter) *intstr.IntOrString {
switch {
case p.Min == p.Max && len(p.Values) == 0:
v := intstr.FromInt(int(p.Max))
return &v
case len(p.Values) == 1:
v := intstr.FromString(p.Values[0])
return &v
default:
return nil
}
}
3 changes: 2 additions & 1 deletion internal/server/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"

optimizev1beta2 "github.com/thestormforge/optimize-controller/v2/api/v1beta2"
"github.com/thestormforge/optimize-controller/v2/internal/experiment"
"github.com/thestormforge/optimize-go/pkg/api"
experimentsv1alpha1 "github.com/thestormforge/optimize-go/pkg/api/experiments/v1alpha1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -52,7 +53,7 @@ func parameters(exp *optimizev1beta2.Experiment) []experimentsv1alpha1.Parameter

for _, p := range exp.Spec.Parameters {
// This is a special case to omit parameters client side
if p.Min == p.Max && len(p.Values) == 0 {
if experiment.ParameterConstant(p) != nil {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestFromCluster(t *testing.T) {
Parameters: []optimizev1beta2.Parameter{
{Name: "one", Min: 0, Max: 1, Baseline: &one},
{Name: "two", Min: 0, Max: 2, Baseline: &two},
{Name: "three", Values: []string{"three"}, Baseline: &three},
{Name: "three", Values: []string{"three", "four"}, Baseline: &three},
},
},
},
Expand All @@ -241,7 +241,7 @@ func TestFromCluster(t *testing.T) {
{
Type: experimentsv1alpha1.ParameterTypeCategorical,
Name: "three",
Values: []string{"three"},
Values: []string{"three", "four"},
},
},
},
Expand Down

0 comments on commit 723cb6f

Please sign in to comment.