generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 135
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
Migrate k8s #311
Merged
gravesm
merged 23 commits into
ansible-collections:2.x-refactor
from
alinabuzachis:migrate_k8s
Feb 4, 2022
Merged
Migrate k8s #311
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
37d81d0
Use refactored module_utils
alinabuzachis 22a25fd
Fix runner
alinabuzachis 1f365de
Fix runner
alinabuzachis 8d5b10f
Update runner.py
alinabuzachis 543e35f
black runner
alinabuzachis 2a926e6
Fix units
alinabuzachis 01197cd
Fix ResourceTimeout
alinabuzachis 658219f
Attempt to fix 'Create custom resource'
alinabuzachis 6d9fada
Update svc.find_resource(..., fail=True)
alinabuzachis fd57e3c
Attempt to fix integration tests
alinabuzachis 87a6fab
Fix apiVersion for Job
alinabuzachis d90cd5c
Fix crd
alinabuzachis 1566445
Add exception = None
alinabuzachis d79680e
Fix apiVersion for definition
alinabuzachis 9ec3c03
Fix assert
alinabuzachis 4c38fc9
Fix returned results
alinabuzachis 024113f
Update runner to return results accordingly
alinabuzachis cd6c4ac
Fix assert
alinabuzachis 4728487
Add validate-missing
alinabuzachis f2572d4
Merge branch '2.x-refactor' into migrate_k8s
alinabuzachis 28b8769
Update client.py
alinabuzachis a7ef348
Fix failures
gravesm 8b41c8f
Fix black formatting
gravesm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,29 +171,35 @@ def patch_resource( | |
msg = "Failed to patch object: {0}".format(reason) | ||
raise CoreException(msg) from e | ||
|
||
def retrieve(self, resource: Resource, definition: Dict) -> Dict: | ||
def retrieve(self, resource: Resource, definition: Dict) -> ResourceInstance: | ||
state = self.module.params.get("state", None) | ||
append_hash = self.module.params.get("append_hash", False) | ||
name = definition["metadata"].get("name") | ||
generate_name = definition["metadata"].get("generateName") | ||
namespace = definition["metadata"].get("namespace") | ||
label_selectors = self.module.params.get("label_selectors") | ||
results = { | ||
"changed": False, | ||
"result": {}, | ||
} | ||
existing = None | ||
existing: ResourceInstance = None | ||
|
||
try: | ||
# ignore append_hash for resources other than ConfigMap and Secret | ||
if append_hash and definition["kind"] in ["ConfigMap", "Secret"]: | ||
name = "%s-%s" % (name, generate_hash(definition)) | ||
definition["metadata"]["name"] = name | ||
params = dict(name=name) | ||
if name: | ||
name = "%s-%s" % (name, generate_hash(definition)) | ||
definition["metadata"]["name"] = name | ||
elif generate_name: | ||
definition["metadata"]["generateName"] = "%s-%s" % ( | ||
generate_name, | ||
generate_hash(definition), | ||
) | ||
params = {} | ||
if name: | ||
params["name"] = name | ||
if namespace: | ||
params["namespace"] = namespace | ||
if label_selectors: | ||
params["label_selector"] = ",".join(label_selectors) | ||
existing = self.client.get(resource, **params) | ||
if "name" in params or "label_selector" in params: | ||
existing = self.client.get(resource, **params) | ||
except (NotFoundError, MethodNotAllowedError): | ||
pass | ||
except ForbiddenError as e: | ||
|
@@ -210,10 +216,7 @@ def retrieve(self, resource: Resource, definition: Dict) -> Dict: | |
msg = "Failed to retrieve requested object: {0}".format(reason) | ||
raise CoreException(msg) from e | ||
|
||
if existing: | ||
results["result"] = existing.to_dict() | ||
|
||
return results | ||
return existing | ||
|
||
def find( | ||
self, | ||
|
@@ -345,10 +348,12 @@ def create(self, resource: Resource, definition: Dict) -> Dict: | |
results["result"] = k8s_obj | ||
|
||
if wait and not self.module.check_mode: | ||
definition["metadata"].update({"name": k8s_obj["metadata"]["name"]}) | ||
waiter = get_waiter(self.client, resource, condition=wait_condition) | ||
success, results["result"], results["duration"] = waiter.wait( | ||
timeout=wait_timeout, sleep=wait_sleep, name=name, namespace=namespace, | ||
timeout=wait_timeout, | ||
sleep=wait_sleep, | ||
name=k8s_obj["metadata"]["name"], | ||
namespace=namespace, | ||
) | ||
|
||
results["changed"] = True | ||
|
@@ -358,7 +363,7 @@ def create(self, resource: Resource, definition: Dict) -> Dict: | |
'"{0}" "{1}": Resource creation timed out'.format( | ||
definition["kind"], origin_name | ||
), | ||
**results | ||
results, | ||
) | ||
|
||
return results | ||
|
@@ -431,7 +436,7 @@ def apply( | |
'"{0}" "{1}": Resource apply timed out'.format( | ||
definition["kind"], origin_name | ||
), | ||
**results | ||
results, | ||
) | ||
|
||
return results | ||
|
@@ -493,7 +498,7 @@ def replace( | |
'"{0}" "{1}": Resource replacement timed out'.format( | ||
definition["kind"], origin_name | ||
), | ||
**results | ||
results, | ||
) | ||
|
||
return results | ||
|
@@ -514,16 +519,25 @@ def update( | |
wait_condition = self.module.params["wait_condition"] | ||
results = {"changed": False, "result": {}} | ||
|
||
if self.module.check_mode and not self.module.client.dry_run: | ||
if self.module.check_mode and not self.client.dry_run: | ||
k8s_obj = dict_merge(existing.to_dict(), _encode_stringdata(definition)) | ||
else: | ||
exception = None | ||
for merge_type in self.module.params.get("merge_type") or [ | ||
"strategic-merge", | ||
"merge", | ||
]: | ||
k8s_obj = self.patch_resource( | ||
resource, definition, name, namespace, merge_type=merge_type, | ||
) | ||
try: | ||
k8s_obj = self.patch_resource( | ||
resource, definition, name, namespace, merge_type=merge_type, | ||
) | ||
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. You need to add 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. Right! Thanks, missed that! |
||
exception = None | ||
except CoreException as e: | ||
exception = e | ||
continue | ||
break | ||
if exception: | ||
raise exception | ||
|
||
success = True | ||
results["result"] = k8s_obj | ||
|
@@ -545,7 +559,7 @@ def update( | |
'"{0}" "{1}": Resource update timed out'.format( | ||
definition["kind"], origin_name | ||
), | ||
**results | ||
results, | ||
) | ||
|
||
return results | ||
|
@@ -581,6 +595,15 @@ def _empty_resource_list() -> bool: | |
if self.module.check_mode and not self.client.dry_run: | ||
return results | ||
else: | ||
if name: | ||
params["name"] = name | ||
|
||
if namespace: | ||
params["namespace"] = namespace | ||
|
||
if label_selectors: | ||
params["label_selector"] = ",".join(label_selectors) | ||
|
||
if delete_options: | ||
body = { | ||
"apiVersion": "v1", | ||
|
@@ -614,7 +637,7 @@ def _empty_resource_list() -> bool: | |
'"{0}" "{1}": Resource deletion timed out'.format( | ||
definition["kind"], origin_name | ||
), | ||
**results | ||
results, | ||
) | ||
|
||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The failure with the crd test is coming from here. We need to carry over the logic to test multiple merge types, since we are right now failing on the first try. So something like this in the
else
block: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.
I removed the changed I make to the crd.yml file and added this block. But it keeps failing.