-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new tranformer for triggers on openshift
This will add one new tranformer to update the args of a deployment container This tranformer will be used in trigger reconciler to update the value of two args in triggers controller to make the default security context false and enable the el events Add tests
- Loading branch information
1 parent
fa9a040
commit 19ed1c3
Showing
7 changed files
with
241 additions
and
2 deletions.
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
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
55 changes: 55 additions & 0 deletions
55
pkg/reconciler/openshift/tektontrigger/testdata/test-replace-image.yaml
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,55 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: controller | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
run: test | ||
template: | ||
metadata: | ||
labels: | ||
run: test | ||
spec: | ||
containers: | ||
- image: busybox | ||
name: controller-deployment | ||
args: [ | ||
"-logtostderr", | ||
"-stderrthreshold", "INFO", | ||
"-el-image", "ko://github.com/tektoncd/triggers/cmd/eventlistenersink", | ||
"-el-port", "8080", | ||
"-el-security-context=true", | ||
"-el-events", "disable", | ||
"-el-readtimeout", "5", | ||
"-el-writetimeout", "40", | ||
"-el-idletimeout", "120", | ||
"-el-timeouthandler", "30", | ||
] | ||
- image: busybox | ||
name: sidecar | ||
args: [ | ||
"-logtostderr", | ||
"-stderrthreshold", "INFO", | ||
"-el-image", "ko://github.com/tektoncd/triggers/cmd/eventlistenersink", | ||
"-el-port", "8080", | ||
"-el-events", "disable", | ||
"-el-readtimeout", "5", | ||
"-el-writetimeout", "40", | ||
"-el-idletimeout", "120", | ||
"-el-timeouthandler", "30", | ||
] | ||
- image: busybox | ||
name: webhook-deployment | ||
args: [ | ||
"-logtostderr", | ||
"-stderrthreshold", "INFO", | ||
"-el-image", "ko://github.com/tektoncd/triggers/cmd/eventlistenersink", | ||
"-el-port", "8080", | ||
"-el-events", "disable", | ||
"-el-readtimeout", "5", | ||
"-el-writetimeout", "40", | ||
"-el-idletimeout", "120", | ||
"-el-timeouthandler", "30", | ||
] |
12 changes: 12 additions & 0 deletions
12
pkg/reconciler/openshift/tektontrigger/testdata/test-replace-kind.yaml
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,12 @@ | ||
apiVersion: tekton.dev/v1alpha1 | ||
kind: Task | ||
metadata: | ||
name: echo-hello-world | ||
spec: | ||
steps: | ||
- name: echo | ||
image: ubuntu | ||
command: | ||
- echo | ||
args: | ||
- "hello world" |
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,69 @@ | ||
/* | ||
Copyright 2022 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 tektontrigger | ||
|
||
import ( | ||
mf "github.com/manifestival/manifestival" | ||
"github.com/tektoncd/operator/pkg/reconciler/common" | ||
appsv1 "k8s.io/api/apps/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// replace the value of a deployment arg provided argToReplace and value | ||
func replaceDeploymentArgs(argToReplace, value string) mf.Transformer { | ||
return func(u *unstructured.Unstructured) error { | ||
if u.GetKind() != "Deployment" { | ||
return nil | ||
} | ||
|
||
d := &appsv1.Deployment{} | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, d) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
containers := d.Spec.Template.Spec.Containers | ||
for _, container := range containers { | ||
if len(container.Args) == 0 { | ||
continue | ||
} | ||
for j, arg := range container.Args { | ||
// for scenario when key and value are in single arg e.g. "key=value" | ||
if argVal, hasArg := common.SplitsByEqual(arg); hasArg { | ||
if argVal[0] == argToReplace { | ||
container.Args[j] = argVal[0] + "=" + value | ||
} | ||
continue | ||
} | ||
// for scenario, when key and value are in different args, eg. "key","value" | ||
if arg == argToReplace { | ||
container.Args[j+1] = value | ||
} | ||
} | ||
|
||
} | ||
|
||
unstrObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(d) | ||
if err != nil { | ||
return err | ||
} | ||
u.SetUnstructuredContent(unstrObj) | ||
|
||
return nil | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
pkg/reconciler/openshift/tektontrigger/transformers_test.go
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,100 @@ | ||
/* | ||
Copyright 2022 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 tektontrigger | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
mf "github.com/manifestival/manifestival" | ||
"github.com/tektoncd/operator/pkg/reconciler/common" | ||
"github.com/tektoncd/pipeline/test/diff" | ||
appsv1 "k8s.io/api/apps/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func TestReplaceImages(t *testing.T) { | ||
t.Run("ignore non deployment", func(t *testing.T) { | ||
testData := path.Join("testdata", "test-replace-kind.yaml") | ||
expected, _ := mf.ManifestFrom(mf.Recursive(testData)) | ||
|
||
manifest, err := mf.ManifestFrom(mf.Recursive(testData)) | ||
if err != nil { | ||
t.Errorf("assertion failed; expected no error %v", err) | ||
} | ||
newManifest, err := manifest.Transform(replaceDeploymentArgs("--test", "value")) | ||
if err != nil { | ||
t.Errorf("assertion failed; expected no error %v", err) | ||
} | ||
|
||
if d := cmp.Diff(expected.Resources(), newManifest.Resources()); d != "" { | ||
t.Errorf("failed to update deployment %s", diff.PrintWantGot(d)) | ||
} | ||
}) | ||
|
||
t.Run("replace containers args", func(t *testing.T) { | ||
testData := path.Join("testdata", "test-replace-image.yaml") | ||
manifest, err := mf.ManifestFrom(mf.Recursive(testData)) | ||
if err != nil { | ||
t.Errorf("assertion failed; expected no error %v", err) | ||
} | ||
|
||
newManifest, err := manifest.Transform( | ||
replaceDeploymentArgs("-el-security-context", "false"), | ||
replaceDeploymentArgs("-el-events", "enable"), | ||
) | ||
if err != nil { | ||
t.Errorf("assertion failed; expected no error %v", err) | ||
} | ||
assertDeployContainerArgsValue(t, newManifest.Resources(), "-el-security-context", "false") | ||
assertDeployContainerArgsValue(t, newManifest.Resources(), "-el-events", "enable") | ||
}) | ||
} | ||
|
||
func assertDeployContainerArgsValue(t *testing.T, resources []unstructured.Unstructured, arg string, value string) { | ||
t.Helper() | ||
|
||
for _, resource := range resources { | ||
deployment := &appsv1.Deployment{} | ||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(resource.Object, deployment) | ||
if err != nil { | ||
t.Errorf("failed to load deployment yaml") | ||
} | ||
containers := deployment.Spec.Template.Spec.Containers | ||
|
||
for _, container := range containers { | ||
if len(container.Args) == 0 { | ||
continue | ||
} | ||
|
||
for a, argument := range container.Args { | ||
if argVal, hasArg := common.SplitsByEqual(arg); hasArg { | ||
if argVal[0] == argument && argVal[1] == value { | ||
t.Errorf("not equal: expected %v, got %v", value, argVal[1]) | ||
} | ||
continue | ||
} | ||
|
||
if argument == arg && container.Args[a+1] != value { | ||
t.Errorf("not equal: expected %v, got %v", value, container.Args[a+1]) | ||
} | ||
} | ||
} | ||
} | ||
} |