use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.
the class KubernetesClientUtil method deleteEntities.
public static void deleteEntities(KubernetesClient kubernetes, String namespace, Set<HasMetadata> entities, String s2iBuildNameSuffix, Logger log) {
List<HasMetadata> list = new ArrayList<>(entities);
// For OpenShift cluster, also delete s2i buildconfig
OpenShiftClient openshiftClient = new Controller(kubernetes).getOpenShiftClientOrNull();
if (openshiftClient != null) {
for (HasMetadata entity : list) {
if ("ImageStream".equals(getKind(entity))) {
ImageName imageName = new ImageName(entity.getMetadata().getName());
String buildName = getS2IBuildName(imageName, s2iBuildNameSuffix);
log.info("Deleting resource BuildConfig " + namespace + "/" + buildName);
openshiftClient.buildConfigs().inNamespace(namespace).withName(buildName).delete();
}
}
}
// lets delete in reverse order
Collections.reverse(list);
for (HasMetadata entity : list) {
log.info("Deleting resource " + getKind(entity) + " " + namespace + "/" + getName(entity));
kubernetes.resource(entity).inNamespace(namespace).cascading(true).delete();
}
}
use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.
the class ClientToolsService method getKubeCtlExecutable.
public File getKubeCtlExecutable() {
OpenShiftClient openShiftClient = controller.getOpenShiftClientOrNull();
String command = openShiftClient != null ? "oc" : "kubectl";
String missingCommandMessage;
File file = ProcessUtil.findExecutable(log, command);
if (file == null && command.equals("oc")) {
file = ProcessUtil.findExecutable(log, command);
missingCommandMessage = "commands oc or kubectl";
} else {
missingCommandMessage = "command " + command;
}
if (file == null) {
throw new IllegalStateException("Could not find " + missingCommandMessage + ". Please install the necessary binaries and ensure they get added to your $PATH");
}
return file;
}
use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildService method waitForOpenShiftBuildToComplete.
private void waitForOpenShiftBuildToComplete(OpenShiftClient client, Build build) throws MojoExecutionException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch logTerminateLatch = new CountDownLatch(1);
final String buildName = KubernetesHelper.getName(build);
final AtomicReference<Build> buildHolder = new AtomicReference<>();
// Don't query for logs directly, Watch over the build pod:
waitUntilPodIsReady(buildName + "-build", 20, log);
log.info("Waiting for build " + buildName + " to complete...");
try (LogWatch logWatch = client.pods().withName(buildName + "-build").watchLog()) {
KubernetesClientUtil.printLogsAsync(logWatch, "Failed to tail build log", logTerminateLatch, log);
Watcher<Build> buildWatcher = getBuildWatcher(latch, buildName, buildHolder);
try (Watch watcher = client.builds().withName(buildName).watch(buildWatcher)) {
// Check if the build is already finished to avoid waiting indefinitely
Build lastBuild = client.builds().withName(buildName).get();
String lastStatus = KubernetesResourceUtil.getBuildStatusPhase(lastBuild);
if (Builds.isFinished(lastStatus)) {
log.debug("Build %s is already finished", buildName);
buildHolder.set(lastBuild);
latch.countDown();
}
waitUntilBuildFinished(latch);
logTerminateLatch.countDown();
build = buildHolder.get();
String status = KubernetesResourceUtil.getBuildStatusPhase(build);
if (Builds.isFailed(status) || Builds.isCancelled(status)) {
throw new MojoExecutionException("OpenShift Build " + buildName + ": " + KubernetesResourceUtil.getBuildStatusReason(build));
}
log.info("Build " + buildName + " " + status);
}
}
}
use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildService method updateOrCreateBuildConfig.
private String updateOrCreateBuildConfig(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder, ImageConfiguration imageConfig) {
ImageName imageName = new ImageName(imageConfig.getName());
String buildName = getS2IBuildName(config, imageName);
String imageStreamName = getImageStreamName(imageName);
String outputImageStreamTag = imageStreamName + ":" + (imageName.getTag() != null ? imageName.getTag() : "latest");
BuildStrategy buildStrategyResource = createBuildStrategy(imageConfig, config.getOpenshiftBuildStrategy());
BuildOutput buildOutput = new BuildOutputBuilder().withNewTo().withKind("ImageStreamTag").withName(outputImageStreamTag).endTo().build();
// Fetch exsting build config
BuildConfig buildConfig = client.buildConfigs().withName(buildName).get();
if (buildConfig != null) {
// lets verify the BC
BuildConfigSpec spec = getBuildConfigSpec(buildConfig);
validateSourceType(buildName, spec);
if (config.getBuildRecreateMode().isBuildConfig()) {
// Delete and recreate afresh
client.buildConfigs().withName(buildName).delete();
return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput);
} else {
// Update & return
return updateBuildConfig(client, buildName, buildStrategyResource, buildOutput, spec);
}
} else {
// Create afresh
return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput);
}
}
use of io.fabric8.openshift.client.OpenShiftClient in project fabric8-maven-plugin by fabric8io.
the class OpenshiftBuildService method logBuildBuildFailedDetails.
private void logBuildBuildFailedDetails(OpenShiftClient client, String buildName) {
try {
BuildConfig build = client.buildConfigs().withName(buildName).get();
ObjectReference ref = build.getSpec().getStrategy().getSourceStrategy().getFrom();
String kind = ref.getKind();
String name = ref.getName();
if ("DockerImage".equals(kind)) {
log.error("Please, ensure that the Docker image '%s' exists and is accessible by OpenShift", name);
} else if ("ImageStreamTag".equals(kind)) {
String namespace = ref.getNamespace();
String namespaceInfo = "current";
String namespaceParams = "";
if (namespace != null && !namespace.isEmpty()) {
namespaceInfo = "'" + namespace + "'";
namespaceParams = " -n " + namespace;
}
log.error("Please, ensure that the ImageStream Tag '%s' exists in the %s namespace (with 'oc get is%s')", name, namespaceInfo, namespaceParams);
}
} catch (Exception ex) {
log.error("Unable to get detailed information from the BuildServiceConfig: " + ex.getMessage());
}
}
Aggregations