use of io.fabric8.kubernetes.client.dsl.MixedOperation in project fabric8 by fabric8io.
the class Controller method applyResource.
public <T extends HasMetadata, L, D> void applyResource(T resource, String sourceName, MixedOperation<T, L, D, ? extends Resource<T, D>> resources) throws Exception {
String namespace = getNamespace();
String resourceNamespace = getNamespace(resource);
if (Strings.isNotBlank(namespace) && Strings.isNullOrBlank(resourceNamespace)) {
getOrCreateMetadata(resource).setNamespace(namespace);
}
String id = getName(resource);
String kind = getKind(resource);
Objects.notNull(id, "No name for " + resource + " " + sourceName);
if (isServicesOnlyMode()) {
LOG.debug("Ignoring " + kind + ": " + namespace + ":" + id);
return;
}
T old = resources.inNamespace(namespace).withName(id).get();
if (isRunning(old)) {
if (UserConfigurationCompare.configEqual(resource, old)) {
LOG.info(kind + " has not changed so not doing anything");
} else {
if (isRecreateMode()) {
LOG.info("Deleting " + kind + ": " + id);
resources.inNamespace(namespace).withName(id).delete();
doCreateResource(resource, namespace, sourceName, resources);
} else {
LOG.info("Updating " + kind + " from " + sourceName);
try {
Object answer = resources.inNamespace(namespace).withName(id).replace(resource);
logGeneratedEntity("Updated " + kind + ": ", namespace, resource, answer);
} catch (Exception e) {
onApplyError("Failed to update " + kind + " from " + sourceName + ". " + e + ". " + resource, e);
}
}
}
} else {
if (!isAllowCreate()) {
LOG.warn("Creation disabled so not creating a " + kind + " from " + sourceName + " namespace " + namespace + " name " + getName(resource));
} else {
doCreateResource(resource, namespace, sourceName, resources);
}
}
}
use of io.fabric8.kubernetes.client.dsl.MixedOperation in project fabric8 by fabric8io.
the class Controller method doCreateResource.
protected <T extends HasMetadata, L, D> void doCreateResource(T resource, String namespace, String sourceName, MixedOperation<T, L, D, ? extends Resource<T, D>> resources) throws Exception {
String kind = getKind(resource);
LOG.info("Creating a " + kind + " from " + sourceName + " namespace " + namespace + " name " + getName(resource));
try {
Object answer;
if (Strings.isNotBlank(namespace)) {
answer = resources.inNamespace(namespace).create(resource);
} else {
answer = resources.inNamespace(getNamespace()).create(resource);
}
logGeneratedEntity("Created " + kind + ": ", namespace, resource, answer);
} catch (Exception e) {
onApplyError("Failed to create " + kind + " from " + sourceName + ". " + e + ". " + resource, e);
}
}
Aggregations