Skip to content

Commit

Permalink
Update contributor.md and example resources (#1839)
Browse files Browse the repository at this point in the history
* update typing

* update contributor.md
  • Loading branch information
rnetser committed May 23, 2024
1 parent 831ca96 commit a223f3b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 80 deletions.
81 changes: 16 additions & 65 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,70 +31,21 @@ pre-commit install
- If the fix is needed in a released version, once your pull request is merged, cherry-pick it to the relevant branch(s).
Add `/cherry-pick <target branch to cherry-pick to>` to the PR comment.

## Adding a new module (resource)

To support working with a new (or missing) resource:

- Add a new file under `ocp_resources`, names as the resource.
If the resource name is composed of multiple words, separate them with an underscore.
For example: `ImageStream` filename is `image_stream.py`
- Create a class named as the resource kind.
For example: `ImageStream` -> `class ImageStream`
- Inherit from the relevant class.
If the resource is cluster-scoped, inherit from `Resource` else from `NamespacedResource`.
For example: `class ImageStream(NamespacedResource):`
- Add the resource's apiGroup (`apiVersion` prefix) as `api_group`.
For example: `image.openshift.io`
- Add a link to the resource's API reference.
For example: [ImageStream API reference](https://docs.openshift.com/container-platform/4.11/rest_api/image_apis/imagestream-image-openshift-io-v1.html#imagestream-image-openshift-io-v1)
- Add an `__init__` function.
Define all the required arguments that are **required** to instantiate a new resource.
Optional parameters may be added as well.
For example:

```
def __init__(
self,
name=None,
namespace=None,
client=None,
lookup_policy=False,
tags=None,
teardown=True,
privileged_client=None,
yaml_file=None,
delete_timeout=TIMEOUT_4MINUTES,
**kwargs,
):
super().__init__(
name=name,
namespace=namespace,
client=client,
teardown=teardown,
privileged_client=privileged_client,
yaml_file=yaml_file,
delete_timeout=delete_timeout,
**kwargs,
)
self.tags = tags
self.lookup_policy = lookup_policy
```

- Use `to_dict` to set the values in the resource body. Make sure you call `super().to_dict()` first.
For example:
## General
- Add typing to new code; typing is enforced using [mypy](https://mypy-lang.org/)

```
def to_dict(self) -> None:
super().to_dict()
if not self.yaml_file:
self.res.update(
{
"spec": {
"lookupPolicy": {"local": self.lookup_policy},
"tags": self.tags,
}
}
)
```
## Adding a new module (resource)

- Check [imageStreams](ocp_resources/image_stream.py) for reference.
A new resource must follow rules:
- A new file named as the resource under `ocp_resources`; If the resource name is composed of multiple words, separate them with an underscore.
- A class named as the resource kind.
- Inherit from the relevant class; If the resource is cluster-scoped, inherit from `Resource` else from `NamespacedResource`.
- API group:
- Under `ocp_resources.resource.Resource.ApiGroup`
- Resource's apiGroup (`apiVersion` prefix) as `api_group`
- A link to the resource's API reference.
- Implement `__init__` function;
Define all the required arguments that are **required** to instantiate the new resource. Optional parameters may be added as well.
- Implement `to_dict` function.

Check [ConfigMap](ocp_resources/configmap.py) and [Backup](ocp_resources/backup.py) for reference.
29 changes: 17 additions & 12 deletions ocp_resources/backup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import Any, List

from ocp_resources.resource import NamespacedResource

Expand All @@ -13,11 +15,11 @@ class Backup(NamespacedResource):

def __init__(
self,
included_namespaces=None,
excluded_resources=None,
snapshot_move_data=False,
storage_location=None,
**kwargs,
included_namespaces: List[str] | None = None,
excluded_resources: List[str] | None = None,
snapshot_move_data: bool = False,
storage_location: str = "",
**kwargs: Any,
):
"""
Args:
Expand All @@ -37,15 +39,18 @@ def __init__(

def to_dict(self) -> None:
super().to_dict()

if not self.yaml_file:
spec_dict = {}
spec_dict = self.res["spec"]

if self.included_namespaces:
spec_dict.update({"includedNamespaces": self.included_namespaces})
spec_dict["includedNamespaces"] = self.included_namespaces

if self.excluded_resources:
spec_dict.update({"excludedResources": self.excluded_resources})
spec_dict["excludedResources"] = self.excluded_resources

if self.snapshot_move_data:
spec_dict.update({"snapshotMoveData": self.snapshot_move_data})
spec_dict["snapshotMoveData"] = self.snapshot_move_data

if self.storage_location:
spec_dict.update({"storageLocation": self.storage_location})
if spec_dict:
self.res.update({"spec": spec_dict})
spec_dict["storageLocation"] = self.storage_location
11 changes: 8 additions & 3 deletions ocp_resources/configmap.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import annotations
from typing import Any, Dict

from ocp_resources.resource import NamespacedResource


Expand All @@ -10,17 +13,19 @@ class ConfigMap(NamespacedResource):

def __init__(
self,
data=None,
**kwargs,
data: Dict[str, Any] | None = None,
**kwargs: Any,
):
"""
Args:
data (dict, optional): key-value configuration pairs.
"""
super().__init__(**kwargs)

self.data = data

def to_dict(self) -> None:
super().to_dict()

if not self.yaml_file and self.data:
self.res.setdefault("data", {}).update(self.data)
self.res["data"] = self.data

0 comments on commit a223f3b

Please sign in to comment.