• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

coryodaniel / k8s / 807e93631268e5fd52ca29e3e4755088cf11bf27-PR-262

pending completion
807e93631268e5fd52ca29e3e4755088cf11bf27-PR-262

Pull #262

github

mruoss
add possibility to wait for delete
Pull Request #262: add possibility to wait for delete

6 of 6 new or added lines in 1 file covered. (100.0%)

732 of 1009 relevant lines covered (72.55%)

44.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.23
/lib/k8s/client.ex
1
defmodule K8s.Client do
2
  @moduledoc """
3
  Kubernetes API Client.
4

5
  Functions return `K8s.Operation`s that represent kubernetes operations.
6

7
  To run operations pass them to: `run/2`, or `run/3`
8

9
  When specifying kinds the format should either be in the literal kubernetes kind name (eg `"ServiceAccount"`)
10
  or the downcased version seen in kubectl (eg `"serviceaccount"`). A string or atom may be used.
11

12
  ## Examples
13
  ```elixir
14
  "Deployment", "deployment", :Deployment, :deployment
15
  "ServiceAccount", "serviceaccount", :ServiceAccount, :serviceaccount
16
  "HorizontalPodAutoscaler", "horizontalpodautoscaler", :HorizontalPodAutoscaler, :horizontalpodautoscaler
17
  ```
18

19
  `http_opts` to `K8s.Client.Runner` modules are `K8s.Client.HTTPProvider` HTTP options.
20
  """
21

22
  @type path_param :: {:name, String.t()} | {:namespace, binary() | :all}
23
  @type path_params :: [path_param]
24

25
  @mgmt_param_defaults %{
26
    field_manager: "elixir",
27
    force: true
28
  }
29

30
  alias K8s.Client.Runner.StreamTo
31
  alias K8s.Operation
32
  alias K8s.Client.Runner.{Async, Base, Stream, Wait}
33

34
  defdelegate put_conn(operation, conn), to: Operation
35

36
  @doc "alias of `K8s.Client.Runner.Base.run/1`"
37
  defdelegate run(operation), to: Base
38

39
  @doc "alias of `K8s.Client.Runner.Base.run/2`"
40
  defdelegate run(conn, operation), to: Base
41

42
  @doc "alias of `K8s.Client.Runner.Base.run/3`"
43
  defdelegate run(conn, operation, http_opts), to: Base
44
  @doc "alias of `K8s.Client.Runner.Async.run/3`"
45

46
  defdelegate async(conn, operations), to: Async, as: :run
47

48
  @doc "alias of `K8s.Client.Runner.Async.run/3`"
49
  defdelegate async(conn, operations, http_opts), to: Async, as: :run
50

51
  @doc "alias of `K8s.Client.Runner.Async.run/3`"
52
  defdelegate parallel(conn, operations, http_opts), to: Async, as: :run
53

54
  @doc "alias of `K8s.Client.Runner.Wait.run/2`"
55
  defdelegate wait_until(operation, wait_opts), to: Wait, as: :run
56

57
  @doc "alias of `K8s.Client.Runner.Wait.run/3`"
58
  defdelegate wait_until(conn, operation, wait_opts), to: Wait, as: :run
59

60
  @doc "alias of `K8s.Client.Runner.Stream.run/1`"
61
  defdelegate stream(operation), to: Stream, as: :run
62

63
  @doc "alias of `K8s.Client.Runner.Stream.run/2`"
64
  defdelegate stream(conn, operation), to: Stream, as: :run
65

66
  @doc "alias of `K8s.Client.Runner.Stream.run/3`"
67
  defdelegate stream(conn, operation, http_opts), to: Stream, as: :run
68

69
  @doc "alias of `K8s.Client.Runner.StreamTo.run/2`"
70
  defdelegate stream_to(operation, stream_to), to: StreamTo, as: :run
71

72
  @doc "alias of `K8s.Client.Runner.StreamTo.run/3`"
73
  defdelegate stream_to(conn, operation, stream_to), to: StreamTo, as: :run
74

75
  @doc "alias of `K8s.Client.Runner.StreamTo.run/4`"
76
  defdelegate stream_to(conn, operation, http_opts, stream_to), to: StreamTo, as: :run
