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

added support for kubernetes endpoint #6697

Merged
merged 2 commits into from
Mar 15, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"loc.input.help.azureSubscriptionEndpoint": "Select an Azure subscription, which has your Azure Container Registry.",
"loc.input.label.kubernetesCluster": "Kubernetes cluster",
"loc.input.help.kubernetesCluster": "Select an Azure Managed Cluster.",
"loc.input.label.kubernetesServiceEndpoint": "Kubernetes Service Connection",
"loc.input.help.kubernetesServiceEndpoint": "Select a Kubernetes service connection.",
"loc.input.label.namespace": "Namespace",
"loc.input.help.namespace": "Specify K8 namespace to use. Use Tiller namespace in advance section to specify tiller namespace.",
"loc.input.label.command": "Command",
Expand Down
49 changes: 49 additions & 0 deletions Tasks/HelmDeploy/src/clusters/armkubernetescluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";

import tl = require('vsts-task-lib/task');
import { AzureAksService } from 'azure-arm-rest/azure-arm-aks-service';
import { AzureRMEndpoint } from 'azure-arm-rest/azure-arm-endpoint';
import { AzureEndpoint, AKSCluster, AKSClusterAccessProfile} from 'azure-arm-rest/azureModels';

function extractResourceGroup(id: string): string {
var array = id.split('/');
return array[array.findIndex(str=> str.toUpperCase() === "resourceGroups".toUpperCase()) + 1];
}

// Get cluster info
async function getCluster(aks: AzureAksService, clusterName: string) : Promise<AKSCluster> {
var temp: AKSCluster[] = await aks.list();
var cluster = temp.find((element) => {
return element.name.toLowerCase() === clusterName.toLowerCase();
});

if(!cluster) {
tl.error(tl.loc("ClusterNotFound", clusterName));
throw new Error(tl.loc("ClusterNotFound", clusterName));
}

tl.debug(tl.loc("KubernetesClusterInfo", cluster.id, cluster.properties.kubernetesVersion, cluster.properties.provisioningState));
if(cluster.properties.provisioningState.toLowerCase() !== "succeeded") {
tl.warning(tl.loc("ClusterNotProvisioned", clusterName, cluster.properties.provisioningState));
}
return cluster;
}

// get kubeconfig file content
async function getKubeConfigFromAKS(azureSubscriptionEndpoint: string, clusterName: string) : Promise<string> {
var azureEndpoint: AzureEndpoint = await (new AzureRMEndpoint(azureSubscriptionEndpoint)).getEndpoint();
var aks = new AzureAksService(azureEndpoint);
var cluster = await getCluster(aks, clusterName);
var resourceGroup = extractResourceGroup(cluster.id);
tl.debug(tl.loc("KubernetesClusterResourceGroup", cluster.name, resourceGroup));

var clusterInfo : AKSClusterAccessProfile = await aks.getAccessProfile(resourceGroup, clusterName);
var Base64 = require('js-base64').Base64;
return Base64.decode(clusterInfo.properties.kubeConfig);
}

export async function getKubeConfig(): Promise<string> {
var clusterName : string = tl.getInput("kubernetesCluster", true);
var azureSubscriptionEndpoint : string = tl.getInput("azureSubscriptionEndpoint", true);
return getKubeConfigFromAKS(azureSubscriptionEndpoint, clusterName);
}
8 changes: 8 additions & 0 deletions Tasks/HelmDeploy/src/clusters/generickubernetescluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

import tl = require('vsts-task-lib/task');

export async function getKubeConfig(): Promise<string> {
var kubernetesServiceEndpoint = tl.getInput("kubernetesServiceEndpoint", true);
return tl.getEndpointAuthorizationParameter(kubernetesServiceEndpoint, 'kubeconfig', false);
}
55 changes: 12 additions & 43 deletions Tasks/HelmDeploy/src/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,23 @@ import * as commonCommandOptions from "./commoncommandoption"

tl.setResourcePath(path.join(__dirname, '..' , 'task.json'));

function extractResourceGroup(id: string): string {
var array = id.split('/');
return array[array.findIndex(str=> str.toUpperCase() === "resourceGroups".toUpperCase()) + 1];
}

function getKubeConfigFilePath(): string {
var userdir = helmutil.getTaskTempDir();
return path.join(userdir, "config");
}

