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

close #307 by removing logic which adds duplicate anchors to local tasks #308

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
99 changes: 73 additions & 26 deletions pkg/components/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ func addAnchorResult(task *tektonv1beta1api.Task) error {
if hasLabel {
return nil
}

for _, step := range task.Spec.Steps {
if step.Name == "anchor" {
return nil
}
}
task.Spec.Results = append(task.Spec.Results, tektonv1beta1api.TaskResult{
Name: "anchor",
Description: "An anchor to allow other tasks to depend on this task.",
Expand Down Expand Up @@ -248,7 +252,6 @@ func addAnchorParameter(task *tektonv1beta1api.Task) error {
return nil
}
}

task.Spec.Params = append(task.Spec.Params, tektonv1beta1api.ParamSpec{
Name: "anchors",
Description: "A list of tasks that this task depends on",
Expand All @@ -272,39 +275,83 @@ func addEnvVarsToTask(task *tektonv1beta1api.Task) error {
if componentType != Producer {
return nil
}
scanIDFound := false
scanStartTimeFound := false
scanTagsFound := false

task.Spec.Params = append(task.Spec.Params, tektonv1beta1api.ParamSpecs{
{
Name: "dracon_scan_id",
Type: tektonv1beta1api.ParamTypeString,
},
{
Name: "dracon_scan_start_time",
Type: tektonv1beta1api.ParamTypeString,
},
{
Name: "dracon_scan_tags",
Type: tektonv1beta1api.ParamTypeString,
},
}...)

for i, step := range task.Spec.Steps {
step.Env = append(step.Env, []corev1.EnvVar{
for _, param := range task.Spec.Params {
if param.Name == "dracon_scan_id" {
scanIDFound = true
} else if param.Name == "dracon_scan_start_time" {
scanStartTimeFound = true
} else if param.Name == "dracon_scan_tags" {
scanTagsFound = true
}
}
if !scanIDFound {
task.Spec.Params = append(task.Spec.Params, tektonv1beta1api.ParamSpecs{
{
Name: "DRACON_SCAN_TIME",
Value: "$(params.dracon_scan_start_time)",
Name: "dracon_scan_id",
Type: tektonv1beta1api.ParamTypeString,
},
}...)
}
if !scanStartTimeFound {
task.Spec.Params = append(task.Spec.Params, tektonv1beta1api.ParamSpecs{
{
Name: "DRACON_SCAN_ID",
Value: "$(params.dracon_scan_id)",
Name: "dracon_scan_start_time",
Type: tektonv1beta1api.ParamTypeString,
},
}...)
}
if !scanTagsFound {
task.Spec.Params = append(task.Spec.Params, tektonv1beta1api.ParamSpecs{
{
Name: "DRACON_SCAN_TAGS",
Value: "$(params.dracon_scan_tags)",
Name: "dracon_scan_tags",
Type: tektonv1beta1api.ParamTypeString,
},
}...)
task.Spec.Steps[i] = step
}

scanIDFound = false
scanStartTimeFound = false
scanTagsFound = false
for i, step := range task.Spec.Steps {
for _, envVar := range step.Env {
if envVar.Name == "DRACON_SCAN_TIME" {
scanStartTimeFound = true
} else if envVar.Name == "DRACON_SCAN_ID" {
scanIDFound = true
} else if envVar.Name == "DRACON_SCAN_TAGS" {
scanTagsFound = true
}
}
if !scanStartTimeFound {
step.Env = append(step.Env, []corev1.EnvVar{
{
Name: "DRACON_SCAN_TIME",
Value: "$(params.dracon_scan_start_time)",
},
}...)
}
if !scanIDFound {
step.Env = append(step.Env, []corev1.EnvVar{
{
Name: "DRACON_SCAN_ID",
Value: "$(params.dracon_scan_id)",
},
}...)
}
if !scanTagsFound {
step.Env = append(step.Env, []corev1.EnvVar{
{
Name: "DRACON_SCAN_TAGS",
Value: "$(params.dracon_scan_tags)",
},
}...)
}

task.Spec.Steps[i] = step
}
return nil
}
100 changes: 100 additions & 0 deletions pkg/components/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -115,3 +116,102 @@ version: %s
require.Equal(t, "anchor", task.Spec.Steps[len(task.Spec.Steps)-1].Name, "task %s has no anchor step", task.Name)
}
}

func TestProcessTasksAnchorsEnvExist(t *testing.T) {
taskList, err := LoadTasks(
context.Background(),
[]string{
"testdata/base/task.yaml",
"testdata/sources/git/task.yaml",
"testdata/producers/cdxgen/task.yaml",
"testdata/producers/aggregator/task.yaml",
"testdata/enrichers/aggregator/task.yaml",
"testdata/consumers/arangodb/task.yaml",
},
)
require.NoError(t, err)

// add anchors and env vars once
require.NoError(t, ProcessTasks(taskList...))

// ensure anchors have been added once
requireAnchors(t, taskList)

// ensure envvars have been added once
requireEnvVars(t, taskList)

// add anchors and env vars again
require.NoError(t, ProcessTasks(taskList...))

// ensure anchors have been added once
requireAnchors(t, taskList)
requireEnvVars(t, taskList)
}

//revive:disable:cyclomatic High complexity score but easy to understand
//revive:disable:cognitive-complexity High complexity score but easy to understand
func requireAnchors(t *testing.T, taskList []*tektonv1beta1api.Task) {
for _, task := range taskList {
require.NotEmpty(t, task.Spec.Steps)
if strings.Contains(task.Name, "base") || strings.Contains(task.Name, "git") { // base and git tasks have no anchors
continue
}

anchorParamNames := []string{}
for _, param := range task.Spec.Params {
if param.Name == "anchors" {
anchorParamNames = append(anchorParamNames, "anchors")
}
}

anchorNames := []string{}
for _, step := range task.Spec.Steps {
if step.Name == "anchor" {
anchorNames = append(anchorNames, "anchor")
}
}
anchorResultNames := []string{}
for _, result := range task.Spec.Results {
if result.Name == "anchor" {
anchorResultNames = append(anchorResultNames, "anchor")
}
}
require.Equal(t, len(anchorParamNames), 1)

if strings.Contains(task.Name, "consumer") {
continue // consumers have no anchor steps
}
require.Equal(t, len(anchorNames), 1)
require.Equal(t, len(anchorResultNames), 1)
}
}

//revive:disable:cyclomatic High complexity score but easy to understand
//revive:disable:cognitive-complexity High complexity score but easy to understand
func requireEnvVars(t *testing.T, taskList []*tektonv1beta1api.Task) {
envVars := []string{"DRACON_SCAN_TIME", "DRACON_SCAN_ID", "DRACON_SCAN_TAGS"}
params := []string{"dracon_scan_id", "dracon_scan_start_time", "dracon_scan_tags"}
for _, task := range taskList {
taskParams := []string{}
if !strings.Contains(task.Name, "producer") || strings.Contains(task.Name, "aggregator") { // env vars are for producers only
continue
}
for _, p := range task.Spec.Params {
if slices.Contains(params, p.Name) {
taskParams = append(taskParams, p.Name)
}
}

require.Equalf(t, len(taskParams), len(params), "task %s has missing params", task.Name)
for _, step := range task.Spec.Steps {
require.NotEmptyf(t, step.Env, "task %s has no env vars", task.Name)
stepEnvVars := []string{}
for _, env := range step.Env {
if slices.Contains(envVars, env.Name) {
stepEnvVars = append(stepEnvVars, env.Name)
}
}
require.Equalf(t, len(envVars), len(stepEnvVars), "task %s has env vars %#v", task.Name, stepEnvVars)
}
}
}