Beispiel #1
0
class Fetch(Operation):
    def __init__(self, qbertclient=None, args=None):
        self.cluster_name = args.name if args.name else None
        self.cluster_uuid = args.uuid if args.uuid else None
        self.use_creds = args.use_creds

        kcfg_path = args.kubeconfig if args.kubeconfig else ""
        self.kubeconfig = Kubeconfig(kcfg_path=kcfg_path)

        if PYTHON2:
            super(Fetch, self).__init__(qbertclient)
        else:
            super().__init__(qbertclient)
        pass

    def run(self):
        """ Using the qbert API, download a kubeconfig file for the specified cluster

        Returns:
            qbertconfig.Kubeconfig object
        """

        LOG.debug("Cluster: '%s' (%s)", self.cluster_name, self.cluster_uuid)

        cluster = self.connection.find_cluster(self.cluster_uuid,
                                               self.cluster_name)
        credentials = self.connection.get_credentials()
        cloud_fqdn = self.connection.get_cloud_fqdn()

        bearer_token = ""
        if not self.use_creds:
            bearer_token = self.connection.get_keystone_token()
        else:
            credential_string = json.dumps(credentials)
            if PYTHON2:
                bearer_token = base64.b64encode(bytes(credential_string))
            else:
                bearer_token = base64.b64encode(
                    bytes(credential_string, 'utf-8'))
            # base64.b4encode gives us a bytes, convert back to string
            bearer_token = bearer_token.decode('utf-8')

        raw_kubeconfig = self.connection.get_kubeconfig(cluster)
        # Replace the token placeholder with a real "token"
        raw_kubeconfig['users'][0]['user']['token'] = bearer_token

        # Organize the kubeconfig for merging
        new_kubeconfig = Kubeconfig(kcfg=raw_kubeconfig)
        new_kubeconfig.organize_kubeconfig(cluster, cloud_fqdn,
                                           credentials['username'])

        # Merge and return the kubeconfigs
        self.kubeconfig.merge_kubeconfigs(new_kubeconfig)
        self.kubeconfig.save()
        print("Successfully fetched kubeconfig for cluster %s (%s)" %
              (cluster['name'], cluster['uuid']))
        return self.kubeconfig
Beispiel #2
0
    def test_adding_duplicate(self):
        """
        Adding a kubeconfig that exists should not change the object
        """
        initial_kubeconfig = self.kubeconfig
        incoming_kubeconfig = Kubeconfig(kcfg=samples.BASE_TEST_KUBECONFIG)

        initial_kubeconfig.merge_kubeconfigs(incoming_kubeconfig)
        self.assertEqual(initial_kubeconfig,
                         Kubeconfig(kcfg=samples.BASE_TEST_KUBECONFIG))
Beispiel #3
0
    def test_adding_partial_match(self):
        """
        Adding a kubeconfig with some matching entities should append new ones and update others
        """
        initial_kubeconfig = self.kubeconfig
        incoming_kubeconfig = Kubeconfig(
            kcfg=samples.PARTIAL_UNIQUE_KUBECONFIG)

        initial_kubeconfig.merge_kubeconfigs(incoming_kubeconfig)
        self.assertEqual(initial_kubeconfig,
                         Kubeconfig(kcfg=samples.BASE_TEST_KUBECONFIG))
Beispiel #4
0
    def __init__(self, qbertclient=None, args=None):
        self.cluster_name = args.name if args.name else None
        self.cluster_uuid = args.uuid if args.uuid else None
        self.use_creds = args.use_creds

        kcfg_path = args.kubeconfig if args.kubeconfig else ""
        self.kubeconfig = Kubeconfig(kcfg_path=kcfg_path)

        if PYTHON2:
            super(Fetch, self).__init__(qbertclient)
        else:
            super().__init__(qbertclient)
        pass
Beispiel #5
0
    def test_fetch_save(self, mock_qbertclient_init):
        """
        Fetching a kubeconfig should save it to the filesystem and return the Kubeconfig object
        """
        # Mock the QbertClient's init to do nothing
        mock_qbertclient_init.return_value = None

        # Mock the functions we use from QbertClient
        qc = QbertClient()
        qc.find_cluster = MagicMock(return_value=qbertclient_samples.FIND_CLUSTER_DEFAULT)
        qc.get_credentials = MagicMock(return_value=qbertclient_samples.GET_CREDENTIALS_DEFAULT)
        qc.get_cloud_fqdn = MagicMock(return_value=qbertclient_samples.GET_CLOUD_FQDN_DEFAULT)
        qc.get_keystone_token = MagicMock(return_value=qbertclient_samples.GET_KEYSTONE_TOKEN_DEFAULT)
        qc.get_kubeconfig = MagicMock(return_value=qbertclient_samples.GET_KUBECONFIG_DEFAULT)

        # pretend we're parsing args via argparse
        args = samples.DEFAULT_PARSED_ARGS

        fetcher = Fetch(qbertclient=qc, args=args)
        fetched_kubeconfig = fetcher.run()
        self.assertIsInstance(fetched_kubeconfig, Kubeconfig)

        # check the filesystem for the kubeconfig file
        fromfile_kubeconfig = Kubeconfig(kcfg_path=args.kubeconfig)
        self.assertEquals(fetched_kubeconfig, fromfile_kubeconfig)
Beispiel #6
0
    def setUp(self):
        # create temporary file to use for kubeconfig
        with tempfile.NamedTemporaryFile(prefix='qbertconfig',
                                         delete=False) as kcfg_f:
            self.kubeconfig_path = kcfg_f.name

        # load one profile into here
        # use vars as if we were a user at the command line
        self.kubeconfig = Kubeconfig(kcfg_path=self.kubeconfig_path,
                                     kcfg=samples.BASE_TEST_KUBECONFIG)