-
Notifications
You must be signed in to change notification settings - Fork 105
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
Remap image_pull_policy
field on Container
from ImagePullPolicy
enum to str
#877
Changes from all commits
c4b495c
b6e651c
3c1955d
2f13b3d
121528c
8d18f61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import json | ||
import logging | ||
import sys | ||
from typing import Dict, List, Set, Tuple | ||
from typing import Dict, List, Optional, Set, Tuple, Union | ||
|
||
import requests | ||
|
||
|
@@ -56,17 +56,26 @@ | |
# these are specifications of objects with fields that are marked to have a union type of IntOrString. However, K8s | ||
# only accepts one or the other, unfortunately. Here, we remap those fields from their respective `$ref`s, which | ||
# contain a specific type, to another type. The mapping is from the `$ref` to a tuple of the existing type and the | ||
# new type. The dictionary model is: | ||
# new type. While the first piece of the mapping tuple must be defined the second one is optional - if the second | ||
# part of the tuple is None then the field will be removed. The dictionary model is: | ||
# { object name: { field name: ( ( existing field, existing value ) , ( new field, new value ) ) } } | ||
INT_OR_STRING_FIELD_REMAPPING: Dict[str, Dict[str, Tuple[Tuple[str, str], Tuple[str, str]]]] = { | ||
FIELD_REMAPPINGS: Dict[ | ||
str, Dict[str, Tuple[Tuple[str, Union[str, List[str]]], Optional[Tuple[str, Union[str, List[str]]]]]] | ||
] = { | ||
"io.k8s.api.core.v1.HTTPGetAction": { | ||
"port": ( | ||
("$ref", "#/definitions/io.k8s.apimachinery.pkg.util.intstr.IntOrString"), | ||
("type", "integer"), | ||
), | ||
}, | ||
"io.k8s.api.core.v1.Container": { | ||
"imagePullPolicy": ( | ||
("enum", None), | ||
None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First part of the tuple says "original field, original field value" second one says "new field, new field value". New field value of None means delete the original field. So, this is removing the enum from Container, allowing codegen to put a string in place of the Container image pull policy enum, and stop generating the ImagePullPolicy enum altogether. Later in the script you see we have to add a specification for IPP manually for otherwise it's not fully backwards compatible, even if it's not used by Container! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah of course, I was misreading the outer tuple. Makes sense (almost!), though I would suggest a refactor to helper classes if you have time/want to in a separate PR |
||
) | ||
}, | ||
} | ||
for obj_name, field in INT_OR_STRING_FIELD_REMAPPING.items(): | ||
for obj_name, field in FIELD_REMAPPINGS.items(): | ||
try: | ||
curr_field = spec["definitions"][obj_name] | ||
except KeyError as e: | ||
|
@@ -94,7 +103,12 @@ | |
|
||
# get the tuple of the existing field and value, and the new field and value | ||
existing_field, existing_value = field[property_to_change][0] | ||
new_field, new_value = field[property_to_change][1] | ||
if field[property_to_change][1] is None: | ||
# if the second one is absent it means we want to delete the existing field | ||
del curr_property[existing_field] | ||
continue | ||
else: | ||
new_field, new_value = field[property_to_change][1] | ||
|
||
# check that the existing field and value are the same as the current field and value | ||
assert curr_property[existing_field] == existing_value, ( | ||
|
@@ -108,6 +122,21 @@ | |
if existing_field != new_field: | ||
del curr_property[existing_field] | ||
|
||
# there are also some specifications that have to be introduced manually for backwards compatibility purposes. This | ||
# block allows us to define those specifications and add them to the spec. | ||
MANUAL_SPECIFICATIONS: List[Tuple[str, Dict]] = [ | ||
( | ||
"io.k8s.api.core.v1.ImagePullPolicy", | ||
{ | ||
"description": "An enum that contains available image pull policy options.", | ||
"type": "string", | ||
"enum": ["Always", "Never", "IfNotPresent"], | ||
}, | ||
), | ||
] | ||
for obj_name, obj_spec in MANUAL_SPECIFICATIONS: | ||
spec["definitions"][obj_name] = obj_spec | ||
|
||
# finally, we write the spec to the output file that is passed to use assuming the client wants to perform | ||
# something with this file | ||
with open(output_file, "w+") as f: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure the type is helping at this point 😅 The comment is helpful enough, and longer term maybe we should create a helper class within this script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea... I was very reluctant to add this type, especially as I realized it's growing. Yea, we'll have to refactor this a bit in the future.