-
Notifications
You must be signed in to change notification settings - Fork 917
Adding k8s deploy action to cloud assets #7826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
288 changes: 288 additions & 0 deletions
288
enterprise/cloud.oracle/src/org/netbeans/modules/cloud/oracle/assets/RunInClusterAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,288 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import com.oracle.bmc.Region; | ||
| import com.oracle.bmc.http.Priorities; | ||
| import com.oracle.bmc.http.client.HttpClient; | ||
| import com.oracle.bmc.http.client.HttpRequest; | ||
| import com.oracle.bmc.http.client.Method; | ||
| import com.oracle.bmc.http.client.jersey.JerseyHttpProvider; | ||
| import com.oracle.bmc.http.signing.RequestSigningFilter; | ||
| import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
| import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; | ||
| import io.fabric8.kubernetes.api.model.apps.DeploymentList; | ||
| import io.fabric8.kubernetes.client.Config; | ||
| import io.fabric8.kubernetes.client.KubernetesClient; | ||
| import io.fabric8.kubernetes.client.KubernetesClientBuilder; | ||
| import java.awt.event.ActionEvent; | ||
| import java.awt.event.ActionListener; | ||
| import java.io.UnsupportedEncodingException; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URLEncoder; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.time.Instant; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.function.Consumer; | ||
| import org.netbeans.api.progress.ProgressHandle; | ||
| import org.netbeans.api.project.Project; | ||
| import org.netbeans.api.project.ProjectInformation; | ||
| import org.netbeans.api.project.ProjectUtils; | ||
| import org.netbeans.modules.cloud.oracle.OCIManager; | ||
| import org.netbeans.modules.cloud.oracle.OCIProfile; | ||
| import org.netbeans.modules.cloud.oracle.compute.ClusterItem; | ||
| import org.netbeans.modules.cloud.oracle.developer.ContainerTagItem; | ||
| import org.netbeans.modules.cloud.oracle.steps.ProjectStep; | ||
| import org.openide.awt.ActionID; | ||
| import org.openide.awt.ActionRegistration; | ||
| import org.openide.util.Exceptions; | ||
| import org.openide.util.Lookup; | ||
| import org.openide.util.NbBundle; | ||
| import org.openide.util.RequestProcessor; | ||
| import org.snakeyaml.engine.v2.api.Dump; | ||
| import org.snakeyaml.engine.v2.api.DumpSettings; | ||
| import org.snakeyaml.engine.v2.api.Load; | ||
| import org.snakeyaml.engine.v2.api.LoadSettings; | ||
|
|
||
| /** | ||
| * | ||
| * @author Jan Horvath | ||
| */ | ||
| @ActionID( | ||
| category = "Tools", | ||
| id = "org.netbeans.modules.cloud.oracle.actions.RunInClusterAction" | ||
| ) | ||
| @ActionRegistration( | ||
| displayName = "#RunInCluster", | ||
| asynchronous = true | ||
| ) | ||
|
|
||
| @NbBundle.Messages({ | ||
| "RunInCluster=Run in OKE Cluster", | ||
| "Deploying=Deploying project \"{0}\" to the cluster \"{1}\"" | ||
| }) | ||
| public class RunInClusterAction implements ActionListener { | ||
|
|
||
| private final ContainerTagItem context; | ||
| private static final String APP = "app"; //NOI18N | ||
| private static final String DEFAULT = "default"; //NOI18N | ||
| private static final RequestProcessor RP = new RequestProcessor(RunInClusterAction.class); | ||
|
|
||
| public RunInClusterAction(ContainerTagItem context) { | ||
| this.context = context; | ||
| } | ||
|
|
||
| @Override | ||
| public void actionPerformed(ActionEvent e) { | ||
| RP.post(() -> runInCluster()); | ||
| } | ||
|
|
||
| private void runInCluster() { | ||
| ClusterItem cluster = CloudAssets.getDefault().getItem(ClusterItem.class); | ||
| if (cluster.getConfig() == null) { | ||
| cluster.update(); | ||
| } | ||
| if (cluster.getConfig() == null) { | ||
| throw new RuntimeException("Invalid cluster configuration"); | ||
| } | ||
| ProgressHandle h; | ||
| String projectName; | ||
| try { | ||
| Project project = Steps.getDefault() | ||
| .executeMultistep(new ProjectStep(), Lookup.EMPTY) | ||
| .thenApply(values -> { | ||
| return values.getValueForStep(ProjectStep.class); | ||
| }) | ||
| .get(); | ||
| ProjectInformation pi = ProjectUtils.getInformation(project); | ||
| projectName = pi.getDisplayName(); | ||
| h = ProgressHandle.createHandle(Bundle.Deploying(projectName, cluster.getName())); | ||
| } catch (InterruptedException | ExecutionException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| try { | ||
| h.start(); | ||
| runWithClient(cluster, client -> { | ||
| Deployment existingDeployment = null; | ||
| DeploymentList dList = client.apps().deployments().list(); | ||
| for (Deployment deployment : dList.getItems()) { | ||
| if (projectName.equals(deployment.getMetadata().getName())) { | ||
| existingDeployment = deployment; | ||
| } | ||
| } | ||
| if (existingDeployment != null) { | ||
| client.apps() | ||
| .deployments() | ||
| .inNamespace(DEFAULT) | ||
| .withName(projectName) | ||
| .edit(d -> new DeploymentBuilder(d) | ||
| .editSpec() | ||
| .editTemplate() | ||
| .editSpec() | ||
| .editFirstContainer() | ||
| .withImage(context.getUrl()) // New image version | ||
| .endContainer() | ||
| .endSpec() | ||
| .endTemplate() | ||
| .endSpec() | ||
| .build()); | ||
|
|
||
| client.apps() | ||
| .deployments() | ||
| .inNamespace(DEFAULT) | ||
| .withName(projectName) | ||
| .edit(d -> new DeploymentBuilder(d) | ||
| .editSpec() | ||
| .editTemplate() | ||
| .editMetadata() | ||
| .addToAnnotations("kubectl.kubernetes.io/restartedAt", Instant.now().toString()) //NOI18N | ||
| .endMetadata() | ||
| .endTemplate() | ||
| .endSpec() | ||
| .build()); | ||
|
|
||
| } else { | ||
| Deployment newDeployment = new DeploymentBuilder() | ||
| .withNewMetadata() | ||
| .withName(projectName) | ||
| .addToLabels(APP, projectName) | ||
| .endMetadata() | ||
| .withNewSpec() | ||
| .withReplicas(3) | ||
| .withNewSelector() | ||
| .addToMatchLabels(APP, projectName) | ||
| .endSelector() | ||
| .withNewTemplate() | ||
| .withNewMetadata() | ||
| .addToLabels(APP, projectName) | ||
| .endMetadata() | ||
| .withNewSpec() | ||
| .addNewImagePullSecret().withName("docker-bearer-vscode-generated-ocirsecret").endImagePullSecret() //NOI18N | ||
| .addNewContainer() | ||
| .withName(projectName) | ||
| .withImage(context.getUrl()) | ||
| .addNewPort() | ||
| .withContainerPort(8080) | ||
| .endPort() | ||
| .endContainer() | ||
| .endSpec() | ||
| .endTemplate() | ||
| .endSpec() | ||
| .build(); | ||
|
|
||
| client.apps() | ||
| .deployments() | ||
| .inNamespace(DEFAULT) | ||
| .resource(newDeployment) | ||
| .create(); | ||
| } | ||
| }); | ||
| } finally { | ||
| h.finish(); | ||
| } | ||
| } | ||
|
|
||
| private void runWithClient(ClusterItem cluster, Consumer<KubernetesClient> consumer) { | ||
| Config config = prepareConfig(cluster.getConfig(), cluster); | ||
jhorvath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try (KubernetesClient client = new KubernetesClientBuilder().withConfig(config).build();) { | ||
| consumer.accept(client); | ||
| } catch (Exception e) { | ||
| Exceptions.printStackTrace(e); | ||
jhorvath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| private Config prepareConfig(String content, ClusterItem cluster) { | ||
| String token = getBearerToken(cluster); | ||
| LoadSettings settings = LoadSettings.builder().build(); | ||
| Load load = new Load(settings); | ||
| Map<String, Object> data = (Map<String, Object>) load.loadFromString(content); | ||
|
|
||
| List<Map<String, Object>> users = (List<Map<String, Object>>) data.get("users"); //NOI18N | ||
| if (users == null) { | ||
| throw new RuntimeException("Invalid cluster configuration"); | ||
| } | ||
| Map exec = null; | ||
| for (Map<String, Object> userEntry : users) { | ||
jhorvath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Map<String, Object> user = (Map<String, Object>) userEntry.get("user"); //NOI18N | ||
| if (user != null && user.containsKey("exec")) { //NOI18N | ||
| exec = (Map) user.remove("exec"); //NOI18N | ||
| } | ||
| } | ||
| String clusterId = null; | ||
| if (exec != null && "oci".equals(exec.get("command"))) { //NOI18N | ||
| List commandArgs = (List) exec.get("args"); //NOI18N | ||
| boolean clusterIdNext = false; | ||
| for (Object arg : commandArgs) { | ||
| if ("--cluster-id".equals(arg)) { | ||
| clusterIdNext = true; | ||
| continue; | ||
| } | ||
| if (clusterIdNext) { | ||
| clusterId = (String) arg; | ||
| break; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| if (!cluster.getKey().getValue().equals(clusterId)) { | ||
| throw new RuntimeException("Failed to read cluster config"); //NOI18N | ||
| } | ||
| Dump dump = new Dump(DumpSettings.builder().build()); | ||
| String noExec = dump.dumpToString(data); | ||
| Config config = Config.fromKubeconfig(noExec); | ||
| config.setOauthToken(token); | ||
| return config; | ||
| } | ||
|
|
||
| private String getBearerToken(ClusterItem cluster) { | ||
| try { | ||
| OCIProfile profile = OCIManager.getDefault().getActiveProfile(cluster); | ||
| Region region = Region.fromRegionCodeOrId(cluster.getRegionCode()); | ||
| URI uri = new URI(String.format( | ||
| "https://containerengine.%s.oraclecloud.com/cluster_request/%s", //NOI18N | ||
| region.getRegionId(), cluster.getKey().getValue() | ||
| )); | ||
|
|
||
| RequestSigningFilter requestSigningFilter | ||
| = RequestSigningFilter.fromAuthProvider(profile.getAuthenticationProvider()); | ||
|
|
||
| HttpClient client = JerseyHttpProvider.getInstance().newBuilder() | ||
| .registerRequestInterceptor(Priorities.AUTHENTICATION, requestSigningFilter) | ||
| .baseUri(uri).build(); | ||
|
|
||
| HttpRequest request = client.createRequest(Method.GET); | ||
|
|
||
| request.execute().toCompletableFuture(); | ||
jhorvath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| List<String> tokenUrlList = request.headers().get("authorization"); //NOI18N | ||
jhorvath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| String authorization = URLEncoder.encode(tokenUrlList.get(0), "UTF-8"); //NOI18N | ||
| List<String> dateList = request.headers().get("date"); //NOI18N | ||
| String date = URLEncoder.encode(dateList.get(0), "UTF-8"); //NOI18N | ||
| String baseString = String.format("%s?authorization=%s&date=%s", uri.toASCIIString(), authorization, date); //NOI18N | ||
| byte[] urlBytes = baseString.getBytes(StandardCharsets.UTF_8); | ||
| return Base64.getUrlEncoder().encodeToString(urlBytes); | ||
| } catch (URISyntaxException | UnsupportedEncodingException ex) { | ||
| throw new RuntimeException(ex); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.