Skip to content

Commit

Permalink
flatten metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
mclarke47 authored and dthomson25 committed Jul 14, 2020
1 parent d3a61a8 commit 93003e4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 23 deletions.
35 changes: 23 additions & 12 deletions experiments/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,25 +497,36 @@ func (ec *experimentContext) newAnalysisRun(analysis v1alpha1.ExperimentAnalysis
if err != nil {
return nil, err
}
name := fmt.Sprintf("%s-%s", ec.ex.Name, analysis.Name)

run, err := analysisutil.NewAnalysisRunFromClusterTemplate(clusterTemplate, args, name, "", ec.ex.Namespace)
if err != nil {
return nil, err
}
instanceID := analysisutil.GetInstanceID(ec.ex)
if instanceID != "" {
run.Labels = map[string]string{v1alpha1.LabelKeyControllerInstanceID: ec.ex.Labels[v1alpha1.LabelKeyControllerInstanceID]}
}
run.OwnerReferences = []metav1.OwnerReference{*metav1.NewControllerRef(ec.ex, controllerKind)}
return run, nil
} else {
template, err := ec.analysisTemplateLister.AnalysisTemplates(ec.ex.Namespace).Get(analysis.TemplateName)
if err != nil {
return nil, err
}
}
// TODO same as analysis.go
name := fmt.Sprintf("%s-%s", ec.ex.Name, analysis.Name)
name := fmt.Sprintf("%s-%s", ec.ex.Name, analysis.Name)

run, err := analysisutil.NewAnalysisRunFromTemplate(template, args, name, "", ec.ex.Namespace)
if err != nil {
return nil, err
}
instanceID := analysisutil.GetInstanceID(ec.ex)
if instanceID != "" {
run.Labels = map[string]string{v1alpha1.LabelKeyControllerInstanceID: ec.ex.Labels[v1alpha1.LabelKeyControllerInstanceID]}
run, err := analysisutil.NewAnalysisRunFromTemplate(template, args, name, "", ec.ex.Namespace)
if err != nil {
return nil, err
}
instanceID := analysisutil.GetInstanceID(ec.ex)
if instanceID != "" {
run.Labels = map[string]string{v1alpha1.LabelKeyControllerInstanceID: ec.ex.Labels[v1alpha1.LabelKeyControllerInstanceID]}
}
run.OwnerReferences = []metav1.OwnerReference{*metav1.NewControllerRef(ec.ex, controllerKind)}
return run, nil
}
run.OwnerReferences = []metav1.OwnerReference{*metav1.NewControllerRef(ec.ex, controllerKind)}
return run, nil
}

// verifyAnalysisTemplate verifies an AnalysisTemplate. For now, it simply means that it exists
Expand Down
5 changes: 1 addition & 4 deletions rollout/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,7 @@ func (c *Controller) newAnalysisRunFromRollout(roCtx rolloutContext, rolloutAnal
}

}
// TODO should we create a separate run?
// or flatten the cluster templates into the namespaced templates?
// or disallow mixing and matching namespaced and cluster templates (I'd prefer not to)
run, err = analysisutil.NewAnalysisRunFromTemplates(templates, args, name, "", r.Namespace)
run, err = analysisutil.NewAnalysisRunFromTemplates(templates, clusterTemplates, args, name, "", r.Namespace)
if err != nil {
return nil, err
}
Expand Down
62 changes: 55 additions & 7 deletions utils/analysis/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ func CreateWithCollisionCounter(logCtx *log.Entry, analysisRunIf argoprojclient.
}
}

