From 0408e5388fe794691078188f7f3b3dbcb99b7580 Mon Sep 17 00:00:00 2001
From: Michal Nasiadka <mnasiadka@gmail.com>
Date: Fri, 26 Mar 2021 10:15:38 +0000
Subject: [PATCH] Add --nocache option to container image build

Change-Id: I11f0677681e51a59bcbb171d8da0e14c853d6c5c
---
 ansible/container-image-build.yml             |  3 ++
 kayobe/cli/commands.py                        | 14 ++++++--
 kayobe/tests/unit/cli/test_commands.py        | 32 +++++++++++++++++++
 .../kolla-build-nocache-b21726b66543ee18.yaml |  5 +++
 4 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 releasenotes/notes/kolla-build-nocache-b21726b66543ee18.yaml

diff --git a/ansible/container-image-build.yml b/ansible/container-image-build.yml
index f8bfe69c5..6d462909f 100644
--- a/ansible/container-image-build.yml
+++ b/ansible/container-image-build.yml
@@ -4,6 +4,8 @@
   vars:
     # Set this to True to push images to the registry when built.
     push_images: False
+    # Set this to True to skip using cache.
+    nocache: False
     # Set this variable to a space-separated list of regexes to override the
     # default set of images.
     container_image_regexes: ""
@@ -52,6 +54,7 @@
           {% if item.type is defined %}--type {{ item.type }}{% endif %}
           {% if kolla_docker_registry is not none %}--registry {{ kolla_docker_registry }}{% endif %}
           {% if push_images | bool %}--push{% endif %}
+          {% if nocache | bool %}--nocache{% endif %}
           {{ item.regexes }} 2>&1 | tee --append {{ kolla_build_log_path }}
         executable: /bin/bash
       with_items: "{{ container_image_sets }}"
diff --git a/kayobe/cli/commands.py b/kayobe/cli/commands.py
index 94fb42eca..eca2243e0 100644
--- a/kayobe/cli/commands.py
+++ b/kayobe/cli/commands.py
@@ -760,6 +760,8 @@ class SeedContainerImageBuild(KayobeAnsibleMixin, VaultMixin, Command):
         parser = super(SeedContainerImageBuild, self).get_parser(
             prog_name)
         group = parser.add_argument_group("Container Image Build")
+        group.add_argument("--nocache", action="store_true",
+                           help="whether to not use cache")
         group.add_argument("--push", action="store_true",
                            help="whether to push images to a registry after "
                                 "building")
@@ -773,7 +775,10 @@ class SeedContainerImageBuild(KayobeAnsibleMixin, VaultMixin, Command):
         playbooks = _build_playbook_list(
             "container-image-builders-check", "kolla-build",
             "container-image-build")
-        extra_vars = {"push_images": parsed_args.push}
+        extra_vars = {
+            "nocache": parsed_args.nocache,
+            "push_images": parsed_args.push
+        }
         if parsed_args.regex:
             regexes = " ".join(parsed_args.regex)
             extra_vars["container_image_regexes"] = regexes
@@ -1483,6 +1488,8 @@ class OvercloudContainerImageBuild(KayobeAnsibleMixin, VaultMixin, Command):
         parser = super(OvercloudContainerImageBuild, self).get_parser(
             prog_name)
         group = parser.add_argument_group("Container Image Build")
+        group.add_argument("--nocache", action="store_true",
+                           help="whether to not use cache")
         group.add_argument("--push", action="store_true",
                            help="whether to push images to a registry after "
                                 "building")
@@ -1496,7 +1503,10 @@ class OvercloudContainerImageBuild(KayobeAnsibleMixin, VaultMixin, Command):
         playbooks = _build_playbook_list(
             "container-image-builders-check", "kolla-build",
             "container-image-build")
-        extra_vars = {"push_images": parsed_args.push}
+        extra_vars = {
+            "nocache": parsed_args.nocache,
+            "push_images": parsed_args.push
+        }
         if parsed_args.regex:
             regexes = " ".join(parsed_args.regex)
             extra_vars["container_image_regexes"] = regexes
diff --git a/kayobe/tests/unit/cli/test_commands.py b/kayobe/tests/unit/cli/test_commands.py
index 4339eb567..0ec4b45dd 100644
--- a/kayobe/tests/unit/cli/test_commands.py
+++ b/kayobe/tests/unit/cli/test_commands.py
@@ -694,6 +694,7 @@ class TestCase(unittest.TestCase):
                     "container_image_sets": (
                         "{{ seed_container_image_sets }}"),
                     "push_images": False,
+                    "nocache": False
                 }
             ),
         ]
@@ -720,6 +721,35 @@ class TestCase(unittest.TestCase):
                 extra_vars={
                     "container_image_regexes": "^regex1$ ^regex2$",
                     "push_images": True,
+                    "nocache": False
+                }
+            ),
+        ]
+        self.assertEqual(expected_calls, mock_run.call_args_list)
+
+    @mock.patch.object(commands.KayobeAnsibleMixin,
+                       "run_kayobe_playbooks")
+    def test_seed_container_image_build_with_nocache(self, mock_run):
+        command = commands.SeedContainerImageBuild(TestApp(), [])
+        parser = command.get_parser("test")
+        parsed_args = parser.parse_args(["--nocache"])
+        result = command.run(parsed_args)
+        self.assertEqual(0, result)
+        expected_calls = [
+            mock.call(
+                mock.ANY,
+                [
+                    utils.get_data_files_path(
+                        "ansible", "container-image-builders-check.yml"),
+                    utils.get_data_files_path("ansible", "kolla-build.yml"),
+                    utils.get_data_files_path(
+                        "ansible", "container-image-build.yml")
+                ],
+                extra_vars={
+                    "container_image_sets": (
+                        "{{ seed_container_image_sets }}"),
+                    "push_images": False,
+                    "nocache": True
                 }
             ),
         ]
@@ -1635,6 +1665,7 @@ class TestCase(unittest.TestCase):
                     "container_image_sets": (
                         "{{ overcloud_container_image_sets }}"),
                     "push_images": False,
+                    "nocache": False
                 }
             ),
         ]
@@ -1661,6 +1692,7 @@ class TestCase(unittest.TestCase):
                 extra_vars={
                     "container_image_regexes": "^regex1$ ^regex2$",
                     "push_images": True,
+                    "nocache": False
                 }
             ),
         ]
diff --git a/releasenotes/notes/kolla-build-nocache-b21726b66543ee18.yaml b/releasenotes/notes/kolla-build-nocache-b21726b66543ee18.yaml
new file mode 100644
index 000000000..c9a1c7cc9
--- /dev/null
+++ b/releasenotes/notes/kolla-build-nocache-b21726b66543ee18.yaml
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    Added new option (``--nocache``) to ``kayobe seed container image build``
+    and ``kayobe overcloud container image build`` to skip using build cache.