-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from OmegaVVeapon/fix/init-container
Support for METHOD env var LIST mode
- Loading branch information
Showing
10 changed files
with
309 additions
and
130 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import os | ||
from misc import get_required_env_var | ||
|
||
def label_is_satisfied(meta, **_): | ||
"""Runs the logic for LABEL and LABEL_VALUE and tells us if we need to watch the resource""" | ||
label = get_required_env_var('LABEL') | ||
label_value = os.getenv('LABEL_VALUE') | ||
|
||
# if there are no labels in the resource, there's no point in checking further | ||
if 'labels' not in meta: | ||
return False | ||
|
||
# If LABEL_VALUE wasn't set but we find the LABEL, that's good enough | ||
if label_value is None and label in meta['labels'].keys(): | ||
return True | ||
|
||
# If LABEL_VALUE was set, it needs to be the value of LABEL for one of the key-vars in the dict | ||
for key, value in meta['labels'].items(): | ||
if key == label and value == label_value: | ||
return True | ||
|
||
return False | ||
|
||
def resource_is_desired(body, **_): | ||
"""Runs the logic for the RESOURCE environment variable""" | ||
resource = os.getenv('RESOURCE', 'configmap') | ||
|
||
kind = body['kind'].lower() | ||
|
||
return resource in (kind, 'both') |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import os | ||
import pykube | ||
from io_helpers import write_file | ||
from conditions import label_is_satisfied | ||
from misc import log_env_vars, get_scope | ||
import logging | ||
|
||
def _get_configmaps(namespace): | ||
try: | ||
api = pykube.HTTPClient(pykube.KubeConfig.from_env()) | ||
configmaps = pykube.ConfigMap.objects(api).filter(namespace=namespace) | ||
return configmaps | ||
except pykube.exceptions.HTTPError as e: | ||
if e.code in [409]: | ||
pass | ||
else: | ||
raise | ||
|
||
def _get_secrets(namespace): | ||
try: | ||
api = pykube.HTTPClient(pykube.KubeConfig.from_env()) | ||
configmaps = pykube.Secret.objects(api).filter(namespace=namespace) | ||
return configmaps | ||
except pykube.exceptions.HTTPError as e: | ||
if e.code in [409]: | ||
pass | ||
else: | ||
raise | ||
|
||
def one_run(): | ||
"""Search through all the ConfigMaps and Secrets in the specified namespaces. If they meet the label requirements, | ||
copy the files to the destination. Update and delete operations not needed in this mode""" | ||
|
||
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
||
# Log some useful variables for troubleshooting | ||
log_env_vars(logger) | ||
|
||
scope = get_scope() | ||
|
||
resource = os.getenv('RESOURCE', 'configmap') | ||
|
||
if resource in ('configmap', 'both'): | ||
for namespace in scope['namespaces']: | ||
configmaps = _get_configmaps(namespace) | ||
for configmap in configmaps: | ||
if label_is_satisfied(configmap.obj['metadata']): | ||
write_file("create", configmap.obj, configmap.kind, logger) | ||
|
||
if resource in ('secret', 'both'): | ||
for namespace in scope['namespaces']: | ||
secrets = _get_secrets(namespace) | ||
for secret in secrets: | ||
if label_is_satisfied(secret.obj['metadata']): | ||
write_file("create", secret.obj, secret.kind, logger) |
Oops, something went wrong.