blob: 0a1c5dea3cfc9d122de47b3f8ad99264d45d75a3 [file] [log] [blame]
Jian Li07c27f32020-10-08 02:57:45 +09001/*
2 * Copyright 2020-present Open Networking Foundation
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 */
16package org.onosproject.k8snetworking.impl;
17
18import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
Andrea Campanella80427622022-06-09 08:11:25 -070019import com.eclipsesource.json.JsonObject;
Jian Li07c27f32020-10-08 02:57:45 +090020import org.onosproject.cluster.ClusterService;
21import org.onosproject.cluster.LeadershipService;
22import org.onosproject.cluster.NodeId;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.k8snetworking.api.K8sNetwork;
26import org.onosproject.k8snetworking.api.K8sNetworkService;
27import org.onosproject.k8snode.api.K8sNode;
28import org.onosproject.k8snode.api.K8sNodeEvent;
29import org.onosproject.k8snode.api.K8sNodeListener;
30import org.onosproject.k8snode.api.K8sNodeService;
31import org.osgi.service.component.annotations.Activate;
32import org.osgi.service.component.annotations.Component;
33import org.osgi.service.component.annotations.Deactivate;
34import org.osgi.service.component.annotations.Reference;
35import org.osgi.service.component.annotations.ReferenceCardinality;
36import org.slf4j.Logger;
37
38import javax.ws.rs.client.Client;
39import javax.ws.rs.client.ClientBuilder;
40import javax.ws.rs.client.Entity;
41import javax.ws.rs.client.WebTarget;
42import javax.ws.rs.core.MediaType;
43import javax.ws.rs.core.Response;
44import java.util.Objects;
45import java.util.concurrent.ExecutorService;
46
47import static java.util.concurrent.Executors.newSingleThreadExecutor;
48import static org.onlab.util.Tools.groupedThreads;
49import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
50import static org.onosproject.k8snetworking.impl.OsgiPropertyConstants.SERVICE_IP_CIDR_DEFAULT;
51import static org.onosproject.k8snetworking.util.K8sNetworkingUtil.getBclassIpPrefixFromCidr;
52import static org.onosproject.k8snode.api.K8sApiConfig.Mode.PASSTHROUGH;
53import static org.slf4j.LoggerFactory.getLogger;
54
55/**
56 * Provides kubernetes and openstack integration feature.
57 */
58@Component(immediate = true)
59public class K8sOpenstackIntegrationHandler {
60
61 private final Logger log = getLogger(getClass());
62
63 private static final String K8S_NODE_IP = "k8sNodeIp";
64 private static final String OS_K8S_INT_PORT_NAME = "osK8sIntPortName";
Jian Lif4818b02020-11-04 15:58:18 +090065 private static final String OS_K8S_EXT_PORT_NAME = "osK8sExtPortName";
Jian Li07c27f32020-10-08 02:57:45 +090066 private static final String POD_CIDR = "podCidr";
67 private static final String SERVICE_CIDR = "serviceCidr";
68 private static final String POD_GW_IP = "podGwIp";
69 private static final String K8S_INT_OS_PORT_MAC = "k8sIntOsPortMac";
70 private static final String ONOS_PORT = "8181";
71 private static final String OS_K8S_INTEGRATION_EP = "onos/openstacknetworking/integration/";
72 private static final String ONOS_USERNAME = "karaf";
73 private static final String ONOS_PASSWORD = "karaf";
74 private static final String B_CLASS_SUFFIX = ".0.0/16";
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY)
77 protected CoreService coreService;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY)
80 protected ClusterService clusterService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY)
83 protected LeadershipService leadershipService;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY)
86 protected K8sNodeService k8sNodeService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY)
89 protected K8sNetworkService k8sNetworkService;
90
91 private final InternalK8sNodeListener k8sNodeListener =
92 new InternalK8sNodeListener();
93 private final ExecutorService eventExecutor = newSingleThreadExecutor(
94 groupedThreads(this.getClass().getSimpleName(), "event-handler", log));
95
96 private ApplicationId appId;
97 private NodeId localNodeId;
98
99 @Activate
100 protected void activate() {
101 appId = coreService.registerApplication(K8S_NETWORKING_APP_ID);
102 localNodeId = clusterService.getLocalNode().id();
103 k8sNodeService.addListener(k8sNodeListener);
104
105
106 log.info("Started");
107 }
108
109 @Deactivate
110 protected void deactivate() {
111 k8sNodeService.removeListener(k8sNodeListener);
112
113 log.info("Stopped");
114 }
115
116 private void setCniPtNodeRules(K8sNode k8sNode, boolean install) {
117 K8sNetwork network = k8sNetworkService.network(k8sNode.hostname());
118 String k8sNodeIp = k8sNode.nodeIp().toString();
119 String gatewayIp = network.gatewayIp().toString();
120 String nodePodCidr = network.cidr();
121 String srcPodPrefix = getBclassIpPrefixFromCidr(nodePodCidr);
122 String podCidr = srcPodPrefix + B_CLASS_SUFFIX;
123 String osK8sIntPortName = k8sNode.osToK8sIntgPatchPortName();
Jian Lif4818b02020-11-04 15:58:18 +0900124 String k8sIntOsPortMac = k8sNode.portMacByName(k8sNode.intgBridge(),
125 k8sNode.k8sIntgToOsPatchPortName()).toString();
Jian Li07c27f32020-10-08 02:57:45 +0900126
127 String path = install ? "node/pt-install" : "node/pt-uninstall";
128
129 String jsonString = "";
130
Andrea Campanella80427622022-06-09 08:11:25 -0700131 jsonString = new JsonObject()
132 .set(K8S_NODE_IP, k8sNodeIp)
133 .set(POD_GW_IP, gatewayIp)
134 .set(POD_CIDR, podCidr)
135 .set(SERVICE_CIDR, SERVICE_IP_CIDR_DEFAULT)
136 .set(OS_K8S_INT_PORT_NAME, osK8sIntPortName)
137 .set(K8S_INT_OS_PORT_MAC, k8sIntOsPortMac)
138 .toString();
139 log.info("push integration configuration {}", jsonString);
Jian Li07c27f32020-10-08 02:57:45 +0900140
141 HttpAuthenticationFeature feature =
142 HttpAuthenticationFeature.basic(ONOS_USERNAME, ONOS_PASSWORD);
143
144 final Client client = ClientBuilder.newClient();
145 client.register(feature);
146 String host = "http://" + k8sNode.managementIp().toString() + ":" + ONOS_PORT + "/";
147 String endpoint = host + OS_K8S_INTEGRATION_EP;
148 WebTarget wt = client.target(endpoint).path(path);
149 Response response = wt.request(MediaType.APPLICATION_JSON_TYPE)
150 .put(Entity.json(jsonString));
151 final int status = response.getStatus();
152
153 if (status != 200) {
154 log.error("Failed to install/uninstall openstack k8s CNI PT rules.");
155 }
156 }
157
Jian Lif4818b02020-11-04 15:58:18 +0900158 private void setCniPtNodePortRules(K8sNode k8sNode, boolean install) {
159 String k8sNodeIp = k8sNode.nodeIp().toString();
160 String osK8sExtPortName = k8sNode.osToK8sExtPatchPortName();
161
162 String path = install ? "nodeport/pt-install" : "nodeport/pt-uninstall";
163
164 String jsonString = "";
165
Andrea Campanella80427622022-06-09 08:11:25 -0700166 jsonString = new JsonObject()
167 .set(K8S_NODE_IP, k8sNodeIp)
168 .set(SERVICE_CIDR, SERVICE_IP_CIDR_DEFAULT)
169 .set(OS_K8S_EXT_PORT_NAME, osK8sExtPortName)
170 .toString();
171 log.info("push integration configuration {}", jsonString);
Jian Lif4818b02020-11-04 15:58:18 +0900172
173 HttpAuthenticationFeature feature =
174 HttpAuthenticationFeature.basic(ONOS_USERNAME, ONOS_PASSWORD);
175
176 final Client client = ClientBuilder.newClient();
177 client.register(feature);
178 String host = "http://" + k8sNode.managementIp().toString() + ":" + ONOS_PORT + "/";
179 String endpoint = host + OS_K8S_INTEGRATION_EP;
180 WebTarget wt = client.target(endpoint).path(path);
181 Response response = wt.request(MediaType.APPLICATION_JSON_TYPE)
182 .put(Entity.json(jsonString));
183 final int status = response.getStatus();
184
185 if (status != 200) {
186 log.error("Failed to install/uninstall openstack k8s CNI PT node port rules.");
187 }
188 }
189
Jian Li07c27f32020-10-08 02:57:45 +0900190 private class InternalK8sNodeListener implements K8sNodeListener {
191
192 @Override
193 public boolean isRelevant(K8sNodeEvent event) {
194 return event.subject().mode() == PASSTHROUGH;
195 }
196
197 private boolean isRelevantHelper() {
198 return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
199 }
200
201 @Override
202 public void event(K8sNodeEvent event) {
203 switch (event.type()) {
204 case K8S_NODE_COMPLETE:
205 eventExecutor.execute(() -> processNodeCompletion(event.subject()));
206 break;
Jian Li8fa74cfa2020-11-25 16:49:34 +0900207 case K8S_NODE_OFF_BOARDED:
208 eventExecutor.execute(() -> processNodeOffboard(event.subject()));
Jian Li07c27f32020-10-08 02:57:45 +0900209 break;
210 default:
211 break;
212 }
213 }
214
215 private void processNodeCompletion(K8sNode k8sNode) {
216 if (!isRelevantHelper()) {
217 return;
218 }
219
220 setCniPtNodeRules(k8sNode, true);
Jian Lif4818b02020-11-04 15:58:18 +0900221 setCniPtNodePortRules(k8sNode, true);
Jian Li07c27f32020-10-08 02:57:45 +0900222 }
223
Jian Li8fa74cfa2020-11-25 16:49:34 +0900224 private void processNodeOffboard(K8sNode k8sNode) {
Jian Li07c27f32020-10-08 02:57:45 +0900225 if (!isRelevantHelper()) {
226 return;
227 }
228
229 setCniPtNodeRules(k8sNode, false);
Jian Lif4818b02020-11-04 15:58:18 +0900230 setCniPtNodePortRules(k8sNode, false);
Jian Li07c27f32020-10-08 02:57:45 +0900231 }
232 }
233}