use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sNetworkingUtil method k8sClient.
/**
* Obtains workable kubernetes client.
*
* @param service kubernetes API service
* @return kubernetes client
*/
public static KubernetesClient k8sClient(K8sApiConfigService service) {
K8sApiConfig config = service.apiConfigs().stream().findAny().orElse(null);
if (config == null) {
log.error("Failed to find valid kubernetes API configuration.");
return null;
}
KubernetesClient client = k8sClient(config);
if (client == null) {
log.error("Failed to connect to kubernetes API server.");
return null;
}
return client;
}
use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sSyncStateCommand method doExecute.
@Override
protected void doExecute() {
K8sApiConfigService configService = get(K8sApiConfigService.class);
K8sPodAdminService podAdminService = get(K8sPodAdminService.class);
K8sNamespaceAdminService namespaceAdminService = get(K8sNamespaceAdminService.class);
K8sServiceAdminService serviceAdminService = get(K8sServiceAdminService.class);
K8sIngressAdminService ingressAdminService = get(K8sIngressAdminService.class);
K8sEndpointsAdminService endpointsAdminService = get(K8sEndpointsAdminService.class);
K8sNetworkAdminService networkAdminService = get(K8sNetworkAdminService.class);
K8sNetworkPolicyAdminService networkPolicyAdminService = get(K8sNetworkPolicyAdminService.class);
K8sApiConfig config = configService.apiConfigs().stream().findAny().orElse(null);
if (config == null) {
log.error("Failed to find valid kubernetes API configuration.");
return;
}
KubernetesClient client = K8sNetworkingUtil.k8sClient(config);
if (client == null) {
log.error("Failed to connect to kubernetes API server.");
return;
}
print("\nSynchronizing kubernetes namespaces");
print(NAMESPACE_FORMAT, "Name", "Phase", "Labels");
client.namespaces().list().getItems().forEach(ns -> {
if (namespaceAdminService.namespace(ns.getMetadata().getUid()) != null) {
namespaceAdminService.updateNamespace(ns);
} else {
namespaceAdminService.createNamespace(ns);
}
printNamespace(ns);
});
print("Synchronizing kubernetes services");
print(SERVICE_FORMAT, "Name", "Cluster IP", "Ports");
client.services().inAnyNamespace().list().getItems().forEach(svc -> {
if (serviceAdminService.service(svc.getMetadata().getUid()) != null) {
serviceAdminService.updateService(svc);
} else {
serviceAdminService.createService(svc);
}
printService(svc);
});
print("\nSynchronizing kubernetes endpoints");
print(ENDPOINTS_FORMAT, "Name", "IP Addresses", "Ports");
client.endpoints().inAnyNamespace().list().getItems().forEach(ep -> {
if (endpointsAdminService.endpoints(ep.getMetadata().getUid()) != null) {
endpointsAdminService.updateEndpoints(ep);
} else {
endpointsAdminService.createEndpoints(ep);
}
printEndpoints(ep);
});
print("\nSynchronizing kubernetes pods");
print(POD_FORMAT, "Name", "Namespace", "IP", "Containers");
client.pods().inAnyNamespace().list().getItems().forEach(pod -> {
if (podAdminService.pod(pod.getMetadata().getUid()) != null) {
podAdminService.updatePod(pod);
} else {
podAdminService.createPod(pod);
}
syncPortFromPod(pod, networkAdminService);
printPod(pod);
});
print("\nSynchronizing kubernetes ingresses");
print(INGRESS_FORMAT, "Name", "Namespace", "LB Addresses");
client.extensions().ingresses().inAnyNamespace().list().getItems().forEach(ingress -> {
if (ingressAdminService.ingress(ingress.getMetadata().getUid()) != null) {
ingressAdminService.updateIngress(ingress);
} else {
ingressAdminService.createIngress(ingress);
}
printIngresses(ingress);
});
print("\nSynchronizing kubernetes network policies");
print(NETWORK_POLICY_FORMAT, "Name", "Namespace", "Types");
client.network().networkPolicies().inAnyNamespace().list().getItems().forEach(policy -> {
if (networkPolicyAdminService.networkPolicy(policy.getMetadata().getUid()) != null) {
networkPolicyAdminService.updateNetworkPolicy(policy);
} else {
networkPolicyAdminService.createNetworkPolicy(policy);
}
printNetworkPolicy(policy);
});
}
use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sApiConfigListCommand method doExecute.
@Override
protected void doExecute() {
K8sApiConfigService configService = get(K8sApiConfigService.class);
List<K8sApiConfig> configs = Lists.newArrayList(configService.apiConfigs());
configs.sort(Comparator.comparing(K8sApiConfig::ipAddress));
if (outputJson()) {
print("%s", json(configs));
} else {
print(FORMAT, "Scheme", "IpAddress", "Port", "State");
for (K8sApiConfig config : configs) {
print(FORMAT, config.scheme().name(), config.ipAddress().toString(), config.port(), config.state().name());
}
print("Total %s API configs", configService.apiConfigs().size());
}
}
use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sApiConfigListCommand method json.
private String json(List<K8sApiConfig> configs) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode result = mapper.createArrayNode();
for (K8sApiConfig config : configs) {
result.add(jsonForEntity(config, K8sApiConfig.class));
}
return prettyJson(mapper, result.toString());
}
use of org.onosproject.k8snode.api.K8sApiConfig in project onos by opennetworkinglab.
the class K8sNodeWebResource method createApiConfigs.
/**
* Creates a set of kubernetes API config from the JSON input stream.
*
* @param input kubernetes API configs JSON input stream
* @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
* is malformed
* @onos.rsModel K8sApiConfig
*/
@POST
@Path("api")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createApiConfigs(InputStream input) {
log.trace(String.format(MESSAGE_NODE, CREATE));
readApiConfigConfiguration(input).forEach(config -> {
K8sApiConfig existing = configAdminService.apiConfig(endpoint(config));
if (existing == null) {
configAdminService.createApiConfig(config);
}
});
UriBuilder locationBuilder = uriInfo.getBaseUriBuilder().path(API_CONFIGS);
return created(locationBuilder.build()).build();
}
Aggregations