def test_parse_kubeconf_from_file_failure(self): _, tmpfilename = tempfile.mkstemp() f = open(tmpfilename, 'w') f.write("{ 'hello': 'world'}") f.close() with pytest.raises(KeyError): KubeConfig.parse_kubeconf(tmpfilename)
def test_from_file(self): """ Test parsing a hello world JSON example and returning back the respective anymarkup content """ _, tmpfilename = tempfile.mkstemp() f = open(tmpfilename, 'w') f.write("{ 'hello': 'world'}") f.close() KubeConfig.from_file(tmpfilename)
def init(self): self.oc_artifacts = {} logger.debug("Given config: %s", self.config) if self.config.get("namespace"): self.namespace = self.config.get("namespace") logger.info("Using namespace %s", self.namespace) self._process_artifacts() if self.dryrun: return ''' Config_file: If a config_file has been provided, use the configuration from the file and load the associated generated file. If a config_file exists (--provider-config) use that. Params: If any provider specific parameters have been provided, load the configuration through the answers.conf file .kube/config: If no config file or params are provided by user then try to find and use a config file at the default location. no config at all: If no .kube/config file can be found then try to connect to the default unauthenticated http://localhost:8080/api end-point. ''' default_config_loc = os.path.join(Utils.getRoot(), Utils.getUserHome().strip('/'), '.kube/config') if self.config_file: logger.debug("Provider configuration provided") self.api = Client(KubeConfig.from_file(self.config_file), "openshift") elif self._check_required_params(): logger.debug("Generating .kube/config from given parameters") self.api = Client(self._from_required_params(), "openshift") elif os.path.isfile(default_config_loc): logger.debug( ".kube/config exists, using default configuration file") self.api = Client(KubeConfig.from_file(default_config_loc), "openshift") else: self.config["provider-api"] = OC_DEFAULT_API self.api = Client(self._from_required_params(), "openshift") self._check_namespaces()
def init(self): self.k8s_artifacts = {} logger.debug("Given config: %s", self.config) if self.config.get("namespace"): self.namespace = self.config.get("namespace") logger.info("Using namespace %s", self.namespace) self._process_artifacts() if self.dryrun: return ''' Config_file: If a config_file has been provided, use the configuration from the file and load the associated generated file. If a config_file exists (--provider-config) use that. Params: If any provider specific parameters have been provided, load the configuration through the answers.conf file .kube/config: If no config file or params are provided by user then try to find and use a config file at the default location. no config at all: If no .kube/config file can be found then try to connect to the default unauthenticated http://localhost:8080/api end-point. ''' default_config_loc = os.path.join( Utils.getRoot(), Utils.getUserHome().strip('/'), '.kube/config') if self.config_file: logger.debug("Provider configuration provided") self.api = Client(KubeConfig.from_file(self.config_file), "kubernetes") elif self._check_required_params(): logger.debug("Generating .kube/config from given parameters") self.api = Client(self._from_required_params(), "kubernetes") elif os.path.isfile(default_config_loc): logger.debug(".kube/config exists, using default configuration file") self.api = Client(KubeConfig.from_file(default_config_loc), "kubernetes") else: self.config["provider-api"] = K8S_DEFAULT_API self.api = Client(self._from_required_params(), "kubernetes") # Check if the namespace that the app is being deployed to is available self._check_namespaces()
def _from_required_params(self): ''' Create a default configuration from passed environment parameters. ''' self._check_required_params(exception=True) paramdict = self._build_param_dict() logger.debug("Building from required params") # Generate the configuration from the paramters config = KubeConfig().from_params(api=paramdict[PROVIDER_API_KEY], auth=paramdict[PROVIDER_AUTH_KEY], ca=paramdict[PROVIDER_CA_KEY], verify=paramdict[PROVIDER_TLS_VERIFY_KEY]) logger.debug("Passed configuration for .kube/config %s" % config) return config
def test_parse_kubeconf_data_insecure(self): """ Test parsing kubeconf data with current context containing cluster, user, namespace info and skipping tls verification """ kubecfg_data = { 'current-context': 'context2', 'contexts': [ { 'name': 'context1', }, { 'name': 'context2', 'context': { 'cluster': 'cluster1', 'user': '******', 'namespace': 'namespace1' } } ], 'clusters': [ { 'name': 'cluster1', 'cluster': { 'insecure-skip-tls-verify': 'true', 'server': 'server1' } } ], 'users': [ { 'name': 'user1', 'user': { 'token': 'token1' } } ] } self.assertEqual(KubeConfig.parse_kubeconf_data(kubecfg_data), {'provider-api': 'server1', 'provider-auth': 'token1', 'namespace': 'namespace1', 'provider-tlsverify': False, 'provider-cafile': None})
def test_parse_kubeconf_data_cafile(self): """ Test parsing kubeconf data with current context containing cluster, user, namespace info and certificate-authority """ kubecfg_data = { 'current-context': 'context2', 'contexts': [ { 'name': 'context1', }, { 'name': 'context2', 'context': { 'cluster': 'cluster1', 'user': '******', 'namespace': 'namespace1' } } ], 'clusters': [ { 'name': 'cluster1', 'cluster': { 'certificate-authority': '/foo/bar', 'server': 'server1' } } ], 'users': [ { 'name': 'user1', 'user': { 'token': 'token1' } } ] } self.assertEqual(KubeConfig.parse_kubeconf_data(kubecfg_data), {'provider-api': 'server1', 'provider-auth': 'token1', 'namespace': 'namespace1', 'provider-tlsverify': True, 'provider-cafile': '/foo/bar'})
def test_parse_kubeconf_data_cafile(self): """ Test parsing kubeconf data with current context containing cluster, user, namespace info and certificate-authority """ kubecfg_data = { 'current-context': 'context2', 'contexts': [{ 'name': 'context1', }, { 'name': 'context2', 'context': { 'cluster': 'cluster1', 'user': '******', 'namespace': 'namespace1' } }], 'clusters': [{ 'name': 'cluster1', 'cluster': { 'certificate-authority': '/foo/bar', 'server': 'server1' } }], 'users': [{ 'name': 'user1', 'user': { 'token': 'token1' } }] } self.assertEqual( KubeConfig.parse_kubeconf_data(kubecfg_data), { 'provider-api': 'server1', 'provider-auth': 'token1', 'namespace': 'namespace1', 'provider-tlsverify': True, 'provider-cafile': '/foo/bar' })
def test_parse_kubeconf_data_insecure(self): """ Test parsing kubeconf data with current context containing cluster, user, namespace info and skipping tls verification """ kubecfg_data = { 'current-context': 'context2', 'contexts': [{ 'name': 'context1', }, { 'name': 'context2', 'context': { 'cluster': 'cluster1', 'user': '******', 'namespace': 'namespace1' } }], 'clusters': [{ 'name': 'cluster1', 'cluster': { 'insecure-skip-tls-verify': 'true', 'server': 'server1' } }], 'users': [{ 'name': 'user1', 'user': { 'token': 'token1' } }] } self.assertEqual( KubeConfig.parse_kubeconf_data(kubecfg_data), { 'provider-api': 'server1', 'provider-auth': 'token1', 'namespace': 'namespace1', 'provider-tlsverify': False, 'provider-cafile': None })
def _set_config_values(self): """ Reads providerapi, namespace and accesstoken from answers.conf and corresponding values from providerconfig (if set). Use one that is set, if both are set and have conflicting values raise exception. Raises: ProviderFailedException: values in providerconfig and answers.conf are in conflict """ # First things first, if we are running inside of an openshift pod via # `oc new-app` then get the config from the environment (files/env vars) # NOTE: pick up provider_tls_verify from answers if exists if Utils.running_on_openshift(): self.providerapi = Utils.get_openshift_api_endpoint_from_env() self.namespace = os.environ['POD_NAMESPACE'] self.access_token = os.environ['TOKEN_ENV_VAR'] self.provider_ca = OPENSHIFT_POD_CA_FILE self.provider_tls_verify = \ self.config.get(PROVIDER_TLS_VERIFY_KEY, True) return # No need to process other information # initialize result to default values result = {PROVIDER_API_KEY: self.providerapi, PROVIDER_AUTH_KEY: self.access_token, NAMESPACE_KEY: self.namespace, PROVIDER_TLS_VERIFY_KEY: self.provider_tls_verify, PROVIDER_CA_KEY: self.provider_ca} # create keys in dicts and initialize values to None answers = dict.fromkeys(result) providerconfig = dict.fromkeys(result) # get values from answers.conf for k in result.keys(): answers[k] = self.config.get(k) # get values from providerconfig if self.config_file: providerconfig = KubeConfig.parse_kubeconf(self.config_file) # decide between values from answers.conf and providerconfig # if only one is set use that, report if they are in conflict for k in result.keys(): if answers[k] is not None and providerconfig[k] is None: result[k] = answers[k] elif answers[k] is None and providerconfig[k] is not None: result[k] = providerconfig[k] elif answers[k] is not None and providerconfig[k] is not None: if answers[k] == providerconfig[k]: result[k] = answers[k] else: msg = "There are conflicting values in %s (%s) and %s (%s)"\ % (self.config_file, providerconfig[k], ANSWERS_FILE, answers[k]) logger.error(msg) raise ProviderFailedException(msg) logger.debug("config values: %s" % result) # this items are required, they have to be not None for k in [PROVIDER_API_KEY, PROVIDER_AUTH_KEY, NAMESPACE_KEY]: if result[k] is None: msg = "You need to set %s in %s" % (k, ANSWERS_FILE) logger.error(msg) raise ProviderFailedException(msg) # set config values self.providerapi = result[PROVIDER_API_KEY] self.access_token = result[PROVIDER_AUTH_KEY] self.namespace = result[NAMESPACE_KEY] self.provider_tls_verify = result[PROVIDER_TLS_VERIFY_KEY] if result[PROVIDER_CA_KEY]: # if we are in container translate path to path on host self.provider_ca = Utils.get_real_abspath(result[PROVIDER_CA_KEY]) else: self.provider_ca = None
def test_parse_kubeconf_from_file(self): example_kubeconfig = os.path.dirname(__file__) + '/external/example_kubeconfig' KubeConfig.parse_kubeconf(example_kubeconfig)
def test_from_params(self): KubeConfig.from_params("foo", "bar", "foo", "bar")
def test_parse_kubeconf_from_file(self): example_kubeconfig = os.path.dirname( __file__) + '/external/example_kubeconfig' KubeConfig.parse_kubeconf(example_kubeconfig)