Skip to content
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

Use AKS api instead of generic one #17

Merged
merged 1 commit into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
<java.level>7</java.level>
<jenkins-test-harness.version>2.1</jenkins-test-harness.version>

<azuresdk.version>1.3.0</azuresdk.version>
<azure-credentials-plugin.version>1.6.0</azure-credentials-plugin.version>
<azure-commons.version>0.2.5</azure-commons.version>
<kubernetes-cd.version>0.1.4</kubernetes-cd.version>
<azuresdk.version>1.20.1</azuresdk.version>
<azure-credentials-plugin.version>1.6.1</azure-credentials-plugin.version>
<azure-commons.version>0.2.10</azure-commons.version>
<kubernetes-cd.version>0.2.3</kubernetes-cd.version>
</properties>

<name>Azure Container Service Plugin</name>
Expand Down Expand Up @@ -103,6 +103,12 @@
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>${azuresdk.version}</version>
<exclusions>
<exclusion>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import com.google.common.collect.ImmutableSet;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.ContainerService;
import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerService;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.resources.GenericResource;
import com.microsoft.azure.management.resources.ResourceGroup;
import com.microsoft.azure.util.AzureBaseCredentials;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
package com.microsoft.jenkins.acs.commands;

import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.resources.GenericResource;
import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils;
import com.microsoft.jenkins.acs.orchestrators.DeploymentConfig;
import com.microsoft.jenkins.acs.util.AzureHelper;
import com.microsoft.jenkins.acs.util.Constants;
import com.microsoft.jenkins.acs.util.DeployHelper;
import com.microsoft.jenkins.azurecommons.core.credentials.TokenCredentialData;
import hudson.FilePath;
import hudson.model.Item;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -51,28 +46,14 @@ protected FilePath[] resolveConfigFiles() throws IOException, InterruptedExcepti
@Override
protected void prepareKubeconfig(FilePath kubeconfigFile) throws Exception {
Azure azureClient = AzureHelper.buildClient(token);
String id = ResourceUtils.constructResourceId(
azureClient.subscriptionId(),
getResourceGroupName(),
Constants.AKS_PROVIDER,
"accessProfiles",
"clusterAdmin",
String.format("%s/%s", Constants.AKS_RESOURCE_TYPE, getContainerServiceName()));

GenericResource resource = azureClient.genericResources().getById(id);
Object properties = resource.properties();
try {
String userConfig = DeployHelper.getProperty(
properties, "kubeConfig", String.class);
if (StringUtils.isBlank(userConfig)) {
throw new IllegalStateException("Null user kubeconfig returned from Azure");
}
byte[] kubeconfig = Base64.decodeBase64(userConfig);
try (OutputStream out = kubeconfigFile.write()) {
out.write(kubeconfig);
}
} catch (IllegalArgumentException | ClassCastException e) {
throw new IllegalStateException("Failed to get kubeconfig", e);
byte[] adminKubeConfigContent = azureClient.kubernetesClusters()
.getAdminKubeConfigContent(getResourceGroupName(), getContainerServiceName());

if (ArrayUtils.isEmpty(adminKubeConfigContent)) {
throw new IllegalStateException("Null user kubeconfig returned from Azure");
}
try (OutputStream out = kubeconfigFile.write()) {
out.write(adminKubeConfigContent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package com.microsoft.jenkins.acs.commands;

import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.jenkins.acs.Messages;
import com.microsoft.jenkins.acs.util.Constants;
import com.microsoft.jenkins.azurecommons.command.CommandState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.network.LoadBalancer;
import com.microsoft.azure.management.network.LoadBalancerBackend;
import com.microsoft.azure.management.network.LoadBalancerFrontend;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import com.google.common.annotations.VisibleForTesting;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.ContainerService;
import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerService;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.jenkins.acs.AzureACSPlugin;
import com.microsoft.jenkins.acs.Messages;
import com.microsoft.jenkins.acs.util.AzureHelper;
Expand Down Expand Up @@ -139,7 +139,7 @@ static class TaskResult implements Serializable {
private static final long serialVersionUID = 1L;

private CommandState commandState = CommandState.Unknown;
private ContainerServiceOrchestratorTypes orchestratorType;
private transient ContainerServiceOrchestratorTypes orchestratorType;
private String fqdn;
private String adminUsername;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package com.microsoft.jenkins.acs.commands;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.jenkins.acs.AzureACSPlugin;
import com.microsoft.jenkins.acs.Messages;
import com.microsoft.jenkins.acs.orchestrators.DeploymentConfig;
Expand Down Expand Up @@ -131,9 +131,10 @@ void setMasterHost(String masterHost) {
}

static class KubernetesDeployWorker extends MasterToSlaveCallable<TaskResult, Exception> {
private static final long serialVersionUID = -1307651925021528450L;
private TaskListener taskListener;
private DeploymentConfig.Factory configFactory;
private ContainerServiceOrchestratorTypes orchestratorType;
private transient ContainerServiceOrchestratorTypes orchestratorType;
private FilePath workspace;
private EnvVars envVars;
private String managementFqdn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap;
import com.jcraft.jsch.JSchException;
import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.jenkins.acs.AzureACSPlugin;
import com.microsoft.jenkins.acs.Messages;
import com.microsoft.jenkins.acs.orchestrators.DeploymentConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.google.common.annotations.VisibleForTesting;
import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.jenkins.acs.AzureACSPlugin;
import com.microsoft.jenkins.acs.Messages;
import com.microsoft.jenkins.acs.orchestrators.DeploymentConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.microsoft.jenkins.acs.orchestrators;

import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.jenkins.acs.Messages;
import hudson.EnvVars;
import hudson.FilePath;
Expand All @@ -9,6 +9,10 @@
import java.io.Serializable;
import java.util.List;

import static com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes.DCOS;
import static com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes.KUBERNETES;
import static com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes.SWARM;

public abstract class DeploymentConfig implements Serializable {

private final FilePath[] configFiles;
Expand Down Expand Up @@ -56,16 +60,15 @@ public DeploymentConfig build(ContainerServiceOrchestratorTypes type,
throw new IllegalArgumentException(Messages.ACSDeploymentContext_noConfigFilesFound(configFilePaths));
}

switch (type) {
case DCOS:
return new MarathonDeploymentConfig(configFiles);
case KUBERNETES:
return new KubernetesDeploymentConfig(configFiles);
case SWARM:
return new SwarmDeploymentConfig(configFiles);
default:
throw new IllegalArgumentException(
Messages.ACSDeploymentContext_orchestratorNotSupported(type));
if (DCOS.equals(type)) {
return new MarathonDeploymentConfig(configFiles);
} else if (KUBERNETES.equals(type)) {
return new KubernetesDeploymentConfig(configFiles);
} else if (SWARM.equals(type)) {
return new SwarmDeploymentConfig(configFiles);
} else {
throw new IllegalArgumentException(
Messages.ACSDeploymentContext_orchestratorNotSupported(type));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microsoft.azure.management.network.Protocol;
import com.microsoft.jenkins.acs.Messages;
import com.microsoft.jenkins.acs.util.Constants;
import hudson.FilePath;

import java.io.IOException;
Expand Down Expand Up @@ -74,7 +75,7 @@ public List<ServicePort> getServicePorts() throws IOException, InterruptedExcept
Protocol protocol = Protocol.TCP;
JsonNode protocolNode = element.get("protocol");
if (protocolNode != null && protocolNode.asText().equalsIgnoreCase("udp")) {
protocol = Protocol.UDP;
protocol = Constants.UDP;
}

servicePorts.add(new ServicePort(hostPort, containerPort, protocol));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.microsoft.azure.management.network.LoadBalancingRule;
import com.microsoft.azure.management.network.Protocol;
import com.microsoft.azure.management.network.TransportProtocol;
import com.microsoft.jenkins.acs.util.Constants;

public class ServicePort {

Expand Down Expand Up @@ -31,10 +32,10 @@ public Protocol getProtocol() {
public TransportProtocol getTransportProtocol() {
if (protocol.equals(Protocol.TCP)) {
return TransportProtocol.TCP;
} else if (protocol.equals(Protocol.UDP)) {
} else if (protocol.equals(Constants.UDP)) {
return TransportProtocol.UDP;
} else {
return new TransportProtocol(protocol.toString());
return TransportProtocol.fromString(protocol.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.microsoft.azure.management.network.Protocol;
import com.microsoft.jenkins.acs.Messages;
import com.microsoft.jenkins.acs.util.Constants;
import hudson.FilePath;
import hudson.Util;
import org.yaml.snakeyaml.Yaml;
Expand Down Expand Up @@ -138,7 +139,7 @@ private List<ServicePort> parsePortShortSyntax(String def) throws InvalidFormatE

Protocol protocol = Protocol.TCP;
if (protocolText.equalsIgnoreCase("/udp")) {
protocol = Protocol.UDP;
protocol = Constants.UDP;
}

if (extEnd - ext != interEnd - inter) {
Expand Down Expand Up @@ -177,7 +178,7 @@ private List<ServicePort> parsePortLongSyntax(Map node) throws InvalidFormatExce
if (protocolNode != null && protocolNode instanceof String) {
final String protocolText = (String) protocolNode;
if (protocolText.equalsIgnoreCase("udp")) {
protocol = Protocol.UDP;
protocol = Constants.UDP;
}
}

Expand Down
38 changes: 22 additions & 16 deletions src/main/java/com/microsoft/jenkins/acs/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

package com.microsoft.jenkins.acs.util;

import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;

import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.network.Protocol;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

import static com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes.DCOS;
import static com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes.KUBERNETES;
import static com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes.SWARM;

public final class Constants {
public static final String PLUGIN_NAME = "AzureJenkinsACS";

Expand Down Expand Up @@ -68,16 +74,18 @@ public final class Constants {

public static final String MARATHON_DOCKER_CFG_ARCHIVE_URI = "MARATHON_DOCKER_CFG_ARCHIVE_URI";

public static final Protocol UDP = Protocol.fromString("Udp");

public static final Set<ContainerServiceOrchestratorTypes> SUPPORTED_ORCHESTRATOR = new HashSet<>(Arrays.asList(
ContainerServiceOrchestratorTypes.KUBERNETES,
ContainerServiceOrchestratorTypes.DCOS,
ContainerServiceOrchestratorTypes.SWARM
KUBERNETES,
DCOS,
SWARM
));

public static final Set<String> SUPPORTED_ORCHESTRATOR_NAMES = new HashSet<>(Arrays.asList(
ContainerServiceOrchestratorTypes.KUBERNETES.toString(),
ContainerServiceOrchestratorTypes.DCOS.toString(),
ContainerServiceOrchestratorTypes.SWARM.toString()
KUBERNETES.toString(),
DCOS.toString(),
SWARM.toString()
));

public static final String AKS = "AKS";
Expand Down Expand Up @@ -108,16 +116,14 @@ public final class Constants {
public static final String AI_RESOURCE_NAME = "ResourceName";

public static int sshPort(ContainerServiceOrchestratorTypes type) {
switch (type) {
case DCOS:
return DCOS_SSH_PORT;
case KUBERNETES:
return KUBERNETES_SSH_PORT;
case SWARM:
return SWARM_SSH_PORT;
default:
return -1;
if (DCOS.equals(type)) {
return DCOS_SSH_PORT;
} else if (KUBERNETES.equals(type)) {
return KUBERNETES_SSH_PORT;
} else if (SWARM.equals(type)) {
return SWARM_SSH_PORT;
}
return -1;
}

private Constants() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package com.microsoft.jenkins.acs.commands;

import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.jenkins.acs.ACSDeploymentContext;
import com.microsoft.jenkins.acs.util.Constants;
import com.microsoft.jenkins.azurecommons.command.CommandState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void createLoadBalancerRules() throws IOException, EnablePortCommand.Inva

Assert.assertEquals(2, update.rules.size());

Assert.assertEquals("JLBRuleTCP8081", update.rules.get(0).name);
Assert.assertEquals("JLBRuleTcp8081", update.rules.get(0).name);
Assert.assertEquals(TransportProtocol.TCP, update.rules.get(0).protocol);
Assert.assertEquals("frontend", update.rules.get(0).frontend);
Assert.assertEquals(8081, update.rules.get(0).frontendPort);
Expand All @@ -319,7 +319,7 @@ public void createLoadBalancerRules() throws IOException, EnablePortCommand.Inva
update.rules.get(0).idleTimeoutInMinutes);
Assert.assertEquals(LoadDistribution.DEFAULT, update.rules.get(0).loadDistribution);

Assert.assertEquals("JLBRuleTCP8082", update.rules.get(1).name);
Assert.assertEquals("JLBRuleTcp8082", update.rules.get(1).name);
Assert.assertEquals(TransportProtocol.TCP, update.rules.get(1).protocol);
Assert.assertEquals("frontend", update.rules.get(1).frontend);
Assert.assertEquals(8082, update.rules.get(1).frontendPort);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
package com.microsoft.jenkins.acs.commands;

import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.compute.ContainerService;
import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.compute.ContainerServices;
import com.microsoft.azure.management.containerservice.ContainerService;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerServices;
import com.microsoft.jenkins.azurecommons.command.CommandState;
import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

package com.microsoft.jenkins.acs.orchestrators;

import com.microsoft.azure.management.compute.ContainerServiceOrchestratorTypes;
import com.microsoft.azure.management.containerservice.ContainerServiceOrchestratorTypes;
import hudson.EnvVars;
import hudson.FilePath;
import org.junit.Test;
Expand Down
Loading