This repository has been archived by the owner on Sep 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Update the EphemeralVolumeClaim story-telling #97
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9d58db
Fix the alignment of code samples, remove an extra space on each line
3a90cef
Fix the code issues in the EphemeralVolumeClaim operator in docs
f14ff1e
Decrease the size of the volumes - for easier local experimentation
9d750c5
Show the actual output of some commands and operator's actions
2dacc44
Warn that Minikube cannot handle the PVC/PV resizes properly
8c54ea9
Refer to the full implementation of EphemeralVolumeClaim operator
5d8466e
Mention that `kubernetes` must be installed before use
nolar cfd8296
Fix the imports (as per PR comments)
nolar 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,24 +30,25 @@ __ https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/ | |
Let's extend the creation handler: | ||
|
||
.. code-block:: python | ||
:name: adopting | ||
:linenos: | ||
:caption: ephemeral.py | ||
:emphasize-lines: 18 | ||
:name: adopting | ||
:linenos: | ||
:caption: ephemeral.py | ||
:emphasize-lines: 18 | ||
|
||
import os | ||
import kopf | ||
import kubernetes | ||
import yaml | ||
|
||
@kopf.on.create('zalando.org', 'v1', 'ephemeralvolumeclaims') | ||
def create_fn(meta, spec, namespace, logger, **kwargs): | ||
def create_fn(meta, spec, namespace, logger, body, **kwargs): | ||
|
||
name = meta.get('name') | ||
size = spec.get('size') | ||
if not size: | ||
raise kopf.PermanentError(f"Size must be set. Got {size!r}.") | ||
|
||
path = os.path.join(os.path.dirname(__file__), 'pvc-tpl.yaml') | ||
path = os.path.join(os.path.dirname(__file__), 'pvc.yaml') | ||
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.
|
||
tmpl = open(path, 'rt').read() | ||
text = tmpl.format(name=name, size=size) | ||
data = yaml.safe_load(text) | ||
|
@@ -62,7 +63,9 @@ Let's extend the creation handler: | |
|
||
logger.info(f"PVC child is created: %s", obj) | ||
|
||
With this one line, `kopf.adopt` marks the PVC as child of EVC. | ||
return {'pvc-name': obj.metadata.name} | ||
|
||
With this one line, `kopf.adopt` marks the PVC as a child of EVC. | ||
This includes: the name auto-generation (if absent), the label propagation, | ||
the namespace assignment to the parent's object namespace, | ||
and, finally, the owner referencing. | ||
|
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 |
---|---|---|
|
@@ -2,23 +2,18 @@ | |
Updating the objects | ||
==================== | ||
|
||
.. warning:: | ||
Unfortunately, Minikube cannot handle the PVC/PV resizing, | ||
as it uses the HostPath provider internally. | ||
You can either skip this step of the tutorial, | ||
or you can use an external Kubernetes cluster | ||
with real dynamically sized volumes. | ||
|
||
Previously (:doc:`creation`), | ||
we have implemented a handler for the creation of an ``EphemeralVolumeClaim`` (EVC), | ||
and created the corresponding ``PersistantVolumeClaim`` (PVC). | ||
|
||
What will happen if we change the size of the EVC when it already exists? | ||
E.g., with: | ||
|
||
.. code-block:: bash | ||
|
||
kubectl edit evc my-claim | ||
|
||
Or by patching it: | ||
|
||
.. code-block:: bash | ||
|
||
kubectl patch evc my-claim -p '{"spec": {"resources": {"requests": {"storage": "100G"}}}}' | ||
|
||
The PVC must be updated accordingly to match its parent EVC. | ||
|
||
First, we have to remember the name of the created PVC: | ||
|
@@ -28,17 +23,17 @@ with one additional line: | |
.. code-block:: python | ||
:linenos: | ||
:caption: ephemeral.py | ||
:emphasize-lines: 23 | ||
:emphasize-lines: 24 | ||
|
||
@kopf.on.create('zalando.org', 'v1', 'ephemeralvolumeclaims') | ||
def create_fn(spec, meta, namespace, logger, **kwargs): | ||
def create_fn(body, spec, meta, namespace, logger, **kwargs): | ||
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. don't need |
||
|
||
name = meta.get('name') | ||
size = spec.get('size') | ||
if not size: | ||
raise kopf.PermanentError(f"Size must be set. Got {size!r}.") | ||
|
||
path = os.path.join(os.path.dirname(__file__), 'pvc-tpl.yaml') | ||
path = os.path.join(os.path.dirname(__file__), 'pvc.yaml') | ||
tmpl = open(path, 'rt').read() | ||
text = tmpl.format(size=size, name=name) | ||
data = yaml.safe_load(text) | ||
|
@@ -61,11 +56,16 @@ We can see that with kubectl: | |
|
||
.. code-block:: bash | ||
|
||
kubectl describe evc my-claim | ||
kubectl get -o yaml evc my-claim | ||
|
||
.. code-block:: none | ||
.. code-block:: yaml | ||
|
||
TODO | ||
spec: | ||
size: 1G | ||
status: | ||
create_fn: | ||
pvc-name: my-claim | ||
kopf: {} | ||
|
||
Let's add a yet another handler, but for the "update" cause. | ||
This handler gets this stored PVC name from the creation handler, | ||
|
@@ -74,11 +74,11 @@ and patches the PVC with the new size from the EVC:: | |
@kopf.on.update('zalando.org', 'v1', 'ephemeralvolumeclaims') | ||
def update_fn(spec, status, namespace, logger, **kwargs): | ||
|
||
size = spec.get('create_fn', {}).get('size', None) | ||
size = spec.get('size', None) | ||
if not size: | ||
raise kopf.PermanentError(f"Size must be set. Got {size!r}.") | ||
|
||
pvc_name = status['pvc-name'] | ||
pvc_name = status['create_fn']['pvc-name'] | ||
pvc_patch = {'spec': {'resources': {'requests': {'storage': size}}}} | ||
|
||
api = kubernetes.client.CoreV1Api() | ||
|
@@ -89,3 +89,36 @@ and patches the PVC with the new size from the EVC:: | |
) | ||
|
||
logger.info(f"PVC child is updated: %s", obj) | ||
|
||
Now, let's change the EVC's size: | ||
|
||
.. code-block:: bash | ||
|
||
kubectl edit evc my-claim | ||
|
||
Or by patching it: | ||
|
||
.. code-block:: bash | ||
|
||
kubectl patch evc my-claim --type merge -p '{"spec": {"size": "2G"}}' | ||
|
||
Keep in mind the PVC size can only be increased, never decreased. | ||
|
||
Give the operator few seconds to handle the change. | ||
|
||
Check the size of the actual PV behind the PVC, which is now increased: | ||
|
||
.. code-block:: bash | ||
|
||
kubectl get pv | ||
|
||
.. code-block:: none | ||
|
||
NAME CAPACITY ACCESS MODES ... | ||
pvc-a37b65bd-8384-11e9-b857-42010a800265 2Gi RWO ... | ||
|
||
.. warning:: | ||
Kubernetes & ``kubectl`` improperly show the capacity of PVCs: | ||
it remains the same (1G) event after the change. | ||
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.
|
||
The size of actual PV (Persistent Volume) of each PVC is important! | ||
This issue is not related to Kopf, so we go around it. |
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.
Nitpick: IMO it might be worth a note
pip install kubernetes
.