77

78
  @doc """
79
  Returns a `PATCH` operation to server-side-apply the given resource.
80

81
  [K8s Docs](https://kubernetes.io/docs/reference/using-api/server-side-apply/):
82

83
  ## Examples
84
    Apply a deployment with management parameteres
85
      iex> deployment = K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml")
86
      ...> K8s.Client.apply(deployment, field_manager: "my-operator", force: true)
87
      %K8s.Operation{
88
        method: :patch,
89
        verb: :patch,
90
        api_version: "apps/v1",
91
        name: "Deployment",
92
        path_params: [namespace: "test", name: "nginx"],
93
        data: K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml"),
94
        query_params: [fieldManager: "my-operator", force: true],
95
        header_params: ["Content-Type": "application/apply-patch+yaml"],
96
      }
97
  """
98
  @spec apply(map(), keyword()) :: Operation.t()
99
  def apply(resource, mgmt_params \\ []) do
1✔
100
    field_manager = Keyword.get(mgmt_params, :field_manager, @mgmt_param_defaults[:field_manager])
2✔
101
    force = Keyword.get(mgmt_params, :force, @mgmt_param_defaults[:force])
2✔
102

103
    Operation.build(:patch, resource,
2✔
104
      field_manager: field_manager,
105
      force: force,
106
      patch_type: :apply
107
    )
108
  end
109

110
  @doc """
111
  Returns a `PATCH` operation to server-side-apply the given subresource given a resource's details and a subresource map.
112

113
  ## Examples
114

115
    Apply a status to a pod:
116
      iex> pod_with_status_subresource = K8s.Resource.from_file!("test/support/manifests/nginx-pod.yaml") |> Map.put("status", %{"message" => "some message"})
117
      ...> K8s.Client.apply("v1", "pods/status", [namespace: "default", name: "nginx"], pod_with_status_subresource, field_manager: "my-operator", force: true)
118
      %K8s.Operation{
119
        method: :patch,
120
        verb: :patch,
121
        api_version: "v1",
122
        name: "pods/status",
123
        path_params: [namespace: "default", name: "nginx"],
124
        data: K8s.Resource.from_file!("test/support/manifests/nginx-pod.yaml") |> Map.put("status", %{"message" => "some message"}),
125
        query_params: [fieldManager: "my-operator", force: true],
126
        header_params: ["Content-Type": "application/apply-patch+yaml"]
127
      }
128
  """
129
  @spec apply(binary, Operation.name_t(), Keyword.t(), map(), keyword()) :: Operation.t()
130
  def apply(
131
        api_version,
132
        kind,
133
        path_params,
134
        subresource,
135
        mgmt_params \\ []
×
136
      ) do
137
    field_manager = Keyword.get(mgmt_params, :field_manager, @mgmt_param_defaults[:field_manager])
1✔
138
    force = Keyword.get(mgmt_params, :force, @mgmt_param_defaults[:force])
1✔
139

140
    Operation.build(:patch, api_version, kind, path_params, subresource,
1✔
141
      field_manager: field_manager,
142
      force: force,
143
      patch_type: :apply
144
    )
145
  end
146

147
  @doc """
148
  Returns a `GET` operation for a resource given a Kubernetes manifest. May be a partial manifest as long as it contains:
149

150
    * apiVersion
151
    * kind
152
    * metadata.name
153
    * metadata.namespace (if applicable)
154

155
  [K8s Docs](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/):
156

157
  > Get will retrieve a specific resource object by name.
158

159
  ## Examples
160
    Getting a pod
161

162
      iex> pod = %{
163
      ...>   "apiVersion" => "v1",
164
      ...>   "kind" => "Pod",
165
      ...>   "metadata" => %{"name" => "nginx-pod", "namespace" => "test"},
166
      ...>   "spec" => %{"containers" => %{"image" => "nginx"}}
167
      ...> }
168
      ...> K8s.Client.get(pod)
169
      %K8s.Operation{
170
        method: :get,
171
        verb: :get,
172
        api_version: "v1",
173
        name: "Pod",
174
        path_params: [namespace: "test", name: "nginx-pod"],
175
      }
176
  """
