protected override int OnExecute(CommandLineApplication app) { var config = Parent.GetK8SConfiguration(); var clusters = config.Clusters ?? Enumerable.Empty <Cluster>(); if (!clusters.Any()) { Reporter.Output.WriteLine("No clusters found"); return(1); } TableFormatter.Print(clusters, "", null, new Dictionary <string, Func <Cluster, object> > { { "NAME", x => x.Name } }); return(0); }
protected override int OnExecute(CommandLineApplication app) { var config = Parent.GetK8SConfiguration(); var contextsToShow = config.Contexts ?? Enumerable.Empty <Context>(); if (!contextsToShow.Any()) { Reporter.Output.WriteLine("No contexts found"); return(1); } if (!string.IsNullOrEmpty(ContextName)) { var matchingContext = config.Contexts.FirstOrDefault(c => c.Name.Equals(ContextName, StringComparison.InvariantCultureIgnoreCase)); if (matchingContext == null) { Reporter.Output.WriteError($"No context exists with the name: \"{ContextName}\"."); return(1); } contextsToShow = new List <Context> { matchingContext }; } TableFormatter.Print(contextsToShow, "", null, new Dictionary <string, Func <Context, object> > { { "CURRENT", x => x.Name.Equals(config.CurrentContext, StringComparison.InvariantCultureIgnoreCase) ? "*" : "" }, { "NAME", x => x.Name }, { "CLUSTER", x => x.ContextDetails.Cluster }, { "AUTHINFO", x => x.ContextDetails.User }, { "NAMESPACE", x => string.IsNullOrEmpty(x.Namespace) ? "" : x.Namespace } }); return(0); }
protected override int OnExecute(CommandLineApplication app) { var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); var podList = new V1PodList(); try { IKubernetes client = new Kubernetes(config); if (Parent.AllNamespaces) { podList = client.ListPodForAllNamespaces(); } else { podList = client.ListNamespacedPod("default"); } } catch (HttpRequestException ex) { Reporter.Output.WriteError(ex.InnerException.Message); return(1); } var getOutputList = new List <GetPodOutput>(); foreach (var item in podList.Items) { var totalContainers = item.Status.ContainerStatuses.Count; var readyContainers = item.Status.ContainerStatuses.Count(cs => cs.Ready); var restarts = item.Status.ContainerStatuses.Sum(cs => cs.RestartCount); var readyString = $"{readyContainers}/{totalContainers}"; var getPodOutput = new GetPodOutput { Name = item.Metadata.Name, Ready = readyString, Status = item.Status.Phase, Restarts = restarts, Age = DateTime.UtcNow - item.Metadata.CreationTimestamp, Namespace = item.Metadata.NamespaceProperty }; getOutputList.Add(getPodOutput); } var outputDict = new Dictionary <string, Func <GetPodOutput, object> >(); if (Parent.AllNamespaces) { outputDict.Add("NAMESPACE", x => x.Namespace); } outputDict.Add("NAME", x => x.Name); outputDict.Add("READY", x => x.Ready); outputDict.Add("STATUS", x => x.Status); outputDict.Add("RESTARTS", x => x.Restarts); outputDict.Add("AGE", x => x.AgeString); TableFormatter.Print(getOutputList, "No resources found", null, outputDict); return(0); }