use of oracle.kubernetes.operator.helpers.ServerKubernetesObjects in project weblogic-kubernetes-operator by oracle.
the class Main method dispatchServiceWatch.
private static void dispatchServiceWatch(Watch.Response<V1Service> item) {
V1Service s = item.object;
if (s != null) {
V1ObjectMeta metadata = s.getMetadata();
String domainUID = metadata.getLabels().get(LabelConstants.DOMAINUID_LABEL);
String serverName = metadata.getLabels().get(LabelConstants.SERVERNAME_LABEL);
String channelName = metadata.getLabels().get(LabelConstants.CHANNELNAME_LABEL);
String clusterName = metadata.getLabels().get(LabelConstants.CLUSTERNAME_LABEL);
if (domainUID != null) {
DomainPresenceInfo info = domains.get(domainUID);
ServerKubernetesObjects sko = null;
if (info != null) {
if (serverName != null) {
ServerKubernetesObjects created = new ServerKubernetesObjects();
ServerKubernetesObjects current = info.getServers().putIfAbsent(serverName, created);
sko = current != null ? current : created;
}
switch(item.type) {
case "ADDED":
if (sko != null) {
if (channelName != null) {
sko.getChannels().put(channelName, s);
} else {
sko.getService().set(s);
}
} else if (clusterName != null) {
info.getClusters().put(clusterName, s);
}
break;
case "MODIFIED":
if (sko != null) {
if (channelName != null) {
V1Service skoService = sko.getChannels().get(channelName);
if (skoService != null) {
sko.getChannels().replace(channelName, skoService, s);
}
} else {
V1Service skoService = sko.getService().get();
if (skoService != null) {
sko.getService().compareAndSet(skoService, s);
}
}
} else if (clusterName != null) {
V1Service clusterService = info.getClusters().get(clusterName);
if (clusterService != null) {
info.getClusters().replace(clusterName, clusterService, s);
}
}
break;
case "DELETED":
if (sko != null) {
if (channelName != null) {
V1Service oldService = sko.getChannels().put(channelName, null);
if (oldService != null) {
// Service was deleted, but sko still contained a non-null entry
LOGGER.info(MessageKeys.SERVER_SERVICE_DELETED, domainUID, metadata.getNamespace(), serverName);
doCheckAndCreateDomainPresence(info.getDomain(), true);
}
} else {
V1Service oldService = sko.getService().getAndSet(null);
if (oldService != null) {
// Service was deleted, but sko still contained a non-null entry
LOGGER.info(MessageKeys.SERVER_SERVICE_DELETED, domainUID, metadata.getNamespace(), serverName);
doCheckAndCreateDomainPresence(info.getDomain(), true);
}
}
} else if (clusterName != null) {
V1Service oldService = info.getClusters().put(clusterName, null);
if (oldService != null) {
// Service was deleted, but clusters still contained a non-null entry
LOGGER.info(MessageKeys.CLUSTER_SERVICE_DELETED, domainUID, metadata.getNamespace(), clusterName);
doCheckAndCreateDomainPresence(info.getDomain(), true);
}
}
break;
case "ERROR":
default:
}
}
}
}
}
use of oracle.kubernetes.operator.helpers.ServerKubernetesObjects in project weblogic-kubernetes-operator by oracle.
the class Main method begin.
private static void begin() {
// read the operator configuration
String namespace = System.getenv("OPERATOR_NAMESPACE");
if (namespace == null) {
namespace = "default";
}
Collection<String> targetNamespaces = getTargetNamespaces(namespace);
String serviceAccountName = tuningAndConfig.get("serviceaccount");
if (serviceAccountName == null) {
serviceAccountName = "default";
}
principal = "system:serviceaccount:" + namespace + ":" + serviceAccountName;
LOGGER.info(MessageKeys.OP_CONFIG_NAMESPACE, namespace);
StringBuilder tns = new StringBuilder();
Iterator<String> it = targetNamespaces.iterator();
while (it.hasNext()) {
tns.append(it.next());
if (it.hasNext()) {
tns.append(", ");
}
}
LOGGER.info(MessageKeys.OP_CONFIG_TARGET_NAMESPACES, tns.toString());
LOGGER.info(MessageKeys.OP_CONFIG_SERVICE_ACCOUNT, serviceAccountName);
try {
// Initialize logging factory with JSON serializer for later logging
// that includes k8s objects
LoggingFactory.setJSON(new JSON());
// start the REST server
startRestServer(principal, targetNamespaces);
// create the Custom Resource Definitions if they are not already there
CRDHelper.checkAndCreateCustomResourceDefinition();
try {
HealthCheckHelper healthCheck = new HealthCheckHelper(namespace, targetNamespaces);
version = healthCheck.performK8sVersionCheck();
healthCheck.performNonSecurityChecks();
healthCheck.performSecurityChecks(serviceAccountName);
} catch (ApiException e) {
LOGGER.warning(MessageKeys.EXCEPTION, e);
}
// check for any existing resources and add the watches on them
// this would happen when the Domain was running BEFORE the Operator starts up
LOGGER.info(MessageKeys.LISTING_DOMAINS);
for (String ns : targetNamespaces) {
initialized.put(ns, Boolean.TRUE);
Step domainList = callBuilderFactory.create().listDomainAsync(ns, new ResponseStep<DomainList>(null) {
@Override
public NextAction onFailure(Packet packet, ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
if (statusCode == CallBuilder.NOT_FOUND) {
return onSuccess(packet, null, statusCode, responseHeaders);
}
return super.onFailure(packet, e, statusCode, responseHeaders);
}
@Override
public NextAction onSuccess(Packet packet, DomainList result, int statusCode, Map<String, List<String>> responseHeaders) {
if (result != null) {
for (Domain dom : result.getItems()) {
doCheckAndCreateDomainPresence(dom);
}
}
// main logic now happens in the watch handlers
domainWatchers.put(ns, createDomainWatcher(ns, result != null ? result.getMetadata().getResourceVersion() : ""));
return doNext(packet);
}
});
Step initialize = ConfigMapHelper.createScriptConfigMapStep(ns, new ConfigMapAfterStep(ns, callBuilderFactory.create().with($ -> {
$.labelSelector = LabelConstants.DOMAINUID_LABEL + "," + LabelConstants.CREATEDBYOPERATOR_LABEL;
}).listPodAsync(ns, new ResponseStep<V1PodList>(callBuilderFactory.create().with($ -> {
$.labelSelector = LabelConstants.DOMAINUID_LABEL + "," + LabelConstants.CREATEDBYOPERATOR_LABEL;
}).listServiceAsync(ns, new ResponseStep<V1ServiceList>(callBuilderFactory.create().with($ -> {
$.labelSelector = LabelConstants.DOMAINUID_LABEL + "," + LabelConstants.CREATEDBYOPERATOR_LABEL;
}).listIngressAsync(ns, new ResponseStep<V1beta1IngressList>(domainList) {
@Override
public NextAction onFailure(Packet packet, ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
if (statusCode == CallBuilder.NOT_FOUND) {
return onSuccess(packet, null, statusCode, responseHeaders);
}
return super.onFailure(packet, e, statusCode, responseHeaders);
}
@Override
public NextAction onSuccess(Packet packet, V1beta1IngressList result, int statusCode, Map<String, List<String>> responseHeaders) {
if (result != null) {
for (V1beta1Ingress ingress : result.getItems()) {
String domainUID = IngressWatcher.getIngressDomainUID(ingress);
String clusterName = IngressWatcher.getIngressClusterName(ingress);
if (domainUID != null && clusterName != null) {
DomainPresenceInfo created = new DomainPresenceInfo(ns);
DomainPresenceInfo info = domains.putIfAbsent(domainUID, created);
if (info == null) {
info = created;
}
info.getIngresses().put(clusterName, ingress);
}
}
}
ingressWatchers.put(ns, createIngressWatcher(ns, result != null ? result.getMetadata().getResourceVersion() : ""));
return doNext(packet);
}
})) {
@Override
public NextAction onFailure(Packet packet, ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
if (statusCode == CallBuilder.NOT_FOUND) {
return onSuccess(packet, null, statusCode, responseHeaders);
}
return super.onFailure(packet, e, statusCode, responseHeaders);
}
@Override
public NextAction onSuccess(Packet packet, V1ServiceList result, int statusCode, Map<String, List<String>> responseHeaders) {
if (result != null) {
for (V1Service service : result.getItems()) {
String domainUID = ServiceWatcher.getServiceDomainUID(service);
String serverName = ServiceWatcher.getServiceServerName(service);
String channelName = ServiceWatcher.getServiceChannelName(service);
if (domainUID != null && serverName != null) {
DomainPresenceInfo created = new DomainPresenceInfo(ns);
DomainPresenceInfo info = domains.putIfAbsent(domainUID, created);
if (info == null) {
info = created;
}
ServerKubernetesObjects csko = new ServerKubernetesObjects();
ServerKubernetesObjects current = info.getServers().putIfAbsent(serverName, csko);
ServerKubernetesObjects sko = current != null ? current : csko;
if (channelName != null) {
sko.getChannels().put(channelName, service);
} else {
sko.getService().set(service);
}
}
}
}
serviceWatchers.put(ns, createServiceWatcher(ns, result != null ? result.getMetadata().getResourceVersion() : ""));
return doNext(packet);
}
})) {
@Override
public NextAction onFailure(Packet packet, ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
if (statusCode == CallBuilder.NOT_FOUND) {
return onSuccess(packet, null, statusCode, responseHeaders);
}
return super.onFailure(packet, e, statusCode, responseHeaders);
}
@Override
public NextAction onSuccess(Packet packet, V1PodList result, int statusCode, Map<String, List<String>> responseHeaders) {
if (result != null) {
for (V1Pod pod : result.getItems()) {
String domainUID = PodWatcher.getPodDomainUID(pod);
String serverName = PodWatcher.getPodServerName(pod);
if (domainUID != null && serverName != null) {
DomainPresenceInfo created = new DomainPresenceInfo(ns);
DomainPresenceInfo info = domains.putIfAbsent(domainUID, created);
if (info == null) {
info = created;
}
ServerKubernetesObjects csko = new ServerKubernetesObjects();
ServerKubernetesObjects current = info.getServers().putIfAbsent(serverName, csko);
ServerKubernetesObjects sko = current != null ? current : csko;
sko.getPod().set(pod);
}
}
}
podWatchers.put(ns, createPodWatcher(ns, result != null ? result.getMetadata().getResourceVersion() : ""));
return doNext(packet);
}
})));
engine.createFiber().start(initialize, new Packet(), new CompletionCallback() {
@Override
public void onCompletion(Packet packet) {
// no-op
}
@Override
public void onThrowable(Packet packet, Throwable throwable) {
LOGGER.severe(MessageKeys.EXCEPTION, throwable);
}
});
}
// delete stranded resources
for (Map.Entry<String, DomainPresenceInfo> entry : domains.entrySet()) {
String domainUID = entry.getKey();
DomainPresenceInfo info = entry.getValue();
if (info != null) {
if (info.getDomain() == null) {
// no domain resource
deleteDomainPresence(info.getNamespace(), domainUID);
}
}
}
} catch (Throwable e) {
LOGGER.warning(MessageKeys.EXCEPTION, e);
} finally {
LOGGER.info(MessageKeys.OPERATOR_SHUTTING_DOWN);
}
}
use of oracle.kubernetes.operator.helpers.ServerKubernetesObjects in project weblogic-kubernetes-operator by oracle.
the class Main method dispatchPodWatch.
private static void dispatchPodWatch(Watch.Response<V1Pod> item) {
V1Pod p = item.object;
if (p != null) {
V1ObjectMeta metadata = p.getMetadata();
String domainUID = metadata.getLabels().get(LabelConstants.DOMAINUID_LABEL);
String serverName = metadata.getLabels().get(LabelConstants.SERVERNAME_LABEL);
if (domainUID != null) {
DomainPresenceInfo info = domains.get(domainUID);
if (info != null && serverName != null) {
ServerKubernetesObjects created = new ServerKubernetesObjects();
ServerKubernetesObjects current = info.getServers().putIfAbsent(serverName, created);
ServerKubernetesObjects sko = current != null ? current : created;
if (sko != null) {
switch(item.type) {
case "ADDED":
sko.getPod().set(p);
break;
case "MODIFIED":
V1Pod skoPod = sko.getPod().get();
if (skoPod != null) {
// If the skoPod is null then the operator deleted this pod
// and modifications are to the terminating pod
sko.getPod().compareAndSet(skoPod, p);
}
break;
case "DELETED":
V1Pod oldPod = sko.getPod().getAndSet(null);
if (oldPod != null) {
// Pod was deleted, but sko still contained a non-null entry
LOGGER.info(MessageKeys.POD_DELETED, domainUID, metadata.getNamespace(), serverName);
doCheckAndCreateDomainPresence(info.getDomain(), true);
}
break;
case "ERROR":
default:
}
}
}
}
}
}
use of oracle.kubernetes.operator.helpers.ServerKubernetesObjects in project weblogic-kubernetes-operator by oracle.
the class Main method scaleDownIfNecessary.
private static Step scaleDownIfNecessary(DomainPresenceInfo info, Collection<String> servers, Step next) {
Domain dom = info.getDomain();
DomainSpec spec = dom.getSpec();
boolean shouldStopAdmin = false;
String sc = spec.getStartupControl();
if (sc != null && StartupControlConstants.NONE_STARTUPCONTROL.equals(sc.toUpperCase())) {
shouldStopAdmin = true;
next = DomainStatusUpdater.createAvailableStep(DomainStatusUpdater.ALL_STOPPED_AVAILABLE_REASON, next);
}
String adminName = spec.getAsName();
Map<String, ServerKubernetesObjects> currentServers = info.getServers();
Collection<Map.Entry<String, ServerKubernetesObjects>> serversToStop = new ArrayList<>();
for (Map.Entry<String, ServerKubernetesObjects> entry : currentServers.entrySet()) {
if ((shouldStopAdmin || !entry.getKey().equals(adminName)) && !servers.contains(entry.getKey())) {
serversToStop.add(entry);
}
}
if (!serversToStop.isEmpty()) {
return new ServerDownIteratorStep(serversToStop, next);
}
return next;
}
Aggregations