-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace variables in Sidecar Script block
A while ago we added Script block support to Sidecars but forgot to perform variable replacement on them. We perform variable replacement on pretty much every other important field in a Sidecar so we should probably also do it in Scripts. This commit applies variables used in Sidecar Script blocks and adds a few tests for it (copied from the existing step_replacements func).
- Loading branch information
1 parent
995e44f
commit 09c7fe2
Showing
4 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright 2020 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1beta1 | ||
|
||
import ( | ||
"github.com/tektoncd/pipeline/pkg/substitution" | ||
) | ||
|
||
func ApplySidecarReplacements(sidecar *Sidecar, stringReplacements map[string]string, arrayReplacements map[string][]string) { | ||
sidecar.Script = substitution.ApplyReplacements(sidecar.Script, stringReplacements) | ||
ApplyContainerReplacements(&sidecar.Container, stringReplacements, arrayReplacements) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
Copyright 2019 The Tekton Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package v1beta1_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestApplySidecarReplacements(t *testing.T) { | ||
replacements := map[string]string{ | ||
"replace.me": "replaced!", | ||
} | ||
|
||
arrayReplacements := map[string][]string{ | ||
"array.replace.me": {"val1", "val2"}, | ||
} | ||
|
||
s := v1beta1.Sidecar{ | ||
Script: "$(replace.me)", | ||
Container: corev1.Container{ | ||
Name: "$(replace.me)", | ||
Image: "$(replace.me)", | ||
Command: []string{"$(array.replace.me)"}, | ||
Args: []string{"$(array.replace.me)"}, | ||
WorkingDir: "$(replace.me)", | ||
EnvFrom: []corev1.EnvFromSource{{ | ||
ConfigMapRef: &corev1.ConfigMapEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "$(replace.me)", | ||
}, | ||
}, | ||
SecretRef: &corev1.SecretEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "$(replace.me)", | ||
}, | ||
}, | ||
}}, | ||
Env: []corev1.EnvVar{{ | ||
Name: "not_me", | ||
Value: "$(replace.me)", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "$(replace.me)", | ||
}, | ||
Key: "$(replace.me)", | ||
}, | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "$(replace.me)", | ||
}, | ||
Key: "$(replace.me)", | ||
}, | ||
}, | ||
}}, | ||
VolumeMounts: []corev1.VolumeMount{{ | ||
Name: "$(replace.me)", | ||
MountPath: "$(replace.me)", | ||
SubPath: "$(replace.me)", | ||
}}, | ||
}, | ||
} | ||
|
||
expected := v1beta1.Sidecar{ | ||
Script: "replaced!", | ||
Container: corev1.Container{ | ||
Name: "replaced!", | ||
Image: "replaced!", | ||
Command: []string{"val1", "val2"}, | ||
Args: []string{"val1", "val2"}, | ||
WorkingDir: "replaced!", | ||
EnvFrom: []corev1.EnvFromSource{{ | ||
ConfigMapRef: &corev1.ConfigMapEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "replaced!", | ||
}, | ||
}, | ||
SecretRef: &corev1.SecretEnvSource{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "replaced!", | ||
}, | ||
}, | ||
}}, | ||
Env: []corev1.EnvVar{{ | ||
Name: "not_me", | ||
Value: "replaced!", | ||
ValueFrom: &corev1.EnvVarSource{ | ||
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "replaced!", | ||
}, | ||
Key: "replaced!", | ||
}, | ||
SecretKeyRef: &corev1.SecretKeySelector{ | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "replaced!", | ||
}, | ||
Key: "replaced!", | ||
}, | ||
}, | ||
}}, | ||
VolumeMounts: []corev1.VolumeMount{{ | ||
Name: "replaced!", | ||
MountPath: "replaced!", | ||
SubPath: "replaced!", | ||
}}, | ||
}, | ||
} | ||
v1beta1.ApplySidecarReplacements(&s, replacements, arrayReplacements) | ||
if d := cmp.Diff(s, expected); d != "" { | ||
t.Errorf("Container replacements failed: %s", d) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters