public override void ExecuteCmdlet() { base.ExecuteCmdlet(); switch (ParameterSetName) { case IdParameterSet: { var resource = new ResourceIdentifier(Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } case InputObjectParameterSet: { var resource = new ResourceIdentifier(InputObject.Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } } RunCmdLet(() => { if (!GeneralUtilities.Probe("kubectl")) { throw new AzPSApplicationException(Resources.KubectlIsRequriedToBeInstalledAndOnYourPathToExecute); } var tmpFileName = Path.GetTempFileName(); var credentials = Client.ManagedClusters.ListClusterAdminCredentials(ResourceGroupName, Name).Kubeconfigs; var encoded = credentials.First(credential => credential.Name.Equals("clusterUser")).Value; AzureSession.Instance.DataStore.WriteFile( tmpFileName, Encoding.UTF8.GetString(encoded)); WriteVerbose(string.Format( Resources.RunningKubectlGetPodsKubeconfigNamespaceSelector, tmpFileName)); var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "kubectl", Arguments = $"get pods --kubeconfig {tmpFileName} --namespace kube-system --output name --selector k8s-app=kubernetes-dashboard", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; proc.Start(); var dashPodName = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); // remove "pods/" or "pod/" dashPodName = dashPodName.Substring(dashPodName.IndexOf('/') + 1).TrimEnd('\r', '\n'); var procDashboardPort = new Process { StartInfo = new ProcessStartInfo { FileName = "kubectl", Arguments = $"get pods --kubeconfig {tmpFileName} --namespace kube-system --selector k8s-app=kubernetes-dashboard --output jsonpath='{{.items[0].spec.containers[0].ports[0].containerPort}}'", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; procDashboardPort.Start(); var dashboardPortOutput = procDashboardPort.StandardOutput.ReadToEnd(); procDashboardPort.WaitForExit(); dashboardPortOutput = dashboardPortOutput.Replace("'", ""); int dashboardPort = int.Parse(dashboardPortOutput); string protocol = dashboardPort == 8443 ? "https" : "http"; string dashboardUrl = $"{protocol}://{ListenAddress}:{ListenPort}"; //TODO: check in cloudshell //TODO: support for --address {ListenAddress} WriteVerbose(string.Format( Resources.RunningInBackgroundJobKubectlTunnel, tmpFileName, dashPodName)); var exitingJob = JobRepository.Jobs.FirstOrDefault(j => j.Name == "Kubectl-Tunnel"); if (exitingJob != null) { WriteVerbose(Resources.StoppingExistingKubectlTunnelJob); exitingJob.StopJob(); JobRepository.Remove(exitingJob); } var job = new KubeTunnelJob(tmpFileName, dashPodName, ListenPort, dashboardPort); if (!DisableBrowser) { WriteVerbose(Resources.SettingUpBrowserPop); job.StartJobCompleted += (sender, evt) => { WriteVerbose(string.Format(Resources.StartingBrowser, dashboardUrl)); PopBrowser(dashboardUrl); }; } JobRepository.Add(job); job.StartJob(); WriteObject(job); }); }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); switch (ParameterSetName) { case IdParameterSet: { var resource = new ResourceIdentifier(Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } case InputObjectParameterSet: { var resource = new ResourceIdentifier(InputObject.Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } } RunCmdLet(() => { if (!GeneralUtilities.Probe("kubectl")) { throw new CmdletInvocationException(Resources.KubectlIsRequriedToBeInstalledAndOnYourPathToExecute); } var tmpFileName = Path.GetTempFileName(); var encoded = Client.ManagedClusters.GetAccessProfiles(ResourceGroupName, Name, "clusterUser") .KubeConfig; AzureSession.Instance.DataStore.WriteFile( tmpFileName, Encoding.UTF8.GetString(Convert.FromBase64String(encoded))); WriteVerbose(string.Format( Resources.RunningKubectlGetPodsKubeconfigNamespaceSelector, tmpFileName)); var proc = new Process { StartInfo = new ProcessStartInfo { FileName = "kubectl", Arguments = $"get pods --kubeconfig {tmpFileName} --namespace kube-system --output name --selector k8s-app=kubernetes-dashboard", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; proc.Start(); var dashPodName = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); // remove "pods/" dashPodName = dashPodName.Substring(5).TrimEnd('\r', '\n'); WriteVerbose(string.Format( Resources.RunningInBackgroundJobKubectlTunnel, tmpFileName, dashPodName)); var exitingJob = JobRepository.Jobs.FirstOrDefault(j => j.Name == "Kubectl-Tunnel"); if (exitingJob != null) { WriteVerbose(Resources.StoppingExistingKubectlTunnelJob); exitingJob.StopJob(); JobRepository.Remove(exitingJob); } var job = new KubeTunnelJob(tmpFileName, dashPodName); if (!DisableBrowser) { WriteVerbose(Resources.SettingUpBrowserPop); job.StartJobCompleted += (sender, evt) => { WriteVerbose(string.Format(Resources.StartingBrowser, ProxyUrl)); PopBrowser(ProxyUrl); }; } JobRepository.Add(job); job.StartJob(); WriteObject(job); }); }