示例#1
0
    def __init__(self, context):
        super(KubernetesController, self).__init__(context)
        self._namespace = 'default'
        self._labels = {"application": "patroni"}
        self._label_selector = ','.join('{0}={1}'.format(k, v)
                                        for k, v in self._labels.items())
        os.environ['PATRONI_KUBERNETES_LABELS'] = json.dumps(self._labels)
        os.environ['PATRONI_KUBERNETES_USE_ENDPOINTS'] = 'true'

        from patroni.dcs.kubernetes import k8s_client, k8s_config
        k8s_config.load_kube_config(context='local')
        self._client = k8s_client
        self._api = self._client.CoreV1Api()
示例#2
0
    def test_load_kube_config(self):
        config = {
            "current-context":
            "local",
            "contexts": [{
                "name": "local",
                "context": {
                    "user": "******",
                    "cluster": "local"
                }
            }],
            "clusters": [{
                "name": "local",
                "cluster": {
                    "server": "https://a:1/",
                    "certificate-authority": "a"
                }
            }],
            "users": [{
                "name": "local",
                "user": {
                    "username": "******",
                    "password": "******",
                    "client-certificate": "c"
                }
            }]
        }
        with patch.object(builtins, 'open',
                          mock_open(read_data=json.dumps(config))):
            k8s_config.load_kube_config()
            self.assertEqual(k8s_config.server, 'https://a:1')
            self.assertEqual(
                k8s_config.pool_config, {
                    'ca_certs': 'a',
                    'cert_file': 'c',
                    'cert_reqs': 'CERT_REQUIRED',
                    'maxsize': 10,
                    'num_pools': 10
                })

        config["users"][0]["user"]["token"] = "token"
        with patch.object(builtins, 'open',
                          mock_open(read_data=json.dumps(config))):
            k8s_config.load_kube_config()
            self.assertEqual(k8s_config.headers.get('authorization'),
                             'Bearer token')