如果删除失败,Kubernetes客户端删除返回true

时间:2018-03-08 15:30:29

标签: java spring kubernetes fabric8

我们正在使用kubernetes客户端测试我们的代码。

我们遇到了一些有趣的行为,如果删除,例如配置图:

  • GET返回200DELETE返回200delete()返回true
  • GET返回200DELETE返回404delete()返回true
  • GET返回404DELETE根本没有被调用:delete()返回false
  • GET返回200DELETE返回400delete()抛出KubernetesClientException
  • GET返回400delete()抛出KubernetesClientException

所以我们想知道,如果真的一致,200/404会返回true404/nc会返回false。两者都应返回true或两者都应返回false

所以我们想知道,这可能是一个错误吗?

根据io.fabric8.kubernetes.client.dsl.base.BaseOperation.delete(),如果false

,则返回404
  try {
    deleteList();
    return true;
  } catch (KubernetesClientException e) {
    if (e.getCode() != 404) {
      throw e;
    }
    return false;
  }

以下是测试示例:

// GET return 200
// DELETE return 200
// kubernetesClient.delete() return TRUE
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(200, configMapList).once();
server.expect().delete().withPath(format("/api/v1/namespaces/{}/configmaps/{}", namespace, configMapName)).andReturn(200, StringUtils.EMPTY).once();
actual = kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
assertTrue(actual)

// GET return 200
// DELETE return 404
// kubernetesClient.delete() return TRUE
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(200, configMapList).once();
server.expect().delete().withPath(format("/api/v1/namespaces/{}/configmaps/{}", namespace, configMapName)).andReturn(404, StringUtils.EMPTY).once();
actual = kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
assertTrue(actual)

// GET return 404
// kubernetesClient.delete() return FALSE
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(404, StringUtils.EMPTY).once();
actual = kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
assertFalse(actual)

// GET return 200
// DELETE return 400
// kubernetesClient.delete() throws KubernetesClientException
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(200, configMapList).once();
server.expect().delete().withPath(format("/api/v1/namespaces/{}/configmaps/{}", namespace, configMapName)).andReturn(400, StringUtils.EMPTY).once();
try {
    kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
    fail();
} catch (io.fabric8.kubernetes.client.KubernetesClientException e) {
    assertTrue(true);
}

// GET return 400
// kubernetesClient.delete() throws KubernetesClientException
server.expect().get().withPath(format("/api/v1/namespaces/{}/configmaps?labelSelector={}%3D{}", namespace, name, value)).andReturn(400, StringUtils.EMPTY).once();
try {
    kubernetesClient.configMaps().inNamespace(namespace).withLabelSelector(labelSelector).delete();
    fail();
} catch (io.fabric8.kubernetes.client.KubernetesClientException e) {
    assertTrue(true);
}

0 个答案:

没有答案