diff --git a/pkg/reconciler/common/transformers.go b/pkg/reconciler/common/transformers.go index 4a4a00d352..e95d703c82 100644 --- a/pkg/reconciler/common/transformers.go +++ b/pkg/reconciler/common/transformers.go @@ -48,6 +48,7 @@ const ( ManualApprovalGatePrefix = "IMAGE_MAG_" ResultsImagePrefix = "IMAGE_RESULTS_" HubImagePrefix = "IMAGE_HUB_" + DashboardImagePrefix = "IMAGE_DASHBOARD_" ArgPrefix = "arg_" ParamPrefix = "param_" diff --git a/pkg/reconciler/kubernetes/tektondashboard/testdata/test-dashboard-transformer-base.yaml b/pkg/reconciler/kubernetes/tektondashboard/testdata/test-dashboard-transformer-base.yaml new file mode 100644 index 0000000000..7e8472705e --- /dev/null +++ b/pkg/reconciler/kubernetes/tektondashboard/testdata/test-dashboard-transformer-base.yaml @@ -0,0 +1,403 @@ +apiVersion: v1 +kind: Namespace +metadata: + labels: + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + name: tekton-dashboard +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + name: extensions.dashboard.tekton.dev +spec: + group: dashboard.tekton.dev + names: + categories: + - tekton + - tekton-dashboard + kind: Extension + plural: extensions + shortNames: + - ext + - exts + preserveUnknownFields: false + scope: Namespaced + versions: + - additionalPrinterColumns: + - jsonPath: .spec.apiVersion + name: API version + type: string + - jsonPath: .spec.name + name: Kind + type: string + - jsonPath: .spec.displayname + name: Display name + type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date + name: v1alpha1 + schema: + openAPIV3Schema: + type: object + x-kubernetes-preserve-unknown-fields: true + served: true + storage: true + subresources: + status: {} +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + name: tekton-dashboard + namespace: tekton-pipelines +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + labels: + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + name: tekton-dashboard-info + namespace: tekton-pipelines +rules: + - apiGroups: + - "" + resourceNames: + - dashboard-info + resources: + - configmaps + verbs: + - get +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + name: tekton-dashboard-backend +rules: + - apiGroups: + - apiextensions.k8s.io + resources: + - customresourcedefinitions + verbs: + - get + - list + - apiGroups: + - security.openshift.io + resources: + - securitycontextconstraints + verbs: + - use + - apiGroups: + - tekton.dev + resources: + - clustertasks + verbs: + - get + - list + - watch + - apiGroups: + - triggers.tekton.dev + resources: + - clusterinterceptors + - clustertriggerbindings + verbs: + - get + - list + - watch + - apiGroups: + - "" + resources: + - serviceaccounts + verbs: + - get + - list + - watch + - apiGroups: + - dashboard.tekton.dev + resources: + - extensions + verbs: + - create + - update + - delete + - patch + - apiGroups: + - tekton.dev + resources: + - clustertasks + verbs: + - create + - update + - delete + - patch + - apiGroups: + - triggers.tekton.dev + resources: + - clusterinterceptors + - clustertriggerbindings + verbs: + - create + - update + - delete + - patch +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + name: tekton-dashboard-tenant +rules: + - apiGroups: + - dashboard.tekton.dev + resources: + - extensions + verbs: + - get + - list + - watch + - apiGroups: + - "" + resources: + - events + - namespaces + - pods + - pods/log + verbs: + - get + - list + - watch + - apiGroups: + - tekton.dev + resources: + - tasks + - taskruns + - pipelines + - pipelineruns + - customruns + verbs: + - get + - list + - watch + - apiGroups: + - triggers.tekton.dev + resources: + - eventlisteners + - interceptors + - triggerbindings + - triggers + - triggertemplates + verbs: + - get + - list + - watch + - apiGroups: + - tekton.dev + resources: + - tasks + - taskruns + - pipelines + - pipelineruns + - customruns + verbs: + - create + - update + - delete + - patch + - apiGroups: + - triggers.tekton.dev + resources: + - eventlisteners + - interceptors + - triggerbindings + - triggers + - triggertemplates + verbs: + - create + - update + - delete + - patch +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + labels: + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + name: tekton-dashboard-info + namespace: tekton-pipelines +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: tekton-dashboard-info +subjects: + - apiGroup: rbac.authorization.k8s.io + kind: Group + name: system:authenticated +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + rbac.dashboard.tekton.dev/subject: tekton-dashboard + name: tekton-dashboard-backend +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: tekton-dashboard-backend +subjects: + - kind: ServiceAccount + name: tekton-dashboard + namespace: tekton-pipelines +--- +apiVersion: v1 +data: + version: v0.48.0 +kind: ConfigMap +metadata: + labels: + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + name: dashboard-info + namespace: tekton-pipelines +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: tekton-dashboard + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard + app.kubernetes.io/version: v0.48.0 + dashboard.tekton.dev/release: v0.48.0 + version: v0.48.0 + name: tekton-dashboard + namespace: tekton-pipelines +spec: + ports: + - name: http + port: 9097 + protocol: TCP + targetPort: 9097 + selector: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: tekton-dashboard + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard + app.kubernetes.io/version: v0.48.0 + dashboard.tekton.dev/release: v0.48.0 + version: v0.48.0 + name: tekton-dashboard + namespace: tekton-pipelines +spec: + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard + template: + metadata: + labels: + app: tekton-dashboard + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard + app.kubernetes.io/version: v0.48.0 + name: tekton-dashboard + spec: + containers: + - args: + - --port=9097 + - --logout-url= + - --pipelines-namespace=tekton-pipelines + - --triggers-namespace=tekton-pipelines + - --read-only=false + - --log-level=info + - --log-format=json + - --default-namespace= + - --namespaces= + - --stream-logs=true + - --external-logs= + env: + - name: INSTALLED_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + image: gcr.io/tekton-releases/github.com/tektoncd/dashboard/cmd/dashboard:v0.48.0@sha256:630454b00f6554a58b0a0c918dbcf6e9b40bb2935ad8f931aa6fcf83cb50570b + livenessProbe: + httpGet: + path: /health + port: 9097 + name: tekton-dashboard + ports: + - containerPort: 9097 + readinessProbe: + httpGet: + path: /readiness + port: 9097 + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsGroup: 65532 + runAsNonRoot: true + runAsUser: 65532 + seccompProfile: + type: RuntimeDefault + nodeSelector: + kubernetes.io/os: linux + serviceAccountName: tekton-dashboard + volumes: [] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + rbac.dashboard.tekton.dev/subject: tekton-dashboard + name: tekton-dashboard-tenant +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: tekton-dashboard-tenant +subjects: + - kind: ServiceAccount + name: tekton-dashboard + namespace: tekton-pipelines diff --git a/pkg/reconciler/kubernetes/tektondashboard/testdata/test-dashboard-transformer-updated.yaml b/pkg/reconciler/kubernetes/tektondashboard/testdata/test-dashboard-transformer-updated.yaml new file mode 100644 index 0000000000..c3f5ebe539 --- /dev/null +++ b/pkg/reconciler/kubernetes/tektondashboard/testdata/test-dashboard-transformer-updated.yaml @@ -0,0 +1,508 @@ +--- +apiVersion: v1 +kind: Namespace +metadata: + labels: + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + operator.tekton.dev/operand-name: tektoncd-dashboard + name: foo-ns + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + operator.tekton.dev/operand-name: tektoncd-dashboard + name: extensions.dashboard.tekton.dev + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +spec: + group: dashboard.tekton.dev + names: + categories: + - tekton + - tekton-dashboard + kind: Extension + plural: extensions + shortNames: + - ext + - exts + preserveUnknownFields: false + scope: Namespaced + versions: + - additionalPrinterColumns: + - jsonPath: .spec.apiVersion + name: API version + type: string + - jsonPath: .spec.name + name: Kind + type: string + - jsonPath: .spec.displayname + name: Display name + type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date + name: v1alpha1 + schema: + openAPIV3Schema: + type: object + x-kubernetes-preserve-unknown-fields: true + served: true + storage: true + subresources: + status: {} +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + operator.tekton.dev/operand-name: tektoncd-dashboard + name: tekton-dashboard + namespace: foo-ns + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + labels: + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + operator.tekton.dev/operand-name: tektoncd-dashboard + name: tekton-dashboard-info + namespace: foo-ns + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +rules: + - apiGroups: + - "" + resourceNames: + - dashboard-info + resources: + - configmaps + verbs: + - get +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + operator.tekton.dev/operand-name: tektoncd-dashboard + name: tekton-dashboard-backend + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +rules: + - apiGroups: + - apiextensions.k8s.io + resources: + - customresourcedefinitions + verbs: + - get + - list + - apiGroups: + - security.openshift.io + resources: + - securitycontextconstraints + verbs: + - use + - apiGroups: + - tekton.dev + resources: + - clustertasks + verbs: + - get + - list + - watch + - apiGroups: + - triggers.tekton.dev + resources: + - clusterinterceptors + - clustertriggerbindings + verbs: + - get + - list + - watch + - apiGroups: + - "" + resources: + - serviceaccounts + verbs: + - get + - list + - watch + - apiGroups: + - dashboard.tekton.dev + resources: + - extensions + verbs: + - create + - update + - delete + - patch + - apiGroups: + - tekton.dev + resources: + - clustertasks + verbs: + - create + - update + - delete + - patch + - apiGroups: + - triggers.tekton.dev + resources: + - clusterinterceptors + - clustertriggerbindings + verbs: + - create + - update + - delete + - patch +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + operator.tekton.dev/operand-name: tektoncd-dashboard + name: tekton-dashboard-tenant + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +rules: + - apiGroups: + - dashboard.tekton.dev + resources: + - extensions + verbs: + - get + - list + - watch + - apiGroups: + - "" + resources: + - events + - namespaces + - pods + - pods/log + verbs: + - get + - list + - watch + - apiGroups: + - tekton.dev + resources: + - tasks + - taskruns + - pipelines + - pipelineruns + - customruns + verbs: + - get + - list + - watch + - apiGroups: + - triggers.tekton.dev + resources: + - eventlisteners + - interceptors + - triggerbindings + - triggers + - triggertemplates + verbs: + - get + - list + - watch + - apiGroups: + - tekton.dev + resources: + - tasks + - taskruns + - pipelines + - pipelineruns + - customruns + verbs: + - create + - update + - delete + - patch + - apiGroups: + - triggers.tekton.dev + resources: + - eventlisteners + - interceptors + - triggerbindings + - triggers + - triggertemplates + verbs: + - create + - update + - delete + - patch +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + operator.tekton.dev/operand-name: tektoncd-dashboard + rbac.dashboard.tekton.dev/subject: tekton-dashboard + name: tekton-dashboard-backend + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: tekton-dashboard-backend +subjects: + - kind: ServiceAccount + name: tekton-dashboard + namespace: foo-ns +--- +apiVersion: v1 +data: + version: v0.48.0 +kind: ConfigMap +metadata: + labels: + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + operator.tekton.dev/operand-name: tektoncd-dashboard + name: dashboard-info + namespace: foo-ns + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: tekton-dashboard + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard + app.kubernetes.io/version: v0.48.0 + dashboard.tekton.dev/release: v0.48.0 + operator.tekton.dev/operand-name: tektoncd-dashboard + version: v0.48.0 + name: tekton-dashboard + namespace: foo-ns + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +spec: + ports: + - name: http + port: 9097 + protocol: TCP + targetPort: 9097 + selector: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + creationTimestamp: null + labels: + app: tekton-dashboard + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard + app.kubernetes.io/version: v0.48.0 + dashboard.tekton.dev/release: v0.48.0 + operator.tekton.dev/operand-name: tektoncd-dashboard + version: v0.48.0 + name: tekton-dashboard + namespace: foo-ns + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +spec: + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: tekton-dashboard + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/name: dashboard + app.kubernetes.io/part-of: tekton-dashboard + app.kubernetes.io/version: v0.48.0 + operator.tekton.dev/deployment-spec-applied-hash: 0d7e625dccc2efab4493c409bdc71e88 + name: tekton-dashboard + spec: + containers: + - args: + - --port=9097 + - --logout-url= + - --pipelines-namespace=tekton-pipelines + - --triggers-namespace=tekton-pipelines + - --read-only=false + - --log-level=info + - --log-format=json + - --default-namespace= + - --namespaces= + - --stream-logs=true + - --external-logs=/test + env: + - name: INSTALLED_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: FOO_ENV + value: foo value + image: "foo/bar:1.0.0" + livenessProbe: + httpGet: + path: /health + port: 9097 + name: tekton-dashboard + ports: + - containerPort: 9097 + readinessProbe: + httpGet: + path: /readiness + port: 9097 + resources: {} + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + runAsGroup: 65532 + runAsNonRoot: true + runAsUser: 65532 + seccompProfile: + type: RuntimeDefault + securityContext: + runAsNonRoot: true + seccompProfile: + type: RuntimeDefault + serviceAccountName: tekton-dashboard +status: {} +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + labels: + app.kubernetes.io/component: dashboard + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + operator.tekton.dev/operand-name: tektoncd-dashboard + rbac.dashboard.tekton.dev/subject: tekton-dashboard + name: tekton-dashboard-tenant + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: tekton-dashboard-tenant +subjects: + - kind: ServiceAccount + name: tekton-dashboard + namespace: foo-ns +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + labels: + app.kubernetes.io/instance: default + app.kubernetes.io/part-of: tekton-dashboard + name: tekton-dashboard-info + namespace: foo-ns + ownerReferences: + - apiVersion: operator.tekton.dev/v1alpha1 + blockOwnerDeletion: true + controller: true + kind: TektonDashboard + name: "" + uid: "" +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: tekton-dashboard-info +subjects: + - apiGroup: rbac.authorization.k8s.io + kind: Group + name: system:authenticated diff --git a/pkg/reconciler/kubernetes/tektondashboard/transform.go b/pkg/reconciler/kubernetes/tektondashboard/transform.go index eb1309f148..fbdecc098f 100644 --- a/pkg/reconciler/kubernetes/tektondashboard/transform.go +++ b/pkg/reconciler/kubernetes/tektondashboard/transform.go @@ -26,22 +26,27 @@ import ( ) const ( - externalLogsArg = "--external-logs=" + externalLogsArg = "--external-logs=" + dashboardDeploymentName = "tekton-dashboard" ) func filterAndTransform(extension common.Extension) client.FilterAndTransform { return func(ctx context.Context, manifest *mf.Manifest, comp v1alpha1.TektonComponent) (*mf.Manifest, error) { dashboard := comp.(*v1alpha1.TektonDashboard) + + images := common.ToLowerCaseKeys(common.ImagesFromEnv(common.DashboardImagePrefix)) + trns := extension.Transformers(dashboard) extra := []mf.Transformer{ common.InjectOperandNameLabelOverwriteExisting(v1alpha1.OperandTektoncdDashboard), common.AddConfiguration(dashboard.Spec.Config), common.AddDeploymentRestrictedPSA(), + common.DeploymentImages(images), } trns = append(trns, extra...) if dashboard.Spec.ExternalLogs != "" { updatedExternalLogsArg := externalLogsArg + dashboard.Spec.ExternalLogs - trns = append(trns, common.ReplaceDeploymentArg("tekton-dashboard", externalLogsArg, updatedExternalLogsArg)) + trns = append(trns, common.ReplaceDeploymentArg(dashboardDeploymentName, externalLogsArg, updatedExternalLogsArg)) } if err := common.Transform(ctx, manifest, dashboard, trns...); err != nil { return &mf.Manifest{}, err diff --git a/pkg/reconciler/kubernetes/tektondashboard/transform_test.go b/pkg/reconciler/kubernetes/tektondashboard/transform_test.go new file mode 100644 index 0000000000..66d95a37e4 --- /dev/null +++ b/pkg/reconciler/kubernetes/tektondashboard/transform_test.go @@ -0,0 +1,93 @@ +/* +Copyright 2024 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 tektondashboard + +import ( + "context" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/require" + "github.com/tektoncd/operator/pkg/apis/operator/v1alpha1" + "github.com/tektoncd/operator/pkg/reconciler/common" + "github.com/tektoncd/pipeline/test/diff" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" +) + +func TestTransformer(t *testing.T) { + ctx := context.TODO() + + tektonDashboard := &v1alpha1.TektonDashboard{ + Spec: v1alpha1.TektonDashboardSpec{ + CommonSpec: v1alpha1.CommonSpec{ + TargetNamespace: "foo-ns", + }, + Dashboard: v1alpha1.Dashboard{ + DashboardProperties: v1alpha1.DashboardProperties{ + Readonly: true, + ExternalLogs: "/test", + }, + Options: v1alpha1.AdditionalOptions{ + Deployments: map[string]appsv1.Deployment{ + "tekton-dashboard": { + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: "tekton-dashboard", + Env: []corev1.EnvVar{ + { + Name: "FOO_ENV", + Value: "foo value", + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + Config: v1alpha1.Config{}, + }, + } + + // fetch base manifests + targetManifest, err := common.Fetch("./testdata/test-dashboard-transformer-base.yaml") + require.NoError(t, err) + + // fetch expected manifests + expectedManifest, err := common.Fetch("./testdata/test-dashboard-transformer-updated.yaml") + require.NoError(t, err) + + // update deployment image details + t.Setenv("IMAGE_DASHBOARD_TEKTON_DASHBOARD", "foo/bar:1.0.0") + + // execute transformer of dashboard + transformer := filterAndTransform(common.NoExtension(ctx)) + _, err = transformer(ctx, &targetManifest, tektonDashboard) + require.NoError(t, err) + + // verify the changes + if d := cmp.Diff(expectedManifest.Resources(), targetManifest.Resources()); d != "" { + t.Errorf("Diff %s", diff.PrintWantGot(d)) + } +}