Ejemplo n.º 1
0
    def _run_main(self, parsed_args, parsed_globals):
        client = EKSClient(self._session, parsed_args.name,
                           parsed_args.role_arn, parsed_globals)
        new_cluster_dict = client.get_cluster_entry()
        new_user_dict = client.get_user_entry()

        config_selector = KubeconfigSelector(os.environ.get("KUBECONFIG", ""),
                                             parsed_args.kubeconfig)
        config = config_selector.choose_kubeconfig(new_cluster_dict["name"])
        updating_existing = config.has_cluster(new_cluster_dict["name"])
        appender = KubeconfigAppender()
        new_context_dict = appender.insert_cluster_user_pair(
            config, new_cluster_dict, new_user_dict, parsed_args.alias)

        if parsed_args.dry_run:
            uni_print(config.dump_content())
        else:
            writer = KubeconfigWriter()
            writer.write_kubeconfig(config)

            if updating_existing:
                uni_print("Updated context {0} in {1}\n".format(
                    new_context_dict["name"], config.path))
            else:
                uni_print("Added new context {0} to {1}\n".format(
                    new_context_dict["name"], config.path))

            if parsed_args.verbose:
                self._display_entries(
                    [new_context_dict, new_user_dict, new_cluster_dict])
Ejemplo n.º 2
0
class TestKubeconfigWriter(unittest.TestCase):
    def setUp(self):
        self._writer = KubeconfigWriter()

    def test_write_order(self):
        content = OrderedDict([
            ("current-context", "context"),
            ("apiVersion", "v1")
        ])
        file_to_write = tempfile.NamedTemporaryFile(mode='w').name
        self.addCleanup(os.remove, file_to_write)

        config = Kubeconfig(file_to_write, content)
        self._writer.write_kubeconfig(config)

        with open(file_to_write, 'r') as stream:
            self.assertMultiLineEqual(stream.read(),
                                      "current-context: context\n"
                                      "apiVersion: v1\n")
    def test_write_makedirs(self):
        content = OrderedDict([
            ("current-context", "context"),
            ("apiVersion", "v1")
        ])
        containing_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, containing_dir)
        config_path = os.path.join(containing_dir,
                                   "dir1",
                                   "dir2",
                                   "dir3")

        config = Kubeconfig(config_path, content)
        self._writer.write_kubeconfig(config)

        with open(config_path, 'r') as stream:
            self.assertMultiLineEqual(stream.read(),
                                      "current-context: context\n"
                                      "apiVersion: v1\n")

    def test_write_directory(self):
        content = OrderedDict([
            ("current-context", "context"),
            ("apiVersion", "v1")
        ])
        containing_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, containing_dir)

        config = Kubeconfig(containing_dir, content)
        self.assertRaises(KubeconfigInaccessableError,
                          self._writer.write_kubeconfig,
                          config)
Ejemplo n.º 3
0
 def test_not_world_readable(self):
     tmpdir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, tmpdir)
     config_path = os.path.join(tmpdir, "config")
     config = Kubeconfig(config_path, None)
     KubeconfigWriter().write_kubeconfig(config)
     stat = os.stat(config_path)
     self.assertEqual(stat.st_mode & 0o777, 0o600)
Ejemplo n.º 4
0
    def _run_main(self, parsed_args, parsed_globals):
        client = EKSClient(self._session,
                           parsed_args.name,
                           parsed_args.role_arn,
                           parsed_globals)
        new_cluster_dict = client.get_cluster_entry()
        new_user_dict = client.get_user_entry()

        config_selector = KubeconfigSelector(
            os.environ.get("KUBECONFIG", ""),
            parsed_args.kubeconfig
        )
        config = config_selector.choose_kubeconfig(
            new_cluster_dict["name"]
        )
        updating_existing = config.has_cluster(new_cluster_dict["name"])
        appender = KubeconfigAppender()
        new_context_dict = appender.insert_cluster_user_pair(config,
                                                             new_cluster_dict,
                                                             new_user_dict,
                                                             parsed_args.alias)

        if parsed_args.dry_run:
            uni_print(config.dump_content())
        else:
            writer = KubeconfigWriter()
            writer.write_kubeconfig(config)

            if updating_existing:
                uni_print("Updated context {0} in {1}\n".format(
                    new_context_dict["name"], config.path
                ))
            else:
                uni_print("Added new context {0} to {1}\n".format(
                    new_context_dict["name"], config.path
                ))

            if parsed_args.verbose:
                self._display_entries([
                    new_context_dict,
                    new_user_dict,
                    new_cluster_dict
                ])

        warn_of_missing_dependencies()
Ejemplo n.º 5
0
 def test_truncates(self):
     tmpdir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, tmpdir)
     config_path = os.path.join(tmpdir, "config")
     with open(config_path, "w+") as f:
         f.write("#" * 100)
     KubeconfigWriter().write_kubeconfig(Kubeconfig(config_path, {}))
     empty_stat = os.stat(config_path)
     self.assertLessEqual(empty_stat.st_size, 4, "file should be '{}[newline]', 3/4 bytes long ")
Ejemplo n.º 6
0
class TestKubeconfigWriter(unittest.TestCase):
    def setUp(self):
        self._writer = KubeconfigWriter()

    def test_write_order(self):
        content = OrderedDict([("current-context", "context"),
                               ("apiVersion", "v1")])
        file_to_write = tempfile.NamedTemporaryFile(mode='w').name
        self.addCleanup(os.remove, file_to_write)

        config = Kubeconfig(file_to_write, content)
        self._writer.write_kubeconfig(config)

        with open(file_to_write, 'r') as stream:
            self.assertMultiLineEqual(
                stream.read(), "current-context: context\n"
                "apiVersion: v1\n")

    def test_write_makedirs(self):
        content = OrderedDict([("current-context", "context"),
                               ("apiVersion", "v1")])
        containing_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, containing_dir)
        config_path = os.path.join(containing_dir, "dir1", "dir2", "dir3")

        config = Kubeconfig(config_path, content)
        self._writer.write_kubeconfig(config)

        with open(config_path, 'r') as stream:
            self.assertMultiLineEqual(
                stream.read(), "current-context: context\n"
                "apiVersion: v1\n")

    def test_write_directory(self):
        content = OrderedDict([("current-context", "context"),
                               ("apiVersion", "v1")])
        containing_dir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, containing_dir)

        config = Kubeconfig(containing_dir, content)
        self.assertRaises(KubeconfigInaccessableError,
                          self._writer.write_kubeconfig, config)
Ejemplo n.º 7
0
 def setUp(self):
     self._writer = KubeconfigWriter()
Ejemplo n.º 8
0
 def setUp(self):
     self._writer = KubeconfigWriter()