Source:
<code>public Pod deploy(Pod pod) throws InfrastructureException { putLabel(pod, CHE_WORKSPACE_ID_LABEL, workspaceId); ObjectMeta metadata = pod.getMetadata(); PodSpec podSpec = pod.getSpec(); podSpec.setRestartPolicy("Always"); Watch toCloseOnException = null; try { final CompletableFuture<Pod> createFuture = new CompletableFuture<>(); final Watch createWatch = clientFactory .create(workspaceId) .pods() .inNamespace(namespace) .watch(new CreateWatcher(createFuture, workspaceId)); <START> toCloseOnException = createWatch; <END> Deployment deployment = clientFactory .create(workspaceId) .extensions() .deployments() .inNamespace(namespace) .createNew() .withMetadata(metadata) .withNewSpec() .withReplicas(1) .withNewTemplate() .withMetadata(metadata) .withSpec(podSpec) .endTemplate() .endSpec() .done(); return createFuture.get(POD_CREATION_TIMEOUT_MIN, TimeUnit.MINUTES); } catch (KubernetesClientException e) { throw new KubernetesInfrastructureException(e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new InfrastructureException( "Interrupted while waiting for Pod creation. " + e.getMessage()); } catch (ExecutionException e) { throw new InfrastructureException( "Error occured while waiting for Pod creation. " + e.getMessage()); } catch (TimeoutException e) { throw new InfrastructureException("Pod creation timeout exceeded " + e.getMessage()); } finally { if (toCloseOnException != null) { toCloseOnException.close(); } } }</code><technical_language>move createWatch declaration try block additional variable close in finally block</technical_language>

Target:
public Pod deploy(Pod pod) throws InfrastructureException { putLabel(pod, CHE_WORKSPACE_ID_LABEL, workspaceId); ObjectMeta metadata = pod.getMetadata(); String originalName = metadata.getName(); putLabel(pod, CHE_DEPLOYMENT_NAME_LABEL, originalName); PodSpec podSpec = pod.getSpec(); podSpec.setRestartPolicy("Always"); final CompletableFuture<Pod> createFuture = new CompletableFuture<>(); final Watch createWatch = clientFactory .create(workspaceId) .pods() .inNamespace(namespace) .watch(new CreateWatcher(createFuture, workspaceId, originalName)); try { clientFactory .create(workspaceId) .extensions() .deployments() .inNamespace(namespace) .createNew() .withMetadata(metadata) .withNewSpec() .withReplicas(1) .withNewTemplate() .withMetadata(metadata) .withSpec(podSpec) .endTemplate() .endSpec() .done(); return createFuture.get(POD_CREATION_TIMEOUT_MIN, TimeUnit.MINUTES); } catch (KubernetesClientException e) { throw new KubernetesInfrastructureException(e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new InfrastructureException( String.format( "Interrupted while waiting for Pod creation. -id: %s -message: %s", metadata.getName(), e.getMessage())); } catch (ExecutionException e) { throw new InfrastructureException( String.format( "Error occured while waiting for Pod creation. -id: %s -message: %s", metadata.getName(), e.getCause().getMessage())); } catch (TimeoutException e) { throw new InfrastructureException( String.format( "Pod creation timeout exceeded. -id: %s -message: %s", metadata.getName(), e.getMessage())); } finally { createWatch.close(); } }