Skip to content

Commit

Permalink
Merge pull request #101 from victoriavilasb/api-version-v1
Browse files Browse the repository at this point in the history
Support to apiextensions.k8s.io/v1
  • Loading branch information
coryodaniel authored Apr 9, 2021
2 parents c5f2b3f + 8ef59c4 commit 56da8e0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 12 deletions.
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ if Mix.env() == :test do
k8s_client: Bonny.K8sMockClient,
controllers: [Widget, Cog],
group: "example.com",
cluster_name: :test
cluster_name: :test,
api_version: "apiextensions.k8s.io/v1beta1"
end

if Mix.env() == :dev do
Expand Down
8 changes: 8 additions & 0 deletions lib/bonny/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ defmodule Bonny.Config do
end
end

@doc """
Kubernetes APIVersion used. Defaults to `apiextensions.k8s.io/v1`
"""
@spec api_version() :: binary
def api_version() do
Application.get_env(:bonny, :api_version, "apiextensions.k8s.io/v1beta1")
end

@doc """
`K8s.Cluster` name used for this operator. Defaults to `:default`
"""
Expand Down
39 changes: 32 additions & 7 deletions lib/bonny/crd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule Bonny.CRD do
"""
alias Bonny.CRD

@api_version "apiextensions.k8s.io/v1beta1"
@kind "CustomResourceDefinition"

@typep names_t :: %{
Expand Down Expand Up @@ -100,16 +99,21 @@ defmodule Bonny.CRD do
version: v1
```
"""
@spec to_manifest(Bonny.CRD.t()) :: map
def to_manifest(%CRD{} = crd) do
@spec to_manifest(Bonny.CRD.t(), String.t()) :: map
def to_manifest(%CRD{} = crd, api_version \\ "apiextensions.k8s.io/v1beta1") do
spec = case api_version do
"apiextensions.k8s.io/v1" -> format_spec_v1(crd)
_ -> format_spec_v1beta1(crd)
end

%{
apiVersion: @api_version,
apiVersion: api_version,
kind: @kind,
metadata: %{
name: "#{crd.names.plural}.#{crd.group}",
labels: Bonny.Operator.labels()
},
spec: format_spec(crd)
spec: spec
}
end

Expand All @@ -135,8 +139,8 @@ defmodule Bonny.CRD do
]
end

@spec format_spec(Bonny.CRD.t()) :: map
defp format_spec(%CRD{scope: scope} = crd) do
@spec format_spec_v1beta1(Bonny.CRD.t()) :: map
defp format_spec_v1beta1(%CRD{scope: scope} = crd) do
cased_scope = String.capitalize("#{scope}")

crd
Expand All @@ -145,6 +149,27 @@ defmodule Bonny.CRD do
|> rename_keys(keys_to_rename())
end

@spec format_spec_v1(Bonny.CRD.t()) :: map
defp format_spec_v1(%CRD{version: version, additional_printer_columns: additional_printer_columns, scope: scope} = crd) do
cased_scope = String.capitalize("#{scope}")

additional_printer_columns_v1 = additional_printer_columns
|> Enum.map(fn elem -> rename_keys(elem, %{JSONPath: :jsonPath}) end)

crd
|> Map.from_struct()
|> Map.drop([:version, :additional_printer_columns])
|> Map.put(:scope, cased_scope)
|> Map.put(:versions, [%{
name: version,
scheme: %{
openAPIV3Scheme: %{
additionalPrinterColumns: additional_printer_columns_v1
}
}
}])
end

@spec rename_keys(map, map) :: map
defp rename_keys(map, keymap) do
Enum.reduce(keymap, map, fn {oldkey, newkey}, agg ->
Expand Down
2 changes: 1 addition & 1 deletion lib/bonny/operator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Bonny.Operator do
@spec crds() :: list(map())
def crds() do
Enum.map(Bonny.Config.controllers(), fn controller ->
Bonny.CRD.to_manifest(controller.crd())
Bonny.CRD.to_manifest(controller.crd(), Bonny.Config.api_version())
end)
end

Expand Down
46 changes: 43 additions & 3 deletions test/bonny/crd_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ defmodule Bonny.CRDTest do
doctest Bonny.CRD
alias Bonny.CRD

describe "to_manifest/1" do
test "generates a kubernetes manifest" do
describe "to_manifest/2" do
test "generates a kubernetes manifest in version v1beta1" do
spec = Widget.crd()
manifest = CRD.to_manifest(spec)
manifest = CRD.to_manifest(spec, "apiextensions.k8s.io/v1beta1")

expected = %{
apiVersion: "apiextensions.k8s.io/v1beta1",
Expand Down Expand Up @@ -36,5 +36,45 @@ defmodule Bonny.CRDTest do

assert expected == manifest
end

test "generates a kubernetes manifest in version v1" do
spec = Widget.crd()
manifest = CRD.to_manifest(spec, "apiextensions.k8s.io/v1")

expected = %{
apiVersion: "apiextensions.k8s.io/v1",
kind: "CustomResourceDefinition",
metadata: %{
name: "widgets.example.com",
labels: %{"k8s-app" => "bonny"}
},
spec: %{
group: "example.com",
names: %{kind: "Widget", plural: "widgets", shortNames: nil, singular: "widget"},
scope: "Namespaced",
versions: [
%{
name: "v1",
scheme: %{
openAPIV3Scheme: %{
additionalPrinterColumns: [
%{jsonPath: ".spec.test", description: "test", name: "test", type: "string"},
%{
jsonPath: ".metadata.creationTimestamp",
description:
"CreationTimestamp is a timestamp representing the server time when this object was created. It is not guaranteed to be set in happens-before order across separate operations. Clients may not set this value. It is represented in RFC3339 form and is in UTC.\n\n Populated by the system. Read-only. Null for lists. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata",
name: "Age",
type: "date"
}
]
}
}
}
]
}
}

assert expected == manifest
end
end
end

0 comments on commit 56da8e0

Please sign in to comment.