177
  @spec get(map()) :: Operation.t()
178
  def get(%{} = resource), do: Operation.build(:get, resource)
2✔
179

180
  @doc """
181
  Returns a `GET` operation for a resource by version, kind/resource type, name, and optionally namespace.
182

183
  [K8s Docs](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/):
184

185
  > Get will retrieve a specific resource object by name.
186

187
  ## Examples
188
    Get the nginx deployment in the default namespace:
189
      iex> K8s.Client.get("apps/v1", "Deployment", namespace: "test", name: "nginx")
190
      %K8s.Operation{
191
        method: :get,
192
        verb: :get,
193
        api_version: "apps/v1",
194
        name: "Deployment",
195
        path_params: [namespace: "test", name: "nginx"]
196
      }
197

198
    Get the nginx deployment in the default namespace by passing the kind as atom.
199
      iex> K8s.Client.get("apps/v1", :deployment, namespace: "test", name: "nginx")
200
      %K8s.Operation{
201
        method: :get,
202
        verb: :get,
203
        api_version: "apps/v1",
204
        name: :deployment,
205
        path_params: [namespace: "test", name: "nginx"]}
206

207
    Get the nginx deployment's status:
208
      iex> K8s.Client.get("apps/v1", "deployments/status", namespace: "test", name: "nginx")
209
      %K8s.Operation{
210
        method: :get,
211
        verb: :get,
212
        api_version: "apps/v1",
213
        name: "deployments/status",
214
        path_params: [namespace: "test", name: "nginx"]}
215

216
    Get the nginx deployment's scale:
217
      iex> K8s.Client.get("v1", "deployments/scale", namespace: "test", name: "nginx")
218
      %K8s.Operation{
219
        method: :get,
220
        verb: :get,
221
        api_version: "v1",
222
        name: "deployments/scale",
223
        path_params: [namespace: "test", name: "nginx"]}
224

225
  """
226
  @spec get(binary, Operation.name_t(), path_params | nil) :: Operation.t()
227
  def get(api_version, kind, path_params \\ []),
1✔
228
    do: Operation.build(:get, api_version, kind, path_params)
9✔
229

230
  @doc """
231
  Returns a `GET` operation to list all resources by version, kind, and namespace.
232

233
  Given the namespace `:all` as an atom, will perform a list across all namespaces.
234

235
  [K8s Docs](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/):
236

237
  > List will retrieve all resource objects of a specific type within a namespace, and the results can be restricted to resources matching a selector query.
238
  > List All Namespaces: Like List but retrieves resources across all namespaces.
239

240
  ## Examples
241

242
      iex> K8s.Client.list("v1", "Pod", namespace: "default")
243
      %K8s.Operation{
244
        method: :get,
245
        verb: :list,
246
        api_version: "v1",
247
        name: "Pod",
248
        path_params: [namespace: "default"]
249
      }
250

251
      iex> K8s.Client.list("apps/v1", "Deployment", namespace: :all)
252
      %K8s.Operation{
253
        method: :get,
254
        verb: :list_all_namespaces,
255
        api_version: "apps/v1",
256
        name: "Deployment",
257
        path_params: []
258
      }
259

260
  """
261
  @spec list(binary, Operation.name_t(), path_params | nil) :: Operation.t()
262
  def list(api_version, kind, path_params \\ [])
7✔
263

264
  def list(api_version, kind, namespace: :all),
265
    do: Operation.build(:list_all_namespaces, api_version, kind, [])
2✔
266

267
  def list(api_version, kind, path_params),
268
    do: Operation.build(:list, api_version, kind, path_params)
18✔
269

270
  @doc """
271
  Returns a `GET` operation to list all resources by version, kind, and namespace.
272

273
  Given the namespace `:all` as an atom, will perform a list across all namespaces.
274

275
  [K8s Docs](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/):
276

277
  > List will retrieve all resource objects of a specific type within a namespace, and the results can be restricted to resources matching a selector query.
278
  > List All Namespaces: Like List but retrieves resources across all namespaces.
279

280
  ## Examples
281

282
      iex> K8s.Client.list("v1", "Pod", namespace: "default")
283
      %K8s.Operation{
284
        method: :get,
285
        verb: :list,
286
        api_version: "v1",
287
        name: "Pod",
288
        path_params: [namespace: "default"]
289
      }
290

291
      iex> K8s.Client.list("apps/v1", "Deployment", namespace: :all)
292
      %K8s.Operation{
293
        method: :get,
294
        verb: :list_all_namespaces,
295
        api_version: "apps/v1",
296
        name: "Deployment",
297
        path_params: []
298
      }
299

300
  """
