Пример #1
0
    def __init__(self, filename=None, data=None, kubeconfig_path=None):
        """
        Creates a new instance of BytesOrFile.

        :Parameters:
           - `filename`: A full path to a file
           - `data`: base64 encoded bytes
        """
        self._path = None
        self._bytes = None
        if filename is not None and data is not None:
            raise TypeError(
                "filename or data kwarg must be specified, not both")
        elif filename is not None:

            path = Path(filename)
            # If relative path is given, should be made absolute with respect to the directory of the kube config
            # https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#file-references
            if not path.is_absolute():
                if kubeconfig_path:
                    path = kubeconfig_path.parent.joinpath(path)
                else:
                    raise exceptions.PyKubeError(
                        "{} passed as relative path, but cannot determine location of kube config"
                        .format(filename))

            if not path.is_file():
                raise exceptions.PyKubeError(
                    "'{}' file does not exist".format(filename))
            self._path = path
        elif data is not None:
            self._bytes = base64.b64decode(data)
        else:
            raise TypeError("filename or data kwarg must be specified")
Пример #2
0
 def user(self):
     """
     Returns the current user by exposing as a read-only property.
     """
     if self.current_context is None:
         raise exceptions.PyKubeError("current context not set; call set_current_context")
     return self.users.get(self.contexts[self.current_context].get("user", ""), {})
Пример #3
0
 def cluster(self):
     """
     Returns the current selected cluster by exposing as a
     read-only property.
     """
     if self.current_context is None:
         raise exceptions.PyKubeError("current context not set; call set_current_context")
     return self.clusters[self.contexts[self.current_context]["cluster"]]
Пример #4
0
    def from_file(cls, filename):
        """
        Creates an instance of the KubeConfig class from a kubeconfig file.

        :Parameters:
           - `filename`: The full path to the configuration file
        """
        filename = os.path.expanduser(filename)
        if not os.path.isfile(filename):
            raise exceptions.PyKubeError("Configuration file {} not found".format(filename))
        with open(filename) as f:
            doc = yaml.safe_load(f.read())
        self = cls(doc)
        self.filename = filename
        return self
Пример #5
0
    def from_file(cls, filename=None, **kwargs):
        """
        Creates an instance of the KubeConfig class from a kubeconfig file.

        :param filename: The full path to the configuration file. Defaults to ~/.kube/config
        """
        if not filename:
            filename = os.getenv('KUBECONFIG', '~/.kube/config')
        filename = os.path.expanduser(filename)
        if not os.path.isfile(filename):
            raise exceptions.PyKubeError("Configuration file {} not found".format(filename))
        with open(filename) as f:
            doc = yaml.safe_load(f.read())
        self = cls(doc, **kwargs)
        self.filename = filename
        return self
Пример #6
0
    def __init__(self, filename=None, data=None):
        """
        Creates a new instance of BytesOrFile.

        :Parameters:
           - `filename`: A full path to a file
           - `data`: base64 encoded bytes
        """
        self._filename = None
        self._bytes = None
        if filename is not None and data is not None:
            raise TypeError("filename or data kwarg must be specified, not both")
        elif filename is not None:
            if not os.path.isfile(filename):
                raise exceptions.PyKubeError("'{}' file does not exist".format(filename))
            self._filename = filename
        elif data is not None:
            self._bytes = base64.b64decode(data)
        else:
            raise TypeError("filename or data kwarg must be specified")
Пример #7
0
 def current_context(self):
     if self._current_context is None:
         raise exceptions.PyKubeError(
             "current context not set; call set_current_context")
     return self._current_context