• 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

70.59
/lib/k8s/discovery/resource_naming.ex
1
defmodule K8s.Discovery.ResourceNaming do
2
  @moduledoc false
3

4
  @doc """
5
  Rules for matching various name/kind inputs against a resource manifest.
6

7
  ## Examples
8
      Supports atom inputs
9
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Deployment", "name" => "deployments"}, :deployment)
10
      true
11

12
      Supports singular form match on `"kind"`
13
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Deployment", "name" => "deployments"}, "Deployment")
14
      true
15

16
      Supports case insensitive match on `"name"`
17
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Deployment", "name" => "deployments"}, "Deployments")
18
      true
19

20
      Supports matching tuples of `{resource, subresource}` kind
21
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Eviction", "name" => "pods/eviction"}, {"Pod", "Eviction"})
22
      true
23

24
      Supports matching tuples of `{resource, subresource}` kind
25
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Deployment", "name" => "deployments/status"}, {"Deployment", "Status"})
26
      true
27

28
      Supports matching subresources
29
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Deployment", "name" => "deployments/status"}, "deployments/status")
30
      true
31

32
      Supports matching subresources
33
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Scale", "name" => "deployments/scale"}, "deployments/scale")
34
      true
35

36
      Does not select subresources when `"kind"` matches
37
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Deployment", "name" => "deployments/status"}, :deployment)
38
      false
39

40
      Does not select subresources when `"kind"` matches
41
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Deployment", "name" => "deployments/status"}, "Deployment")
42
      false
43

44
      Does not select subresources when `"kind"` matches
45
      iex> K8s.Discovery.ResourceNaming.matches?(%{"kind" => "Deployment", "name" => "deployments/status"}, "Deployments")
46
      false
47

48
  """
49
  @spec matches?(map(), K8s.Operation.name_t()) :: boolean()
50
  def matches?(resource, arg) when is_atom(arg), do: matches?(resource, Atom.to_string(arg))
69✔
51

52
  def matches?(resource, arg) when is_binary(arg) do
53
    exact_name_match?(resource, arg) ||
6✔
54
      exact_kind_match?(resource, arg) ||
834✔
55
      downcased_kind_match?(resource, arg) ||
761✔
56
      capitalized_name_match?(resource, arg) ||
840✔
57
      no_match(resource, arg)
739✔
58
  end
59

60
  # This is not the best match. Its not checking pluralization of the `kind`, so could lead to conflicts
61
  # for similarly named resources that have the same types of applicable subresources.
62
  def matches?(%{"kind" => subkind, "name" => name} = _subresource, {kind, subkind}) do
63
    resource_kind_as_nameish = String.downcase(kind)
×
64
    String.starts_with?(name, resource_kind_as_nameish)
×
65
  end
66

67
  def matches?(%{"kind" => kind, "name" => name} = _subresource, {kind, subkind}) do
68
    subkind_as_nameish = String.downcase(subkind)
×
69
    String.ends_with?(name, "/" <> subkind_as_nameish)
×
70
  end
71

72
  def matches?(_map, {_kind, _subkind}), do: false
×
73

74
  @spec exact_name_match?(map, binary) :: boolean
75
  defp exact_name_match?(%{"name" => name}, input), do: name == input
840✔
76

77
  @spec exact_kind_match?(map, binary) :: boolean
78
  defp exact_kind_match?(%{"kind" => kind, "name" => name}, input) do
79
    kind == input && !subresource_name?(name)
834✔
80
  end
81

82
  @spec downcased_kind_match?(map, binary) :: boolean
83
  defp downcased_kind_match?(%{"kind" => kind, "name" => name}, input),
84
    do: String.downcase(kind) == input && !subresource_name?(name)
761✔
85

86
  @spec capitalized_name_match?(map, binary) :: boolean
87
  defp capitalized_name_match?(%{"name" => name}, input), do: String.downcase(input) == name
739✔
88

89
  @spec no_match(any, any) :: false
90
  defp no_match(_, _), do: false
739✔
91

92
  @spec subresource_name?(binary()) :: boolean()
93
  def subresource_name?(name), do: String.contains?(name, "/")
95✔
94
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