github.com/alloyci/alloy-runner@v1.0.1-0.20180222164613-925503ccafd6/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  	log "github.com/Sirupsen/logrus"
    27  	"k8s.io/kubernetes/pkg/api"
    28  	"k8s.io/kubernetes/pkg/client/restclient"
    29  	client "k8s.io/kubernetes/pkg/client/unversioned"
    30  	"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
    31  	remotecommandserver "k8s.io/kubernetes/pkg/kubelet/server/remotecommand"
    32  )
    33  
    34  // RemoteExecutor defines the interface accepted by the Exec command - provided for test stubbing
    35  type RemoteExecutor interface {
    36  	Execute(method string, url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool) error
    37  }
    38  
    39  // DefaultRemoteExecutor is the standard implementation of remote command execution
    40  type DefaultRemoteExecutor struct{}
    41  
    42  func (*DefaultRemoteExecutor) Execute(method string, url *url.URL, config *restclient.Config, stdin io.Reader, stdout, stderr io.Writer, tty bool) error {
    43  	exec, err := remotecommand.NewExecutor(config, method, url)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	return exec.Stream(remotecommandserver.SupportedStreamingProtocols, stdin, stdout, stderr, tty)
    48  }
    49  
    50  // ExecOptions declare the arguments accepted by the Exec command
    51  type ExecOptions struct {
    52  	Namespace     string
    53  	PodName       string
    54  	ContainerName string
    55  	Stdin         bool
    56  	Command       []string
    57  
    58  	In  io.Reader
    59  	Out io.Writer
    60  	Err io.Writer
    61  
    62  	Executor RemoteExecutor
    63  	Client   *client.Client
    64  	Config   *restclient.Config
    65  }
    66  
    67  // Run executes a validated remote execution against a pod.
    68  func (p *ExecOptions) Run() error {
    69  	pod, err := p.Client.Pods(p.Namespace).Get(p.PodName)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	if pod.Status.Phase != api.PodRunning {
    75  		return fmt.Errorf("Pod '%s' (on namespace '%s') is not running and cannot execute commands; current phase is '%s'",
    76  			p.PodName, p.Namespace, pod.Status.Phase)
    77  	}
    78  
    79  	containerName := p.ContainerName
    80  	if len(containerName) == 0 {
    81  		log.Infof("defaulting container name to '%s'", pod.Spec.Containers[0].Name)
    82  		containerName = pod.Spec.Containers[0].Name
    83  	}
    84  
    85  	// TODO: refactor with terminal helpers from the edit utility once that is merged
    86  	var stdin io.Reader
    87  	if p.Stdin {
    88  		stdin = p.In
    89  	}
    90  
    91  	// TODO: consider abstracting into a client invocation or client helper
    92  	req := p.Client.RESTClient.Post().
    93  		Resource("pods").
    94  		Name(pod.Name).
    95  		Namespace(pod.Namespace).
    96  		SubResource("exec").
    97  		Param("container", containerName)
    98  	req.VersionedParams(&api.PodExecOptions{
    99  		Container: containerName,
   100  		Command:   p.Command,
   101  		Stdin:     stdin != nil,
   102  		Stdout:    p.Out != nil,
   103  		Stderr:    p.Err != nil,
   104  	}, api.ParameterCodec)
   105  
   106  	return p.Executor.Execute("POST", req.URL(), p.Config, stdin, p.Out, p.Err, false)
   107  }