301
  @spec watch(binary, Operation.name_t(), path_params | nil) :: Operation.t()
302
  def watch(api_version, kind, path_params \\ [])
×
303

304
  def watch(api_version, kind, namespace: :all),
305
    do: Operation.build(:watch_all_namespaces, api_version, kind, [])
×
306

307
  def watch(api_version, kind, path_params),
308
    do: Operation.build(:watch, api_version, kind, path_params)
×
309

310
  @doc """
311
  Returns a `POST` `K8s.Operation` to create the given resource.
312

313
  ## Examples
314

315
      iex>  deployment = K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml")
316
      ...> K8s.Client.create(deployment)
317
      %K8s.Operation{
318
        method: :post,
319
        path_params: [namespace: "test", name: "nginx"],
320
        verb: :create,
321
        api_version: "apps/v1",
322
        name: "Deployment",
323
        data: K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml")
324
      }
325
  """
326
  @spec create(map()) :: Operation.t()
327
  def create(
328
        %{
329
          "apiVersion" => api_version,
330
          "kind" => kind,
331
          "metadata" => %{"namespace" => ns, "name" => name}
332
        } = resource
333
      ) do
334
    Operation.build(:create, api_version, kind, [namespace: ns, name: name], resource)
7✔
335
  end
336

337
  def create(
338
        %{
339
          "apiVersion" => api_version,
340
          "kind" => kind,
341
          "metadata" => %{"namespace" => ns, "generateName" => _}
342
        } = resource
343
      ) do
344
    Operation.build(:create, api_version, kind, [namespace: ns], resource)
1✔
345
  end
346

347
  # Support for creating resources that are cluster-scoped, like Namespaces.
348
  def create(
349
        %{"apiVersion" => api_version, "kind" => kind, "metadata" => %{"name" => name}} = resource
350
      ) do
351
    Operation.build(:create, api_version, kind, [name: name], resource)
×
352
  end
353

354
  def create(
355
        %{"apiVersion" => api_version, "kind" => kind, "metadata" => %{"generateName" => _}} =
356
          resource
357
      ) do
358
    Operation.build(:create, api_version, kind, [], resource)
1✔
359
  end
360

361
  @doc """
362
  Returns a `POST` `K8s.Operation` to create the given subresource.
363

364
  Used for creating subresources like `Scale` or `Eviction`.
365

366
  ## Examples
367

368
  Evicting a pod
369
      iex> eviction = K8s.Resource.from_file!("test/support/manifests/eviction-policy.yaml")
370
      ...>  K8s.Client.create("v1", "pods/eviction", [namespace: "default", name: "nginx"], eviction)
371
      %K8s.Operation{
372
        api_version: "v1",
373
        method: :post,
374
        name: "pods/eviction",
375
        path_params: [namespace: "default", name: "nginx"],
376
        verb: :create,
377
        data: K8s.Resource.from_file!("test/support/manifests/eviction-policy.yaml")
378
      }
379
  """
380
  @spec create(binary, Operation.name_t(), Keyword.t(), map()) :: Operation.t()
381
  def create(api_version, kind, path_params, subresource),
382
    do: Operation.build(:create, api_version, kind, path_params, subresource)
1✔
383

384
  @doc """
385
  Returns a `POST` `K8s.Operation` to create the given subresource.
386

387
  Used for creating subresources like `Scale` or `Eviction`.
388

389
  ## Examples
390

391
  Evicting a pod
392
      iex> pod = K8s.Resource.from_file!("test/support/manifests/nginx-pod.yaml")
393
      ...> eviction = K8s.Resource.from_file!("test/support/manifests/eviction-policy.yaml")
394
      ...> K8s.Client.create(pod, eviction)
395
      %K8s.Operation{
396
        api_version: "v1",
397
        data: K8s.Resource.from_file!("test/support/manifests/eviction-policy.yaml"),
398
        method: :post, name: {"Pod", "Eviction"},
399
        path_params: [namespace: "default", name: "nginx"],
400
        verb: :create
401
      }
402
  """
