|
| 1 | +package namespaced |
| 2 | + |
| 3 | +import ( |
| 4 | + corev1 "k8s.io/api/core/v1" |
| 5 | + networkv1 "k8s.io/api/networking/v1" |
| 6 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 7 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 8 | + "k8s.io/utils/ptr" |
| 9 | + "kubevirt.io/containerized-data-importer/pkg/common" |
| 10 | + |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + allowIngressToMetrics = "cdi-allow-ingress-to-metrics" |
| 16 | + allowUploadProxyCommunications = "cdi-allow-uploadproxy-communications" |
| 17 | + allowIngressToCdiAPIWebhook = "cdi-allow-cdi-api-webhook-server" |
| 18 | + allowEgressToImporterMetrics = "cdi-allow-cdi-deployment-importer-metrics" |
| 19 | +) |
| 20 | + |
| 21 | +func createNetworkPolicies(args *FactoryArgs) []client.Object { |
| 22 | + return []client.Object{ |
| 23 | + newIngressToMetricsNP(args.Namespace), |
| 24 | + newUploadProxyCommunicationsNP(args.Namespace), |
| 25 | + newIngressToCdiAPIWebhookNP(args.Namespace), |
| 26 | + newCdiDeploymentToImporterMetricsNP(args.Namespace), |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func newNetworkPolicy(namespace, name string, spec *networkv1.NetworkPolicySpec) *networkv1.NetworkPolicy { |
| 31 | + return &networkv1.NetworkPolicy{ |
| 32 | + TypeMeta: metav1.TypeMeta{ |
| 33 | + APIVersion: "networking.k8s.io/v1", |
| 34 | + Kind: "NetworkPolicy", |
| 35 | + }, |
| 36 | + ObjectMeta: metav1.ObjectMeta{ |
| 37 | + Name: name, |
| 38 | + Namespace: namespace, |
| 39 | + Labels: map[string]string{common.CDIComponentLabel: ""}, |
| 40 | + }, |
| 41 | + Spec: *spec, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func newIngressToMetricsNP(namespace string) *networkv1.NetworkPolicy { |
| 46 | + return newNetworkPolicy( |
| 47 | + namespace, |
| 48 | + allowIngressToMetrics, |
| 49 | + &networkv1.NetworkPolicySpec{ |
| 50 | + PodSelector: metav1.LabelSelector{ |
| 51 | + MatchLabels: map[string]string{common.PrometheusLabelKey: common.PrometheusLabelValue}, |
| 52 | + }, |
| 53 | + PolicyTypes: []networkv1.PolicyType{networkv1.PolicyTypeIngress}, |
| 54 | + Ingress: []networkv1.NetworkPolicyIngressRule{ |
| 55 | + { |
| 56 | + Ports: []networkv1.NetworkPolicyPort{ |
| 57 | + { |
| 58 | + Port: ptr.To(intstr.FromInt32(8443)), |
| 59 | + Protocol: ptr.To(corev1.ProtocolTCP), |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +func newUploadProxyCommunicationsNP(namespace string) *networkv1.NetworkPolicy { |
| 69 | + return newNetworkPolicy( |
| 70 | + namespace, |
| 71 | + allowUploadProxyCommunications, |
| 72 | + &networkv1.NetworkPolicySpec{ |
| 73 | + PodSelector: metav1.LabelSelector{ |
| 74 | + MatchLabels: map[string]string{common.CDIComponentLabel: common.CDIUploadProxyResourceName}, |
| 75 | + }, |
| 76 | + PolicyTypes: []networkv1.PolicyType{ |
| 77 | + networkv1.PolicyTypeIngress, |
| 78 | + networkv1.PolicyTypeEgress, |
| 79 | + }, |
| 80 | + Egress: []networkv1.NetworkPolicyEgressRule{ |
| 81 | + { |
| 82 | + To: []networkv1.NetworkPolicyPeer{ |
| 83 | + { |
| 84 | + PodSelector: &metav1.LabelSelector{ |
| 85 | + MatchLabels: map[string]string{common.CDIComponentLabel: common.UploadServerCDILabel}, |
| 86 | + }, |
| 87 | + NamespaceSelector: &metav1.LabelSelector{}, |
| 88 | + }, |
| 89 | + }, |
| 90 | + Ports: []networkv1.NetworkPolicyPort{ |
| 91 | + { |
| 92 | + Port: ptr.To(intstr.FromInt32(8443)), |
| 93 | + Protocol: ptr.To(corev1.ProtocolTCP), |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + Ingress: []networkv1.NetworkPolicyIngressRule{ |
| 99 | + { |
| 100 | + Ports: []networkv1.NetworkPolicyPort{ |
| 101 | + { |
| 102 | + Port: ptr.To(intstr.FromInt32(8443)), |
| 103 | + Protocol: ptr.To(corev1.ProtocolTCP), |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + ) |
| 110 | +} |
| 111 | + |
| 112 | +func newIngressToCdiAPIWebhookNP(namespace string) *networkv1.NetworkPolicy { |
| 113 | + return newNetworkPolicy( |
| 114 | + namespace, |
| 115 | + allowIngressToCdiAPIWebhook, |
| 116 | + &networkv1.NetworkPolicySpec{ |
| 117 | + PodSelector: metav1.LabelSelector{ |
| 118 | + MatchLabels: map[string]string{common.CDIComponentLabel: common.CDIApiServerResourceName}, |
| 119 | + }, |
| 120 | + PolicyTypes: []networkv1.PolicyType{networkv1.PolicyTypeIngress}, |
| 121 | + Ingress: []networkv1.NetworkPolicyIngressRule{ |
| 122 | + { |
| 123 | + Ports: []networkv1.NetworkPolicyPort{ |
| 124 | + { |
| 125 | + Port: ptr.To(intstr.FromInt32(8443)), |
| 126 | + Protocol: ptr.To(corev1.ProtocolTCP), |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +func newCdiDeploymentToImporterMetricsNP(namespace string) *networkv1.NetworkPolicy { |
| 136 | + return newNetworkPolicy( |
| 137 | + namespace, |
| 138 | + allowEgressToImporterMetrics, |
| 139 | + &networkv1.NetworkPolicySpec{ |
| 140 | + PodSelector: metav1.LabelSelector{ |
| 141 | + MatchLabels: map[string]string{common.CDIComponentLabel: common.CDIControllerResourceName}, |
| 142 | + }, |
| 143 | + PolicyTypes: []networkv1.PolicyType{networkv1.PolicyTypeEgress}, |
| 144 | + Egress: []networkv1.NetworkPolicyEgressRule{ |
| 145 | + { |
| 146 | + To: []networkv1.NetworkPolicyPeer{ |
| 147 | + { |
| 148 | + PodSelector: &metav1.LabelSelector{ |
| 149 | + MatchLabels: map[string]string{common.PrometheusLabelKey: common.PrometheusLabelValue}, |
| 150 | + }, |
| 151 | + NamespaceSelector: &metav1.LabelSelector{}, |
| 152 | + }, |
| 153 | + }, |
| 154 | + Ports: []networkv1.NetworkPolicyPort{ |
| 155 | + { |
| 156 | + Port: ptr.To(intstr.FromInt32(8443)), |
| 157 | + Protocol: ptr.To(corev1.ProtocolTCP), |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + ) |
| 164 | +} |
0 commit comments