Skip to content

Commit

Permalink
Allow ex to use availableAt and end Time as args
Browse files Browse the repository at this point in the history
  • Loading branch information
dthomson25 committed Feb 7, 2020
1 parent beeeb56 commit 2174523
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/features/analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ spec:
templateName: mann-whitney
args:
- name: start-time
value: 2019-09-14T01:40:10Z
value: "{{experiment.availableAtTime}}"
- name: end-time
value: 2019-09-14T02:40:10Z
value: "{{experiment.endAtTime}}"
```


Expand Down
15 changes: 14 additions & 1 deletion utils/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"strconv"
"strings"
"time"

"github.com/valyala/fasttemplate"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -16,15 +17,27 @@ const (
openBracket = "{{"
closeBracket = "}}"
experimentPodTemplateHash = "templates.%s.podTemplateHash"
experimentAvailableAt = "experiment.availableAtTime"
experimentEndsAt = "experiment.endsAtTime"
)

// ResolveExperimentArgValue substitutes values from the experiment (i.e. a template's pod hash) in the args value field
// ResolveExperimentArgsValue substitutes values from the experiment (i.e. a template's pod hash) in the args value field
func ResolveExperimentArgsValue(argTemplate string, ex *v1alpha1.Experiment, templateRSs map[string]*appsv1.ReplicaSet) (string, error) {
t, err := fasttemplate.NewTemplate(argTemplate, openBracket, closeBracket)
if err != nil {
return "", err
}
argsMap := make(map[string]string)
if ex.Status.AvailableAt != nil {
argsMap[experimentAvailableAt] = ex.Status.AvailableAt.Format(time.RFC3339)
if ex.Spec.Duration != "" {
duration, err := ex.Spec.Duration.Duration()
if err != nil {
return "", err
}
argsMap[experimentEndsAt] = ex.Status.AvailableAt.Add(duration).Format(time.RFC3339)
}
}
for _, template := range ex.Spec.Templates {
if rs, ok := templateRSs[template.Name]; ok {
argsMap[fmt.Sprintf(experimentPodTemplateHash, template.Name)] = rs.Labels[v1alpha1.DefaultRolloutUniqueLabelKey]
Expand Down
27 changes: 27 additions & 0 deletions utils/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package query
import (
"fmt"
"testing"
"time"

"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -17,13 +18,32 @@ func TestResolveExperimentArgsValueInvalidTemplate(t *testing.T) {
assert.Equal(t, fmt.Errorf("Cannot find end tag=\"}}\" in the template=\"test-{{args.var\" starting from \"args.var\""), err)
}

func TestResolveExperimentArgsValueInvalidDuration(t *testing.T) {
now := metav1.Now()
ex := &v1alpha1.Experiment{
Spec: v1alpha1.ExperimentSpec{
Duration: "asdf",
},
Status: v1alpha1.ExperimentStatus{
AvailableAt: &now,
},
}
_, err := ResolveExperimentArgsValue("test", ex, nil)
assert.Equal(t, fmt.Errorf("time: invalid duration asdf"), err)
}

func TestResolveExperimentArgsValue(t *testing.T) {
now := metav1.Now()
ex := &v1alpha1.Experiment{
Spec: v1alpha1.ExperimentSpec{
Duration: "1m",
Templates: []v1alpha1.TemplateSpec{{
Name: "test",
}},
},
Status: v1alpha1.ExperimentStatus{
AvailableAt: &now,
},
}
rsMap := map[string]*appsv1.ReplicaSet{
"test": {
Expand All @@ -37,6 +57,13 @@ func TestResolveExperimentArgsValue(t *testing.T) {
argValue, err := ResolveExperimentArgsValue("{{templates.test.podTemplateHash}}", ex, rsMap)
assert.Nil(t, err)
assert.Equal(t, "abcd", argValue)
argValue, err = ResolveExperimentArgsValue("{{experiment.availableAtTime}}", ex, rsMap)
assert.Nil(t, err)
assert.Equal(t, now.Format(time.RFC3339), argValue)
argValue, err = ResolveExperimentArgsValue("{{experiment.endsAtTime}}", ex, rsMap)
assert.Nil(t, err)
assert.Equal(t, now.Add(1*time.Minute).Format(time.RFC3339), argValue)

}

func TestResolveArgsWithNoSubstitution(t *testing.T) {
Expand Down

0 comments on commit 2174523

Please sign in to comment.