diff --git a/Tasks/HelmDeploy/Strings/resources.resjson/en-US/resources.resjson b/Tasks/HelmDeploy/Strings/resources.resjson/en-US/resources.resjson index bd6879ee7ab2..a754c3586b0b 100644 --- a/Tasks/HelmDeploy/Strings/resources.resjson/en-US/resources.resjson +++ b/Tasks/HelmDeploy/Strings/resources.resjson/en-US/resources.resjson @@ -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", diff --git a/Tasks/HelmDeploy/src/clusters/armkubernetescluster.ts b/Tasks/HelmDeploy/src/clusters/armkubernetescluster.ts new file mode 100644 index 000000000000..7463164281b1 --- /dev/null +++ b/Tasks/HelmDeploy/src/clusters/armkubernetescluster.ts @@ -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 { + 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 { + 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 { + var clusterName : string = tl.getInput("kubernetesCluster", true); + var azureSubscriptionEndpoint : string = tl.getInput("azureSubscriptionEndpoint", true); + return getKubeConfigFromAKS(azureSubscriptionEndpoint, clusterName); +} diff --git a/Tasks/HelmDeploy/src/clusters/generickubernetescluster.ts b/Tasks/HelmDeploy/src/clusters/generickubernetescluster.ts new file mode 100644 index 000000000000..a96b74cfbcb7 --- /dev/null +++ b/Tasks/HelmDeploy/src/clusters/generickubernetescluster.ts @@ -0,0 +1,8 @@ +"use strict"; + +import tl = require('vsts-task-lib/task'); + +export async function getKubeConfig(): Promise { + var kubernetesServiceEndpoint = tl.getInput("kubernetesServiceEndpoint", true); + return tl.getEndpointAuthorizationParameter(kubernetesServiceEndpoint, 'kubeconfig', false); +} \ No newline at end of file diff --git a/Tasks/HelmDeploy/src/helm.ts b/Tasks/HelmDeploy/src/helm.ts index 222cc677afc1..6d164711cfa9 100644 --- a/Tasks/HelmDeploy/src/helm.ts +++ b/Tasks/HelmDeploy/src/helm.ts @@ -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 { - 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 { - 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 { - return getKubeConfig(azureSubscriptionEndpoint, clusterName).then((config) => { +async function getKubeConfigFile(): Promise { + return getClusterType().getKubeConfig().then((config) => { var configFilePath = getKubeConfigFilePath(); tl.debug(tl.loc("KubeConfigFilePath", configFilePath)); fs.writeFileSync(configFilePath, config); @@ -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(); @@ -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]); } diff --git a/Tasks/HelmDeploy/src/helminit.ts b/Tasks/HelmDeploy/src/helmcommands/helminit.ts similarity index 94% rename from Tasks/HelmDeploy/src/helminit.ts rename to Tasks/HelmDeploy/src/helmcommands/helminit.ts index eba2ce669e17..13045e1c9c71 100644 --- a/Tasks/HelmDeploy/src/helminit.ts +++ b/Tasks/HelmDeploy/src/helmcommands/helminit.ts @@ -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); diff --git a/Tasks/HelmDeploy/src/helminstall.ts b/Tasks/HelmDeploy/src/helmcommands/helminstall.ts similarity index 96% rename from Tasks/HelmDeploy/src/helminstall.ts rename to Tasks/HelmDeploy/src/helmcommands/helminstall.ts index 821160822d65..f163f7a95612 100644 --- a/Tasks/HelmDeploy/src/helminstall.ts +++ b/Tasks/HelmDeploy/src/helmcommands/helminstall.ts @@ -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); diff --git a/Tasks/HelmDeploy/src/uinotimplementedcommands.ts b/Tasks/HelmDeploy/src/helmcommands/uinotimplementedcommands.ts similarity index 91% rename from Tasks/HelmDeploy/src/uinotimplementedcommands.ts rename to Tasks/HelmDeploy/src/helmcommands/uinotimplementedcommands.ts index 21a21a4016c1..17a8ecf6009c 100644 --- a/Tasks/HelmDeploy/src/uinotimplementedcommands.ts +++ b/Tasks/HelmDeploy/src/helmcommands/uinotimplementedcommands.ts @@ -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); diff --git a/Tasks/HelmDeploy/task.json b/Tasks/HelmDeploy/task.json index c3f79e0d12e5..c4a4812b2250 100644 --- a/Tasks/HelmDeploy/task.json +++ b/Tasks/HelmDeploy/task.json @@ -13,7 +13,7 @@ "version": { "Major": 0, "Minor": 1, - "Patch": 2 + "Patch": 3 }, "demands": [], "preview": "true", @@ -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" @@ -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", diff --git a/Tasks/HelmDeploy/task.loc.json b/Tasks/HelmDeploy/task.loc.json index d9fdb1ba511b..f9e041207575 100644 --- a/Tasks/HelmDeploy/task.loc.json +++ b/Tasks/HelmDeploy/task.loc.json @@ -13,7 +13,7 @@ "version": { "Major": 0, "Minor": 1, - "Patch": 2 + "Patch": 3 }, "demands": [], "preview": "true", @@ -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" @@ -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", @@ -219,4 +232,4 @@ "FailedToListClusters": "ms-resource:loc.messages.FailedToListClusters", "RetryingRequest": "ms-resource:loc.messages.RetryingRequest" } -} \ No newline at end of file +}