use of org.onosproject.k8snetworking.api.K8sNamespaceAdminService 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);
});
}
Aggregations