Initial commit to implement external loadbalancer for kubernetes pods.
- Adds elb-related parameters in KubevirtNode class
- Defines KubernetesExternalLb and related service, manager and store.
Change-Id: I68476887af493c7daeaf1dca0320218f1764653f
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLb.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLb.java
new file mode 100644
index 0000000..96ecf12
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLb.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.api;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.Objects;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Default implementation of kubernetes external load balancer.
+ */
+public final class DefaultKubernetesExternalLb implements KubernetesExternalLb {
+
+ private static final String NOT_NULL_MSG = "Loadbalancer % cannot be null";
+
+ private final String serviceName;
+ private final String loadbalancerIp;
+ private final Set<Integer> nodePortSet;
+ private final Set<Integer> portSet;
+ private final Set<String> endpointSet;
+ private final String electedGateway;
+
+ public DefaultKubernetesExternalLb(String serviceName, String loadbalancerIp,
+ Set<Integer> nodePortSet, Set<Integer> portSet,
+ Set<String> endpointSet, String electedGateway) {
+ this.serviceName = serviceName;
+ this.loadbalancerIp = loadbalancerIp;
+ this.nodePortSet = nodePortSet;
+ this.portSet = portSet;
+ this.endpointSet = endpointSet;
+ this.electedGateway = electedGateway;
+ }
+
+ @Override
+ public String serviceName() {
+ return serviceName;
+ }
+
+ @Override
+ public String loadBalancerIp() {
+ return loadbalancerIp;
+ }
+
+ @Override
+ public Set<Integer> nodePortSet() {
+ return ImmutableSet.copyOf(nodePortSet);
+ }
+
+ @Override
+ public Set<Integer> portSet() {
+ return ImmutableSet.copyOf(portSet);
+ }
+
+ @Override
+ public Set<String> endpointSet() {
+ return ImmutableSet.copyOf(endpointSet);
+ }
+
+ @Override
+ public String electedGateway() {
+ return electedGateway;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ DefaultKubernetesExternalLb that = (DefaultKubernetesExternalLb) o;
+ return serviceName.equals(that.serviceName) && loadbalancerIp.equals(that.loadbalancerIp) &&
+ Objects.equals(nodePortSet, that.nodePortSet) &&
+ Objects.equals(portSet, that.portSet) &&
+ Objects.equals(endpointSet, that.endpointSet) &&
+ Objects.equals(electedGateway, that.electedGateway);
+ }
+
+ @Override
+ public KubernetesExternalLb updateElectedGateway(String electedGateway) {
+ return DefaultKubernetesExternalLb.builder()
+ .serviceName(serviceName)
+ .loadBalancerIp(loadbalancerIp)
+ .nodePortSet(nodePortSet)
+ .portSet(portSet)
+ .endpointSet(endpointSet)
+ .electedGateway(electedGateway)
+ .build();
+ }
+ @Override
+ public int hashCode() {
+ return Objects.hash(serviceName, loadbalancerIp);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("serviceName", serviceName)
+ .add("loadbalancerIp", loadbalancerIp)
+ .add("nodePort", nodePortSet)
+ .add("port", portSet)
+ .add("endpointSet", endpointSet)
+ .add("electedGateway", electedGateway)
+ .toString();
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static final class Builder implements KubernetesExternalLb.Builder {
+ private String serviceName;
+ private String loadbalancerIp;
+ private Set<Integer> nodePortSet;
+ private Set<Integer> portSet;
+ private Set<String> endpointSet;
+ private String electedGateway;
+
+ private Builder() {
+ }
+
+ @Override
+ public KubernetesExternalLb build() {
+ checkArgument(serviceName != null, NOT_NULL_MSG, "serviceName");
+ checkArgument(loadbalancerIp != null, NOT_NULL_MSG, "loadbalancerIp");
+ checkArgument(!nodePortSet.isEmpty(), NOT_NULL_MSG, "nodePortSet");
+ checkArgument(!portSet.isEmpty(), NOT_NULL_MSG, "portSet");
+
+ return new DefaultKubernetesExternalLb(serviceName, loadbalancerIp,
+ nodePortSet, portSet, endpointSet, electedGateway);
+ }
+
+ @Override
+ public Builder serviceName(String serviceName) {
+ this.serviceName = serviceName;
+ return this;
+ }
+
+ @Override
+ public Builder loadBalancerIp(String loadBalancerIp) {
+ this.loadbalancerIp = loadBalancerIp;
+ return this;
+ }
+
+ @Override
+ public Builder nodePortSet(Set<Integer> nodePortSet) {
+ this.nodePortSet = nodePortSet;
+ return this;
+ }
+
+ @Override
+ public Builder portSet(Set<Integer> portSet) {
+ this.portSet = portSet;
+ return this;
+ }
+
+ @Override
+ public Builder endpointSet(Set<String> endpointSet) {
+ this.endpointSet = endpointSet;
+ return this;
+ }
+
+ public Builder electedGateway(String electedGateway) {
+ this.electedGateway = electedGateway;
+ return this;
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLb.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLb.java
new file mode 100644
index 0000000..bc754db
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLb.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.api;
+
+import java.util.Set;
+
+/**
+ * Representation of kubernetes external load balancer.
+ */
+public interface KubernetesExternalLb {
+ /**
+ * Returns the service name.
+ *
+ * @return service name
+ */
+ String serviceName();
+
+ /**
+ * Returns the load balancer IP.
+ *
+ * @return load balancer IP
+ */
+ String loadBalancerIp();
+
+ /**
+ * Returns the set of node port.
+ *
+ * @return node port
+ */
+ Set<Integer> nodePortSet();
+
+ /**
+ * Returns the set of port.
+ *
+ * @return port number
+ */
+ Set<Integer> portSet();
+
+ /**
+ * Returns the set of endpoint.
+ *
+ * @return endpoint set
+ */
+ Set<String> endpointSet();
+
+ /**
+ * Returns the elected gateway node for this service.
+ *
+ * @return gateway node hostname
+ */
+ String electedGateway();
+
+ /**
+ * Updates the elected gateway node host name.
+ *
+ * @param electedGateway updated elected gateway node hostname
+ * @return kubernetes external lb with the updated gateway node hostname
+ */
+ KubernetesExternalLb updateElectedGateway(String electedGateway);
+
+ interface Builder {
+ /**
+ * Builds an immutable kubernetes external load balancer instance.
+ *
+ * @return kubernetes external load balancer
+ */
+ KubernetesExternalLb build();
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied service name.
+ *
+ * @param serviceName external load balancer service name
+ * @return external load balancer builder
+ */
+ Builder serviceName(String serviceName);
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied load balancer Ip.
+ *
+ * @param loadBalancerIp external load balancer Ip
+ * @return external load balancer builder
+ */
+ Builder loadBalancerIp(String loadBalancerIp);
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied node port set.
+ *
+ * @param nodePortSet node port set
+ * @return external load balancer builder
+ */
+ Builder nodePortSet(Set<Integer> nodePortSet);
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied port set.
+ *
+ * @param portSet port set
+ * @return external load balancer builder
+ */
+ Builder portSet(Set<Integer> portSet);
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied endpoint set.
+ *
+ * @param endpointSet endpoint set
+ * @return external load balancer builder
+ */
+ Builder endpointSet(Set<String> endpointSet);
+
+ /**
+ * Returns kubernetes external load balancer builder with supplied elected gateway.
+ *
+ * @param gateway gateway node hostname
+ * @return gateway node hostname
+ */
+ Builder electedGateway(String gateway);
+ }
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbAdminService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbAdminService.java
new file mode 100644
index 0000000..c53d409
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbAdminService.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.api;
+
+/**
+ * Service for administering the inventory of kubernetes external lb service.
+ */
+public interface KubernetesExternalLbAdminService extends KubernetesExternalLbService {
+ /**
+ * Create a kubernetes external load balancer with the given information.
+ *
+ * @param externalLb a new load balancer
+ */
+ void createExternalLb(KubernetesExternalLb externalLb);
+
+ /**
+ * Update a kubernetes external load balancer with the given information.
+ *
+ * @param externalLb the updated load balancer
+ */
+ void updateExternalLb(KubernetesExternalLb externalLb);
+
+ /**
+ * Removes the load balancer.
+ *
+ * @param serviceName load balancer name
+ */
+ void removeExternalLb(String serviceName);
+
+ /**
+ * Removes all load balancers.
+ */
+ void clear();
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbEvent.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbEvent.java
new file mode 100644
index 0000000..15f3905
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbEvent.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.api;
+
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Manages inventory of kubernetes external load balancer states; not intended for direct use.
+ */
+public class KubernetesExternalLbEvent
+ extends AbstractEvent<KubernetesExternalLbEvent.Type, KubernetesExternalLb> {
+
+ public KubernetesExternalLbEvent(Type type, KubernetesExternalLb subject) {
+ super(type, subject);
+ }
+
+ /**
+ * Kubernetes external lb events.
+ */
+ public enum Type {
+ /**
+ * Signifies that a new kubevirt load balancer is created.
+ */
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_CREATED,
+
+ /**
+ * Signifies that a kubevirt load balancer is removed.
+ */
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_REMOVED,
+
+ /**
+ * Signifies that a kubevirt load balancer is updated.
+ */
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_UPDATED,
+ }
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbListener.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbListener.java
new file mode 100644
index 0000000..1a48764
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbListener.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.api;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Listener for kubernetes external lb event.
+ */
+public interface KubernetesExternalLbListener extends EventListener<KubernetesExternalLbEvent> {
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbService.java
new file mode 100644
index 0000000..eeac4f2
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbService.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.api;
+
+import org.onosproject.event.ListenerService;
+
+import java.util.Set;
+
+public interface KubernetesExternalLbService
+ extends ListenerService<KubernetesExternalLbEvent, KubernetesExternalLbListener> {
+
+ /**
+ * Returns the kubernetes external lb with the supplied service name.
+ *
+ * @param serviceName service name
+ * @return kubernetes external load balancer
+ */
+ KubernetesExternalLb loadBalancer(String serviceName);
+
+ /**
+ * Returns all kubernetes external lb's registered in the service.
+ *
+ * @return set of kubernetes external lb's
+ */
+ Set<KubernetesExternalLb> loadBalancers();
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStore.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStore.java
new file mode 100644
index 0000000..996cbf0
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStore.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.api;
+
+import org.onosproject.store.Store;
+
+import java.util.Set;
+
+/**
+ * Manages inventory of kubernetes external load balancer states; not intended for direct use.
+ */
+public interface KubernetesExternalLbStore
+ extends Store<KubernetesExternalLbEvent, KubernetesExternalLbStoreDelegate> {
+
+ /**
+ * Creates a new kubernetes external lb.
+ *
+ * @param lb kubernetes external lb
+ */
+ void createLoadBalancer(KubernetesExternalLb lb);
+
+ /**
+ * Updates a new kubernetes external lb.
+ *
+ * @param lb kubernetes external lb
+ */
+ void updateLoadBalancer(KubernetesExternalLb lb);
+
+ /**
+ * Removes the kubernetes external lb with the given lb name.
+ *
+ * @param serviceName service name
+ * @return kubernetes external lb
+ */
+ KubernetesExternalLb removeLoadBalancer(String serviceName);
+
+ /**
+ * Returns the kubernetes external lb with the given lb name.
+ *
+ * @param serviceName service name
+ * @return kubernetes external lb
+ */
+ KubernetesExternalLb loadBalancer(String serviceName);
+
+ /**
+ * Returns all kubernetes external lbs.
+ *
+ * @return set of kubernetes external lbs
+ */
+ Set<KubernetesExternalLb> loadBalancers();
+
+ /**
+ * Removes all kubernetes external lbs.
+ */
+ void clear();
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStoreDelegate.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStoreDelegate.java
new file mode 100644
index 0000000..a7fba41
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubernetesExternalLbStoreDelegate.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.api;
+
+import org.onosproject.store.StoreDelegate;
+
+/**
+ * Kubernetes external lb store delegate abstraction.
+ */
+public interface KubernetesExternalLbStoreDelegate extends StoreDelegate<KubernetesExternalLbEvent> {
+}
diff --git a/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLbTest.java b/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLbTest.java
new file mode 100644
index 0000000..a4a661f
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/test/java/org/onosproject/kubevirtnetworking/api/DefaultKubernetesExternalLbTest.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.api;
+
+import com.google.common.collect.Sets;
+import com.google.common.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Set;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Unit tests for the default Kubernetes external lb class.
+ */
+public class DefaultKubernetesExternalLbTest {
+ private static final String SERVICE_NAME_1 = "service_name_1";
+ private static final String SERVICE_NAME_2 = "service_name_2";
+ private static final String LOADBALANCER_IP_1 = "1.1.1.1";
+ private static final String LOADBALANCER_IP_2 = "2.2.2.2";
+ private static final Set<Integer> NODE_PORT_SET_1 = Sets.newHashSet(Integer.valueOf(32080));
+ private static final Set<Integer> NODE_PORT_SET_2 = Sets.newHashSet(Integer.valueOf(33080));
+ private static final Set<Integer> PORT_SET_1 = Sets.newHashSet(Integer.valueOf(8080));
+ private static final Set<Integer> PORT_SET_2 = Sets.newHashSet(Integer.valueOf(9090));
+ private static final Set<String> ENDPOINT_SET_1 = Sets.newHashSet(String.valueOf("1.1.2.1"));
+ private static final Set<String> ENDPOINT_SET_2 = Sets.newHashSet(String.valueOf("1.1.2.2"));
+ private static final String ELECTED_GATEWAY_1 = "gateway1";
+ private static final String ELECTED_GATEWAY_2 = "gateway2";
+
+ private KubernetesExternalLb lb1;
+ private KubernetesExternalLb sameAsLb1;
+ private KubernetesExternalLb lb2;
+
+ /**
+ * Tests class immutability.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(DefaultKubernetesExternalLb.class);
+ }
+
+ /**
+ * Initial setup for this unit test.
+ */
+ @Before
+ public void setUp() {
+ lb1 = DefaultKubernetesExternalLb.builder()
+ .serviceName(SERVICE_NAME_1)
+ .loadBalancerIp(LOADBALANCER_IP_1)
+ .nodePortSet(NODE_PORT_SET_1)
+ .portSet(PORT_SET_1)
+ .endpointSet(ENDPOINT_SET_1)
+ .electedGateway(ELECTED_GATEWAY_1)
+ .build();
+
+ sameAsLb1 = DefaultKubernetesExternalLb.builder()
+ .serviceName(SERVICE_NAME_1)
+ .loadBalancerIp(LOADBALANCER_IP_1)
+ .nodePortSet(NODE_PORT_SET_1)
+ .portSet(PORT_SET_1)
+ .endpointSet(ENDPOINT_SET_1)
+ .electedGateway(ELECTED_GATEWAY_1)
+ .build();
+
+ lb2 = DefaultKubernetesExternalLb.builder()
+ .serviceName(SERVICE_NAME_2)
+ .loadBalancerIp(LOADBALANCER_IP_2)
+ .nodePortSet(NODE_PORT_SET_2)
+ .portSet(PORT_SET_2)
+ .endpointSet(ENDPOINT_SET_2)
+ .electedGateway(ELECTED_GATEWAY_2)
+ .build();
+ }
+
+ /**
+ * Tests object equality.
+ */
+ @Test
+ public void testEquality() {
+ new EqualsTester().addEqualityGroup(lb1, sameAsLb1)
+ .addEqualityGroup(lb2)
+ .testEquals();
+ }
+
+ /**
+ * Test object construction.
+ */
+ @Test
+ public void testConstruction() {
+ KubernetesExternalLb lb = lb1;
+
+ assertEquals(SERVICE_NAME_1, lb1.serviceName());
+ assertEquals(LOADBALANCER_IP_1, lb1.loadBalancerIp());
+ assertEquals(NODE_PORT_SET_1, lb1.nodePortSet());
+ assertEquals(PORT_SET_1, lb1.portSet());
+ assertEquals(ENDPOINT_SET_1, lb1.endpointSet());
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubernetesExternalLbStore.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubernetesExternalLbStore.java
new file mode 100644
index 0000000..fce69d7
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/DistributedKubernetesExternalLbStore.java
@@ -0,0 +1,179 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.DefaultKubernetesExternalLb;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLb;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStore;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStoreDelegate;
+import org.onosproject.store.AbstractStore;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.MapEvent;
+import org.onosproject.store.service.MapEventListener;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_CREATED;
+import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_REMOVED;
+import static org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent.Type.KUBERNETES_EXTERNAL_LOAD_BALANCER_UPDATED;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of kubernetes external lb store using consistent map.
+ */
+@Component(immediate = true, service = KubernetesExternalLbStore.class)
+public class DistributedKubernetesExternalLbStore
+ extends AbstractStore<KubernetesExternalLbEvent, KubernetesExternalLbStoreDelegate>
+ implements KubernetesExternalLbStore {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String ERR_NOT_FOUND = " does not exist";
+ private static final String ERR_DUPLICATE = " already exists";
+ private static final String APP_ID = "org.onosproject.kubevirtnetwork";
+
+ private static final KryoNamespace SERIALIZER_KUBERNETES_EXTERNAL_LB = KryoNamespace.newBuilder()
+ .register(KryoNamespaces.API)
+ .register(KubernetesExternalLb.class)
+ .register(DefaultKubernetesExternalLb.class)
+ .register(Collection.class)
+ .build();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected StorageService storageService;
+
+ private final ExecutorService eventExecutor = newSingleThreadExecutor(
+ groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
+
+ private final MapEventListener<String, KubernetesExternalLb> lbMapEventListener =
+ new KubernetesExternalLbMapListener();
+
+ private ConsistentMap<String, KubernetesExternalLb> lbStore;
+
+ @Activate
+ protected void activate() {
+ ApplicationId appId = coreService.registerApplication(APP_ID);
+ lbStore = storageService.<String, KubernetesExternalLb>consistentMapBuilder()
+ .withSerializer(Serializer.using(SERIALIZER_KUBERNETES_EXTERNAL_LB))
+ .withName("kubernetes-lbstore")
+ .withApplicationId(appId)
+ .build();
+
+ lbStore.addListener(lbMapEventListener);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ lbStore.removeListener(lbMapEventListener);
+ eventExecutor.shutdown();
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createLoadBalancer(KubernetesExternalLb lb) {
+ lbStore.compute(lb.serviceName(), (seviceName, existing) -> {
+ final String error = lb.serviceName() + ERR_DUPLICATE;
+ checkArgument(existing == null, error);
+ return lb;
+ });
+ }
+
+ @Override
+ public void updateLoadBalancer(KubernetesExternalLb lb) {
+ lbStore.compute(lb.serviceName(), (seviceName, existing) -> {
+ final String error = lb.serviceName() + ERR_NOT_FOUND;
+ checkArgument(existing != null, error);
+ return lb;
+ });
+ }
+
+ @Override
+ public KubernetesExternalLb removeLoadBalancer(String serviceName) {
+
+ Versioned<KubernetesExternalLb> lb = lbStore.remove(serviceName);
+
+ if (lb == null) {
+ final String error = serviceName + ERR_NOT_FOUND;
+ throw new IllegalArgumentException(error);
+ }
+ return lb.value();
+ }
+
+ @Override
+ public KubernetesExternalLb loadBalancer(String serviceName) {
+ return lbStore.asJavaMap().get(serviceName);
+ }
+
+ @Override
+ public Set<KubernetesExternalLb> loadBalancers() {
+ return ImmutableSet.copyOf(lbStore.asJavaMap().values());
+ }
+
+ @Override
+ public void clear() {
+ lbStore.clear();
+ }
+
+ private class KubernetesExternalLbMapListener implements MapEventListener<String, KubernetesExternalLb> {
+ @Override
+ public void event(MapEvent<String, KubernetesExternalLb> event) {
+ switch (event.type()) {
+ case INSERT:
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubernetesExternalLbEvent(
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_CREATED, event.newValue().value())));
+ break;
+ case UPDATE:
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubernetesExternalLbEvent(
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_UPDATED, event.newValue().value())));
+ break;
+ case REMOVE:
+ eventExecutor.execute(() ->
+ notifyDelegate(new KubernetesExternalLbEvent(
+ KUBERNETES_EXTERNAL_LOAD_BALANCER_REMOVED, event.newValue().value())));
+ break;
+ default:
+ //do nothing
+ break;
+ }
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbManager.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbManager.java
new file mode 100644
index 0000000..e68d289
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubernetesExternalLbManager.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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 org.onosproject.kubevirtnetworking.impl;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.event.ListenerRegistry;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLb;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbAdminService;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbEvent;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbListener;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbService;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStore;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLbStoreDelegate;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provides implementation of administrating and interfacing kubernetes external lb.
+ */
+@Component(
+ immediate = true,
+ service = {KubernetesExternalLbAdminService.class, KubernetesExternalLbService.class}
+)
+public class KubernetesExternalLbManager
+ extends ListenerRegistry<KubernetesExternalLbEvent, KubernetesExternalLbListener>
+ implements KubernetesExternalLbAdminService, KubernetesExternalLbService {
+
+ protected final Logger log = getLogger(getClass());
+
+ private static final String MSG_LOAD_BALANCER = "Kubernetes external lb %s %s";
+ private static final String MSG_CREATED = "created";
+ private static final String MSG_UPDATED = "updated";
+ private static final String MSG_REMOVED = "removed";
+
+ private static final String ERR_NULL_LOAD_BALANCER = "Kubernetes external lb cannot be null";
+ private static final String ERR_NULL_LOAD_BALANCER_NAME = "Kubernetes external lb name cannot be null";
+ private static final String ERR_IN_USE = " still in use";
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected KubernetesExternalLbStore lbStore;
+
+ private final InternalKubernetesExternalLbStorageDelegate delegate =
+ new InternalKubernetesExternalLbStorageDelegate();
+
+ private ApplicationId appId;
+
+ @Activate
+ protected void activate() {
+ appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID);
+
+ lbStore.setDelegate(delegate);
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ lbStore.unsetDelegate(delegate);
+ log.info("Stopped");
+ }
+
+ @Override
+ public void createExternalLb(KubernetesExternalLb externalLb) {
+ checkNotNull(externalLb, ERR_NULL_LOAD_BALANCER);
+ checkArgument(!Strings.isNullOrEmpty(externalLb.serviceName()), ERR_NULL_LOAD_BALANCER_NAME);
+
+
+ lbStore.createLoadBalancer(externalLb);
+ log.info(String.format(MSG_LOAD_BALANCER, externalLb.serviceName(), MSG_CREATED));
+
+ }
+
+ @Override
+ public void updateExternalLb(KubernetesExternalLb externalLb) {
+ checkNotNull(externalLb, ERR_NULL_LOAD_BALANCER);
+ checkArgument(!Strings.isNullOrEmpty(externalLb.serviceName()), ERR_NULL_LOAD_BALANCER_NAME);
+
+ lbStore.updateLoadBalancer(externalLb);
+ log.info(String.format(MSG_LOAD_BALANCER, externalLb.serviceName(), MSG_UPDATED));
+ }
+
+ @Override
+ public void removeExternalLb(String serviceName) {
+ checkArgument(serviceName != null, ERR_NULL_LOAD_BALANCER_NAME);
+
+ synchronized (this) {
+ KubernetesExternalLb externalLb = lbStore.removeLoadBalancer(serviceName);
+
+ if (externalLb != null) {
+ log.info(String.format(MSG_LOAD_BALANCER, externalLb.serviceName(), MSG_REMOVED));
+ }
+ }
+ }
+
+ @Override
+ public void clear() {
+ lbStore.clear();
+ }
+
+ @Override
+ public KubernetesExternalLb loadBalancer(String serviceName) {
+ checkArgument(!Strings.isNullOrEmpty(serviceName), ERR_NULL_LOAD_BALANCER_NAME);
+
+ return lbStore.loadBalancer(serviceName);
+ }
+
+ @Override
+ public Set<KubernetesExternalLb> loadBalancers() {
+ return ImmutableSet.copyOf(lbStore.loadBalancers());
+ }
+
+ private class InternalKubernetesExternalLbStorageDelegate
+ implements KubernetesExternalLbStoreDelegate {
+
+ @Override
+ public void notify(KubernetesExternalLbEvent event) {
+ log.trace("send kubernetes external lb event {}", event);
+ process(event);
+ }
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
index de71f29..3496008 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
@@ -41,6 +41,7 @@
import org.onosproject.cfg.ConfigProperty;
import org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork;
import org.onosproject.kubevirtnetworking.api.DefaultKubevirtPort;
+import org.onosproject.kubevirtnetworking.api.KubernetesExternalLb;
import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer;
@@ -542,6 +543,28 @@
}
/**
+ * Returns the gateway node for the specified kubernetes external lb.
+ * Among gateways, only one gateway would act as a gateway per external lb.
+ * Currently gateway node is selected based on modulo operation with router hashcode.
+ *
+ * @param nodeService kubevirt node service
+ * @param externalLb kubernetes external lb
+ * @return elected gateway node
+ */
+ public static KubevirtNode gatewayNodeForSpecifiedService(KubevirtNodeService nodeService,
+ KubernetesExternalLb externalLb) {
+ //TODO: enhance election logic for a better load balancing
+
+ int numOfGateways = nodeService.completeNodes(GATEWAY).size();
+ if (numOfGateways == 0) {
+ return null;
+ }
+
+ return (KubevirtNode) nodeService.completeNodes(GATEWAY)
+ .toArray()[externalLb.hashCode() % numOfGateways];
+ }
+
+ /**
* Returns the mac address of the router.
*
* @param router kubevirt router
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtNode.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtNode.java
index 45c2e6c..16f7034 100644
--- a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtNode.java
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/DefaultKubevirtNode.java
@@ -19,6 +19,7 @@
import org.apache.commons.lang.StringUtils;
import org.onlab.osgi.DefaultServiceDirectory;
import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
@@ -58,6 +59,10 @@
private final KubevirtNodeState state;
private final Collection<KubevirtPhyInterface> phyIntfs;
private final String gatewayBridgeName;
+ private final String elbBridgeName;
+ private final IpAddress elbIp;
+ private final IpAddress elbGwIp;
+ private final MacAddress elbGwMac;
/**
* A default constructor of kubevirt node.
@@ -72,13 +77,18 @@
* @param state node state
* @param phyIntfs physical interfaces
* @param gatewayBridgeName gateway bridge name
+ * @param elbBridgeName elb bridge name
+ * @param elbIp elb IP address
+ * @param elbGwIp elb gw IP address
+ * @param elbGwMac elb gw MAC address
*/
protected DefaultKubevirtNode(String clusterName, String hostname, Type type,
DeviceId intgBridge, DeviceId tunBridge,
IpAddress managementIp, IpAddress dataIp,
KubevirtNodeState state,
Collection<KubevirtPhyInterface> phyIntfs,
- String gatewayBridgeName) {
+ String gatewayBridgeName, String elbBridgeName, IpAddress elbIp,
+ IpAddress elbGwIp, MacAddress elbGwMac) {
this.clusterName = clusterName;
this.hostname = hostname;
this.type = type;
@@ -89,6 +99,10 @@
this.state = state;
this.phyIntfs = phyIntfs;
this.gatewayBridgeName = gatewayBridgeName;
+ this.elbBridgeName = elbBridgeName;
+ this.elbIp = elbIp;
+ this.elbGwIp = elbGwIp;
+ this.elbGwMac = elbGwMac;
}
@Override
@@ -149,6 +163,10 @@
.state(newState)
.phyIntfs(phyIntfs)
.gatewayBridgeName(gatewayBridgeName)
+ .elbBridgeName(elbBridgeName)
+ .elbIp(elbIp)
+ .elbGwIp(elbGwIp)
+ .elbGwMac(elbGwMac)
.build();
}
@@ -165,6 +183,10 @@
.state(state)
.phyIntfs(phyIntfs)
.gatewayBridgeName(gatewayBridgeName)
+ .elbBridgeName(elbBridgeName)
+ .elbIp(elbIp)
+ .elbGwIp(elbGwIp)
+ .elbGwMac(elbGwMac)
.build();
}
@@ -181,6 +203,10 @@
.state(state)
.phyIntfs(phyIntfs)
.gatewayBridgeName(gatewayBridgeName)
+ .elbBridgeName(elbBridgeName)
+ .elbIp(elbIp)
+ .elbGwIp(elbGwIp)
+ .elbGwMac(elbGwMac)
.build();
}
@@ -232,6 +258,26 @@
return gatewayBridgeName;
}
+ @Override
+ public String elbBridgeName() {
+ return elbBridgeName;
+ }
+
+ @Override
+ public IpAddress elbIp() {
+ return elbIp;
+ }
+
+ @Override
+ public IpAddress elbGwIp() {
+ return elbGwIp;
+ }
+
+ @Override
+ public MacAddress elbGwMac() {
+ return elbGwMac;
+ }
+
private PortNumber tunnelPort(String tunnelType) {
if (dataIp == null) {
return null;
@@ -296,7 +342,11 @@
.dataIp(node.dataIp())
.state(node.state())
.phyIntfs(node.phyIntfs())
- .gatewayBridgeName(node.gatewayBridgeName());
+ .gatewayBridgeName(node.gatewayBridgeName())
+ .elbBridgeName(node.elbBridgeName())
+ .elbIp(node.elbIp())
+ .elbGwIp(node.elbGwIp())
+ .elbGwMac(node.elbGwMac());
}
@Override
@@ -336,6 +386,10 @@
.add("state", state)
.add("phyIntfs", phyIntfs)
.add("gatewayBridgeName", gatewayBridgeName)
+ .add("elbBridgeName", elbBridgeName)
+ .add("elbIp", elbIp)
+ .add("elbGwIp", elbGwIp)
+ .add("elbGwMac", elbGwMac)
.toString();
}
@@ -351,6 +405,10 @@
private KubevirtNodeState state;
private Collection<KubevirtPhyInterface> phyIntfs;
private String gatewayBridgeName;
+ private String elbBridgeName;
+ private IpAddress elbIp;
+ private IpAddress elbGwIp;
+ private MacAddress elbGwMac;
// private constructor not intended to use from external
private Builder() {
@@ -377,7 +435,11 @@
dataIp,
state,
phyIntfs,
- gatewayBridgeName
+ gatewayBridgeName,
+ elbBridgeName,
+ elbIp,
+ elbGwIp,
+ elbGwMac
);
}
@@ -440,5 +502,29 @@
this.gatewayBridgeName = gatewayBridgeName;
return this;
}
+
+ @Override
+ public Builder elbBridgeName(String elbBridgeName) {
+ this.elbBridgeName = elbBridgeName;
+ return this;
+ }
+
+ @Override
+ public Builder elbIp(IpAddress elbIp) {
+ this.elbIp = elbIp;
+ return this;
+ }
+
+ @Override
+ public Builder elbGwIp(IpAddress elbGwIp) {
+ this.elbGwIp = elbGwIp;
+ return this;
+ }
+
+ @Override
+ public Builder elbGwMac(MacAddress elbGwMac) {
+ this.elbGwMac = elbGwMac;
+ return this;
+ }
}
}
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNode.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNode.java
index 8c4844d..0015105 100644
--- a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNode.java
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/KubevirtNode.java
@@ -16,6 +16,7 @@
package org.onosproject.kubevirtnode.api;
import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
@@ -189,6 +190,36 @@
String gatewayBridgeName();
/**
+ * Returns the name of the elb bridge.
+ * Using this bridge, TEG internally communicates with data IP's in worker nodes.
+ *
+ * @return gateway bridge name
+ */
+ String elbBridgeName();
+
+ /**
+ * Returns the internal Ip Address of TEG for kubernetes external lb purpose.
+ *
+ * @return elb ip address
+ */
+ IpAddress elbIp();
+
+ /**
+ * Returns the gateway IP of the elb IP.
+ *
+ * @return elb gw ip address
+ */
+ IpAddress elbGwIp();
+
+ /**
+ * Returns the mac address of the elb gw.
+ *
+ * @return elb gw mac address
+ */
+ MacAddress elbGwMac();
+
+
+ /**
* Builder of new node entity.
*/
interface Builder {
@@ -278,5 +309,37 @@
* @return kubevirt node builder
*/
KubevirtNode.Builder gatewayBridgeName(String gatewayBridgeName);
+
+ /**
+ * Returns kubevirt node builder with supplied elb bridge name.
+ *
+ * @param elbBridgeName elb bridge name
+ * @return kubevirt node builder
+ */
+ KubevirtNode.Builder elbBridgeName(String elbBridgeName);
+
+ /**
+ * Returns kubevirt node builder with supplied supplied elb Ip address.
+ *
+ * @param elbIp elb ip address
+ * @return kubevirt node builder
+ */
+ KubevirtNode.Builder elbIp(IpAddress elbIp);
+
+ /**
+ * Returns kubevirt node builder with supplied supplied elb gw Ip address.
+ *
+ * @param elbGwIp elb gw ip address
+ * @return kubevirt node builder
+ */
+ KubevirtNode.Builder elbGwIp(IpAddress elbGwIp);
+
+ /**
+ * Returns kubevirt node builder with supplied supplied elb gw MAC address.
+ *
+ * @param elbGwMac elb gw mac address
+ * @return kubevirt node builder
+ */
+ KubevirtNode.Builder elbGwMac(MacAddress elbGwMac);
}
}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtShowNodeCommand.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtShowNodeCommand.java
index 5884b8d..5927530 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtShowNodeCommand.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/cli/KubevirtShowNodeCommand.java
@@ -67,6 +67,7 @@
}
private void printNode(KubevirtNode node) {
+ print("node: %s", node.toString());
print("Name: %s", node.hostname());
print(" Type: %s", node.type());
print(" State: %s", node.state());
@@ -82,6 +83,17 @@
print(" Interface: %s", intf.intf());
counter++;
}
+
+ if (node.gatewayBridgeName() != null) {
+ print(" GatewayBridgeName: %s", node.gatewayBridgeName());
+ }
+
+ if (node.elbBridgeName() != null && node.elbIp() != null) {
+ print(" ElbBridgeName: %s", node.elbBridgeName());
+ print(" ElbIp: %s", node.elbIp().toString());
+ print(" ElbGwIp: %s", node.elbGwIp().toString());
+ print(" ElbGwMac: %s", node.elbGwMac().toString());
+ }
}
private String json(KubevirtNode node) {
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodec.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodec.java
index f1ea1c9..ec6f6bf 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodec.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodec.java
@@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.kubevirtnode.api.DefaultKubevirtNode;
@@ -52,6 +53,10 @@
private static final String STATE = "state";
private static final String PHYSICAL_INTERFACES = "phyIntfs";
private static final String GATEWAY_BRIDGE_NAME = "gatewayBridgeName";
+ private static final String ELB_BRIDGE_NAME = "elbBridgeName";
+ private static final String ELB_IP = "elbIp";
+ private static final String ELB_GW_IP = "elbGwIp";
+ private static final String ELB_GW_MAC = "elbGwMac";
private static final String MISSING_MESSAGE = " is required in OpenstackNode";
@@ -96,6 +101,23 @@
result.put(GATEWAY_BRIDGE_NAME, node.gatewayBridgeName());
}
+ //serialize elb bridge and ip address if exist
+ if (node.elbBridgeName() != null) {
+ result.put(ELB_BRIDGE_NAME, node.elbBridgeName());
+ }
+
+ if (node.elbIp() != null) {
+ result.put(ELB_IP, node.elbIp().toString());
+ }
+
+ if (node.elbGwIp() != null) {
+ result.put(ELB_GW_IP, node.elbGwIp().toString());
+ }
+
+ if (node.elbGwMac() != null) {
+ result.put(ELB_GW_MAC, node.elbGwMac().toString());
+ }
+
return result;
}
@@ -151,7 +173,27 @@
nodeBuilder.gatewayBridgeName(externalBridgeJson.asText());
}
- log.trace("node is {}", nodeBuilder.build().toString());
+ JsonNode elbBridgeJson = json.get(ELB_BRIDGE_NAME);
+ if (elbBridgeJson != null) {
+ nodeBuilder.elbBridgeName(elbBridgeJson.asText());
+ }
+
+ JsonNode elbIpJson = json.get(ELB_IP);
+ if (elbIpJson != null) {
+ nodeBuilder.elbIp(IpAddress.valueOf(elbIpJson.asText()));
+ }
+
+ JsonNode elbGwIpJson = json.get(ELB_GW_IP);
+ if (elbIpJson != null) {
+ nodeBuilder.elbGwIp(IpAddress.valueOf(elbGwIpJson.asText()));
+ }
+
+ JsonNode elbGwMacJson = json.get(ELB_GW_MAC);
+ if (elbIpJson != null) {
+ nodeBuilder.elbGwMac(MacAddress.valueOf(elbGwMacJson.asText()));
+ }
+
+ log.trace("node is {}", nodeBuilder.build());
return nodeBuilder.build();
}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtNodeWatcher.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtNodeWatcher.java
index 96662c4..5ff2856 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtNodeWatcher.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/impl/KubevirtNodeWatcher.java
@@ -185,10 +185,8 @@
return;
}
- log.trace("Process node {} creating event from API server.",
- node.getMetadata().getName());
-
KubevirtNode kubevirtNode = buildKubevirtNode(node);
+ log.info("buildKubevirtNode: {}", kubevirtNode.toString());
if (kubevirtNode.type() == WORKER || kubevirtNode.type() == GATEWAY) {
if (!kubevirtNodeAdminService.hasNode(kubevirtNode.hostname())) {
kubevirtNodeAdminService.createNode(kubevirtNode);
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java
index f857364..2589be5 100644
--- a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/util/KubevirtNodeUtil.java
@@ -30,6 +30,7 @@
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
import org.onosproject.kubevirtnode.api.DefaultKubevirtNode;
import org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface;
import org.onosproject.kubevirtnode.api.KubevirtApiConfig;
@@ -84,6 +85,11 @@
private static final String DATA_IP_KEY = SONA_PROJECT_DOMAIN + "/data-ip";
private static final String GATEWAY_CONFIG_KEY = SONA_PROJECT_DOMAIN + "/gateway-config";
private static final String GATEWAY_BRIDGE_NAME = "gatewayBridgeName";
+ private static final String ELB_CONFIG_KEY = SONA_PROJECT_DOMAIN + "/elb-config";
+ private static final String ELB_BRIDGE_NAME = "elbBridgeName";
+ private static final String ELB_IP_KEY = SONA_PROJECT_DOMAIN + "/elb-ip";
+ private static final String ELB_GW_IP_KEY = SONA_PROJECT_DOMAIN + "/elb-gw-ip";
+ private static final String ELB_GW_MAC_KEY = SONA_PROJECT_DOMAIN + "/elb-gw-mac";
private static final String NETWORK_KEY = "network";
private static final String INTERFACE_KEY = "interface";
private static final String PHYS_BRIDGE_ID = "physBridgeId";
@@ -386,6 +392,16 @@
String dataIpStr = annots.get(DATA_IP_KEY);
Set<KubevirtPhyInterface> phys = new HashSet<>();
String gatewayBridgeName = null;
+
+ String elbConfig = annots.get(ELB_CONFIG_KEY);
+ String elbIpStr = annots.get(ELB_IP_KEY);
+ String elbGwIpStr = annots.get(ELB_GW_IP_KEY);
+ String elbGwMacStr = annots.get(ELB_GW_MAC_KEY);
+ String elbBridgeName = null;
+ IpAddress elbIp = null;
+ IpAddress elbGwIp = null;
+ MacAddress elbGwMac = null;
+
try {
if (physnetConfig != null) {
JsonArray configJson = JsonArray.readFrom(physnetConfig);
@@ -423,6 +439,20 @@
nodeType = GATEWAY;
gatewayBridgeName = jsonNode.get(GATEWAY_BRIDGE_NAME).asText();
+
+ if (elbConfig != null && elbIpStr != null && elbGwIpStr != null) {
+ JsonNode elbJsonNode = new ObjectMapper().readTree(elbConfig);
+
+ elbBridgeName = elbJsonNode.get(ELB_BRIDGE_NAME).asText();
+ elbIp = IpAddress.valueOf(elbIpStr);
+ elbGwIp = IpAddress.valueOf(elbGwIpStr);
+ }
+
+ if (elbGwMacStr != null) {
+ elbGwMac = MacAddress.valueOf(elbGwMacStr);
+ } else {
+ //TODO: add the logic that retrieves the MAC address of the elb gw ip.
+ }
}
} catch (JsonProcessingException e) {
log.error("Failed to parse physnet config or gateway config object", e);
@@ -453,6 +483,10 @@
.state(KubevirtNodeState.ON_BOARDED)
.phyIntfs(phys)
.gatewayBridgeName(gatewayBridgeName)
+ .elbBridgeName(elbBridgeName)
+ .elbIp(elbIp)
+ .elbGwIp(elbGwIp)
+ .elbGwMac(elbGwMac)
.build();
}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodecTest.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodecTest.java
index ae3467a..8f89152 100644
--- a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodecTest.java
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodecTest.java
@@ -23,6 +23,7 @@
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.codec.impl.CodecManager;
@@ -105,6 +106,8 @@
.tunBridge(DeviceId.deviceId("br-tun"))
.dataIp(IpAddress.valueOf("20.20.20.2"))
.phyIntfs(ImmutableList.of(phyIntf1, phyIntf2))
+ .elbBridgeName("elbnet")
+ .elbIp(IpAddress.valueOf("10.10.10.1"))
.build();
ObjectNode nodeJson = kubevirtNodeCodec.encode(node, context);
@@ -163,10 +166,15 @@
.tunBridge(DeviceId.deviceId("br-tun"))
.dataIp(IpAddress.valueOf("20.20.20.2"))
.gatewayBridgeName("gateway")
+ .elbBridgeName("elbnet")
+ .elbIp(IpAddress.valueOf("192.168.0.2"))
+ .elbGwIp(IpAddress.valueOf("192.168.0.1"))
+ .elbGwMac(MacAddress.valueOf("AA:BB:CC:DD:EE:FF"))
.build();
ObjectNode nodeJson = kubevirtNodeCodec.encode(node, context);
assertThat(nodeJson, matchesKubevirtNode(node));
+
}
/**
@@ -185,6 +193,10 @@
assertThat(node.intgBridge().toString(), is("of:00000000000000a1"));
assertThat(node.tunBridge().toString(), is("of:00000000000000a2"));
assertThat(node.gatewayBridgeName(), is("gateway"));
+ assertThat(node.elbBridgeName(), is("elbnet"));
+ assertThat(node.elbIp().toString(), is("192.168.0.2"));
+ assertThat(node.elbGwIp().toString(), is("192.168.0.1"));
+ assertThat(node.elbGwMac().toString(), is("AA:BB:CC:DD:EE:FF"));
}
/**
diff --git a/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtGatewayNode.json b/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtGatewayNode.json
index 1284fec..85a11a7 100644
--- a/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtGatewayNode.json
+++ b/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtGatewayNode.json
@@ -5,5 +5,9 @@
"dataIp": "172.16.130.4",
"integrationBridge": "of:00000000000000a1",
"tunnelBridge": "of:00000000000000a2",
- "gatewayBridgeName": "gateway"
+ "gatewayBridgeName": "gateway",
+ "elbBridgeName": "elbnet",
+ "elbIp": "192.168.0.2",
+ "elbGwIp": "192.168.0.1",
+ "elbGwMac": "aa:bb:cc:dd:ee:ff"
}
\ No newline at end of file