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

Feature: Added fetch_always functionality #2

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/external_dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ $ kapitan compile --fetch
This will download the dependencies and store them at their respective `output_path`.
By default, kapitan does not overwrite existing items with the same name as that of the fetched dependencies.

Use the `--force` flag to force fetch (update cache with freshly fetched dependencies) and overwrite any existing item sharing the same name in the `output_path`.
Use the `--force-fetch` flag to force fetch (update cache with freshly fetched dependencies) and overwrite any existing item sharing the same name in the `output_path`.

```
$ kapitan compile --fetch --force
$ kapitan compile --force-fetch
```

Use the `--cache` flag to cache the fetched items in the `.dependency_cache` directory in the root project directory.
Expand Down
4 changes: 2 additions & 2 deletions docs/inventory.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ $ kapitan compile --fetch
This will download the dependencies and store them at their respective `output_path`.
By default, kapitan does not overwrite an existing item with the same name as that of the fetched inventory items.

Use the `--force` flag to force fetch (update cache with freshly fetched items) and overwrite inventory items of the same name in the `output_path`.
Use the `--force-fetch` flag to force fetch (update cache with freshly fetched items) and overwrite inventory items of the same name in the `output_path`.

```
$ kapitan compile --fetch --force
$ kapitan compile --fetch --force-fetch
```

Use the `--cache` flag to cache the fetched items in the `.dependency_cache` directory in the root project directory.
Expand Down
5 changes: 3 additions & 2 deletions docs/kap_proposals/kap_1_external_dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Git type may also include `ref` and `subdir` parameters as illustrated below:
source: <git_url>
subdir: relative/path/in/repository
ref: <commit_hash/branch/tag>
force_fetch: <bool>
```

If the file already exists at `output_path`, the fetch will be skipped. For fresh fetch of the dependencies, users may add `--fetch` option as follows:
Expand All @@ -35,12 +36,12 @@ If the file already exists at `output_path`, the fetch will be skipped. For fres
$ kapitan compile --fetch
```

Users can also add the `fetch_always: true` option to the `kapitan.compile` in the inventory in order to force fresh fetch of the dependencies every time.
Users can also add the `force_fetch: true` option to the `kapitan.dependencies` in the inventory in order to force fetch of the dependencies of the target every time.

## Implementation details

### Dependencies

