use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sManagementWebResource method syncStates.
/**
* Synchronizes the all states with kubernetes API server.
*
* @return 200 OK with sync result, 404 not found
* @throws InterruptedException exception
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("sync/states")
public Response syncStates() {
K8sApiConfig config = configService.apiConfigs().stream().findAny().orElse(null);
if (config == null) {
throw new ItemNotFoundException("Failed to find valid kubernetes API configuration.");
}
KubernetesClient client = K8sNetworkingUtil.k8sClient(config);
if (client == null) {
throw new ItemNotFoundException("Failed to connect to kubernetes API server.");
}
client.namespaces().list().getItems().forEach(ns -> {
if (namespaceAdminService.namespace(ns.getMetadata().getUid()) != null) {
namespaceAdminService.updateNamespace(ns);
} else {
namespaceAdminService.createNamespace(ns);
}
});
client.services().inAnyNamespace().list().getItems().forEach(svc -> {
if (serviceAdminService.service(svc.getMetadata().getUid()) != null) {
serviceAdminService.updateService(svc);
} else {
serviceAdminService.createService(svc);
}
});
client.endpoints().inAnyNamespace().list().getItems().forEach(ep -> {
if (endpointsAdminService.endpoints(ep.getMetadata().getUid()) != null) {
endpointsAdminService.updateEndpoints(ep);
} else {
endpointsAdminService.createEndpoints(ep);
}
});
client.pods().inAnyNamespace().list().getItems().forEach(pod -> {
if (podAdminService.pod(pod.getMetadata().getUid()) != null) {
podAdminService.updatePod(pod);
} else {
podAdminService.createPod(pod);
}
syncPortFromPod(pod, networkAdminService);
});
client.extensions().ingresses().inAnyNamespace().list().getItems().forEach(ingress -> {
if (ingressAdminService.ingress(ingress.getMetadata().getUid()) != null) {
ingressAdminService.updateIngress(ingress);
} else {
ingressAdminService.createIngress(ingress);
}
});
client.network().networkPolicies().inAnyNamespace().list().getItems().forEach(policy -> {
if (policyAdminService.networkPolicy(policy.getMetadata().getUid()) != null) {
policyAdminService.updateNetworkPolicy(policy);
} else {
policyAdminService.createNetworkPolicy(policy);
}
});
return ok(mapper().createObjectNode()).build();
}
use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sNodeWebResource method deleteApiConfig.
/**
* Removes a kubernetes API config.
*
* @param endpoint kubernetes API endpoint
* @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed
*/
@DELETE
@Path("api/{endpoint : .+}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response deleteApiConfig(@PathParam("endpoint") String endpoint) {
log.trace(String.format(MESSAGE_NODE, REMOVE));
K8sApiConfig existing = configAdminService.apiConfig(nullIsIllegal(endpoint, ENDPOINT + ERROR_MESSAGE));
if (existing == null) {
log.warn("There is no API configuration to delete : {}", endpoint);
return Response.notModified().build();
} else {
configAdminService.removeApiConfig(endpoint);
}
return Response.noContent().build();
}
use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sNodeWebResource method readApiConfigConfiguration.
private Set<K8sApiConfig> readApiConfigConfiguration(InputStream input) {
Set<K8sApiConfig> configSet = Sets.newHashSet();
try {
JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
ArrayNode configs = (ArrayNode) jsonTree.path(API_CONFIGS);
configs.forEach(config -> {
try {
ObjectNode objectNode = config.deepCopy();
K8sApiConfig k8sApiConfig = codec(K8sApiConfig.class).decode(objectNode, this);
configSet.add(k8sApiConfig);
} catch (Exception e) {
log.error("Exception occurred due to {}", e);
throw new IllegalArgumentException();
}
});
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return configSet;
}
use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sApiConfigCodec method decode.
@Override
public K8sApiConfig decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
JsonNode clusterNameJson = json.get(CLUSTER_NAME);
String clusterNameStr = "";
if (clusterNameJson == null) {
clusterNameStr = DEFAULT_CLUSTER_NAME;
} else {
clusterNameStr = clusterNameJson.asText();
}
JsonNode segmentIdJson = json.get(SEGMENT_ID);
int segmentId = DEFAULT_SEGMENT_ID;
if (segmentIdJson != null) {
segmentId = segmentIdJson.asInt();
}
JsonNode modeJson = json.get(MODE);
String modeStr = "";
if (modeJson == null) {
modeStr = DEFAULT_CONFIG_MODE;
} else {
modeStr = modeJson.asText();
}
Mode mode = Mode.valueOf(modeStr);
Scheme scheme = Scheme.valueOf(nullIsIllegal(json.get(SCHEME).asText(), SCHEME + MISSING_MESSAGE));
IpAddress ipAddress = IpAddress.valueOf(nullIsIllegal(json.get(IP_ADDRESS).asText(), IP_ADDRESS + MISSING_MESSAGE));
int port = json.get(PORT).asInt();
K8sApiConfig.Builder builder = DefaultK8sApiConfig.builder().clusterName(clusterNameStr).segmentId(segmentId).mode(mode).scheme(scheme).ipAddress(ipAddress).port(port).state(DISCONNECTED);
JsonNode dvrJson = json.get(DVR);
if (dvrJson != null) {
builder.dvr(dvrJson.asBoolean());
}
JsonNode tokenJson = json.get(TOKEN);
JsonNode caCertDataJson = json.get(CA_CERT_DATA);
JsonNode clientCertDataJson = json.get(CLIENT_CERT_DATA);
JsonNode clientKeyDataJson = json.get(CLIENT_KEY_DATA);
String token = "";
String caCertData = "";
String clientCertData = "";
String clientKeyData = "";
if (scheme == HTTPS) {
caCertData = nullIsIllegal(caCertDataJson.asText(), CA_CERT_DATA + MISSING_MESSAGE);
clientCertData = nullIsIllegal(clientCertDataJson.asText(), CLIENT_CERT_DATA + MISSING_MESSAGE);
clientKeyData = nullIsIllegal(clientKeyDataJson.asText(), CLIENT_KEY_DATA + MISSING_MESSAGE);
if (tokenJson != null) {
token = tokenJson.asText();
}
} else {
if (tokenJson != null) {
token = tokenJson.asText();
}
if (caCertDataJson != null) {
caCertData = caCertDataJson.asText();
}
if (clientCertDataJson != null) {
clientCertData = clientCertDataJson.asText();
}
if (clientKeyDataJson != null) {
clientKeyData = clientKeyDataJson.asText();
}
}
if (StringUtils.isNotEmpty(token)) {
builder.token(token);
}
if (StringUtils.isNotEmpty(caCertData)) {
builder.caCertData(caCertData);
}
if (StringUtils.isNotEmpty(clientCertData)) {
builder.clientCertData(clientCertData);
}
if (StringUtils.isNotEmpty(clientKeyData)) {
builder.clientKeyData(clientKeyData);
}
JsonNode extNetworkCidrJson = json.get(EXT_NETWORK_CIDR);
if (extNetworkCidrJson != null) {
builder.extNetworkCidr(IpPrefix.valueOf(extNetworkCidrJson.asText()));
}
Set<HostNodesInfo> infos = new HashSet<>();
ArrayNode infosJson = (ArrayNode) json.get(HOST_NODES_INFO);
if (infosJson != null) {
for (JsonNode infoJson : infosJson) {
HostNodesInfo info = context.codec(HostNodesInfo.class).decode((ObjectNode) infoJson, context);
infos.add(info);
}
builder.infos(infos);
}
return builder.build();
}
use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sApiConfigCodecTest method getK8sApiConfig.
private K8sApiConfig getK8sApiConfig(String resourceName) throws IOException {
InputStream jsonStream = K8sNodeCodecTest.class.getResourceAsStream(resourceName);
JsonNode json = context.mapper().readTree(jsonStream);
assertThat(json, notNullValue());
K8sApiConfig config = k8sApiConfigCodec.decode((ObjectNode) json, context);
assertThat(config, notNullValue());
return config;
}
Aggregations