func NewAnalysisRunFromTemplates(templates []*v1alpha1.AnalysisTemplate, args []v1alpha1.Argument, name, generateName, namespace string) (*v1alpha1.AnalysisRun, error) {
template, err := FlattenTemplates(templates)
func NewAnalysisRunFromTemplates(templates []*v1alpha1.AnalysisTemplate, clusterTemplates []*v1alpha1.ClusterAnalysisTemplate, args []v1alpha1.Argument, name, generateName, namespace string) (*v1alpha1.AnalysisRun, error) {
template, err := FlattenTemplates(templates, clusterTemplates)
if err != nil {
return nil, err
}
Expand All @@ -216,12 +216,12 @@ func NewAnalysisRunFromTemplates(templates []*v1alpha1.AnalysisTemplate, args []
return &ar, nil
}

func FlattenTemplates(templates []*v1alpha1.AnalysisTemplate) (*v1alpha1.AnalysisTemplate, error) {
metrics, err := flattenMetrics(templates)
func FlattenTemplates(templates []*v1alpha1.AnalysisTemplate, clusterTemplates []*v1alpha1.ClusterAnalysisTemplate) (*v1alpha1.AnalysisTemplate, error) {
metrics, err := flattenMetrics(templates, clusterTemplates)
if err != nil {
return nil, err
}
args, err := flattenArgs(templates)
args, err := flattenArgs(templates, clusterTemplates)
if err != nil {
return nil, err
}
Expand All @@ -233,7 +233,7 @@ func FlattenTemplates(templates []*v1alpha1.AnalysisTemplate) (*v1alpha1.Analysi
}, nil
}

func flattenArgs(templates []*v1alpha1.AnalysisTemplate) ([]v1alpha1.Argument, error) {
func flattenArgs(templates []*v1alpha1.AnalysisTemplate, clusterTemplates []*v1alpha1.ClusterAnalysisTemplate) ([]v1alpha1.Argument, error) {
argsMap := map[string]v1alpha1.Argument{}
for i := range templates {
args := templates[i].Spec.Args
Expand All @@ -252,6 +252,23 @@ func flattenArgs(templates []*v1alpha1.AnalysisTemplate) ([]v1alpha1.Argument, e
argsMap[arg.Name] = arg
}
}
for i := range clusterTemplates {
args := clusterTemplates[i].Spec.Args
for j := range args {
arg := args[j]
if storedArg, ok := argsMap[arg.Name]; ok {
if arg.Value != nil && storedArg.Value != nil && *arg.Value != *storedArg.Value {
return nil, fmt.Errorf("two args with the same name have the different values: arg %s", arg.Name)
}
// If the controller have a storedArg with a non-nul value, the storedArg should not be replaced by
// the arg with a nil value
if storedArg.Value != nil {
continue
}
}
argsMap[arg.Name] = arg
}
}
if len(argsMap) == 0 {
return nil, nil
}
Expand All @@ -263,7 +280,7 @@ func flattenArgs(templates []*v1alpha1.AnalysisTemplate) ([]v1alpha1.Argument, e
return args, nil
}

func flattenMetrics(templates []*v1alpha1.AnalysisTemplate) ([]v1alpha1.Metric, error) {
func flattenMetrics(templates []*v1alpha1.AnalysisTemplate, clusterTemplates []*v1alpha1.ClusterAnalysisTemplate) ([]v1alpha1.Metric, error) {
metricMap := map[string]v1alpha1.Metric{}
for i := range templates {
metrics := templates[i].Spec.Metrics
Expand All @@ -276,6 +293,17 @@ func flattenMetrics(templates []*v1alpha1.AnalysisTemplate) ([]v1alpha1.Metric,
}
}
}
for i := range clusterTemplates {
metrics := clusterTemplates[i].Spec.Metrics
for j := range metrics {
metric := metrics[j]
if _, ok := metricMap[metric.Name]; !ok {
metricMap[metric.Name] = metric
} else {
return nil, fmt.Errorf("two metrics have the same name %s", metric.Name)
}
}
}
metrics := make([]v1alpha1.Metric, 0, len(metricMap))
for name := range metricMap {
metric := metricMap[name]
Expand All @@ -284,6 +312,26 @@ func flattenMetrics(templates []*v1alpha1.AnalysisTemplate) ([]v1alpha1.Metric,
return metrics, nil
}

//TODO(dthomson) remove v0.9.0
func NewAnalysisRunFromClusterTemplate(template *v1alpha1.ClusterAnalysisTemplate, args []v1alpha1.Argument, name, generateName, namespace string) (*v1alpha1.AnalysisRun, error) {
newArgs, err := MergeArgs(args, template.Spec.Args)
if err != nil {
return nil, err
}
ar := v1alpha1.AnalysisRun{
ObjectMeta: metav1.ObjectMeta{
Name: name,
GenerateName: generateName,
Namespace: namespace,
},
Spec: v1alpha1.AnalysisRunSpec{
Metrics: template.Spec.Metrics,
Args: newArgs,
},
}
return &ar, nil
}

//TODO(dthomson) remove v0.9.0
func NewAnalysisRunFromTemplate(template *v1alpha1.AnalysisTemplate, args []v1alpha1.Argument, name, generateName, namespace string) (*v1alpha1.AnalysisRun, error) {
newArgs, err := MergeArgs(args, template.Spec.Args)
Expand Down

0 comments on commit 93003e4

Please sign in to comment.