Skip to content

Commit

Permalink
feat(apply): allow mutiple values and secret files
Browse files Browse the repository at this point in the history
This commit adds the support for specifying multiple secret and value
files when applying or deleting. The values and secrets are merged into
one object in the order they are defined.
  • Loading branch information
pallabpain committed Oct 14, 2024
1 parent c068fd0 commit fe0dac9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
12 changes: 6 additions & 6 deletions riocli/apply/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
help='Dry run the yaml files without applying any change')
@click.option('--show-graph', '-g', is_flag=True, default=False,
help='Opens a mermaid.live dependency graph')
@click.option('--values', '-v',
@click.option('--values', '-v', multiple=True, default=(),
help="Path to values yaml file. Key/values "
"specified in the values file can be "
"used as variables in template YAMLs")
@click.option('--secrets', '-s',
@click.option('--secrets', '-s', multiple=True, default=(),
help="Secret files are sops encoded value files. "
"rio-cli expects sops to be authorized for "
"decoding files on this computer")
Expand All @@ -55,8 +55,8 @@
help="Interval between retries defaults to 6")
@click.argument('files', nargs=-1)
def apply(
values: str,
secrets: str,
values: Iterable[str],
secrets: Iterable[str],
files: Iterable[str],
retry_count: int = 50,
retry_interval: int = 6,
Expand Down Expand Up @@ -144,10 +144,10 @@ def apply(
)
@click.option('--dryrun', '-d', is_flag=True, default=False,
help='Dry run the yaml files without applying any change')
@click.option('--values', '-v',
@click.option('--values', '-v', multiple=True, default=(),
help="Path to values yaml file. key/values specified in the"
" values file can be used as variables in template YAMLs")
@click.option('--secrets', '-s',
@click.option('--secrets', '-s', multiple=True, default=(),
help="Secret files are sops encoded value files. riocli expects "
"sops to be authorized for decoding files on this computer")
@click.option('-f', '--force', '--silent', 'silent', is_flag=True,
Expand Down
31 changes: 17 additions & 14 deletions riocli/apply/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,16 @@ class Applier(object):
DEFAULT_MAX_WORKERS = 6
DELETE_POLICY_LABEL = 'rapyuta.io/deletionPolicy'

def __init__(self, files: typing.List, values, secrets):
def __init__(self, files: typing.List, values: typing.List, secrets: typing.List):
self.files = {}
self.values = {}
self.secrets = {}
self.objects = {}
self.resolved_objects = {}
self.input_file_paths = files
self.config = Configuration()
self.graph = TopologicalSorter()
self.environment = init_jinja_environment()
self.diagram = Graphviz(direction='LR', format='svg')

if values:
self.values = self._load_file_content(
values, is_value=True, is_secret=False)[0]

self.values = self._inject_rio_namespace(self.values)

if secrets:
self.secrets = self._load_file_content(
secrets, is_value=True, is_secret=True)[0]

self._process_values_and_secrets(values, secrets)
self._process_file_list(files)

def print_resolved_manifests(self):
Expand Down Expand Up @@ -472,3 +460,18 @@ def _inject_rio_namespace(self, values: typing.Optional[dict] = None) -> dict:
values['rio'] = rio

return values

def _process_values_and_secrets(self, values: typing.List, secrets: typing.List) -> None:
"""Process the values and secrets files and inject them into the manifest files"""
self.values, self.secrets = {}, {}

values = values or []
secrets = secrets or []

for v in values:
self.values.update(self._load_file_content(v, is_value=True)[0])

self.values = self._inject_rio_namespace(self.values)

for s in secrets:
self.secrets.update(self._load_file_content(s, is_secret=True)[0])
27 changes: 18 additions & 9 deletions riocli/apply/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import typing
from datetime import datetime
from shutil import get_terminal_size
from typing import Iterable

import click
import jinja2
Expand Down Expand Up @@ -84,7 +85,11 @@ def parse_variadic_path_args(path_item):
return glob.glob(abs_path, recursive=True)


def process_files_values_secrets(files, values, secrets):
def process_files_values_secrets(
files: Iterable[str],
values: Iterable[str],
secrets: Iterable[str],
):
glob_files = []

for path_item in files:
Expand All @@ -93,17 +98,21 @@ def process_files_values_secrets(files, values, secrets):
f for f in path_glob if os.path.isfile(f)
])

# Remove value files from template files list.
abs_values = values
if values and values != "":
abs_values = os.path.abspath(values)
if abs_values in glob_files:
glob_files.remove(abs_values)
if values:
for v in values:
abs_v = os.path.abspath(v)
if abs_v in glob_files:
glob_files.remove(abs_v)

# Remove secret files from template files list.
abs_secret = secrets
if secrets and secrets != "":
abs_secrets = os.path.abspath(secrets)
if abs_secrets in glob_files:
glob_files.remove(abs_secrets)
if secrets:
for s in secrets:
abs_s = os.path.abspath(s)
if abs_s in glob_files:
glob_files.remove(abs_s)

glob_files = sorted(list(set(glob_files)))
return glob_files, abs_values, abs_secret
Expand Down
4 changes: 2 additions & 2 deletions riocli/chart/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
@click.option('-f', '--force', '--silent', 'silent', is_flag=True,
type=click.BOOL, default=False,
help="Skip confirmation")
@click.option('--values', '-v',
@click.option('--values', '-v', multiple=True, default=(),
help="Path to values yaml file. key/values specified in the "
"values file can be used as variables in template yamls")
@click.option('--secrets', '-s',
@click.option('--secrets', '-s', multiple=True, default=(),
help="Secret files are sops encoded value files. rio-cli "
"expects sops to be authorized for decoding files on "
"this computer")
Expand Down
4 changes: 2 additions & 2 deletions riocli/chart/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
help='Dry run the yaml files without applying any change')
@click.option('-f', '--force', '--silent', 'silent', is_flag=True,
type=click.BOOL, default=False, help="Skip confirmation")
@click.option('--values', '-v',
@click.option('--values', '-v', multiple=True, default=(),
help=("Path to values yaml file. key/values specified in the"
"values file can be used as variables in template yamls"))
@click.option('--secrets', '-s',
@click.option('--secrets', '-s', multiple=True, default=(),
help=("Secret files are sops encoded value files. rio-cli "
"expects sops to be authorized for decoding files on "
"this computer"))
Expand Down

0 comments on commit fe0dac9

Please sign in to comment.