403
  @spec create(map(), map()) :: Operation.t()
404
  def create(
405
        %{
406
          "apiVersion" => api_version,
407
          "kind" => kind,
408
          "metadata" => %{"namespace" => ns, "name" => name}
409
        },
410
        %{"kind" => subkind} = subresource
411
      ) do
412
    Operation.build(
1✔
413
      :create,
414
      api_version,
415
      {kind, subkind},
416
      [namespace: ns, name: name],
417
      subresource
418
    )
419
  end
420

421
  # Support for creating resources that are cluster-scoped, like Namespaces.
422
  def create(
423
        %{"apiVersion" => api_version, "kind" => kind, "metadata" => %{"name" => name}},
424
        %{"kind" => subkind} = subresource
425
      ) do
426
    Operation.build(:create, api_version, {kind, subkind}, [name: name], subresource)
×
427
  end
428

429
  @doc """
430
  Returns a `PATCH` operation to patch the given resource.
431

432
  [K8s Docs](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/):
433

434
  > Patch will apply a change to a specific field. How the change is merged is defined per field. Lists may either be replaced or merged. Merging lists will not preserve ordering.
435
  > Patches will never cause optimistic locking failures, and the last write will win. Patches are recommended when the full state is not read before an update, or when failing on optimistic locking is undesirable. When patching complex types, arrays and maps, how the patch is applied is defined on a per-field basis and may either replace the field's current value, or merge the contents into the current value.
436

437
  ## Examples
438

439
      iex>  deployment = K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml")
440
      ...> K8s.Client.patch(deployment)
441
      %K8s.Operation{
442
        method: :patch,
443
        verb: :patch,
444
        api_version: "apps/v1",
445
        name: "Deployment",
446
        path_params: [namespace: "test", name: "nginx"],
447
        data: K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml"),
448
        header_params: ["Content-Type": "application/merge-patch+json"]
449
      }
450
  """
451
  @spec patch(map()) :: Operation.t()
452
  def patch(%{} = resource),
453
    do: Operation.build(:patch, resource, patch_type: :merge)
1✔
454

455
  @spec patch(map(), patch_type_or_resource :: Operation.patch_type() | map()) :: Operation.t()
456
  def patch(%{} = resource, patch_type) when is_atom(patch_type),
457
    do: Operation.build(:patch, resource, patch_type: patch_type)
×
458

459
  def patch(%{} = resource, subresource) when is_map(resource),
460
    do: patch(resource, subresource, :merge)
×
461

462
  @doc """
463
  Returns a `PATCH` operation to patch the given subresource given a resource's details and a subresource map.
464
  """
465
  @spec patch(
466
          binary,
467
          Operation.name_t(),
468
          Keyword.t(),
469
          map(),
470
          patch_type :: Operation.patch_type()
471
        ) :: Operation.t()
472
  def patch(api_version, kind, path_params, subresource, patch_type \\ :merge),
×
473
    do:
474
      Operation.build(:patch, api_version, kind, path_params, subresource, patch_type: patch_type)
×
475

476
  @doc """
477
  Returns a `PATCH` operation to patch the given subresource given a resource map and a subresource map.
478
  """
479
  @spec patch(map(), map(), patch_type :: Operation.patch_type()) :: Operation.t()
480
  def patch(
481
        %{
482
          "apiVersion" => api_version,
483
          "kind" => kind,
484
          "metadata" => %{"namespace" => ns, "name" => name}
485
        },
486
        %{"kind" => subkind} = subresource,
487
        patch_type
488
      ) do
489
    Operation.build(
×
490
      :patch,
491
      api_version,
492
      {kind, subkind},
493
      [namespace: ns, name: name],
494
      subresource,
495
      patch_type: patch_type
496
    )
497
  end
498

