|
| 1 | +/* |
| 2 | +Copyright 2026 The Tekton Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package tektonpipeline |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + mf "github.com/manifestival/manifestival" |
| 23 | + "github.com/tektoncd/operator/pkg/apis/operator/v1alpha1" |
| 24 | + "github.com/tektoncd/operator/pkg/reconciler/common/networkpolicy" |
| 25 | + "github.com/tektoncd/operator/pkg/reconciler/kubernetes/tektoninstallerset/client" |
| 26 | + networkingv1 "k8s.io/api/networking/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 29 | +) |
| 30 | + |
| 31 | +// proxyWebhookPodSelector matches the proxy-webhook Deployment/Service selector |
| 32 | +// (name: tekton-operator) shipped in cmd/{kubernetes,openshift}/operator/kodata/webhook/webhook.yaml. |
| 33 | +// This label is the same value used by the main operator Deployment in the operator's own |
| 34 | +// namespace (see tektoncd/operator#3227) — harmless here because the proxy-webhook runs in the |
| 35 | +// operand namespace (e.g. tekton-pipelines / openshift-pipelines), never the operator namespace. |
| 36 | +var proxyWebhookPodSelector = metav1.LabelSelector{ |
| 37 | + MatchLabels: map[string]string{"name": "tekton-operator"}, |
| 38 | +} |
| 39 | + |
| 40 | +func pipelineDefaultPolicies(params networkpolicy.PlatformParams) []networkingv1.NetworkPolicy { |
| 41 | + webhookPort := intstr.FromInt32(8443) |
| 42 | + |
| 43 | + return []networkingv1.NetworkPolicy{ |
| 44 | + { |
| 45 | + ObjectMeta: metav1.ObjectMeta{Name: "proxy-webhook"}, |
| 46 | + Spec: networkingv1.NetworkPolicySpec{ |
| 47 | + PodSelector: proxyWebhookPodSelector, |
| 48 | + PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeIngress, networkingv1.PolicyTypeEgress}, |
| 49 | + Ingress: []networkingv1.NetworkPolicyIngressRule{ |
| 50 | + // cidr="" → permissive; restrict via spec.networkPolicy.policies if needed. |
| 51 | + networkpolicy.WebhookIngressRule("", webhookPort), |
| 52 | + }, |
| 53 | + Egress: []networkingv1.NetworkPolicyEgressRule{ |
| 54 | + networkpolicy.DNSEgressRule(params), |
| 55 | + networkpolicy.APIServerEgressRule(params), |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// defaultDenyPolicy returns the scoped default-deny for the proxy-webhook pod. |
| 63 | +func defaultDenyPolicy() networkingv1.NetworkPolicy { |
| 64 | + return networkpolicy.DefaultDenyPolicy("tekton-proxy-webhook-default-deny", proxyWebhookPodSelector) |
| 65 | +} |
| 66 | + |
| 67 | +func (r *Reconciler) reconcileNetworkPolicies(ctx context.Context, tp *v1alpha1.TektonPipeline) error { |
| 68 | + if tp.Spec.NetworkPolicy.Disabled { |
| 69 | + return r.installerSetClient.CleanupCustomSet(ctx, "pipeline-network-policies") |
| 70 | + } |
| 71 | + defaults := append( |
| 72 | + []networkingv1.NetworkPolicy{defaultDenyPolicy()}, |
| 73 | + pipelineDefaultPolicies(r.platformParams)..., |
| 74 | + ) |
| 75 | + manifest, err := networkpolicy.Generate( |
| 76 | + tp.Spec.NetworkPolicy, |
| 77 | + tp.Spec.GetTargetNamespace(), |
| 78 | + defaults, |
| 79 | + ) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + return r.installerSetClient.CustomSet(ctx, tp, "pipeline-network-policies", &manifest, passthroughTransform, nil) |
| 84 | +} |
| 85 | + |
| 86 | +// passthroughTransform is a no-op FilterAndTransform used for pre-built manifests |
| 87 | +// where namespace injection is already handled by Generate. |
| 88 | +func passthroughTransform(_ context.Context, m *mf.Manifest, _ v1alpha1.TektonComponent) (*mf.Manifest, error) { |
| 89 | + return m, nil |
| 90 | +} |
| 91 | + |
| 92 | +// Ensure passthroughTransform satisfies the FilterAndTransform type. |
| 93 | +var _ client.FilterAndTransform = passthroughTransform |
0 commit comments