Skip to content

Commit

Permalink
feat: add formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Jan 4, 2024
1 parent 65aff26 commit 3374a0d
Show file tree
Hide file tree
Showing 2,757 changed files with 149,947 additions and 149,668 deletions.
279 changes: 279 additions & 0 deletions .code-format.xml

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions cli/src/main/java/io/quarkiverse/kubevirt/AbstractVmCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@

public abstract class AbstractVmCommand implements Callable<Integer> {

protected static final List<VirtualMachine> VMS = new ArrayList<>();
protected static final Table<VirtualMachine> TABLE = Tables.forVirtualMachines(VMS);
protected static final List<VirtualMachine> VMS = new ArrayList<>();
protected static final Table<VirtualMachine> TABLE = Tables.forVirtualMachines(VMS);

@Mixin
protected OutputOptionMixin output;
@Mixin
protected OutputOptionMixin output;

@Spec
protected CommandSpec spec;
@Spec
protected CommandSpec spec;

@CommandLine.Option(names = { "-w", "--wait-until-ready" }, description = "Wait until ready")
protected boolean waitUntilReady;
@CommandLine.Option(names = { "-w", "--wait-until-ready" }, description = "Wait until ready")
protected boolean waitUntilReady;

@CommandLine.Option(names = { "-n", "--namespace" }, description = "The name namespace to use")
protected String namespace;
@CommandLine.Option(names = { "-n", "--namespace" }, description = "The name namespace to use")
protected String namespace;

@CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "Display this help message")
protected boolean help;
@CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "Display this help message")
protected boolean help;

public boolean shouldWaitUntilReady() {
return waitUntilReady;
}
public boolean shouldWaitUntilReady() {
return waitUntilReady;
}

public List<VirtualMachine> existingVirtualMachines() {
return namespace != null && !namespace.isEmpty()
? Clients.kubevirt().v1().virtualmachines().inNamespace(namespace).list().getItems()
: Clients.kubevirt().v1().virtualmachines().list().getItems();
}
public List<VirtualMachine> existingVirtualMachines() {
return namespace != null && !namespace.isEmpty()
? Clients.kubevirt().v1().virtualmachines().inNamespace(namespace).list().getItems()
: Clients.kubevirt().v1().virtualmachines().list().getItems();
}
}
210 changes: 105 additions & 105 deletions cli/src/main/java/io/quarkiverse/kubevirt/AbstractVmCreate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,121 +22,121 @@

public abstract class AbstractVmCreate extends AbstractVmCommand {

@Option(names = { "-r", "--replace-existing" }, description = "Replace existing matching VMs.")
protected boolean replaceExisting;
@Option(names = { "-r", "--replace-existing" }, description = "Replace existing matching VMs.")
protected boolean replaceExisting;

@Option(names = { "-k", "--public-key" }, description = "Path to public key.")
protected Path publicKey;
@Option(names = { "-k", "--public-key" }, description = "Path to public key.")
protected Path publicKey;

@Option(names = { "-s", "--ssh-secret" }, description = "The name of the ssh secret.")
protected String sshSecretName = "quarkus-dev-secret";
@Option(names = { "-s", "--ssh-secret" }, description = "The name of the ssh secret.")
protected String sshSecretName = "quarkus-dev-secret";

public abstract InputStream getInputStream();
public abstract InputStream getInputStream();

public void onCreated(List<VirtualMachine> virtualMachines) {
}
public void onCreated(List<VirtualMachine> virtualMachines) {
}

public void onReady(List<VirtualMachine> virtualMachines) {
}

public void onReady(List<VirtualMachine> virtualMachines) {
@Override
public Integer call() {
List<Visitor<?>> visitors = new ArrayList<>();
if (replaceExisting) {
deleteExistingVirtualMachines();
}

@Override
public Integer call() {
List<Visitor<?>> visitors = new ArrayList<>();
if (replaceExisting) {
deleteExistingVirtualMachines();
try (InputStream is = getInputStream()) {
List<VirtualMachine> existingVms = existingVirtualMachines();
if (existingVms.size() > 0) {
output.info("Failed to create VMs. The following VMs already exist.");
VMS.addAll(existingVms);
TABLE.print();
output.info("Please cosnider using --replace-existing");
return CommandLine.ExitCode.SOFTWARE;
}

if (namespace != null && !namespace.isEmpty()) {
visitors.add(new ApplyNamespace(namespace));
}

if (publicKey != null) {
if (!publicKey.toFile().exists()) {
output.error("Public key file: " + publicKey.toAbsolutePath() + " does not exist");
return CommandLine.ExitCode.USAGE;
}

try (InputStream is = getInputStream()) {
List<VirtualMachine> existingVms = existingVirtualMachines();
if (existingVms.size() > 0) {
output.info("Failed to create VMs. The following VMs already exist.");
VMS.addAll(existingVms);
TABLE.print();
output.info("Please cosnider using --replace-existing");
return CommandLine.ExitCode.SOFTWARE;
}

if (namespace != null && !namespace.isEmpty()) {
visitors.add(new ApplyNamespace(namespace));
}

if (publicKey != null) {
if (!publicKey.toFile().exists()) {
output.error("Public key file: " + publicKey.toAbsolutePath() + " does not exist");
return CommandLine.ExitCode.USAGE;
}

Secret secret = new SecretBuilder()
.withNewMetadata()
.withName(sshSecretName)
.withNamespace(namespace)
.endMetadata()
.withType("Opaque")
.addToData("key", Base64.getEncoder().encodeToString(Files.readString(publicKey).getBytes()))
.build();

Clients.kubernetes().resource(secret).serverSideApply();
visitors.add(new AddAccessCredentialsToVirtualMachine(sshSecretName));
}

List<HasMetadata> createdResources = Clients.kubernetes().load(is)
.accept(visitors.toArray(new Visitor[visitors.size()])).create();
final List<VirtualMachine> createdVms = createdResources.stream().filter(i -> i instanceof VirtualMachine)
.map(VirtualMachine.class::cast).collect(Collectors.toList());
onCreated(createdVms);
VMS.addAll(createdVms);

if (!shouldWaitUntilReady()) {
output.info("Created VMs:");
TABLE.print();
} else {
output.info("Waiting for VMs:");
TABLE.print();
Clients.kubernetes().resourceList(createdVms).waitUntilCondition(
vm -> vm instanceof VirtualMachine &&
((VirtualMachine) vm).getStatus() != null &&
((VirtualMachine) vm).getStatus().getReady() != null &&
((VirtualMachine) vm).getStatus().getReady(),
5L, TimeUnit.MINUTES);

List<VirtualMachine> readyVms = Clients.kubernetes().resourceList(createdVms).get().stream()
.filter(i -> i instanceof VirtualMachine).map(VirtualMachine.class::cast).collect(Collectors.toList());
VMS.clear();
VMS.addAll(readyVms);
TABLE.refresh();
onReady(readyVms);
}
return CommandLine.ExitCode.OK;
} catch (Exception e) {
e.printStackTrace();
return CommandLine.ExitCode.SOFTWARE;
}
Secret secret = new SecretBuilder()
.withNewMetadata()
.withName(sshSecretName)
.withNamespace(namespace)
.endMetadata()
.withType("Opaque")
.addToData("key", Base64.getEncoder().encodeToString(Files.readString(publicKey).getBytes()))
.build();

Clients.kubernetes().resource(secret).serverSideApply();
visitors.add(new AddAccessCredentialsToVirtualMachine(sshSecretName));
}

List<HasMetadata> createdResources = Clients.kubernetes().load(is)
.accept(visitors.toArray(new Visitor[visitors.size()])).create();
final List<VirtualMachine> createdVms = createdResources.stream().filter(i -> i instanceof VirtualMachine)
.map(VirtualMachine.class::cast).collect(Collectors.toList());
onCreated(createdVms);
VMS.addAll(createdVms);

if (!shouldWaitUntilReady()) {
output.info("Created VMs:");
TABLE.print();
} else {
output.info("Waiting for VMs:");
TABLE.print();
Clients.kubernetes().resourceList(createdVms).waitUntilCondition(
vm -> vm instanceof VirtualMachine &&
((VirtualMachine) vm).getStatus() != null &&
((VirtualMachine) vm).getStatus().getReady() != null &&
((VirtualMachine) vm).getStatus().getReady(),
5L, TimeUnit.MINUTES);

List<VirtualMachine> readyVms = Clients.kubernetes().resourceList(createdVms).get().stream()
.filter(i -> i instanceof VirtualMachine).map(VirtualMachine.class::cast).collect(Collectors.toList());
VMS.clear();
VMS.addAll(readyVms);
TABLE.refresh();
onReady(readyVms);
}
return CommandLine.ExitCode.OK;
} catch (Exception e) {
e.printStackTrace();
return CommandLine.ExitCode.SOFTWARE;
}

@Override
public List<VirtualMachine> existingVirtualMachines() {
try (InputStream is = getInputStream()) {
return Clients.kubernetes().load(is).accept(new ApplyNamespace(namespace)).get().stream()
.filter(i -> i instanceof VirtualMachine)
.map(VirtualMachine.class::cast).collect(Collectors.toList());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public List<VirtualMachine> existingVirtualMachines() {
try (InputStream is = getInputStream()) {
return Clients.kubernetes().load(is).accept(new ApplyNamespace(namespace)).get().stream()
.filter(i -> i instanceof VirtualMachine)
.map(VirtualMachine.class::cast).collect(Collectors.toList());
} catch (Exception e) {
throw new RuntimeException(e);
}

public void deleteExistingVirtualMachines() {
try (InputStream is = getInputStream()) {
List<HasMetadata> existingResources = Clients.kubernetes().load(is).accept(new ApplyNamespace(namespace)).get();
final List<VirtualMachine> existingVms = existingResources.stream().filter(i -> i instanceof VirtualMachine)
.map(VirtualMachine.class::cast).collect(Collectors.toList());
for (VirtualMachine vm : existingVms) {
Clients.kubernetes().resource(vm).delete();
}
for (VirtualMachine terminating : existingVms) {
Clients.kubernetes().resource(terminating).waitUntilCondition(vm -> vm == null, 1L, TimeUnit.MINUTES);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public void deleteExistingVirtualMachines() {
try (InputStream is = getInputStream()) {
List<HasMetadata> existingResources = Clients.kubernetes().load(is).accept(new ApplyNamespace(namespace)).get();
final List<VirtualMachine> existingVms = existingResources.stream().filter(i -> i instanceof VirtualMachine)
.map(VirtualMachine.class::cast).collect(Collectors.toList());
for (VirtualMachine vm : existingVms) {
Clients.kubernetes().resource(vm).delete();
}
for (VirtualMachine terminating : existingVms) {
Clients.kubernetes().resource(terminating).waitUntilCondition(vm -> vm == null, 1L, TimeUnit.MINUTES);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit 3374a0d

Please sign in to comment.