499
  @doc """
500
  Returns a `PUT` operation to replace/update the given resource.
501

502
  [K8s Docs](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/):
503

504
  > Replacing a resource object will update the resource by replacing the existing spec with the provided one. For read-then-write operations this is safe because an optimistic lock failure will occur if the resource was modified between the read and write. Note: The ResourceStatus will be ignored by the system and will not be updated. To update the status, one must invoke the specific status update operation.
505
  > Note: Replacing a resource object may not result immediately in changes being propagated to downstream objects. For instance replacing a ConfigMap or Secret resource will not result in all Pods seeing the changes unless the Pods are restarted out of band.
506

507
  ## Examples
508

509
      iex>  deployment = K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml")
510
      ...> K8s.Client.update(deployment)
511
      %K8s.Operation{
512
        method: :put,
513
        verb: :update,
514
        api_version: "apps/v1",
515
        name: "Deployment",
516
        path_params: [namespace: "test", name: "nginx"],
517
        data: K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml")
518
      }
519
  """
520
  @spec update(map()) :: Operation.t()
521
  def update(%{} = resource), do: Operation.build(:update, resource)
1✔
522

523
  @doc """
524
  Returns a `PUT` operation to replace/update the given subresource given a resource's details and a subresource map.
525

526
  Used for updating subresources like `Scale` or `Status`.
527

528
  ## Examples
529

530
    Scaling a deployment
531
      iex> scale = K8s.Resource.from_file!("test/support/manifests/scale-replicas.yaml")
532
      ...>  K8s.Client.update("apps/v1", "deployments/scale", [namespace: "default", name: "nginx"], scale)
533
      %K8s.Operation{
534
        api_version: "apps/v1",
535
        data: K8s.Resource.from_file!("test/support/manifests/scale-replicas.yaml"),
536
        method: :put,
537
        name: "deployments/scale",
538
        path_params: [namespace: "default", name: "nginx"],
539
        verb: :update
540
      }
541
  """
542
  @spec update(binary, Operation.name_t(), Keyword.t(), map()) :: Operation.t()
543
  def update(api_version, kind, path_params, subresource),
544
    do: Operation.build(:update, api_version, kind, path_params, subresource)
1✔
545

546
  @doc """
547
  Returns a `PUT` operation to replace/update the given subresource given a resource map and a subresource map.
548

549
  Used for updating subresources like `Scale` or `Status`.
550

551
  ## Examples
552
    Scaling a deployment:
553
      iex> deployment = K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml")
554
      ...> scale = K8s.Resource.from_file!("test/support/manifests/scale-replicas.yaml")
555
      ...> K8s.Client.update(deployment, scale)
556
      %K8s.Operation{
557
        api_version: "apps/v1",
558
        method: :put,
559
        path_params: [namespace: "test", name: "nginx"],
560
        verb: :update,
561
        data: K8s.Resource.from_file!("test/support/manifests/scale-replicas.yaml"),
562
        name: {"Deployment", "Scale"}
563
      }
564
  """
565
  @spec update(map(), map()) :: Operation.t()
566
  def update(
567
        %{
568
          "apiVersion" => api_version,
569
          "kind" => kind,
570
          "metadata" => %{"namespace" => ns, "name" => name}
571
        },
572
        %{"kind" => subkind} = subresource
573
      ) do
574
    Operation.build(
1✔
575
      :update,
576
      api_version,
577
      {kind, subkind},
578
      [namespace: ns, name: name],
579
      subresource
580
    )
581
  end
582

583
  @doc """
584
  Returns a `DELETE` operation for a resource by manifest. May be a partial manifest as long as it contains:
585

586
  * apiVersion
587
  * kind
588
  * metadata.name
589
  * metadata.namespace (if applicable)
590

591
  [K8s Docs](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/):
592

593
  > Delete will delete a resource. Depending on the specific resource, child objects may or may not be garbage collected by the server. See notes on specific resource objects for details.
594

595
  ## Examples
596

597
      iex> deployment = K8s.Resource.from_file!("test/support/manifests/nginx-deployment.yaml")
598
      ...> K8s.Client.delete(deployment)
599
      %K8s.Operation{
600
        method: :delete,
601
        verb: :delete,
602
        api_version: "apps/v1",
603
        name: "Deployment",
604
        path_params: [namespace: "test", name: "nginx"]
605
      }
606

607
  """
