From ed9fb196f901e43f63ab94d5a2b17aadb3205526 Mon Sep 17 00:00:00 2001 From: Bikouo Aubin <79859644+abikouo@users.noreply.github.com> Date: Fri, 12 Aug 2022 15:56:19 +0200 Subject: [PATCH] Openshift Build modules (#134) * Openshift Build modules: Start, Prune * add openshift adm prune builds to ansible-test * fix sanity for downstream * sanity ignore * Remove 2.6 sanity ignores for ansible 2.13 and 2.14 * update code review * update 2 - code review --- molecule/default/tasks/openshift_builds.yml | 245 ++++++++++ molecule/default/verify.yml | 1 + plugins/module_utils/openshift_builds.py | 427 ++++++++++++++++++ plugins/modules/openshift_adm_prune_builds.py | 136 ++++++ plugins/modules/openshift_build.py | 271 +++++++++++ tests/sanity/ignore-2.11.txt | 18 + tests/sanity/ignore-2.12.txt | 18 + tests/sanity/ignore-2.13.txt | 12 + tests/sanity/ignore-2.14.txt | 12 + 9 files changed, 1140 insertions(+) create mode 100644 molecule/default/tasks/openshift_builds.yml create mode 100644 plugins/module_utils/openshift_builds.py create mode 100644 plugins/modules/openshift_adm_prune_builds.py create mode 100644 plugins/modules/openshift_build.py diff --git a/molecule/default/tasks/openshift_builds.yml b/molecule/default/tasks/openshift_builds.yml new file mode 100644 index 0000000..b564f8b --- /dev/null +++ b/molecule/default/tasks/openshift_builds.yml @@ -0,0 +1,245 @@ +- block: + - set_fact: + build_ns: "builds" + build_config: "start-build" + is_name: "ruby" + prune_build: "prune-build" + + - name: Ensure namespace + kubernetes.core.k8s: + kind: Namespace + name: "{{ build_ns }}" + + - name: Create ImageStream + community.okd.k8s: + namespace: "{{ build_ns }}" + definition: + apiVersion: image.openshift.io/v1 + kind: ImageStream + metadata: + name: "{{ is_name }}" + spec: + lookupPolicy: + local: false + tags: [] + + - name: Create build configuration + community.okd.k8s: + namespace: "{{ build_ns }}" + definition: + kind: BuildConfig + apiVersion: build.openshift.io/v1 + metadata: + name: "{{ build_config }}" + spec: + source: + dockerfile: | + FROM openshift/ruby-22-centos7 + RUN sleep 60s + USER ansible + strategy: + type: Docker + output: + to: + kind: "ImageStreamTag" + name: "{{ is_name }}:latest" + + - name: Start Build from Build configuration + community.okd.openshift_build: + namespace: "{{ build_ns }}" + build_config_name: "{{ build_config }}" + register: new_build + + - name: Assert that a build has been created + assert: + that: + - new_build is changed + - new_build.builds.0.metadata.name == "{{ build_config }}-1" + + - name: Start a new Build from previous Build + community.okd.openshift_build: + namespace: "{{ build_ns }}" + build_name: "{{ new_build.builds.0.metadata.name }}" + register: rerun_build + + - name: Assert that another build has been created + assert: + that: + - rerun_build is changed + - rerun_build.builds.0.metadata.name == "{{ build_config }}-2" + + - name: Cancel first build created + community.okd.openshift_build: + namespace: "{{ build_ns }}" + build_name: "{{ build_config }}-1" + state: cancelled + wait: yes + register: cancel + + - name: Assert that the Build was cancelled + assert: + that: + - cancel is changed + - cancel.builds | length == 1 + - cancel.builds.0.metadata.name == "{{ build_config }}-1" + - cancel.builds.0.metadata.namespace == "{{ build_ns }}" + - cancel.builds.0.status.cancelled + + - name: Get Build info + kubernetes.core.k8s_info: + version: build.openshift.io/v1 + kind: Build + namespace: "{{ build_ns }}" + name: "{{ cancel.builds.0.metadata.name }}" + register: build + + - name: Assert that build phase is cancelled + assert: + that: + - build.resources | length == 1 + - build.resources.0.status.cancelled + - build.resources.0.status.phase == 'Cancelled' + + - name: Cancel and restart Build using build config name + community.okd.openshift_build: + namespace: "{{ build_ns }}" + build_config_name: "{{ build_config }}" + state: restarted + build_phases: + - Running + - New + register: restart + + - name: assert that new build was created + assert: + that: + - restart is changed + - restart.builds | length == 1 + - 'restart.builds.0.metadata.name == "{{ build_config }}-3"' + + - name: Get Build 2 info + kubernetes.core.k8s_info: + version: build.openshift.io/v1 + kind: Build + namespace: "{{ build_ns }}" + name: "{{ build_config }}-2" + register: build + + - name: Assert that build phase is cancelled + assert: + that: + - build.resources | length == 1 + - build.resources.0.status.cancelled + - build.resources.0.status.phase == 'Cancelled' + + - name: Get Build info + kubernetes.core.k8s_info: + version: build.openshift.io/v1 + kind: Build + namespace: "{{ build_ns }}" + name: "{{ build_config }}-3" + register: build + + - name: Assert that Build is not cancelled + assert: + that: + - build.resources | length == 1 + - '"cancelled" not in build.resources.0.status' + - "build.resources.0.status.phase in ('New', 'Pending', 'Running')" + + - name: Prune Builds keep younger than 30min + community.okd.openshift_adm_prune_builds: + keep_younger_than: 30 + namespace: "{{ build_ns }}" + register: prune + check_mode: yes + + - name: Assert that no Builds were found + assert: + that: + - not prune.changed + - prune.builds | length == 0 + + - name: Prune Builds without namespace + community.okd.openshift_adm_prune_builds: + register: prune_without_ns + check_mode: yes + + - name: Assert that completed build are candidate for prune + assert: + that: + - prune_without_ns is changed + - prune_without_ns.builds | length > 0 + - '"{{ build_config }}-1" in build_names' + - '"{{ build_config }}-2" in build_names' + vars: + build_names: '{{ prune_without_ns.builds | map(attribute="metadata") | flatten | map(attribute="name") | list }}' + + - name: Prune Builds using namespace + community.okd.openshift_adm_prune_builds: + namespace: "{{ build_ns }}" + register: prune_with_ns + check_mode: yes + + - name: Assert that prune operation found the completed build + assert: + that: + - prune_with_ns is changed + - prune_with_ns.builds | length == 2 + + - name: Check Build before prune + kubernetes.core.k8s_info: + kind: Build + api_version: build.openshift.io/v1 + name: "{{ build_config }}-1" + namespace: "{{ build_ns }}" + register: resource + + - name: Validate that any previous build operation executed with check_mode did not deleted the build + assert: + that: + - resource.resources | length == 1 + + - name: Execute prune operation + community.okd.openshift_adm_prune_builds: + namespace: "{{ build_ns }}" + register: prune + + - name: assert prune is changed + assert: + that: + - prune is changed + + - name: Check Build + kubernetes.core.k8s_info: + kind: Build + api_version: build.openshift.io/v1 + name: "{{ build_config }}-1" + namespace: "{{ build_ns }}" + register: resource + + - name: Assert that the Build does not exist anymore + assert: + that: + - resource.resources | length == 0 + + - name: Check Build + kubernetes.core.k8s_info: + kind: Build + api_version: build.openshift.io/v1 + name: "{{ build_config }}-2" + namespace: "{{ build_ns }}" + register: resource + + - name: Assert that the Build does not exist anymore + assert: + that: + - resource.resources | length == 0 + + always: + - name: Ensure namespace is deleted + kubernetes.core.k8s: + state: absent + kind: Namespace + name: "{{ build_ns }}" + ignore_errors: true diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml index 6bf02d6..b787062 100644 --- a/molecule/default/verify.yml +++ b/molecule/default/verify.yml @@ -64,6 +64,7 @@ - import_tasks: tasks/openshift_adm_prune_auth_clusterroles.yml - import_tasks: tasks/openshift_adm_prune_auth_roles.yml - import_tasks: tasks/openshift_adm_prune_deployments.yml + - import_tasks: tasks/openshift_builds.yml - import_tasks: tasks/openshift_route.yml - import_tasks: tasks/openshift_import_images.yml - import_tasks: tasks/openshift_prune_images.yml diff --git a/plugins/module_utils/openshift_builds.py b/plugins/module_utils/openshift_builds.py new file mode 100644 index 0000000..cac05ee --- /dev/null +++ b/plugins/module_utils/openshift_builds.py @@ -0,0 +1,427 @@ +#!/usr/bin/env python + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from datetime import datetime, timezone, timedelta +import traceback +import time + +from ansible.module_utils._text import to_native + +try: + from ansible_collections.kubernetes.core.plugins.module_utils.common import ( + K8sAnsibleMixin, + get_api_client, + ) + HAS_KUBERNETES_COLLECTION = True +except ImportError as e: + HAS_KUBERNETES_COLLECTION = False + k8s_collection_import_exception = e + K8S_COLLECTION_ERROR = traceback.format_exc() + +try: + from kubernetes.dynamic.exceptions import DynamicApiError +except ImportError as e: + pass + + +class OpenShiftBuilds(K8sAnsibleMixin): + def __init__(self, module): + self.module = module + self.fail_json = self.module.fail_json + self.exit_json = self.module.exit_json + + if not HAS_KUBERNETES_COLLECTION: + self.module.fail_json( + msg="The kubernetes.core collection must be installed", + exception=K8S_COLLECTION_ERROR, + error=to_native(k8s_collection_import_exception), + ) + + super(OpenShiftBuilds, self).__init__(self.module) + + self.params = self.module.params + self.check_mode = self.module.check_mode + self.client = get_api_client(self.module) + + def get_build_config(self, name, namespace): + params = dict( + kind="BuildConfig", + api_version="build.openshift.io/v1", + name=name, + namespace=namespace, + ) + result = self.kubernetes_facts(**params) + return result["resources"] + + def prune(self): + # list replicationcontroller candidate for pruning + kind = 'Build' + api_version = 'build.openshift.io/v1' + resource = self.find_resource(kind=kind, api_version=api_version, fail=True) + + self.max_creation_timestamp = None + keep_younger_than = self.params.get("keep_younger_than") + if keep_younger_than: + now = datetime.now(timezone.utc).replace(tzinfo=None) + self.max_creation_timestamp = now - timedelta(minutes=keep_younger_than) + + def _prunable_build(build): + return build["status"]["phase"] in ("Complete", "Failed", "Error", "Cancelled") + + def _orphan_build(build): + if not _prunable_build(build): + return False + + config = build["status"].get("config", None) + if not config: + return True + build_config = self.get_build_config(config["name"], config["namespace"]) + return len(build_config) == 0 + + def _younger_build(build): + if not self.max_creation_timestamp: + return False + creation_timestamp = datetime.strptime(build['metadata']['creationTimestamp'], '%Y-%m-%dT%H:%M:%SZ') + return creation_timestamp < self.max_creation_timestamp + + predicates = [ + _prunable_build, + ] + if self.params.get("orphans"): + predicates.append(_orphan_build) + if self.max_creation_timestamp: + predicates.append(_younger_build) + + # Get ReplicationController + params = dict( + kind=kind, + api_version=api_version, + namespace=self.params.get("namespace"), + ) + result = self.kubernetes_facts(**params) + candidates = result["resources"] + for pred in predicates: + candidates = list(filter(pred, candidates)) + + if self.check_mode: + changed = len(candidates) > 0 + self.exit_json(changed=changed, builds=candidates) + + changed = False + for build in candidates: + changed = True + try: + name = build["metadata"]["name"] + namespace = build["metadata"]["namespace"] + resource.delete(name=name, namespace=namespace, body={}) + except DynamicApiError as exc: + msg = "Failed to delete Build %s/%s due to: %s" % (namespace, name, exc.body) + self.fail_json(msg=msg, status=exc.status, reason=exc.reason) + except Exception as e: + msg = "Failed to delete Build %s/%s due to: %s" % (namespace, name, to_native(e)) + self.fail_json(msg=msg, error=to_native(e), exception=e) + self.exit_json(changed=changed, builds=candidates) + + def clone_build(self, name, namespace, request): + try: + result = self.client.request( + method="POST", + path="/apis/build.openshift.io/v1/namespaces/{namespace}/builds/{name}/clone".format( + namespace=namespace, + name=name + ), + body=request, + content_type="application/json", + ) + return result.to_dict() + except DynamicApiError as exc: + msg = "Failed to clone Build %s/%s due to: %s" % (namespace, name, exc.body) + self.fail_json(msg=msg, status=exc.status, reason=exc.reason) + except Exception as e: + msg = "Failed to clone Build %s/%s due to: %s" % (namespace, name, to_native(e)) + self.fail_json(msg=msg, error=to_native(e), exception=e) + + def instantiate_build_config(self, name, namespace, request): + try: + result = self.client.request( + method="POST", + path="/apis/build.openshift.io/v1/namespaces/{namespace}/buildconfigs/{name}/instantiate".format( + namespace=namespace, + name=name + ), + body=request, + content_type="application/json", + ) + return result.to_dict() + except DynamicApiError as exc: + msg = "Failed to instantiate BuildConfig %s/%s due to: %s" % (namespace, name, exc.body) + self.fail_json(msg=msg, status=exc.status, reason=exc.reason) + except Exception as e: + msg = "Failed to instantiate BuildConfig %s/%s due to: %s" % (namespace, name, to_native(e)) + self.fail_json(msg=msg, error=to_native(e), exception=e) + + def start_build(self): + + result = None + name = self.params.get("build_config_name") + if not name: + name = self.params.get("build_name") + + build_request = { + "kind": "BuildRequest", + "apiVersion": "build.openshift.io/v1", + "metadata": { + "name": name + }, + "triggeredBy": [ + {"message": "Manually triggered"} + ], + } + + # Overrides incremental + incremental = self.params.get("incremental") + if incremental is not None: + build_request.update( + { + "sourceStrategyOptions": { + "incremental": incremental + } + } + ) + + # Environment variable + if self.params.get("env_vars"): + build_request.update( + { + "env": self.params.get("env_vars") + } + ) + + # Docker strategy option + if self.params.get("build_args"): + build_request.update( + { + "dockerStrategyOptions": { + "buildArgs": self.params.get("build_args") + }, + } + ) + + # caching option + no_cache = self.params.get("no_cache") + if no_cache is not None: + build_request.update( + { + "dockerStrategyOptions": { + "noCache": no_cache + }, + } + ) + + # commit + if self.params.get("commit"): + build_request.update( + { + "revision": { + "git": { + "commit": self.params.get("commit") + } + } + } + ) + + if self.params.get("build_config_name"): + # Instantiate Build from Build config + result = self.instantiate_build_config( + name=self.params.get("build_config_name"), + namespace=self.params.get("namespace"), + request=build_request + ) + + else: + # Re-run Build + result = self.clone_build( + name=self.params.get("build_name"), + namespace=self.params.get("namespace"), + request=build_request + ) + + if result and self.params.get("wait"): + start = datetime.now() + + def _total_wait_time(): + return (datetime.now() - start).seconds + + wait_timeout = self.params.get("wait_timeout") + wait_sleep = self.params.get("wait_sleep") + last_status_phase = None + while _total_wait_time() < wait_timeout: + params = dict( + kind=result["kind"], + api_version=result["apiVersion"], + name=result["metadata"]["name"], + namespace=result["metadata"]["namespace"], + ) + facts = self.kubernetes_facts(**params) + if len(facts["resources"]) > 0: + last_status_phase = facts["resources"][0]["status"]["phase"] + if last_status_phase == "Complete": + result = facts["resources"][0] + break + elif last_status_phase in ("Cancelled", "Error", "Failed"): + self.fail_json( + msg="Unexpected status for Build %s/%s: %s" % ( + result["metadata"]["name"], + result["metadata"]["namespace"], + last_status_phase + ) + ) + time.sleep(wait_sleep) + + if last_status_phase != "Complete": + name = result["metadata"]["name"] + namespace = result["metadata"]["namespace"] + msg = "Build %s/%s has not complete after %d second(s)," \ + "current status is %s" % (namespace, name, wait_timeout, last_status_phase) + + self.fail_json(msg=msg) + + result = [result] if result else [] + self.exit_json(changed=True, builds=result) + + def cancel_build(self, restart): + + kind = 'Build' + api_version = 'build.openshift.io/v1' + + namespace = self.params.get("namespace") + phases = ["new", "pending", "running"] + if len(self.params.get("build_phases")): + phases = [p.lower() for p in self.params.get("build_phases")] + + names = [] + if self.params.get("build_name"): + names.append(self.params.get("build_name")) + else: + build_config = self.params.get("build_config_name") + # list all builds from namespace + params = dict( + kind=kind, + api_version=api_version, + namespace=namespace + ) + resources = self.kubernetes_facts(**params).get("resources", []) + + def _filter_builds(build): + config = build["metadata"].get("labels", {}).get("openshift.io/build-config.name") + return config in build_config + + for item in list(filter(_filter_builds, resources)): + name = item["metadata"]["name"] + if name not in names: + names.append(name) + + if len(names) == 0: + self.exit_json(changed=False, msg="No Build found from namespace %s" % namespace) + + warning = [] + builds_to_cancel = [] + for name in names: + params = dict( + kind=kind, + api_version=api_version, + name=name, + namespace=namespace + ) + + resource = self.kubernetes_facts(**params).get("resources", []) + if len(resource) == 0: + warning.append("Build %s/%s not found" % (namespace, name)) + continue + + resource = resource[0] + phase = resource["status"].get("phase").lower() + + # Build status.phase is matching the requested state and is not completed + if phase in phases: + builds_to_cancel.append(resource) + else: + warning.append("build %s/%s is not in expected phase, found %s" % (namespace, name, phase)) + + changed = False + result = [] + for build in builds_to_cancel: + # Set cancelled to true + build["status"]["cancelled"] = True + name = build["metadata"]["name"] + changed = True + try: + content_type = "application/json" + cancelled_build = self.client.request( + "PUT", + "/apis/build.openshift.io/v1/namespaces/{0}/builds/{1}".format( + namespace, name + ), + body=build, + content_type=content_type, + ).to_dict() + result.append(cancelled_build) + except DynamicApiError as exc: + self.fail_json( + msg="Failed to cancel Build %s/%s due to: %s" % (namespace, name, exc), + reason=exc.reason, + status=exc.status + ) + except Exception as e: + self.fail_json( + msg="Failed to cancel Build %s/%s due to: %s" % (namespace, name, e) + ) + + # Make sure the build phase is really cancelled. + def _wait_until_cancelled(build, wait_timeout, wait_sleep): + start = datetime.now() + last_phase = None + name = build["metadata"]["name"] + while (datetime.now() - start).seconds < wait_timeout: + params = dict( + kind=kind, + api_version=api_version, + name=name, + namespace=namespace + ) + resource = self.kubernetes_facts(**params).get("resources", []) + if len(resource) == 0: + return None, "Build %s/%s not found" % (namespace, name) + resource = resource[0] + last_phase = resource["status"]["phase"] + if last_phase == "Cancelled": + return resource, None + time.sleep(wait_sleep) + return None, "Build %s/%s is not cancelled as expected, current state is %s" % (namespace, name, last_phase) + + if result and self.params.get("wait"): + wait_timeout = self.params.get("wait_timeout") + wait_sleep = self.params.get("wait_sleep") + + wait_result = [] + for build in result: + ret, err = _wait_until_cancelled(build, wait_timeout, wait_sleep) + if err: + self.exit_json(msg=err) + wait_result.append(ret) + result = wait_result + + if restart: + self.start_build() + + self.exit_json(builds=result, changed=changed) + + def execute_module(self): + state = self.params.get("state") + if state == "started": + self.start_build() + else: + restart = bool(state == "restarted") + self.cancel_build(restart=restart) diff --git a/plugins/modules/openshift_adm_prune_builds.py b/plugins/modules/openshift_adm_prune_builds.py new file mode 100644 index 0000000..9b0b3fb --- /dev/null +++ b/plugins/modules/openshift_adm_prune_builds.py @@ -0,0 +1,136 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright (c) 2021, Red Hat +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +# STARTREMOVE (downstream) +DOCUMENTATION = r''' + +module: openshift_adm_prune_builds + +short_description: Prune old completed and failed builds + +version_added: "2.3.0" + +author: + - Aubin Bikouo (@abikouo) + +description: + - This module allow administrators to delete old completed and failed builds. + - Analogous to C(oc adm prune builds). + +extends_documentation_fragment: + - kubernetes.core.k8s_auth_options + +options: + namespace: + description: + - Use to specify namespace for builds to be deleted. + type: str + keep_younger_than: + description: + - Specify the minimum age (in minutes) of a Build for it to be considered a candidate for pruning. + type: int + orphans: + description: + - If C(true), prune all builds whose associated BuildConfig no longer exists and whose status is + complete, failed, error, or cancelled. + type: bool + default: False + +requirements: + - python >= 3.6 + - kubernetes >= 12.0.0 +''' + +EXAMPLES = r''' +# Run deleting older completed and failed builds and also including +# all builds whose associated BuildConfig no longer exists +- name: Run delete orphan Builds + community.okd.openshift_adm_prune_builds: + orphans: True + +# Run deleting older completed and failed builds keep younger than 2hours +- name: Run delete builds, keep younger than 2h + community.okd.openshift_adm_prune_builds: + keep_younger_than: 120 + +# Run deleting builds from specific namespace +- name: Run delete builds from namespace + community.okd.openshift_adm_prune_builds: + namespace: testing_namespace +''' + +RETURN = r''' +builds: + description: + - The builds that were deleted + returned: success + type: complex + contains: + api_version: + description: The versioned schema of this representation of an object. + returned: success + type: str + kind: + description: Represents the REST resource this object represents. + returned: success + type: str + metadata: + description: Standard object metadata. Includes name, namespace, annotations, labels, etc. + returned: success + type: dict + spec: + description: Specific attributes of the object. Will vary based on the I(api_version) and I(kind). + returned: success + type: dict + status: + description: Current status details for the object. + returned: success + type: dict +''' +# ENDREMOVE (downstream) + +import copy + +from ansible.module_utils.basic import AnsibleModule +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import AUTH_ARG_SPEC +from ansible.module_utils._text import to_native + + +def argument_spec(): + args = copy.deepcopy(AUTH_ARG_SPEC) + args.update( + dict( + namespace=dict(type='str'), + keep_younger_than=dict(type='int'), + orphans=dict(type='bool', default=False), + ) + ) + return args + + +def main(): + module = AnsibleModule(argument_spec=argument_spec(), supports_check_mode=True) + + try: + from ansible_collections.community.okd.plugins.module_utils.openshift_builds import ( + OpenShiftBuilds) + + build = OpenShiftBuilds(module) + build.argspec = argument_spec + build.prune() + except Exception as e: + module.fail_json( + msg="An error occurred while running openshift_adm_prune_builds", + error=to_native(e), + exception=e, + ) + + +if __name__ == '__main__': + main() diff --git a/plugins/modules/openshift_build.py b/plugins/modules/openshift_build.py new file mode 100644 index 0000000..08bedcf --- /dev/null +++ b/plugins/modules/openshift_build.py @@ -0,0 +1,271 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Copyright (c) 2021, Red Hat +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +# STARTREMOVE (downstream) +DOCUMENTATION = r''' + +module: openshift_build + +short_description: Start a new build or Cancel running, pending, or new builds. + +version_added: "2.3.0" + +author: + - Aubin Bikouo (@abikouo) + +description: + - This module starts a new build from the provided build config or build name. + - This module also cancel a new, pending or running build by requesting a graceful shutdown of the build. + There may be a delay between requesting the build and the time the build is terminated. + - This can also restart a new build when the current is cancelled. + - Analogous to C(oc cancel-build) and C(oc start-build). + +extends_documentation_fragment: + - kubernetes.core.k8s_auth_options + +options: + state: + description: + - Determines if a Build should be started ,cancelled or restarted. + - When set to C(restarted) a new build will be created after the current build is cancelled. + choices: + - started + - cancelled + - restarted + default: started + type: str + build_name: + description: + - Specify the name of a build which should be re-run. + - Mutually exclusive with parameter I(build_config_name). + type: str + build_config_name: + description: + - Specify the name of a build config from which a new build will be run. + - Mutually exclusive with parameter I(build_name). + type: str + namespace: + description: + - Specify the namespace for the build or the build config. + type: str + required: True + build_args: + description: + - Specify a list of key-value pair to pass to Docker during the build. + type: list + elements: dict + suboptions: + name: + description: + - docker build argument name. + type: str + required: true + value: + description: + - docker build argument value. + type: str + required: true + commit: + description: + - Specify the source code commit identifier the build should use; + requires a build based on a Git repository. + type: str + env_vars: + description: + - Specify a list of key-value pair for an environment variable to set for the build container. + type: list + elements: dict + suboptions: + name: + description: + - Environment variable name. + type: str + required: true + value: + description: + - Environment variable value. + type: str + required: true + incremental: + description: + - Overrides the incremental setting in a source-strategy build, ignored if not specified. + type: bool + no_cache: + description: + - Overrides the noCache setting in a docker-strategy build, ignored if not specified. + type: bool + wait: + description: + - When C(state=started), specify whether to wait for a build to complete + and exit with a non-zero return code if the build fails. + - When I(state=cancelled), specify whether to wait for a build phase to be Cancelled. + default: False + type: bool + wait_sleep: + description: + - Number of seconds to sleep between checks. + - Ignored if C(wait=false). + default: 5 + type: int + wait_timeout: + description: + - How long in seconds to wait for a build to complete. + - Ignored if C(wait=false). + default: 120 + type: int + build_phases: + description: + - List of state for build to cancel. + - Ignored when C(state=started). + type: list + elements: str + choices: + - New + - Pending + - Running + +requirements: + - python >= 3.6 + - kubernetes >= 12.0.0 +''' + +EXAMPLES = r''' +# Starts build from build config default/hello-world +- name: Starts build from build config + community.okd.openshift_build: + namespace: default + build_config_name: hello-world + +# Starts build from a previous build "default/hello-world-1" +- name: Starts build from a previous build + community.okd.openshift_build: + namespace: default + build_name: hello-world-1 + +# Cancel the build with the given name +- name: Cancel build from default namespace + community.okd.openshift_build: + namespace: "default" + build_name: ruby-build-1 + state: cancelled + +# Cancel the named build and create a new one with the same parameters +- name: Cancel build from default namespace and create a new one + community.okd.openshift_build: + namespace: "default" + build_name: ruby-build-1 + state: restarted + +# Cancel all builds created from 'ruby-build' build configuration that are in 'new' state +- name: Cancel build from default namespace and create a new one + community.okd.openshift_build: + namespace: "default" + build_config_name: ruby-build + build_phases: + - New + state: cancelled +''' + +RETURN = r''' +builds: + description: + - The builds that were started/cancelled. + returned: success + type: complex + contains: + api_version: + description: The versioned schema of this representation of an object. + returned: success + type: str + kind: + description: Represents the REST resource this object represents. + returned: success + type: str + metadata: + description: Standard object metadata. Includes name, namespace, annotations, labels, etc. + returned: success + type: dict + spec: + description: Specific attributes of the build. + returned: success + type: dict + status: + description: Current status details for the object. + returned: success + type: dict +''' +# ENDREMOVE (downstream) + +import copy + +from ansible.module_utils.basic import AnsibleModule +from ansible_collections.kubernetes.core.plugins.module_utils.args_common import AUTH_ARG_SPEC +from ansible.module_utils._text import to_native + + +def argument_spec(): + args = copy.deepcopy(AUTH_ARG_SPEC) + + args_options = dict( + name=dict(type='str', required=True), + value=dict(type='str', required=True) + ) + + args.update( + dict( + state=dict(type='str', choices=['started', 'cancelled', 'restarted'], default="started"), + build_args=dict(type='list', elements='dict', options=args_options), + commit=dict(type='str'), + env_vars=dict(type='list', elements='dict', options=args_options), + build_name=dict(type='str'), + build_config_name=dict(type='str'), + namespace=dict(type='str', required=True), + incremental=dict(type='bool'), + no_cache=dict(type='bool'), + wait=dict(type='bool', default=False), + wait_sleep=dict(type='int', default=5), + wait_timeout=dict(type='int', default=120), + build_phases=dict(type='list', elements='str', default=[], choices=["New", "Pending", "Running"]), + ) + ) + return args + + +def main(): + mutually_exclusive = [ + ('build_name', 'build_config_name'), + ] + module = AnsibleModule( + argument_spec=argument_spec(), + mutually_exclusive=mutually_exclusive, + required_one_of=[ + [ + 'build_name', + 'build_config_name', + ] + ], + ) + + try: + from ansible_collections.community.okd.plugins.module_utils.openshift_builds import ( + OpenShiftBuilds) + + build = OpenShiftBuilds(module) + build.argspec = argument_spec + build.execute_module() + except Exception as e: + module.fail_json( + msg="An error occurred while running openshift_start_build module.", + error=to_native(e), + exception=e, + ) + + +if __name__ == '__main__': + main() diff --git a/tests/sanity/ignore-2.11.txt b/tests/sanity/ignore-2.11.txt index 5a927d2..d022fb6 100644 --- a/tests/sanity/ignore-2.11.txt +++ b/tests/sanity/ignore-2.11.txt @@ -146,3 +146,21 @@ plugins/module_utils/openshift_registry.py import-3.5!skip plugins/module_utils/openshift_registry.py compile-2.6!skip plugins/module_utils/openshift_registry.py compile-2.7!skip plugins/module_utils/openshift_registry.py compile-3.5!skip +plugins/module_utils/openshift_builds.py import-2.6!skip +plugins/module_utils/openshift_builds.py import-2.7!skip +plugins/module_utils/openshift_builds.py import-3.5!skip +plugins/module_utils/openshift_builds.py compile-2.6!skip +plugins/module_utils/openshift_builds.py compile-2.7!skip +plugins/module_utils/openshift_builds.py compile-3.5!skip +plugins/modules/openshift_build.py import-2.6!skip +plugins/modules/openshift_build.py import-2.7!skip +plugins/modules/openshift_build.py import-3.5!skip +plugins/modules/openshift_build.py compile-2.6!skip +plugins/modules/openshift_build.py compile-2.7!skip +plugins/modules/openshift_build.py compile-3.5!skip +plugins/modules/openshift_adm_prune_builds.py import-2.6!skip +plugins/modules/openshift_adm_prune_builds.py import-2.7!skip +plugins/modules/openshift_adm_prune_builds.py import-3.5!skip +plugins/modules/openshift_adm_prune_builds.py compile-2.6!skip +plugins/modules/openshift_adm_prune_builds.py compile-2.7!skip +plugins/modules/openshift_adm_prune_builds.py compile-3.5!skip \ No newline at end of file diff --git a/tests/sanity/ignore-2.12.txt b/tests/sanity/ignore-2.12.txt index b18ff70..674e8af 100644 --- a/tests/sanity/ignore-2.12.txt +++ b/tests/sanity/ignore-2.12.txt @@ -133,3 +133,21 @@ plugins/module_utils/openshift_registry.py import-3.5!skip plugins/module_utils/openshift_registry.py compile-2.6!skip plugins/module_utils/openshift_registry.py compile-2.7!skip plugins/module_utils/openshift_registry.py compile-3.5!skip +plugins/module_utils/openshift_builds.py import-2.6!skip +plugins/module_utils/openshift_builds.py import-2.7!skip +plugins/module_utils/openshift_builds.py import-3.5!skip +plugins/module_utils/openshift_builds.py compile-2.6!skip +plugins/module_utils/openshift_builds.py compile-2.7!skip +plugins/module_utils/openshift_builds.py compile-3.5!skip +plugins/modules/openshift_build.py import-2.6!skip +plugins/modules/openshift_build.py import-2.7!skip +plugins/modules/openshift_build.py import-3.5!skip +plugins/modules/openshift_build.py compile-2.6!skip +plugins/modules/openshift_build.py compile-2.7!skip +plugins/modules/openshift_build.py compile-3.5!skip +plugins/modules/openshift_adm_prune_builds.py import-2.6!skip +plugins/modules/openshift_adm_prune_builds.py import-2.7!skip +plugins/modules/openshift_adm_prune_builds.py import-3.5!skip +plugins/modules/openshift_adm_prune_builds.py compile-2.6!skip +plugins/modules/openshift_adm_prune_builds.py compile-2.7!skip +plugins/modules/openshift_adm_prune_builds.py compile-3.5!skip \ No newline at end of file diff --git a/tests/sanity/ignore-2.13.txt b/tests/sanity/ignore-2.13.txt index 6dbc2c0..a54a41f 100644 --- a/tests/sanity/ignore-2.13.txt +++ b/tests/sanity/ignore-2.13.txt @@ -89,3 +89,15 @@ plugins/module_utils/openshift_registry.py import-2.7!skip plugins/module_utils/openshift_registry.py import-3.5!skip plugins/module_utils/openshift_registry.py compile-2.7!skip plugins/module_utils/openshift_registry.py compile-3.5!skip +plugins/module_utils/openshift_builds.py import-2.7!skip +plugins/module_utils/openshift_builds.py import-3.5!skip +plugins/module_utils/openshift_builds.py compile-2.7!skip +plugins/module_utils/openshift_builds.py compile-3.5!skip +plugins/modules/openshift_build.py import-2.7!skip +plugins/modules/openshift_build.py import-3.5!skip +plugins/modules/openshift_build.py compile-2.7!skip +plugins/modules/openshift_build.py compile-3.5!skip +plugins/modules/openshift_adm_prune_builds.py import-2.7!skip +plugins/modules/openshift_adm_prune_builds.py import-3.5!skip +plugins/modules/openshift_adm_prune_builds.py compile-2.7!skip +plugins/modules/openshift_adm_prune_builds.py compile-3.5!skip \ No newline at end of file diff --git a/tests/sanity/ignore-2.14.txt b/tests/sanity/ignore-2.14.txt index 6dbc2c0..a54a41f 100644 --- a/tests/sanity/ignore-2.14.txt +++ b/tests/sanity/ignore-2.14.txt @@ -89,3 +89,15 @@ plugins/module_utils/openshift_registry.py import-2.7!skip plugins/module_utils/openshift_registry.py import-3.5!skip plugins/module_utils/openshift_registry.py compile-2.7!skip plugins/module_utils/openshift_registry.py compile-3.5!skip +plugins/module_utils/openshift_builds.py import-2.7!skip +plugins/module_utils/openshift_builds.py import-3.5!skip +plugins/module_utils/openshift_builds.py compile-2.7!skip +plugins/module_utils/openshift_builds.py compile-3.5!skip +plugins/modules/openshift_build.py import-2.7!skip +plugins/modules/openshift_build.py import-3.5!skip +plugins/modules/openshift_build.py compile-2.7!skip +plugins/modules/openshift_build.py compile-3.5!skip +plugins/modules/openshift_adm_prune_builds.py import-2.7!skip +plugins/modules/openshift_adm_prune_builds.py import-3.5!skip +plugins/modules/openshift_adm_prune_builds.py compile-2.7!skip +plugins/modules/openshift_adm_prune_builds.py compile-3.5!skip \ No newline at end of file