use of io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicyPort in project onos by opennetworkinglab.
the class K8sNetworkPolicyHandler method setAllowRulesByPolicy.
private void setAllowRulesByPolicy(NetworkPolicy policy, boolean install) {
Map<String, Map<String, List<NetworkPolicyPort>>> white = Maps.newConcurrentMap();
int nsHash = namespaceHashByNamespace(k8sNamespaceService, policy.getMetadata().getNamespace());
List<NetworkPolicyIngressRule> ingress = policy.getSpec().getIngress();
if (ingress != null && ingress.size() == 1) {
NetworkPolicyIngressRule rule = ingress.get(0);
if (rule.getFrom().size() == 0 && rule.getPorts().size() == 0) {
setAllowAllRule(nsHash, DIRECTION_INGRESS, install);
}
}
policy.getSpec().getIngress().forEach(i -> {
Map<String, List<NetworkPolicyPort>> direction = Maps.newConcurrentMap();
direction.put(DIRECTION_INGRESS, i.getPorts());
i.getFrom().forEach(peer -> {
// IP block
if (peer.getIpBlock() != null) {
if (peer.getIpBlock().getExcept() != null && peer.getIpBlock().getExcept().size() > 0) {
Map<String, List<NetworkPolicyPort>> blkDirection = Maps.newConcurrentMap();
blkDirection.put(DIRECTION_INGRESS, i.getPorts());
white.compute(peer.getIpBlock().getCidr(), (k, v) -> blkDirection);
setBlackRules(peer.getIpBlock().getCidr(), DIRECTION_INGRESS, peer.getIpBlock().getExcept(), install);
} else {
white.compute(peer.getIpBlock().getCidr(), (k, v) -> direction);
}
}
// POD selector
Set<Pod> pods = podsFromPolicyPeer(peer, policy.getMetadata().getNamespace());
pods.stream().filter(pod -> pod.getStatus().getPodIP() != null).forEach(pod -> {
white.compute(shiftIpDomain(pod.getStatus().getPodIP(), SHIFTED_IP_PREFIX) + "/" + HOST_PREFIX, (m, n) -> direction);
white.compute(pod.getStatus().getPodIP() + "/" + HOST_PREFIX, (m, n) -> direction);
});
// Namespace selector
setAllowNamespaceRules(nsHash, namespacesByPolicyPeer(peer), DIRECTION_INGRESS, install);
});
});
List<NetworkPolicyEgressRule> egress = policy.getSpec().getEgress();
if (egress != null && egress.size() == 1) {
NetworkPolicyEgressRule rule = egress.get(0);
if (rule.getTo().size() == 0 && rule.getPorts().size() == 0) {
setAllowAllRule(nsHash, DIRECTION_EGRESS, install);
}
}
policy.getSpec().getEgress().forEach(e -> {
Map<String, List<NetworkPolicyPort>> direction = Maps.newConcurrentMap();
direction.put(DIRECTION_EGRESS, e.getPorts());
e.getTo().forEach(peer -> {
// IP block
if (peer.getIpBlock() != null) {
if (peer.getIpBlock().getExcept() != null && peer.getIpBlock().getExcept().size() > 0) {
Map<String, List<NetworkPolicyPort>> blkDirection = Maps.newConcurrentMap();
blkDirection.put(DIRECTION_EGRESS, e.getPorts());
white.compute(peer.getIpBlock().getCidr(), (k, v) -> {
if (v != null) {
v.put(DIRECTION_EGRESS, e.getPorts());
return v;
} else {
return blkDirection;
}
});
setBlackRules(peer.getIpBlock().getCidr(), DIRECTION_EGRESS, peer.getIpBlock().getExcept(), install);
} else {
white.compute(peer.getIpBlock().getCidr(), (k, v) -> {
if (v != null) {
v.put(DIRECTION_EGRESS, e.getPorts());
return v;
} else {
return direction;
}
});
}
}
// POD selector
Set<Pod> pods = podsFromPolicyPeer(peer, policy.getMetadata().getNamespace());
pods.stream().filter(pod -> pod.getStatus().getPodIP() != null).forEach(pod -> {
white.compute(shiftIpDomain(pod.getStatus().getPodIP(), SHIFTED_IP_PREFIX) + "/" + HOST_PREFIX, (m, n) -> {
if (n != null) {
n.put(DIRECTION_EGRESS, e.getPorts());
return n;
} else {
return direction;
}
});
white.compute(pod.getStatus().getPodIP() + "/" + HOST_PREFIX, (m, n) -> {
if (n != null) {
n.put(DIRECTION_EGRESS, e.getPorts());
return n;
} else {
return direction;
}
});
});
// Namespace selector
setAllowNamespaceRules(nsHash, namespacesByPolicyPeer(peer), DIRECTION_EGRESS, install);
});
});
setAllowRules(namespaceHashByNamespace(k8sNamespaceService, policy.getMetadata().getNamespace()), white, install);
setBlackToRouteRules(true);
}
use of io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicyPort in project strimzi by strimzi.
the class ZookeeperCluster method generateNetworkPolicy.
/**
* Generates the NetworkPolicies relevant for ZooKeeper nodes
*
* @param operatorNamespace Namespace where the Strimzi Cluster Operator runs. Null if not configured.
* @param operatorNamespaceLabels Labels of the namespace where the Strimzi Cluster Operator runs. Null if not configured.
*
* @return The network policy.
*/
public NetworkPolicy generateNetworkPolicy(String operatorNamespace, Labels operatorNamespaceLabels) {
List<NetworkPolicyIngressRule> rules = new ArrayList<>(2);
NetworkPolicyPort clientsPort = new NetworkPolicyPort();
clientsPort.setPort(new IntOrString(CLIENT_TLS_PORT));
clientsPort.setProtocol("TCP");
NetworkPolicyPort clusteringPort = new NetworkPolicyPort();
clusteringPort.setPort(new IntOrString(CLUSTERING_PORT));
clusteringPort.setProtocol("TCP");
NetworkPolicyPort leaderElectionPort = new NetworkPolicyPort();
leaderElectionPort.setPort(new IntOrString(LEADER_ELECTION_PORT));
leaderElectionPort.setProtocol("TCP");
NetworkPolicyPeer zookeeperClusterPeer = new NetworkPolicyPeer();
LabelSelector labelSelector2 = new LabelSelector();
Map<String, String> expressions2 = new HashMap<>(1);
expressions2.put(Labels.STRIMZI_NAME_LABEL, zookeeperClusterName(cluster));
labelSelector2.setMatchLabels(expressions2);
zookeeperClusterPeer.setPodSelector(labelSelector2);
// Zookeeper only ports - 2888 & 3888 which need to be accessed by the Zookeeper cluster members only
NetworkPolicyIngressRule zookeeperClusteringIngressRule = new NetworkPolicyIngressRuleBuilder().withPorts(clusteringPort, leaderElectionPort).withFrom(zookeeperClusterPeer).build();
rules.add(zookeeperClusteringIngressRule);
// Clients port - needs to be access from outside the Zookeeper cluster as well
NetworkPolicyIngressRule clientsIngressRule = new NetworkPolicyIngressRuleBuilder().withPorts(clientsPort).withFrom().build();
NetworkPolicyPeer kafkaClusterPeer = new NetworkPolicyPeer();
LabelSelector labelSelector = new LabelSelector();
Map<String, String> expressions = new HashMap<>(1);
expressions.put(Labels.STRIMZI_NAME_LABEL, KafkaCluster.kafkaClusterName(cluster));
labelSelector.setMatchLabels(expressions);
kafkaClusterPeer.setPodSelector(labelSelector);
NetworkPolicyPeer entityOperatorPeer = new NetworkPolicyPeer();
LabelSelector labelSelector3 = new LabelSelector();
Map<String, String> expressions3 = new HashMap<>(1);
expressions3.put(Labels.STRIMZI_NAME_LABEL, EntityOperator.entityOperatorName(cluster));
labelSelector3.setMatchLabels(expressions3);
entityOperatorPeer.setPodSelector(labelSelector3);
NetworkPolicyPeer clusterOperatorPeer = new NetworkPolicyPeer();
LabelSelector labelSelector4 = new LabelSelector();
Map<String, String> expressions4 = new HashMap<>(1);
expressions4.put(Labels.STRIMZI_KIND_LABEL, "cluster-operator");
labelSelector4.setMatchLabels(expressions4);
clusterOperatorPeer.setPodSelector(labelSelector4);
ModelUtils.setClusterOperatorNetworkPolicyNamespaceSelector(clusterOperatorPeer, namespace, operatorNamespace, operatorNamespaceLabels);
NetworkPolicyPeer cruiseControlPeer = new NetworkPolicyPeer();
LabelSelector labelSelector5 = new LabelSelector();
Map<String, String> expressions5 = new HashMap<>(1);
expressions5.put(Labels.STRIMZI_NAME_LABEL, CruiseControl.cruiseControlName(cluster));
labelSelector5.setMatchLabels(expressions5);
cruiseControlPeer.setPodSelector(labelSelector5);
// This is a hack because we have no guarantee that the CO namespace has some particular labels
List<NetworkPolicyPeer> clientsPortPeers = new ArrayList<>(4);
clientsPortPeers.add(kafkaClusterPeer);
clientsPortPeers.add(zookeeperClusterPeer);
clientsPortPeers.add(entityOperatorPeer);
clientsPortPeers.add(clusterOperatorPeer);
clientsPortPeers.add(cruiseControlPeer);
clientsIngressRule.setFrom(clientsPortPeers);
rules.add(clientsIngressRule);
if (isMetricsEnabled) {
NetworkPolicyIngressRule metricsRule = new NetworkPolicyIngressRuleBuilder().addNewPort().withNewPort(METRICS_PORT).withProtocol("TCP").endPort().withFrom().build();
rules.add(metricsRule);
}
if (isJmxEnabled) {
NetworkPolicyPort jmxPort = new NetworkPolicyPort();
jmxPort.setPort(new IntOrString(JMX_PORT));
NetworkPolicyIngressRule jmxRule = new NetworkPolicyIngressRuleBuilder().withPorts(jmxPort).withFrom().build();
rules.add(jmxRule);
}
NetworkPolicy networkPolicy = new NetworkPolicyBuilder().withNewMetadata().withName(policyName(cluster)).withNamespace(namespace).withLabels(labels.toMap()).withOwnerReferences(createOwnerReference()).endMetadata().withNewSpec().withPodSelector(labelSelector2).withIngress(rules).endSpec().build();
LOGGER.traceCr(reconciliation, "Created network policy {}", networkPolicy);
return networkPolicy;
}
use of io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicyPort in project strimzi-kafka-operator by strimzi.
the class ZookeeperCluster method generateNetworkPolicy.
/**
* Generates the NetworkPolicies relevant for ZooKeeper nodes
*
* @param operatorNamespace Namespace where the Strimzi Cluster Operator runs. Null if not configured.
* @param operatorNamespaceLabels Labels of the namespace where the Strimzi Cluster Operator runs. Null if not configured.
*
* @return The network policy.
*/
public NetworkPolicy generateNetworkPolicy(String operatorNamespace, Labels operatorNamespaceLabels) {
List<NetworkPolicyIngressRule> rules = new ArrayList<>(2);
NetworkPolicyPort clientsPort = new NetworkPolicyPort();
clientsPort.setPort(new IntOrString(CLIENT_TLS_PORT));
clientsPort.setProtocol("TCP");
NetworkPolicyPort clusteringPort = new NetworkPolicyPort();
clusteringPort.setPort(new IntOrString(CLUSTERING_PORT));
clusteringPort.setProtocol("TCP");
NetworkPolicyPort leaderElectionPort = new NetworkPolicyPort();
leaderElectionPort.setPort(new IntOrString(LEADER_ELECTION_PORT));
leaderElectionPort.setProtocol("TCP");
NetworkPolicyPeer zookeeperClusterPeer = new NetworkPolicyPeer();
LabelSelector labelSelector2 = new LabelSelector();
Map<String, String> expressions2 = new HashMap<>(1);
expressions2.put(Labels.STRIMZI_NAME_LABEL, zookeeperClusterName(cluster));
labelSelector2.setMatchLabels(expressions2);
zookeeperClusterPeer.setPodSelector(labelSelector2);
// Zookeeper only ports - 2888 & 3888 which need to be accessed by the Zookeeper cluster members only
NetworkPolicyIngressRule zookeeperClusteringIngressRule = new NetworkPolicyIngressRuleBuilder().withPorts(clusteringPort, leaderElectionPort).withFrom(zookeeperClusterPeer).build();
rules.add(zookeeperClusteringIngressRule);
// Clients port - needs to be access from outside the Zookeeper cluster as well
NetworkPolicyIngressRule clientsIngressRule = new NetworkPolicyIngressRuleBuilder().withPorts(clientsPort).withFrom().build();
NetworkPolicyPeer kafkaClusterPeer = new NetworkPolicyPeer();
LabelSelector labelSelector = new LabelSelector();
Map<String, String> expressions = new HashMap<>(1);
expressions.put(Labels.STRIMZI_NAME_LABEL, KafkaCluster.kafkaClusterName(cluster));
labelSelector.setMatchLabels(expressions);
kafkaClusterPeer.setPodSelector(labelSelector);
NetworkPolicyPeer entityOperatorPeer = new NetworkPolicyPeer();
LabelSelector labelSelector3 = new LabelSelector();
Map<String, String> expressions3 = new HashMap<>(1);
expressions3.put(Labels.STRIMZI_NAME_LABEL, EntityOperator.entityOperatorName(cluster));
labelSelector3.setMatchLabels(expressions3);
entityOperatorPeer.setPodSelector(labelSelector3);
NetworkPolicyPeer clusterOperatorPeer = new NetworkPolicyPeer();
LabelSelector labelSelector4 = new LabelSelector();
Map<String, String> expressions4 = new HashMap<>(1);
expressions4.put(Labels.STRIMZI_KIND_LABEL, "cluster-operator");
labelSelector4.setMatchLabels(expressions4);
clusterOperatorPeer.setPodSelector(labelSelector4);
ModelUtils.setClusterOperatorNetworkPolicyNamespaceSelector(clusterOperatorPeer, namespace, operatorNamespace, operatorNamespaceLabels);
NetworkPolicyPeer cruiseControlPeer = new NetworkPolicyPeer();
LabelSelector labelSelector5 = new LabelSelector();
Map<String, String> expressions5 = new HashMap<>(1);
expressions5.put(Labels.STRIMZI_NAME_LABEL, CruiseControl.cruiseControlName(cluster));
labelSelector5.setMatchLabels(expressions5);
cruiseControlPeer.setPodSelector(labelSelector5);
// This is a hack because we have no guarantee that the CO namespace has some particular labels
List<NetworkPolicyPeer> clientsPortPeers = new ArrayList<>(4);
clientsPortPeers.add(kafkaClusterPeer);
clientsPortPeers.add(zookeeperClusterPeer);
clientsPortPeers.add(entityOperatorPeer);
clientsPortPeers.add(clusterOperatorPeer);
clientsPortPeers.add(cruiseControlPeer);
clientsIngressRule.setFrom(clientsPortPeers);
rules.add(clientsIngressRule);
if (isMetricsEnabled) {
NetworkPolicyIngressRule metricsRule = new NetworkPolicyIngressRuleBuilder().addNewPort().withNewPort(METRICS_PORT).withProtocol("TCP").endPort().withFrom().build();
rules.add(metricsRule);
}
if (isJmxEnabled) {
NetworkPolicyPort jmxPort = new NetworkPolicyPort();
jmxPort.setPort(new IntOrString(JMX_PORT));
NetworkPolicyIngressRule jmxRule = new NetworkPolicyIngressRuleBuilder().withPorts(jmxPort).withFrom().build();
rules.add(jmxRule);
}
NetworkPolicy networkPolicy = new NetworkPolicyBuilder().withNewMetadata().withName(policyName(cluster)).withNamespace(namespace).withLabels(labels.toMap()).withOwnerReferences(createOwnerReference()).endMetadata().withNewSpec().withPodSelector(labelSelector2).withIngress(rules).endSpec().build();
LOGGER.traceCr(reconciliation, "Created network policy {}", networkPolicy);
return networkPolicy;
}
use of io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicyPort in project debezium by debezium.
the class OcpKafkaConnectController method allowServiceAccess.
/**
* Creates network policy allowing access to ports exposed by Kafka Connect
*
* @return
*/
public NetworkPolicy allowServiceAccess() {
LOGGER.info("Creating NetworkPolicy allowing public access to " + kafkaConnect.getMetadata().getName() + "'s services");
Map<String, String> labels = new HashMap<>();
labels.put("strimzi.io/cluster", kafkaConnect.getMetadata().getName());
labels.put("strimzi.io/kind", "KafkaConnect");
labels.put("strimzi.io/name", kafkaConnect.getMetadata().getName() + "-connect");
List<NetworkPolicyPort> ports = Stream.of(8083, 8404, 9404).map(IntOrString::new).map(p -> new NetworkPolicyPortBuilder().withProtocol("TCP").withPort(p).build()).collect(Collectors.toList());
return ocpUtils.createNetworkPolicy(project, kafkaConnect.getMetadata().getName() + "-allowed", labels, ports);
}
Aggregations