Example #1
0
 def __init__(self, connection: KubernetesServiceConnection):
     config = Configuration()
     try:
         if connection.local:
             load_incluster_config(client_configuration=config)
         else:
             load_kube_config_from_dict(connection.kubeconfig,
                                        client_configuration=config)
         super().__init__(config)
     except ConfigException as exc:
         raise ServiceConnectionInvalid from exc
Example #2
0
 def client(self) -> ApiClient:
     """Get Kubernetes client configured from kubeconfig"""
     config = Configuration()
     try:
         if self.local:
             load_incluster_config(client_configuration=config)
         else:
             load_kube_config_from_dict(self.kubeconfig,
                                        client_configuration=config)
         return ApiClient(config)
     except ConfigException as exc:
         raise ServiceConnectionInvalid from exc
 def validate_kubeconfig(self, kubeconfig):
     """Validate kubeconfig by attempting to load it"""
     if kubeconfig == {}:
         if not self.validated_data["local"]:
             raise serializers.ValidationError(
                 _("You can only use an empty kubeconfig when connecting to a local cluster."
                   ))
         # Empty kubeconfig is valid
         return kubeconfig
     config = Configuration()
     try:
         load_kube_config_from_dict(kubeconfig, client_configuration=config)
     except ConfigException:
         raise serializers.ValidationError(_("Invalid kubeconfig"))
     return kubeconfig