private Task MapPodAsync(PodMapping pod, CancellationToken token)
        {
            return(Task.Run(async() =>
            {
                while (token.IsCancellationRequested == false)
                {
                    var clusterName = string.IsNullOrWhiteSpace(pod.ClusterName)
                        ? await _kubectlService.GetCurrentContextAsync()
                        : pod.ClusterName;

                    var source = $"localhost:{pod.LocalPort}";
                    var target = $"{clusterName}.{pod.Namespace}.{pod.Name}:{pod.RemotePort}";

                    _logger.LogInformation("Forwarding {0} to {1}", source, target);

                    var process = await _kubectlService.PortForwardAsync(pod, token);
                    token.Register(() =>
                    {
                        if (!process.HasExited)
                        {
                            process.Kill(true);
                        }
                    });
                    process.Start();
                    process.WaitForExit();


                    _logger.LogInformation("Port forwarding on {0} --> {1} was terminated. Will try to reload", source,
                                           target);
                    await Task.Delay(500);
                }
            }, token));
        }
Example #2
0
        public async Task <Process> PortForwardAsync(PodMapping podMapping, CancellationToken cancellationToken = default)
        {
            var command = CreateCommand("port-forward ", podMapping.ClusterName, podMapping.Namespace);
            var podName = await FindClosestNameAsync(podMapping);

            if (string.IsNullOrWhiteSpace(podName))
            {
                throw new Exception($"Can't find pod name: {podMapping.Name}");
            }

            command += $"{podName} {podMapping.LocalPort}:{podMapping.RemotePort}";
            return(CreateKubectlProcess(command, false));
        }
Example #3
0
        private async Task <string> FindClosestNameAsync(PodMapping podMapping)
        {
            var pods = await GetPodsAsync(podMapping.ClusterName, podMapping.Namespace);

            return(pods.LastOrDefault(a => a == podMapping.Name || a.StartsWith(podMapping.Name)));
        }