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

add machineset resource implementation #97

Merged
merged 1 commit into from
Aug 24, 2021

Conversation

GuyAfik
Copy link
Contributor

@GuyAfik GuyAfik commented Aug 9, 2021

This patch adds the abilty to create the 'machineset' resource.

Short description:
More details:
What this PR does / why we need it:
Which issue(s) this PR fixes:
Special notes for reviewer:
Bug:

ocp_resources/machineset.py Outdated Show resolved Hide resolved
ocp_resources/machineset.py Outdated Show resolved Hide resolved

def to_dict(self):

data = super().to_dict()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rename data to res, keep our code the same in all resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

ocp_resources/machineset.py Outdated Show resolved Hide resolved
ocp_resources/machineset.py Outdated Show resolved Hide resolved
"labels",
"template",
)
_provider_spec = "providerSpec"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this one is separated from the above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apparently the pre-commit changed that before my commit, ill change that.

ocp_resources/machineset.py Outdated Show resolved Hide resolved
@@ -0,0 +1,91 @@
from ocp_resources.machine import NamespacedResource
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please import from resource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

f"{self.api_group}/cluster-api-machine-type": self.machine_type,
}

data.setdefault(_spec, {})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data.setdefault(_spec, {})
data[_spec]["replicas"] = self.replicas

can be done in one line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack


data.setdefault(_spec, {})
data[_spec]["replicas"] = self.replicas
data[_spec].setdefault(_selector, {})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

"providerSpec",
)

res[_metadata].setdefault(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

res[_metadata].setdefault(_labels, {}).update(
    {
                f"{self.api_group}/cluster-api-cluster": self.cluster_name,
                f"{self.api_group}/cluster-api-machine-role": self.machine_role,
                f"{self.api_group}/cluster-api-machine-type": self.machine_type,
            },
})

Copy link
Contributor Author

@GuyAfik GuyAfik Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between those two statements?

res[_metadata].setdefault(
_labels,
{
f"{self.api_group}/cluster-api-cluster": self.cluster_name,
f"{self.api_group}/cluster-api-machine-role": self.machine_role,
f"{self.api_group}/cluster-api-machine-type": self.machine_type,
},
)

This statement will only overrun the labels in case the "lables" key does not appear in the dict.
So if the user applied its own labels then i will not overrun it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you want to update the existing one if any.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

}
)
res.setdefault(
_spec,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, use update()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

Copy link
Collaborator

@myakove myakove left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.


res[_metadata].setdefault(_labels, {}).update(
{
f"{self.api_group}/cluster-api-cluster": self.cluster_name,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. cluster-api-cluster please save and re-use (applies to all those which are used more than once)
  2. How about saving all in one dict and use dict comprehension to add only the relevant data in each case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. ack
  2. can you share an example of what you mean?


res.setdefault(_spec, {}).update(
{
"replicas": self.replicas,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need setdefault with update for each one.
replicas
selector and so on

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack, updated


def __enter__(self):
super().__enter__()
if self.wait_ready_state and not self.wait(timeout=self.replicas * TIMEOUT):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not part of the creation, if you need to wait for some state, do it in a function.
enter is called when the user do with ....

Copy link
Contributor Author

@GuyAfik GuyAfik Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack, yes i figured it might be nice to give the user an option to wait for the machine-set state while using the context manager, but it might be better to split that

bool: True if updating the machine-set was successful, False otherwise.
"""
LOGGER.info(f"Update machine-set {self.name} to {patches}")
ResourceEditor(patches={self: patches}).update()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to call ResourceEditor from inside a resource
check how we do it in Deployment.scale_replicas

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

"cluster-api-machineset",
)

res[_metadata].setdefault(_labels, {}).update(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discard my previous comments please, make is simple
no need to use setdefault(), just build you dict as needed.
we can assume that anyone that cal to_dict() create new resource.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

if ready_replicas and ready_replicas == self.desired_replicas:
return True
except TimeoutExpiredError:
LOGGER.error(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fit all errors in one LOGGER.error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

def provider_spec_value(self):
return self.instance.spec.template.spec.providerSpec.value

def wait(self, timeout=TIMEOUT, sleep=1):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait please rename, this function wait for replicas

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

)
return False

def scale(self, replicas, wait_timeout=TIMEOUT, sleep=1, wait=True):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scale ditto rename to scale_replicas

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

@@ -0,0 +1,189 @@
import logging
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to machine_set.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

wait (bool): True if waiting for machine-set to reach into 'ready' state, False otherwise.

Returns:
bool: True if scaling the machine set was successful, False otherwise.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns True also if wait=False (and not necessarily if scaling succeeded)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think its necessary to add it to the docstring.

basically if the update function didn't raise an exception, then that means the update was successful.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if wait=False, wait_for_replicas is not called.
A user should know from the docstring what is the expected behavior; for example:
bool: True if scaling the machine set was successful or wait=False, False otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

ocp_resources/machine_set.py Outdated Show resolved Hide resolved
This patch adds the abilty to create the 'machineset' resource.
@sonarcloud
Copy link

sonarcloud bot commented Aug 22, 2021

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@myakove
Copy link
Collaborator

myakove commented Aug 23, 2021

/approve

@GuyAfik
Copy link
Contributor Author

GuyAfik commented Aug 23, 2021

/verified

@rnetser
Copy link
Collaborator

rnetser commented Aug 24, 2021

/approve

@myakove myakove merged commit f2a2178 into RedHatQE:master Aug 24, 2021
@GuyAfik GuyAfik deleted the add_machineset_resource branch August 24, 2021 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants