Skip to content

Commit

Permalink
Adding port forward to OKE Pod
Browse files Browse the repository at this point in the history
  • Loading branch information
jhorvath committed Dec 3, 2024
1 parent 6b34b88 commit d58dfb9
Show file tree
Hide file tree
Showing 32 changed files with 1,478 additions and 90 deletions.
16 changes: 16 additions & 0 deletions enterprise/cloud.oracle/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,22 @@
<specification-version>1.48</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.api.io</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>1.29</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.openide.explorer</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>6.50</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.websvc.restlib</code-name-base>
<build-prerequisite/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @author Jan Horvath
*/
public class CloudChildFactory extends ChildFactory<OCIItem> {
public class CloudChildFactory extends ChildFactory<OCIItem> implements RefreshableKeys {
private static final Logger LOG = Logger.getLogger(CloudChildFactory.class.getName());

private final OCIItem parent;
Expand Down Expand Up @@ -81,6 +81,7 @@ protected Node[] createNodesForKey(OCIItem key) {
return new Node[]{nodeProvider.apply(key, session)};
}

@Override
public void refreshKeys() {
refresh(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.swing.Action;
import org.netbeans.modules.cloud.oracle.items.OCIItem;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.ContextAwareAction;
Expand All @@ -42,7 +43,7 @@ public class OCINode extends AbstractNode {
private RefreshListener refreshListener;

private final OCIItem item;
private final CloudChildFactory factory;
private final ChildFactory factory;
private final OCISessionInitiator session;

public OCINode(OCIItem item) {
Expand All @@ -53,7 +54,11 @@ public OCINode(OCIItem item, OCISessionInitiator session) {
this(new CloudChildFactory(session, item), item, session, Lookups.fixed(item, session));
}

private OCINode(CloudChildFactory factory, OCIItem item, OCISessionInitiator session, Lookup lookup) {
public OCINode(OCIItem item, ChildFactory factory) {
this(factory, item, null, Lookups.singleton(item));
}

private OCINode(ChildFactory factory, OCIItem item, OCISessionInitiator session, Lookup lookup) {
super(Children.create(factory, true), lookup);
setName(item.getName());
this.item = item;
Expand Down Expand Up @@ -113,8 +118,8 @@ public static final List<? extends Action> actionsForPath(String path, Lookup lk

public void refresh() {
RequestProcessor.getDefault().post(() -> {
if (factory != null) {
factory.refreshKeys();
if (factory != null && factory instanceof RefreshableKeys) {
((RefreshableKeys) factory).refreshKeys();
}
update(item);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
* Represents an OCI profile. A profile has a user, tenancy and region
* assigned.
*/
@NbBundle.Messages({
"LBL_HomeRegion=Region: {0}"
})
public final class OCIProfile implements OCISessionInitiator {

/**
Expand Down Expand Up @@ -178,9 +181,6 @@ public Tenancy getTenancyData() {
* @return Optional {@code OCIItem} describing the Tenancy. If
* Optional.empty() OCI configuration was not found
*/
@NbBundle.Messages({
"LBL_HomeRegion=Region: {0}"
})
public Optional<TenancyItem> getTenancy() {
if (configProvider == null) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.netbeans.modules.cloud.oracle;

/**
*
* @author Jan Horvath
*/
public interface RefreshableKeys {

public void refreshKeys();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.netbeans.api.project.ProjectUtils;
import static org.netbeans.modules.cloud.oracle.NotificationUtils.showMessage;
import org.netbeans.modules.cloud.oracle.assets.CloudAssets;
import org.netbeans.modules.cloud.oracle.assets.ConfigMapProvider;
import org.netbeans.modules.cloud.oracle.assets.k8s.ConfigMapProvider;
import org.netbeans.modules.cloud.oracle.assets.Steps;
import org.netbeans.modules.cloud.oracle.compute.ClusterItem;
import org.netbeans.modules.cloud.oracle.steps.CompartmentStep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class RootNode {

private static Node instance = null;

static synchronized Node instance() {
public static synchronized Node instance() {
if (instance == null) {
instance = new AbstractNode(
Children.create(new AssetsChildren(OCIManager.getDefault().getActiveSession()), true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.cloud.oracle.assets;
package org.netbeans.modules.cloud.oracle.assets.k8s;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
Expand All @@ -26,6 +26,7 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import java.util.Map;
import java.util.TreeMap;
import org.netbeans.modules.cloud.oracle.assets.PropertiesGenerator;
import org.netbeans.modules.cloud.oracle.compute.ClusterItem;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.cloud.oracle.assets;
package org.netbeans.modules.cloud.oracle.assets.k8s;

import io.fabric8.kubernetes.api.model.PodTemplateSpec;
import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
Expand Down Expand Up @@ -53,6 +53,7 @@
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;
import org.netbeans.modules.cloud.oracle.NotificationUtils;
import org.netbeans.modules.cloud.oracle.assets.CloudAssets;
import org.netbeans.modules.cloud.oracle.compute.ClusterItem;
import org.openide.util.NbBundle;

Expand Down Expand Up @@ -93,7 +94,7 @@ public CompletableFuture<Object> runCommand(String command, List<Object> argumen
}

public CompletableFuture<Object> createSecretRotationCronJob() {
CompletableFuture completableFuture = new CompletableFuture();
CompletableFuture<Object> completableFuture = new CompletableFuture<> ();
this.cluster = CloudAssets.getDefault().getItem(ClusterItem.class);
KubernetesUtils.runWithClient(cluster, client -> {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.netbeans.modules.cloud.oracle.assets.k8s;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import java.util.ArrayList;
import java.util.List;
import org.netbeans.modules.cloud.oracle.compute.ClusterItem;
import org.netbeans.modules.cloud.oracle.compute.PodItem;
import org.openide.util.NbBundle;

/**
*
* @author Jan Horvath
*/
@NbBundle.Messages({
"ForwardingPorts=Forwarding http://localhost:{0} to a pod {1}"

})
public class KubernetesLoaders {

public static List<PodItem> loadPods(ClusterItem cluster, List<String> deploymentNames) {
final List<PodItem> result = new ArrayList<>();
KubernetesUtils.runWithClient(cluster, client -> {
for (String name : deploymentNames) {
Deployment deployment = client
.apps()
.deployments()
.inNamespace(cluster.getNamespace())
.withName(name)
.get();
if (deployment == null) {
continue;
}

var labelSelector = deployment
.getSpec()
.getSelector()
.getMatchLabels();

PodList podList = client.pods()
.inNamespace(cluster.getNamespace())
.withLabels(labelSelector)
.list();
for (Pod pod : podList.getItems()) {
result.add(new PodItem(cluster,
pod.getMetadata().getNamespace(),
pod.getMetadata().getName()));
}
}
});
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.cloud.oracle.assets;
package org.netbeans.modules.cloud.oracle.assets.k8s;

import com.oracle.bmc.Region;
import com.oracle.bmc.http.Priorities;
Expand All @@ -28,8 +28,6 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.batch.v1.CronJob;
import io.fabric8.kubernetes.api.model.batch.v1.CronJobList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
Expand All @@ -45,7 +43,6 @@
import org.netbeans.modules.cloud.oracle.OCIManager;
import org.netbeans.modules.cloud.oracle.OCIProfile;
import org.netbeans.modules.cloud.oracle.compute.ClusterItem;
import org.openide.util.Exceptions;
import org.snakeyaml.engine.v2.api.Dump;
import org.snakeyaml.engine.v2.api.DumpSettings;
import org.snakeyaml.engine.v2.api.Load;
Expand All @@ -57,7 +54,7 @@
*/
public class KubernetesUtils {

static void runWithClient(ClusterItem cluster, Consumer<KubernetesClient> consumer) {
public static void runWithClient(ClusterItem cluster, Consumer<KubernetesClient> consumer) {
if (cluster.getConfig() == null) {
cluster.update();
}
Expand All @@ -67,8 +64,8 @@ static void runWithClient(ClusterItem cluster, Consumer<KubernetesClient> consum
Config config = prepareConfig(cluster.getConfig(), cluster);
try (KubernetesClient client = new KubernetesClientBuilder().withConfig(config).build();) {
consumer.accept(client);
} catch (Exception e) {
Exceptions.printStackTrace(e);
} catch (Throwable t) {
throw t;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.netbeans.modules.cloud.oracle.assets.k8s;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.netbeans.modules.cloud.oracle.compute.PodItem;
import org.openide.awt.ActionID;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle;

/**
*
* @author Jan Horvath
*/
@ActionID(
category = "Tools",
id = "org.netbeans.modules.cloud.oracle.actions.PortForwardAction"
)
@ActionRegistration(
displayName = "#PortForward",
asynchronous = true
)

@NbBundle.Messages({
"PortForward=Start port forwarding"
})
public class PortForwardAction implements ActionListener {
private final PodItem pod;

public PortForwardAction(PodItem pod) {
this.pod = pod;
}

@Override
public void actionPerformed(ActionEvent e) {
PortForwards.getDefault().startPortForward(pod);
}

}
Loading

0 comments on commit d58dfb9

Please sign in to comment.