// Get cluster info
async function getCluster(aks: AzureAksService, clusterName: string) : Promise<AKSCluster> {
var temp: AKSCluster[] = await aks.list();
var cluster = temp.find((element) => {
return element.name.toLowerCase() === clusterName.toLowerCase();
});

if(!cluster) {
tl.error(tl.loc("ClusterNotFound", clusterName));
throw new Error(tl.loc("ClusterNotFound", clusterName));
}

tl.debug(tl.loc("KubernetesClusterInfo", cluster.id, cluster.properties.kubernetesVersion, cluster.properties.provisioningState));
if(cluster.properties.provisioningState.toLowerCase() !== "succeeded") {
tl.warning(tl.loc("ClusterNotProvisioned", clusterName, cluster.properties.provisioningState));
function getClusterType(): any {
var connectionType = tl.getInput("connectionType", true);
if(connectionType === "Azure Resource Manager") {
return require("./clusters/armkubernetescluster")
}
return cluster;
}

// get kubeconfig file content
async function getKubeConfig(azureSubscriptionEndpoint: string, clusterName: string) : Promise<string> {
var azureEndpoint: AzureEndpoint = await (new AzureRMEndpoint(azureSubscriptionEndpoint)).getEndpoint();
var aks = new AzureAksService(azureEndpoint);
var cluster = await getCluster(aks, clusterName);
var resourceGroup = extractResourceGroup(cluster.id);
tl.debug(tl.loc("KubernetesClusterResourceGroup", cluster.name, resourceGroup));

var clusterInfo : AKSClusterAccessProfile = await aks.getAccessProfile(resourceGroup, clusterName);
var Base64 = require('js-base64').Base64;
return Base64.decode(clusterInfo.properties.kubeConfig);

return require("./clusters/generickubernetescluster")
}

// get kubeconfig file path
async function getKubeConfigFile(azureSubscriptionEndpoint: string, clusterName: string): Promise<string> {
return getKubeConfig(azureSubscriptionEndpoint, clusterName).then((config) => {
async function getKubeConfigFile(): Promise<string> {
return getClusterType().getKubeConfig().then((config) => {
var configFilePath = getKubeConfigFilePath();
tl.debug(tl.loc("KubeConfigFilePath", configFilePath));
fs.writeFileSync(configFilePath, config);
Expand Down Expand Up @@ -87,9 +59,7 @@ function configureHelm() : helmcli {
}

async function run() {
var clusterName : string = tl.getInput("kubernetesCluster", true);
var azureSubscriptionEndpoint : string = tl.getInput("azureSubscriptionEndpoint", true);
var kubeconfigfilePath = await getKubeConfigFile(azureSubscriptionEndpoint, clusterName);
var kubeconfigfilePath = await getKubeConfigFile();
var kubectlCli: kubernetescli = configureKubernetes(kubeconfigfilePath);
var helmCli : helmcli = configureHelm();
kubectlCli.login();
Expand All @@ -112,13 +82,12 @@ function runHelm(helmCli: helmcli) {

var command = tl.getInput("command", true);


var helmCommandMap ={
"init":"./helminit",
"install":"./helminstall"
"init":"./helmcommands/helminit",
"install":"./helmcommands/helminstall"
}

var commandImplementation = require("./uinotimplementedcommands");
var commandImplementation = require("./helmcommands/uinotimplementedcommands");
if(command in helmCommandMap) {
commandImplementation = require(helmCommandMap[command]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import tl = require('vsts-task-lib/task');
import helmcli from "./helmcli";
import helmcli from "./../helmcli";

export function addArguments(helmCli: helmcli) : void {
var waitForTiller = tl.getBoolInput('waitForExecution', false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import tl = require('vsts-task-lib/task');
import helmcli from "./helmcli";
import helmcli from "./../helmcli";

export function addArguments(helmCli: helmcli) : void {
var chart = tl.getInput("chart", false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import tl = require('vsts-task-lib/task');
import helmcli from "./helmcli";
import helmcli from "./../helmcli";

export function addArguments(helmCli: helmcli) : void {
var namespace = tl.getInput("namespace", false);
Expand Down
15 changes: 13 additions & 2 deletions Tasks/HelmDeploy/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 1,
"Patch": 2
"Patch": 3
},
"demands": [],
"preview": "true",
Expand Down Expand Up @@ -42,7 +42,8 @@
"defaultValue": "Azure Resource Manager",
"required": true,
"options": {
"Azure Resource Manager": "Azure Resource Manager"
"Azure Resource Manager": "Azure Resource Manager",
"Kubernetes Service Connection": "Kubernetes Service Connection"
},
"helpMarkDown": "Select a Connection Type.",
"groupName": "cluster"
Expand All @@ -68,6 +69,16 @@
"required": true,
"groupName": "cluster"
},
{
"name": "kubernetesServiceEndpoint",
"aliases": ["kubernetesServiceConnection"],
"type": "connectedService:kubernetes",
"label": "Kubernetes Service Connection",
"helpMarkDown": "Select a Kubernetes service connection.",
"visibleRule": "connectionType = Kubernetes Service Connection",
"required": true,
"groupName": "cluster"
},
{
"name": "namespace",
"label": "Namespace",
Expand Down
19 changes: 16 additions & 3 deletions Tasks/HelmDeploy/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 1,
"Patch": 2
"Patch": 3
},
"demands": [],
"preview": "true",
Expand Down Expand Up @@ -42,7 +42,8 @@
"defaultValue": "Azure Resource Manager",
"required": true,
"options": {
"Azure Resource Manager": "Azure Resource Manager"
"Azure Resource Manager": "Azure Resource Manager",
"Kubernetes Service Connection": "Kubernetes Service Connection"
},
"helpMarkDown": "ms-resource:loc.input.help.connectionType",
"groupName": "cluster"
Expand Down Expand Up @@ -70,6 +71,18 @@
"required": true,
"groupName": "cluster"
},
{
"name": "kubernetesServiceEndpoint",
"aliases": [
"kubernetesServiceConnection"
],
"type": "connectedService:kubernetes",
"label": "ms-resource:loc.input.label.kubernetesServiceEndpoint",
"helpMarkDown": "ms-resource:loc.input.help.kubernetesServiceEndpoint",
"visibleRule": "connectionType = Kubernetes Service Connection",
"required": true,
"groupName": "cluster"
},
{
"name": "namespace",
"label": "ms-resource:loc.input.label.namespace",
Expand Down Expand Up @@ -219,4 +232,4 @@
"FailedToListClusters": "ms-resource:loc.messages.FailedToListClusters",
"RetryingRequest": "ms-resource:loc.messages.RetryingRequest"
}
}
}