| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2021, Red Hat |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
# STARTREMOVE (downstream) |
| 12 |
DOCUMENTATION = r""" |
| 13 |
|
| 14 |
module: openshift_import_image |
| 15 |
|
| 16 |
short_description: Import the latest image information from a tag in a container image registry. |
| 17 |
|
| 18 |
version_added: "2.2.0" |
| 19 |
|
| 20 |
author: |
| 21 |
- Aubin Bikouo (@abikouo) |
| 22 |
|
| 23 |
description: |
| 24 |
- Image streams allow you to control which images are rolled out to your builds and applications. |
| 25 |
- This module fetches the latest version of an image from a remote repository and updates the image stream tag |
| 26 |
if it does not match the previous value. |
| 27 |
- Running the module multiple times will not create duplicate entries. |
| 28 |
- When importing an image, only the image metadata is copied, not the image contents. |
| 29 |
- Analogous to C(oc import-image). |
| 30 |
|
| 31 |
extends_documentation_fragment: |
| 32 |
- kubernetes.core.k8s_auth_options |
| 33 |
|
| 34 |
options: |
| 35 |
namespace: |
| 36 |
description: |
| 37 |
- Use to specify namespace for image stream to create/update. |
| 38 |
type: str |
| 39 |
required: True |
| 40 |
name: |
| 41 |
description: |
| 42 |
- Image stream to import tag into. |
| 43 |
- This can be provided as a list of images streams or a single value. |
| 44 |
type: raw |
| 45 |
required: True |
| 46 |
all: |
| 47 |
description: |
| 48 |
- If set to I(true), import all tags from the provided source on creation or if C(source) is specified. |
| 49 |
type: bool |
| 50 |
default: False |
| 51 |
validate_registry_certs: |
| 52 |
description: |
| 53 |
- If set to I(true), allow importing from registries that have invalid HTTPS certificates. |
| 54 |
or are hosted via HTTP. This parameter will take precedence over the insecure annotation. |
| 55 |
type: bool |
| 56 |
reference_policy: |
| 57 |
description: |
| 58 |
- Allow to request pullthrough for external image when set to I(local). |
| 59 |
default: source |
| 60 |
choices: |
| 61 |
- source |
| 62 |
- local |
| 63 |
type: str |
| 64 |
scheduled: |
| 65 |
description: |
| 66 |
- Set each imported Docker image to be periodically imported from a remote repository. |
| 67 |
type: bool |
| 68 |
default: False |
| 69 |
source: |
| 70 |
description: |
| 71 |
- A Docker image repository to import images from. |
| 72 |
- Should be provided as 'registry.io/repo/image' |
| 73 |
type: str |
| 74 |
|
| 75 |
requirements: |
| 76 |
- python >= 3.6 |
| 77 |
- kubernetes >= 12.0.0 |
| 78 |
- docker-image-py |
| 79 |
""" |
| 80 |
|
| 81 |
EXAMPLES = r""" |
| 82 |
# Import tag latest into a new image stream. |
| 83 |
- name: Import tag latest into new image stream |
| 84 |
community.okd.openshift_import_image: |
| 85 |
namespace: testing |
| 86 |
name: mystream |
| 87 |
source: registry.io/repo/image:latest |
| 88 |
|
| 89 |
# Update imported data for tag latest in an already existing image stream. |
| 90 |
- name: Update imported data for tag latest |
| 91 |
community.okd.openshift_import_image: |
| 92 |
namespace: testing |
| 93 |
name: mystream |
| 94 |
|
| 95 |
# Update imported data for tag 'stable' in an already existing image stream. |
| 96 |
- name: Update imported data for tag latest |
| 97 |
community.okd.openshift_import_image: |
| 98 |
namespace: testing |
| 99 |
name: mystream:stable |
| 100 |
|
| 101 |
# Update imported data for all tags in an existing image stream. |
| 102 |
- name: Update imported data for all tags |
| 103 |
community.okd.openshift_import_image: |
| 104 |
namespace: testing |
| 105 |
name: mystream |
| 106 |
all: true |
| 107 |
|
| 108 |
# Import all tags into a new image stream. |
| 109 |
- name: Import all tags into a new image stream. |
| 110 |
community.okd.openshift_import_image: |
| 111 |
namespace: testing |
| 112 |
name: mystream |
| 113 |
source: registry.io/repo/image:latest |
| 114 |
all: true |
| 115 |
|
| 116 |
# Import all tags into a new image stream for a list of image streams |
| 117 |
- name: Import all tags into a new image stream. |
| 118 |
community.okd.openshift_import_image: |
| 119 |
namespace: testing |
| 120 |
name: |
| 121 |
- mystream1 |
| 122 |
- mystream2 |
| 123 |
- mystream3 |
| 124 |
source: registry.io/repo/image:latest |
| 125 |
all: true |
| 126 |
""" |
| 127 |
|
| 128 |
|
| 129 |
RETURN = r""" |
| 130 |
result: |
| 131 |
description: |
| 132 |
- List with all ImageStreamImport that have been created. |
| 133 |
type: list |
| 134 |
returned: success |
| 135 |
elements: dict |
| 136 |
contains: |
| 137 |
api_version: |
| 138 |
description: The versioned schema of this representation of an object. |
| 139 |
returned: success |
| 140 |
type: str |
| 141 |
kind: |
| 142 |
description: Represents the REST resource this object represents. |
| 143 |
returned: success |
| 144 |
type: str |
| 145 |
metadata: |
| 146 |
description: Standard object metadata. Includes name, namespace, annotations, labels, etc. |
| 147 |
returned: success |
| 148 |
type: dict |
| 149 |
spec: |
| 150 |
description: Specific attributes of the object. Will vary based on the I(api_version) and I(kind). |
| 151 |
returned: success |
| 152 |
type: dict |
| 153 |
status: |
| 154 |
description: Current status details for the object. |
| 155 |
returned: success |
| 156 |
type: dict |
| 157 |
""" |
| 158 |
# ENDREMOVE (downstream) |
| 159 |
|
| 160 |
import copy |
| 161 |
|
| 162 |
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( |
| 163 |
AUTH_ARG_SPEC, |
| 164 |
) |
| 165 |
|
| 166 |
|
| 167 |
def argument_spec(): |
| 168 |
args = copy.deepcopy(AUTH_ARG_SPEC) |
| 169 |
args.update( |
| 170 |
dict( |
| 171 |
namespace=dict(type="str", required=True), |
| 172 |
name=dict(type="raw", required=True), |
| 173 |
all=dict(type="bool", default=False), |
| 174 |
validate_registry_certs=dict(type="bool"), |
| 175 |
reference_policy=dict( |
| 176 |
type="str", choices=["source", "local"], default="source" |
| 177 |
), |
| 178 |
scheduled=dict(type="bool", default=False), |
| 179 |
source=dict(type="str"), |
| 180 |
) |
| 181 |
) |
| 182 |
return args |
| 183 |
|
| 184 |
|
| 185 |
def main(): |
| 186 |
from ansible_collections.community.okd.plugins.module_utils.openshift_import_image import ( |
| 187 |
OpenShiftImportImage, |
| 188 |
) |
| 189 |
|
| 190 |
module = OpenShiftImportImage( |
| 191 |
argument_spec=argument_spec(), supports_check_mode=True |
| 192 |
) |
| 193 |
module.run_module() |
| 194 |
|
| 195 |
|
| 196 |
if __name__ == "__main__": |
| 197 |
main() |