608
  @spec delete(map()) :: Operation.t()
609
  def delete(%{} = resource), do: Operation.build(:delete, resource)
6✔
610

611
  @doc """
612
  Returns a `DELETE` operation for a resource by version, kind, name, and optionally namespace.
613

614
  ## Examples
615

616
      iex> K8s.Client.delete("apps/v1", "Deployment", namespace: "test", name: "nginx")
617
      %K8s.Operation{
618
        method: :delete,
619
        verb: :delete,
620
        api_version: "apps/v1",
621
        name: "Deployment",
622
        path_params: [namespace: "test", name: "nginx"]
623
      }
624

625
  """
626
  @spec delete(binary, Operation.name_t(), path_params | nil) :: Operation.t()
627
  def delete(api_version, kind, path_params),
628
    do: Operation.build(:delete, api_version, kind, path_params)
1✔
629

630
  @doc """
631
  Returns a `DELETE` collection operation for all instances of a cluster scoped resource kind.
632

633
  ## Examples
634

635
      iex> K8s.Client.delete_all("extensions/v1beta1", "PodSecurityPolicy")
636
      %K8s.Operation{
637
        method: :delete,
638
        verb: :deletecollection,
639
        api_version: "extensions/v1beta1",
640
        name: "PodSecurityPolicy",
641
        path_params: []
642
      }
643

644
      iex> K8s.Client.delete_all("storage.k8s.io/v1", "StorageClass")
645
      %K8s.Operation{
646
        method: :delete,
647
        verb: :deletecollection,
648
        api_version: "storage.k8s.io/v1",
649
        name: "StorageClass",
650
        path_params: []
651
      }
652
  """
653
  @spec delete_all(binary(), binary() | atom()) :: Operation.t()
654
  def delete_all(api_version, kind) do
655
    Operation.build(:deletecollection, api_version, kind, [])
2✔
656
  end
657

658
  @doc """
659
  Returns a `DELETE` collection operation for all instances of a resource kind in a specific namespace.
660

661
  ## Examples
662

663
      iex> K8s.Client.delete_all("apps/v1beta1", "ControllerRevision", namespace: "default")
664
      %K8s.Operation{
665
        method: :delete,
666
        verb: :deletecollection,
667
        api_version: "apps/v1beta1",
668
        name: "ControllerRevision",
669
        path_params: [namespace: "default"]
670
      }
671

672
      iex> K8s.Client.delete_all("apps/v1", "Deployment", namespace: "staging")
673
      %K8s.Operation{
674
        method: :delete,
675
        verb: :deletecollection,
676
        api_version: "apps/v1",
677
        name: "Deployment",
678
        path_params: [namespace: "staging"]
679
      }
680
  """
681

682
  @spec delete_all(binary(), binary() | atom(), namespace: binary()) :: Operation.t()
683
  def delete_all(api_version, kind, namespace: namespace) do
684
    Operation.build(:deletecollection, api_version, kind, namespace: namespace)
5✔
685
  end
686

687
  @doc """
688
  Returns a `CONNECT` operation for a pods/exec resource by version, kind/resource type, name, and optionally namespace.
689

690
  [K8s Docs](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/):
691

692
  ## Examples
693
    connect to the nginx deployment in the test namespace:
694
      iex> K8s.Client.connect("apps/v1", "pods/exec", [namespace: "test", name: "nginx"], command: "ls")
695
      %K8s.Operation{
696
        method: :post,
697
        verb: :connect,
698
        api_version: "apps/v1",
699
        name: "pods/exec",
700
        path_params: [namespace: "test", name: "nginx"],
701
        query_params: [{:stdin, true}, {:stdout, true}, {:stderr, true}, {:tty, false}, {:command, "ls"}]
702
      }
703

704
  """
705
  @spec connect(binary(), binary() | atom(), [namespace: binary(), name: binary()], keyword()) ::
706
          Operation.t()
707
  def connect(api_version, kind, [namespace: namespace, name: name], opts \\ []) do
×
708
    Operation.build(:connect, api_version, kind, [namespace: namespace, name: name], nil, opts)
3✔
709
  end
710
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc