|
| 1 | +# Copyright 2016 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import json |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | + |
| 19 | +from .config_exception import ConfigException |
| 20 | + |
| 21 | + |
| 22 | +class ExecProvider(object): |
| 23 | + def __init__(self, exec_config): |
| 24 | + for key in ['command', 'apiVersion']: |
| 25 | + if key not in exec_config: |
| 26 | + raise ConfigException( |
| 27 | + 'exec: malformed request. missing key \'%s\'' % key) |
| 28 | + self.api_version = exec_config['apiVersion'] |
| 29 | + self.args = [exec_config['command']] |
| 30 | + if 'args' in exec_config: |
| 31 | + self.args.extend(exec_config['args']) |
| 32 | + self.env = os.environ.copy() |
| 33 | + if 'env' in exec_config: |
| 34 | + additional_vars = {} |
| 35 | + for item in exec_config['env']: |
| 36 | + name = item['name'] |
| 37 | + value = item['value'] |
| 38 | + additional_vars[name] = value |
| 39 | + self.env.update(additional_vars) |
| 40 | + |
| 41 | + def run(self, previous_response=None): |
| 42 | + kubernetes_exec_info = { |
| 43 | + 'apiVersion': self.api_version, |
| 44 | + 'kind': 'ExecCredential', |
| 45 | + 'spec': { |
| 46 | + 'interactive': True |
| 47 | + } |
| 48 | + } |
| 49 | + if previous_response: |
| 50 | + kubernetes_exec_info['spec']['response'] = previous_response |
| 51 | + self.env['KUBERNETES_EXEC_INFO'] = json.dumps(kubernetes_exec_info) |
| 52 | + process = subprocess.Popen( |
| 53 | + self.args, |
| 54 | + stdout=subprocess.PIPE, |
| 55 | + stderr=subprocess.PIPE, |
| 56 | + env=self.env, |
| 57 | + universal_newlines=True) |
| 58 | + (stdout, stderr) = process.communicate() |
| 59 | + exit_code = process.wait() |
| 60 | + if exit_code != 0: |
| 61 | + msg = 'exec: process returned %d' % exit_code |
| 62 | + stderr = stderr.strip() |
| 63 | + if stderr: |
| 64 | + msg += '. %s' % stderr |
| 65 | + raise ConfigException(msg) |
| 66 | + stdout = stdout |
| 67 | + try: |
| 68 | + data = json.loads(stdout) |
| 69 | + except json.decoder.JSONDecodeError as de: |
| 70 | + raise ConfigException( |
| 71 | + 'exec: failed to decode process output: %s' % de) |
| 72 | + if not all(k in data for k in ('apiVersion', 'kind', 'status')): |
| 73 | + raise ConfigException( |
| 74 | + 'exec: malformed response. plugin returned: %s' % stdout) |
| 75 | + if data['apiVersion'] != self.api_version: |
| 76 | + raise ConfigException( |
| 77 | + 'exec: plugin api version %s does not match %s' % |
| 78 | + (data['apiVersion'], self.api_version)) |
| 79 | + return data['status'] |
0 commit comments