Improve kubectl plugin
This commit is contained in:
parent
ef0b1633e2
commit
9d62ec97de
14 changed files with 1117 additions and 464 deletions
|
|
@ -17,116 +17,188 @@ limitations under the License.
|
|||
package request
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
apiv1 "k8s.io/api/core/v1"
|
||||
"k8s.io/api/extensions/v1beta1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
extensions "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
|
||||
"k8s.io/client-go/tools/remotecommand"
|
||||
"k8s.io/ingress-nginx/cmd/plugin/util"
|
||||
)
|
||||
|
||||
const (
|
||||
ingressPodName = "nginx-ingress-controller"
|
||||
ingressServiceName = "ingress-nginx"
|
||||
)
|
||||
// ChoosePod finds a pod either by deployment or by name
|
||||
func ChoosePod(flags *genericclioptions.ConfigFlags, podName string, deployment string) (apiv1.Pod, error) {
|
||||
if podName != "" {
|
||||
return GetNamedPod(flags, podName)
|
||||
}
|
||||
|
||||
// NamedPodExec finds a pod with the given name, executes a command inside it, and returns stdout
|
||||
func NamedPodExec(flags *genericclioptions.ConfigFlags, podName string, cmd []string) (string, error) {
|
||||
return GetDeploymentPod(flags, deployment)
|
||||
}
|
||||
|
||||
// GetNamedPod finds a pod with the given name
|
||||
func GetNamedPod(flags *genericclioptions.ConfigFlags, name string) (apiv1.Pod, error) {
|
||||
allPods, err := getPods(flags)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return apiv1.Pod{}, err
|
||||
}
|
||||
|
||||
for _, pod := range allPods {
|
||||
if pod.Name == podName {
|
||||
return podExec(flags, &pod, cmd)
|
||||
if pod.Name == name {
|
||||
return pod, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Pod %v not found in namespace %v", podName, util.GetNamespace(flags))
|
||||
return apiv1.Pod{}, fmt.Errorf("Pod %v not found in namespace %v", name, util.GetNamespace(flags))
|
||||
}
|
||||
|
||||
// IngressPodExec finds an ingress-nginx pod in the given namespace, executes a command inside it, and returns stdout
|
||||
func IngressPodExec(flags *genericclioptions.ConfigFlags, cmd []string) (string, error) {
|
||||
ings, err := getIngressPods(flags)
|
||||
// GetDeploymentPod finds a pod from a given deployment
|
||||
func GetDeploymentPod(flags *genericclioptions.ConfigFlags, deployment string) (apiv1.Pod, error) {
|
||||
ings, err := getDeploymentPods(flags, deployment)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return apiv1.Pod{}, err
|
||||
}
|
||||
|
||||
if len(ings) == 0 {
|
||||
return "", fmt.Errorf("No ingress-nginx pods found in namespace %v", util.GetNamespace(flags))
|
||||
return apiv1.Pod{}, fmt.Errorf("No pods for deployment %v found in namespace %v", deployment, util.GetNamespace(flags))
|
||||
}
|
||||
|
||||
return podExec(flags, &ings[0], cmd)
|
||||
return ings[0], nil
|
||||
}
|
||||
|
||||
func podExec(flags *genericclioptions.ConfigFlags, pod *apiv1.Pod, cmd []string) (string, error) {
|
||||
config, err := flags.ToRESTConfig()
|
||||
// GetIngressDefinitions returns an array of Ingress resource definitions
|
||||
func GetIngressDefinitions(flags *genericclioptions.ConfigFlags, namespace string) ([]v1beta1.Ingress, error) {
|
||||
rawConfig, err := flags.ToRESTConfig()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return make([]v1beta1.Ingress, 0), err
|
||||
}
|
||||
|
||||
client, err := corev1.NewForConfig(config)
|
||||
api, err := extensions.NewForConfig(rawConfig)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return make([]v1beta1.Ingress, 0), err
|
||||
}
|
||||
|
||||
namespace, _, err := flags.ToRawKubeConfigLoader().Namespace()
|
||||
pods, err := api.Ingresses(namespace).List(metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return "", err
|
||||
return make([]v1beta1.Ingress, 0), err
|
||||
}
|
||||
|
||||
restClient := client.RESTClient()
|
||||
|
||||
req := restClient.Post().
|
||||
Resource("pods").
|
||||
Name(pod.Name).
|
||||
Namespace(namespace).
|
||||
SubResource("exec").
|
||||
Param("container", ingressPodName)
|
||||
|
||||
req.VersionedParams(&apiv1.PodExecOptions{
|
||||
Container: ingressPodName,
|
||||
Command: cmd,
|
||||
Stdin: false,
|
||||
Stdout: true,
|
||||
Stderr: false,
|
||||
TTY: false,
|
||||
}, scheme.ParameterCodec)
|
||||
|
||||
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
stdout := bytes.NewBuffer(make([]byte, 0))
|
||||
err = exec.Stream(remotecommand.StreamOptions{
|
||||
Stdout: stdout,
|
||||
})
|
||||
|
||||
return stdout.String(), err
|
||||
return pods.Items, nil
|
||||
}
|
||||
|
||||
func getIngressPods(flags *genericclioptions.ConfigFlags) ([]apiv1.Pod, error) {
|
||||
pods, err := getPods(flags)
|
||||
// GetNumEndpoints counts the number of endpoints for the service with the given name
|
||||
func GetNumEndpoints(flags *genericclioptions.ConfigFlags, namespace string, serviceName string) (*int, error) {
|
||||
endpoints, err := GetEndpointsByName(flags, namespace, serviceName)
|
||||
if err != nil {
|
||||
return make([]apiv1.Pod, 0), err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ingressPods := make([]apiv1.Pod, 0)
|
||||
for _, pod := range pods {
|
||||
if pod.Spec.Containers[0].Name == ingressPodName {
|
||||
ingressPods = append(ingressPods, pod)
|
||||
if endpoints == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ret := 0
|
||||
for _, subset := range endpoints.Subsets {
|
||||
ret += len(subset.Addresses)
|
||||
}
|
||||
return &ret, nil
|
||||
}
|
||||
|
||||
// GetEndpointsByName returns the endpoints for the service with the given name
|
||||
func GetEndpointsByName(flags *genericclioptions.ConfigFlags, namespace string, name string) (*apiv1.Endpoints, error) {
|
||||
allEndpoints, err := getEndpoints(flags, namespace)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, endpoints := range allEndpoints {
|
||||
if endpoints.Name == name {
|
||||
return &endpoints, nil
|
||||
}
|
||||
}
|
||||
|
||||
return ingressPods, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var endpointsCache = make(map[string]*[]apiv1.Endpoints)
|
||||
|
||||
func getEndpoints(flags *genericclioptions.ConfigFlags, namespace string) ([]apiv1.Endpoints, error) {
|
||||
cachedEndpoints, ok := endpointsCache[namespace]
|
||||
if ok {
|
||||
return *cachedEndpoints, nil
|
||||
}
|
||||
|
||||
if namespace != "" {
|
||||
tryAllNamespacesEndpointsCache(flags)
|
||||
}
|
||||
|
||||
cachedEndpoints = tryFilteringEndpointsFromAllNamespacesCache(flags, namespace)
|
||||
if cachedEndpoints != nil {
|
||||
return *cachedEndpoints, nil
|
||||
}
|
||||
|
||||
rawConfig, err := flags.ToRESTConfig()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
api, err := corev1.NewForConfig(rawConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
endpointsList, err := api.Endpoints(namespace).List(metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
endpoints := endpointsList.Items
|
||||
|
||||
endpointsCache[namespace] = &endpoints
|
||||
return endpoints, nil
|
||||
}
|
||||
|
||||
func tryAllNamespacesEndpointsCache(flags *genericclioptions.ConfigFlags) {
|
||||
_, ok := endpointsCache[""]
|
||||
if !ok {
|
||||
_, err := getEndpoints(flags, "")
|
||||
if err != nil {
|
||||
endpointsCache[""] = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func tryFilteringEndpointsFromAllNamespacesCache(flags *genericclioptions.ConfigFlags, namespace string) *[]apiv1.Endpoints {
|
||||
allEndpoints, _ := endpointsCache[""]
|
||||
if allEndpoints != nil {
|
||||
endpoints := make([]apiv1.Endpoints, 0)
|
||||
for _, thisEndpoints := range *allEndpoints {
|
||||
if thisEndpoints.Namespace == namespace {
|
||||
endpoints = append(endpoints, thisEndpoints)
|
||||
}
|
||||
}
|
||||
endpointsCache[namespace] = &endpoints
|
||||
return &endpoints
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetServiceByName finds and returns the service definition with the given name
|
||||
func GetServiceByName(flags *genericclioptions.ConfigFlags, name string, services *[]apiv1.Service) (apiv1.Service, error) {
|
||||
if services == nil {
|
||||
servicesArray, err := getServices(flags)
|
||||
if err != nil {
|
||||
return apiv1.Service{}, err
|
||||
}
|
||||
services = &servicesArray
|
||||
}
|
||||
|
||||
for _, svc := range *services {
|
||||
if svc.Name == name {
|
||||
return svc, nil
|
||||
}
|
||||
}
|
||||
|
||||
return apiv1.Service{}, fmt.Errorf("Could not find service %v in namespace %v", name, util.GetNamespace(flags))
|
||||
}
|
||||
|
||||
func getPods(flags *genericclioptions.ConfigFlags) ([]apiv1.Pod, error) {
|
||||
|
|
@ -150,40 +222,20 @@ func getPods(flags *genericclioptions.ConfigFlags) ([]apiv1.Pod, error) {
|
|||
return pods.Items, nil
|
||||
}
|
||||
|
||||
// GetIngressDefinitions returns an array of Ingress resource definitions
|
||||
func GetIngressDefinitions(flags *genericclioptions.ConfigFlags, namespace string) ([]v1beta1.Ingress, error) {
|
||||
rawConfig, err := flags.ToRESTConfig()
|
||||
func getDeploymentPods(flags *genericclioptions.ConfigFlags, deployment string) ([]apiv1.Pod, error) {
|
||||
pods, err := getPods(flags)
|
||||
if err != nil {
|
||||
return make([]v1beta1.Ingress, 0), err
|
||||
return make([]apiv1.Pod, 0), err
|
||||
}
|
||||
|
||||
api, err := extensions.NewForConfig(rawConfig)
|
||||
if err != nil {
|
||||
return make([]v1beta1.Ingress, 0), err
|
||||
}
|
||||
|
||||
pods, err := api.Ingresses(namespace).List(metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return make([]v1beta1.Ingress, 0), err
|
||||
}
|
||||
|
||||
return pods.Items, nil
|
||||
}
|
||||
|
||||
// GetIngressService finds and returns the ingress-nginx service definition
|
||||
func GetIngressService(flags *genericclioptions.ConfigFlags) (apiv1.Service, error) {
|
||||
services, err := getServices(flags)
|
||||
if err != nil {
|
||||
return apiv1.Service{}, err
|
||||
}
|
||||
|
||||
for _, svc := range services {
|
||||
if svc.Name == ingressServiceName {
|
||||
return svc, nil
|
||||
ingressPods := make([]apiv1.Pod, 0)
|
||||
for _, pod := range pods {
|
||||
if pod.Spec.Containers[0].Name == deployment {
|
||||
ingressPods = append(ingressPods, pod)
|
||||
}
|
||||
}
|
||||
|
||||
return apiv1.Service{}, fmt.Errorf("Could not find service %v in namespace %v", ingressServiceName, util.GetNamespace(flags))
|
||||
return ingressPods, nil
|
||||
}
|
||||
|
||||
func getServices(flags *genericclioptions.ConfigFlags) ([]apiv1.Service, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue