Skip to content

Commit

Permalink
refactor: misc fixes for the PodUpload class
Browse files Browse the repository at this point in the history
  • Loading branch information
manusa committed Jan 9, 2024
1 parent 4d4daa8 commit af9ff5f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -85,12 +84,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static io.fabric8.kubernetes.client.utils.internal.OptionalDependencyWrapper.wrapRunWithOptionalDependency;

Expand Down Expand Up @@ -477,7 +476,7 @@ private void copyFile(String source, File target) {
destination = destination.toPath().resolve(filename).toFile();
}

try (OutputStream out = new BufferedOutputStream(new FileOutputStream(destination))) {
try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(destination.toPath()))) {
ExecWatch w = writingOutput(out).exec(readFileCommand(source));
w.exitCode().get();
} catch (Exception e) {
Expand Down Expand Up @@ -668,7 +667,7 @@ public Pod patchReadinessGateStatus(Map<String, Boolean> readiness) {
}
}
}
pod.getStatus().setConditions(conditions.values().stream().collect(Collectors.toList()));
pod.getStatus().setConditions(new ArrayList<>(conditions.values()));
return this.resource(pod).subresource("status").patch();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand All @@ -51,12 +52,13 @@ private PodUpload() {
public static boolean upload(PodOperationsImpl operation, Path pathToUpload)
throws IOException {

if (Utils.isNotNullOrEmpty(operation.getContext().getFile()) && pathToUpload.toFile().isFile()) {
final File toUpload = pathToUpload.toFile();
if (Utils.isNotNullOrEmpty(operation.getContext().getFile()) && toUpload.isFile()) {
return uploadTar(operation, getDirectoryFromFile(operation.getContext().getFile()),
tar -> addFileToTar(new File(operation.getContext().getFile()).getName(), pathToUpload.toFile(), tar));
} else if (Utils.isNotNullOrEmpty(operation.getContext().getDir()) && pathToUpload.toFile().isDirectory()) {
tar -> addFileToTar(new File(operation.getContext().getFile()).getName(), toUpload, tar));
} else if (Utils.isNotNullOrEmpty(operation.getContext().getDir()) && toUpload.isDirectory()) {
return uploadTar(operation, ensureEndsWithSlash(operation.getContext().getDir()), tar -> {
for (File file : pathToUpload.toFile().listFiles()) {
for (File file : Objects.requireNonNull(toUpload.listFiles())) {
addFileToTar(file.getName(), file, tar);
}
});
Expand All @@ -69,12 +71,6 @@ private static String getDirectoryFromFile(String file) {
return ensureEndsWithSlash(directoryTrimmedFromFilePath.isEmpty() ? "/" : directoryTrimmedFromFilePath);
}

private interface UploadProcessor<T extends OutputStream> {

void process(T out) throws IOException;

}

private static boolean upload(PodOperationsImpl operation, String file, UploadProcessor<OutputStream> processor)
throws IOException {

Expand Down Expand Up @@ -102,8 +98,8 @@ private static boolean upload(PodOperationsImpl operation, String file, UploadPr
LOG.debug("failed to complete upload before timeout expired");
return false;
}
Integer exitCode = exitFuture.getNow(null);
if (exitCode != null && exitCode.intValue() != 0) {
final Integer exitCode = exitFuture.getNow(null);
if (exitCode != null && exitCode != 0) {
LOG.debug("upload process failed with exit code {}", exitCode);
return false;
}
Expand Down Expand Up @@ -180,7 +176,7 @@ private static void addFileToTar(String fileName, File file, TarArchiveOutputStr
tar.closeArchiveEntry();
} else if (file.isDirectory()) {
tar.closeArchiveEntry();
for (File fileInDirectory : file.listFiles()) {
for (File fileInDirectory : Objects.requireNonNull(file.listFiles())) {
addFileToTar(fileName + TAR_PATH_DELIMITER + fileInDirectory.getName(), fileInDirectory, tar);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.dsl.internal.uploadable;

import java.io.IOException;
import java.io.OutputStream;

@FunctionalInterface
interface UploadProcessor<T extends OutputStream> {

void process(T out) throws IOException;

}

0 comments on commit af9ff5f

Please sign in to comment.