Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix complex type #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 48 additions & 86 deletions modules/generated/v1.19.nix

Large diffs are not rendered by default.

127 changes: 46 additions & 81 deletions modules/generated/v1.20.nix

Large diffs are not rendered by default.

140 changes: 51 additions & 89 deletions modules/generated/v1.21.nix

Large diffs are not rendered by default.

123 changes: 45 additions & 78 deletions modules/generated/v1.22.nix

Large diffs are not rendered by default.

120 changes: 45 additions & 75 deletions modules/generated/v1.23.nix

Large diffs are not rendered by default.

122 changes: 46 additions & 76 deletions modules/generated/v1.24.nix

Large diffs are not rendered by default.

108 changes: 41 additions & 67 deletions modules/generated/v1.25.nix

Large diffs are not rendered by default.

125 changes: 48 additions & 77 deletions modules/generated/v1.26.nix

Large diffs are not rendered by default.

131 changes: 51 additions & 80 deletions modules/generated/v1.27.nix

Large diffs are not rendered by default.

77 changes: 26 additions & 51 deletions pkgs/generators/k8s/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ with lib; let

submoduleForDefinition = ref: name: kind: group: version: ''(submoduleForDefinition "${ref}" "${name}" "${kind}" "${group}" "${version}")'';

coerceAttrsOfSubmodulesToListByKey = ref: attrMergeKey: listMergeKeys: ''(coerceAttrsOfSubmodulesToListByKey "${ref}" "${attrMergeKey}" [${concatStringsSep " " (map (key: "\"${toString key}\"") listMergeKeys)}])'';
coerceAttrsOfSubmodulesToListByKey = ref: mergeKey: listMapKeys: ''(coerceAttrsOfSubmodulesToListByKey "${ref}" "${mergeKey}" [${concatStringsSep " " (map (key: "\"${toString key}\"") listMapKeys)}])'';

attrsToList = "attrsToList";

Expand Down Expand Up @@ -144,42 +144,26 @@ with lib; let
}
# if a reference is to complex type
else
# make it an attribute set of submodules if only x-kubernetes-patch-merge-key is present, or
# x-kubernetes-patch-merge-key == x-kubernetes-list-map-keys.
if (hasAttr "x-kubernetes-patch-merge-key" property) && (!(hasAttr "x-kubernetes-list-map-keys" property) || (property."x-kubernetes-list-map-keys" == [ property."x-kubernetes-patch-merge-key" ]))
if hasAttr "x-kubernetes-patch-merge-key" property
then
let
mergeKey = property."x-kubernetes-patch-merge-key";
in
{
type = requiredOrNot (coerceAttrsOfSubmodulesToListByKey (refDefinition property.items) mergeKey [ ]);
type = requiredOrNot (coerceAttrsOfSubmodulesToListByKey (refDefinition property.items) mergeKey (
if hasAttr "x-kubernetes-list-map-keys" property
then property."x-kubernetes-list-map-keys"
else [ ]
));
apply = attrsToList;
}
# in other case it's a simple list
else
# make it an attribute set of submodules if only x-kubernetes-patch-merge-key is present, or
# x-kubernetes-patch-merge-key == x-kubernetes-list-map-keys.
if
hasAttr "properties" swagger.definitions.${refDefinition property.items}
&& hasAttr "name" swagger.definitions.${refDefinition property.items}.properties
then
let
mergeKey = "name";
in
{
type = requiredOrNot (coerceAttrsOfSubmodulesToListByKey (refDefinition property.items) mergeKey (
if hasAttr "x-kubernetes-list-map-keys" property
then property."x-kubernetes-list-map-keys"
else [ ]
));
apply = attrsToList;
}
else {
type =
if (refDefinition property.items) == _name
then types.unspecified # do not allow self-referential values
else requiredOrNot (types.listOf (submoduleOf definitions (refDefinition property.items)));
}
else {
type =
if (refDefinition property.items) == _name
then types.unspecified # do not allow self-referential values
else requiredOrNot (types.listOf (submoduleOf definitions (refDefinition property.items)));
}
# in other case it only references a simple type
else {
type = requiredOrNot (types.listOf (mapType property.items));
Expand Down Expand Up @@ -388,21 +372,17 @@ with lib; let

mkOptionDefault = mkOverride 1001;

mergeValuesByKey = attrMergeKey: listMergeKeys: values:
mergeValuesByKey = mergeKey: listMapKeys: values:
listToAttrs (imap0
(i: value: nameValuePair (
if hasAttr attrMergeKey value
then
if isAttrs value.''${attrMergeKey}
then toString value.''${attrMergeKey}.content
else (toString value.''${attrMergeKey})
else
# generate merge key for list elements if it's not present
"__kubenix_list_merge_key_" + (concatStringsSep "" (map (key:
if isAttrs value.''${key}
then toString value.''${key}.content
else (toString value.''${key})
) listMergeKeys))
(concatStringsSep "" (map (key:
if value ? ''${key}
then
if isAttrs value.''${key}
then toString value.''${key}.content
else (toString value.''${key})
else ""
) (unique ([mergeKey] ++ listMapKeys))))
) (value // { _priority = i; }))
values);

Expand All @@ -422,12 +402,7 @@ with lib; let
_priority = mkOption { type = types.nullOr types.int; default = null; };
};
config = definitions."''${ref}".config // {
''${mergeKey} = mkOverride 1002 (
# use name as mergeKey only if it is not coming from mergeValuesByKey
if (!hasPrefix "__kubenix_list_merge_key_" name)
then convertName name
else null
);
''${mergeKey} = mkOverride 1002 (convertName name);
};
});

Expand All @@ -448,10 +423,10 @@ with lib; let
];
});

coerceAttrsOfSubmodulesToListByKey = ref: attrMergeKey: listMergeKeys: (types.coercedTo
coerceAttrsOfSubmodulesToListByKey = ref: mergeKey: listMapKeys: (types.coercedTo
(types.listOf (submoduleOf ref))
(mergeValuesByKey attrMergeKey listMergeKeys)
(types.attrsOf (submoduleWithMergeOf ref attrMergeKey))
(mergeValuesByKey mergeKey listMapKeys)
(types.attrsOf (submoduleWithMergeOf ref mergeKey))
);

definitions = {
Expand Down
17 changes: 17 additions & 0 deletions tests/k8s/defaults.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
with lib; let
inherit (config.kubernetes.api.resources.pods) pod1;
inherit (config.kubernetes.api.resources.pods) pod2;
inherit (config.kubernetes.api.resources.pods) pod3;
in
{
imports = with kubenix.modules; [ test k8s ];
Expand All @@ -22,6 +23,10 @@ in
message = "Should have conditional annotation set";
assertion = pod2.metadata.annotations.conditional-annotation == "value";
}
{
message = "Should have protocol UDP";
assertion = (elemAt (head pod3.spec.containers).ports 1).protocol == "UDP";
}
];
};

Expand All @@ -31,6 +36,18 @@ in
metadata.labels.custom-label = "value";
};

kubernetes.resources.pods.pod3 = {
spec.containers.container1.ports = [
{
containerPort = 80;
}
{
containerPort = 80;
protocol = "UDP";
}
];
};

kubernetes.api.defaults = [
{
resource = "pods";
Expand Down