- [GitPython](https://github.com/gitpython-developers/GitPython) module (and git executable) for git type
- requests module for http[s]
- (optional) [tqdm](https://github.com/tqdm/tqdm) for reporting download progress
- (optional) [tqdm](https://github.com/tqdm/tqdm) for reporting download progress
4 changes: 2 additions & 2 deletions docs/kap_proposals/kap_7_remote_inventory.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ eg. `$ kapitan compile --fetch --cache`
## Force fetching
While fetching, the output path will be recursively checked to see if it contains any file with the same name. If so, kapitan will skip fetching it.

To overwrite the files with the newly downloaded inventory items, we can add the `--force` flag to the compile command, as shown below.
To overwrite the files with the newly downloaded inventory items, we can add the `--force-fetch` flag to the compile command, as shown below.

`$ kapitan compile --fetch --force`
`$ kapitan compile --force-fetch`

## URL type
The URL type can be either git or http(s). Depending on the URL type, the configuration file may have additional arguments.
Expand Down
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ optional arguments:
--quiet set quiet mode, only critical output
--output-path PATH set output path, default is "."
--fetch fetch remote inventories and/or external dependencies
--force overwrite existing inventory and/or dependency item
--force-fetch overwrite existing inventory and/or dependency item
--validate validate compile output against schemas as specified
in inventory
--parallelism INT, -p INT
Expand Down
12 changes: 9 additions & 3 deletions kapitan/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def trigger_compile(args):
reveal=args.reveal,
cache=args.cache,
cache_paths=args.cache_paths,
fetch_inventories=args.fetch,
fetch_dependencies=args.fetch,
force_fetch=args.force,
fetch=args.fetch,
force_fetch=args.force_fetch,
force=args.force, # deprecated
validate=args.validate,
schemas_path=args.schemas_path,
jinja2_filters=args.jinja2_filters,
Expand Down Expand Up @@ -184,6 +184,12 @@ def build_parser():
default=from_dot_kapitan("compile", "fetch", False),
)
compile_parser.add_argument(
"--force-fetch",
help="overwrite existing inventory and/or dependency item",
action="store_true",
default=from_dot_kapitan("compile", "force-fetch", False),
)
compile_parser.add_argument( # deprecated
"--force",
help="overwrite existing inventory and/or dependency item",
action="store_true",
Expand Down
49 changes: 42 additions & 7 deletions kapitan/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,19 @@ def compile_targets(

try:
rendering_start = time.time()
if kwargs.get("fetch_inventories", False):

# check if --fetch or --force-fetch is enabled
force_fetch = kwargs.get("force_fetch", False)
fetch = kwargs.get("fetch", False) or force_fetch

# deprecated --force flag
if kwargs.get("force", False):
logger.info(
"DeprecationWarning: --force is deprecated. Use --force-fetch instead of --force --fetch"
)
force_fetch = True

if fetch:
# skip classes that are not yet available
target_objs = load_target_inventory(inventory_path, updated_targets, ignore_class_notfound=True)
else:
Expand All @@ -93,15 +105,16 @@ def compile_targets(
if not target_objs:
raise CompileError("Error: no targets found")

if kwargs.get("fetch_inventories", False):
# fetch inventory
if fetch:
# new_source checks for new sources in fetched inventory items
new_sources = list(set(list_sources(target_objs)) - cached.inv_sources)
while new_sources:
fetch_inventories(
inventory_path,
target_objs,
dep_cache_dir,
kwargs.get("force_fetch", False),
force_fetch,
pool,
)
cached.reset_inv()
Expand All @@ -113,10 +126,29 @@ def compile_targets(
# reset inventory cache and load target objs to check for missing classes
cached.reset_inv()
target_objs = load_target_inventory(inventory_path, updated_targets, ignore_class_notfound=False)
if kwargs.get("fetch_dependencies", False):
fetch_dependencies(
output_path, target_objs, dep_cache_dir, kwargs.get("force_fetch", False), pool
)
# fetch dependencies
if fetch:
fetch_dependencies(output_path, target_objs, dep_cache_dir, force_fetch, pool)
# fetch targets which have force_fetch: true
elif not kwargs.get("force_fetch", False):
fetch_objs = []
# iterate through targets
for target in target_objs:
try:
# get value of "force_fetch" property
dependencies = target["dependencies"]
# dependencies is still a list
for entry in dependencies:
force_fetch = entry["force_fetch"]
if force_fetch:
fetch_objs.append(target)
except KeyError:
# targets may have no "dependencies" or "force_fetch" key
continue
# fetch dependencies from targets with force_fetch set to true
if fetch_objs:
fetch_dependencies(output_path, fetch_objs, dep_cache_dir, True, pool)

logger.info("Rendered inventory (%.2fs)", time.time() - rendering_start)

worker = partial(
Expand Down Expand Up @@ -620,6 +652,7 @@ def valid_target_obj(target_obj, require_compile=True):
"ref": {"type": "string"},
"unpack": {"type": "boolean"},
"version": {"type": "string"},
"force_fetch": {"type": "boolean"},
},
"required": ["type", "output_path", "source"],
"additionalProperties": False,
Expand All @@ -632,6 +665,7 @@ def valid_target_obj(target_obj, require_compile=True):
"source": {"format": "uri"},
"output_path": {},
"unpack": {},
"force_fetch": {},
},
"additionalProperties": False,
},
Expand All @@ -646,6 +680,7 @@ def valid_target_obj(target_obj, require_compile=True):
"unpack": {},
"chart_name": {"type": "string"},
"version": {"type": "string"},
"force_fetch": {},
},
"required": ["type", "output_path", "source", "chart_name"],
"additionalProperties": False,
Expand Down
12 changes: 5 additions & 7 deletions tests/test_remote_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_unpack_http_inv(self):
rmtree(output_dir)

def test_compile_fetch(self):
"""Run $ kapitan compile --fetch --output-path=some/dir/ --inventory-path=another/dir --targets remoteinv-example remoteinv-nginx zippedinv --force
"""Run $ kapitan compile --force-fetch --output-path=some/dir/ --inventory-path=another/dir --targets remoteinv-example remoteinv-nginx zippedinv
were some/dir/ & another/dir/ are directories chosen by the user

Recursively force fetch inventories and compile
Expand All @@ -110,7 +110,7 @@ def test_compile_fetch(self):
sys.argv = [
"kapitan",
"compile",
"--fetch",
"--force-fetch",
"--output-path",
temp_output,
"--inventory-path",
Expand All @@ -119,7 +119,6 @@ def test_compile_fetch(self):
"remoteinv-example",
"remoteinv-nginx",
"zippedinv",
"--force",
]
main()

Expand Down Expand Up @@ -159,10 +158,10 @@ def test_compile_fetch_classes_that_doesnot_exist_yet(self):
rmtree(temp_dir)

def test_force_fetch(self):
"""Test overwriting inventory items while using the --force flag
"""Test overwriting inventory items while using the --force-fetch flag

runs $ kapitan compile --fetch --cache --output-path=temp_output --inventory-pat=temp_inv --targets remoteinv-example
runs $ kapitan compile --fetch --cache --output-path=temp_output --inventory-pat=temp_inv --targets remoteinv-example --force
runs $ kapitan compile --force-fetch --cache --output-path=temp_output --inventory-pat=temp_inv --targets remoteinv-example
"""

temp_output = tempfile.mkdtemp()
Expand Down Expand Up @@ -205,15 +204,14 @@ def test_force_fetch(self):
sys.argv = [
"kapitan",
"compile",
"--fetch",
"--force-fetch",
"--cache",
"--output-path",
temp_output,
"--inventory-path",
temp_inv,
"--targets",
"remoteinv-example",
"--force",
]
main()
# zippedinv.yml overwritten
Expand Down