use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.
the class Controller method applyNamespace.
/**
* Returns true if the namespace is created
*/
public boolean applyNamespace(Namespace entity) {
String namespace = getOrCreateMetadata(entity).getName();
LOG.info("Using namespace: " + namespace);
String name = getName(entity);
Objects.notNull(name, "No name for " + entity);
Namespace old = kubernetesClient.namespaces().withName(name).get();
if (!isRunning(old)) {
try {
Object answer = kubernetesClient.namespaces().create(entity);
logGeneratedEntity("Created namespace: ", namespace, entity, answer);
return true;
} catch (Exception e) {
onApplyError("Failed to create namespace: " + name + " due " + e.getMessage(), e);
}
}
return false;
}
use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.
the class Controller method applyPod.
public void applyPod(Pod pod, String sourceName) throws Exception {
String namespace = getNamespace();
String id = getName(pod);
Objects.notNull(id, "No name for " + pod + " " + sourceName);
if (isServicesOnlyMode()) {
LOG.debug("Only processing Services right now so ignoring Pod: " + namespace + ":" + id);
return;
}
Pod old = kubernetesClient.pods().inNamespace(namespace).withName(id).get();
if (isRunning(old)) {
if (UserConfigurationCompare.configEqual(pod, old)) {
LOG.info("Pod has not changed so not doing anything");
} else {
if (isRecreateMode()) {
LOG.info("Deleting Pod: " + id);
kubernetesClient.pods().inNamespace(namespace).withName(id).delete();
doCreatePod(pod, namespace, sourceName);
} else {
LOG.info("Updating a Pod from " + sourceName + " namespace " + namespace + " name " + getName(pod));
try {
Object answer = kubernetesClient.pods().inNamespace(namespace).withName(id).replace(pod);
LOG.info("Updated Pod result: " + answer);
} catch (Exception e) {
onApplyError("Failed to update Pod from " + sourceName + ". " + e + ". " + pod, e);
}
}
}
} else {
if (!isAllowCreate()) {
LOG.warn("Creation disabled so not creating a pod from " + sourceName + " namespace " + namespace + " name " + getName(pod));
} else {
doCreatePod(pod, namespace, sourceName);
}
}
}
use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.
the class Controller method applyNamespace.
public void applyNamespace(String namespaceName, Map<String, String> labels) {
if (Strings.isNullOrBlank(namespaceName)) {
return;
}
OpenShiftClient openshiftClient = getOpenShiftClientOrNull();
if (openshiftClient != null && openshiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.PROJECT)) {
ProjectRequest entity = new ProjectRequest();
ObjectMeta metadata = getOrCreateMetadata(entity);
metadata.setName(namespaceName);
String namespace = kubernetesClient.getNamespace();
if (Strings.isNotBlank(namespace)) {
Map<String, String> entityLabels = getOrCreateLabels(entity);
if (labels != null) {
entityLabels.putAll(labels);
} else {
// lets associate this new namespace with the project that it was created from
entityLabels.put("project", namespace);
}
}
applyProjectRequest(entity);
} else {
Namespace entity = new Namespace();
ObjectMeta metadata = getOrCreateMetadata(entity);
metadata.setName(namespaceName);
String namespace = kubernetesClient.getNamespace();
if (Strings.isNotBlank(namespace)) {
Map<String, String> entityLabels = getOrCreateLabels(entity);
if (labels != null) {
entityLabels.putAll(labels);
} else {
// lets associate this new namespace with the project that it was created from
entityLabels.put("project", namespace);
}
}
applyNamespace(entity);
}
}
use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.
the class Controller method applyPersistentVolumeClaim.
public void applyPersistentVolumeClaim(PersistentVolumeClaim entity, String sourceName) throws Exception {
// we cannot update PVCs
boolean alwaysRecreate = true;
String namespace = getNamespace();
String id = getName(entity);
Objects.notNull(id, "No name for " + entity + " " + sourceName);
if (isServicesOnlyMode()) {
LOG.debug("Only processing Services right now so ignoring PersistentVolumeClaim: " + id);
return;
}
PersistentVolumeClaim old = kubernetesClient.persistentVolumeClaims().inNamespace(namespace).withName(id).get();
if (isRunning(old)) {
if (UserConfigurationCompare.configEqual(entity, old)) {
LOG.info("PersistentVolumeClaim has not changed so not doing anything");
} else {
if (alwaysRecreate || isRecreateMode()) {
if (!isRecreateMode() && isIgnoreBoundPersistentVolumeClaims() && isBound(old)) {
LOG.warn("PersistentVolumeClaim " + id + " in namespace " + namespace + " is already bound and will not be replaced with the new one from " + sourceName);
} else {
LOG.info("Deleting PersistentVolumeClaim from namespace " + namespace + " with name " + id);
kubernetesClient.persistentVolumeClaims().inNamespace(namespace).withName(id).delete();
LOG.info("Deleted PersistentVolumeClaim from namespace " + namespace + " with name " + id);
doCreatePersistentVolumeClaim(entity, namespace, sourceName);
}
} else {
LOG.info("Updating a PersistentVolumeClaim from " + sourceName);
try {
Object answer = kubernetesClient.persistentVolumeClaims().inNamespace(namespace).withName(id).replace(entity);
logGeneratedEntity("Updated PersistentVolumeClaim: ", namespace, entity, answer);
} catch (Exception e) {
onApplyError("Failed to update PersistentVolumeClaim from " + sourceName + ". " + e + ". " + entity, e);
}
}
}
} else {
if (!isAllowCreate()) {
LOG.warn("Creation disabled so not creating a PersistentVolumeClaim from " + sourceName + " namespace " + namespace + " name " + getName(entity));
} else {
doCreatePersistentVolumeClaim(entity, namespace, sourceName);
}
}
}
use of io.fabric8.kubernetes.api.model.Namespace in project fabric8 by fabric8io.
the class KubernetesHelper method getServiceURLInCurrentNamespace.
/**
* Returns the URL to access the service; using the environment variables, routes
* or service clusterIP address
*
* @throws IllegalArgumentException if the URL cannot be found for the serviceName and namespace
*/
public static String getServiceURLInCurrentNamespace(KubernetesClient client, String serviceName, String serviceProtocol, String servicePortName, boolean serviceExternal) {
Service srv = null;
String serviceHost = KubernetesServices.serviceToHostOrBlank(serviceName);
String servicePort = KubernetesServices.serviceToPortOrBlank(serviceName, servicePortName);
String serviceProto = serviceProtocol != null ? serviceProtocol : KubernetesServices.serviceToProtocol(serviceName, servicePort);
// 1. Inside Kubernetes: Services as ENV vars
if (!serviceExternal && Strings.isNotBlank(serviceHost) && Strings.isNotBlank(servicePort) && Strings.isNotBlank(serviceProtocol)) {
return serviceProtocol + "://" + serviceHost + ":" + servicePort;
// 2. Anywhere: When namespace is passed System / Env var. Mostly needed for integration tests.
} else {
srv = client.services().withName(serviceName).get();
}
if (srv == null) {
throw new IllegalArgumentException("No kubernetes service could be found for name: " + serviceName);
}
if (Strings.isNullOrBlank(servicePortName) && isOpenShift(client)) {
OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
RouteList routeList = openShiftClient.routes().list();
for (Route route : routeList.getItems()) {
if (route.getSpec().getTo().getName().equals(serviceName)) {
return (serviceProto + "://" + route.getSpec().getHost()).toLowerCase();
}
}
}
ServicePort port = findServicePortByName(srv, servicePortName);
if (port == null) {
throw new RuntimeException("Couldn't find port: " + servicePortName + " for service:" + serviceName);
}
String clusterIP = srv.getSpec().getClusterIP();
if ("None".equals(clusterIP)) {
throw new IllegalStateException("Service: " + serviceName + " in current namespace is head-less. Search for endpoints instead.");
}
return (serviceProto + "://" + clusterIP + ":" + port.getPort()).toLowerCase();
}
Aggregations