def create(self, validated_data): data = super(AppCreatePreviewDiffWithClusterSLZ, self).create(validated_data) namespace_info = self.get_ns_info_by_id( validated_data["namespace_info"]) check_cluster_perm(user=self.context["request"].user, project_id=namespace_info["project_id"], cluster_id=namespace_info["cluster_id"], request=self.context["request"]) with save_to_temporary_dir(data["content"]) as tempdir: with make_kubectl_client( project_id=self.project_id, cluster_id=namespace_info["cluster_id"], access_token=self.access_token) as (client, err): if err: raise serializers.ValidationError( "make kubectl client failed, %s", err) args = [ "kubediff", "--kubeconfig", client.kubeconfig, "--json", "--no-error-on-diff", tempdir ] difference = subprocess.check_output(args) difference = json.loads(difference) data.update(difference=difference) return data
def retrieve(self, request, app_id, *args, **kwargs): app = App.objects.get(id=self.app_id) check_cluster_perm(user=request.user, project_id=app.project_id, cluster_id=app.cluster_id, request=request) content = app.release.content # resources = parser.parse(content, app.namespace) with bcs_utils_client.make_kubectl_client( access_token=self.access_token, project_id=app.project_id, cluster_id=app.cluster_id ) as (client, err): if err: raise APIException(str(err)) state = collect_resource_state(kube_client=client, namespace=app.namespace, content=content) return Response(state)
def make_kubectl_client(self): with make_kubectl_client(project_id=self.app.project_id, cluster_id=self.app.cluster_id, access_token=self.access_token) as (client, err): yield client, err
def make_kubectl_client(self): with make_kubectl_client( project_id=self.release_data.project_id, cluster_id=self.release_data.namespace_info['cluster_id'], access_token=self.access_token) as (client, err): yield client, err