github.com/secure-build/gitlab-runner@v12.5.0+incompatible/executors/kubernetes/exec.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  
    16  This file was modified by James Munnelly (https://gitlab.com/u/munnerz)
    17  */
    18  
    19  package kubernetes
    20  
    21  import (
    22  	"fmt"
    23  	"io"
    24  	"net/url"
    25  
    26  	"github.com/sirupsen/logrus"
    27  	api "k8s.io/api/core/v1"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/apimachinery/pkg/util/runtime"
    30  	"k8s.io/client-go/kubernetes"
    31  	"k8s.io/client-go/kubernetes/scheme"
    32  	restclient "k8s.io/client-go/rest"
    33  	"k8s.io/client-go/tools/remotecommand"
    34  )
    35  
    36  // RemoteExecutor defines the interface accepted by the Exec command - provided for test stubbing
    37  type RemoteExecutor interface {
    38  	Execute(method string, url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool) error
    39  }
    40  
    41  // DefaultRemoteExecutor is the standard implementation of remote command execution
    42  type DefaultRemoteExecutor struct{}
    43  
    44  func (*DefaultRemoteExecutor) Execute(method string, url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool) error {
    45  	exec, err := remotecommand.NewSPDYExecutor(config, method, url)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	return exec.Stream(remotecommand.StreamOptions{
    51  		Stdin:  stdin,
    52  		Stdout: stdout,
    53  		Stderr: stderr,
    54  		Tty:    tty,
    55  	})
    56  }
    57  
    58  // ExecOptions declare the arguments accepted by the Exec command
    59  type ExecOptions struct {
    60  	Namespace     string
    61  	PodName       string
    62  	ContainerName string
    63  	Stdin         bool
    64  	Command       []string
    65  
    66  	In  io.Reader
    67  	Out io.Writer
    68  	Err io.Writer
    69  
    70  	Executor RemoteExecutor
    71  	Client   *kubernetes.Clientset
    72  	Config   *restclient.Config
    73  }
    74  
    75  // Run executes a validated remote execution against a pod.
    76  func (p *ExecOptions) Run() error {
    77  	pod, err := p.Client.CoreV1().Pods(p.Namespace).Get(p.PodName, metav1.GetOptions{})
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	if pod.Status.Phase != api.PodRunning {
    83  		return fmt.Errorf("Pod '%s' (on namespace '%s') is not running and cannot execute commands; current phase is '%s'",
    84  			p.PodName, p.Namespace, pod.Status.Phase)
    85  	}
    86  
    87  	containerName := p.ContainerName
    88  	if len(containerName) == 0 {
    89  		logrus.Infof("defaulting container name to '%s'", pod.Spec.Containers[0].Name)
    90  		containerName = pod.Spec.Containers[0].Name
    91  	}
    92  
    93  	// TODO: refactor with terminal helpers from the edit utility once that is merged
    94  	var stdin io.Reader
    95  	if p.Stdin {
    96  		stdin = p.In
    97  	}
    98  
    99  	// TODO: consider abstracting into a client invocation or client helper
   100  	req := p.Client.CoreV1().RESTClient().Post().
   101  		Resource("pods").
   102  		Name(pod.Name).
   103  		Namespace(pod.Namespace).
   104  		SubResource("exec").
   105  		Param("container", containerName)
   106  	req.VersionedParams(&api.PodExecOptions{
   107  		Container: containerName,
   108  		Command:   p.Command,
   109  		Stdin:     stdin != nil,
   110  		Stdout:    p.Out != nil,
   111  		Stderr:    p.Err != nil,
   112  	}, scheme.ParameterCodec)
   113  
   114  	return p.Executor.Execute("POST", req.URL(), p.Config, stdin, p.Out, p.Err, false)
   115  }
   116  
   117  func init() {
   118  	runtime.ErrorHandlers = append(runtime.ErrorHandlers, func(err error) {
   119  		logrus.WithError(err).Error("K8S stream error")
   120  	})
   121  
   122  	runtime.PanicHandlers = append(runtime.PanicHandlers, func(r interface{}) {
   123  		logrus.Errorf("K8S stream panic: %v", r)
   124  	})
   125  }