forked from nginxinc/kic-reference-architectures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathingress_controller_image_puller_provider.py
62 lines (45 loc) · 2.48 KB
/
ingress_controller_image_puller_provider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import uuid
from typing import Optional, Any, Dict, List
import pulumi
from pulumi.dynamic import CreateResult, UpdateResult, DiffResult, CheckResult, CheckFailure, ReadResult
from ingress_controller_image_base_provider import IngressControllerBaseProvider as BaseProvider
from kic_util.docker_image_name import DockerImageName, DockerImageNameError
from kic_util import external_process
class IngressControllerImagePullerProvider(BaseProvider):
"""Pulumi dynamic provider that pulls ingress container images from an external registry"""
REQUIRED_PROPS: List[str] = ['image_name']
def __init__(self,
resource: Optional[pulumi.Resource] = None,
debug_logger_func=None):
super().__init__(resource=resource, debug_logger_func=debug_logger_func, runner=external_process.run)
def pull(self, props: Any) -> Dict[str, str]:
image_name = props['image_name']
pulumi.log.info(f'pulling from registry: {image_name}', self.resource)
full_image_name = self._docker_pull(image_name)
if full_image_name != image_name:
pulumi.log.info(f'full image name: {full_image_name}', self.resource)
image_id = self._docker_image_id_from_image_name(image_name)
image = DockerImageName.from_name(image_name=image_name, image_id=image_id)
return {'image_id': image.id,
'image_name': str(image),
'image_name_alias': None,
'image_tag': image.tag,
'image_tag_alias': None}
def create(self, props: Any) -> CreateResult:
outputs = self.pull(props)
id_ = str(uuid.uuid4())
return CreateResult(id_=id_, outs=outputs)
def update(self, _id: str, _olds: Any, _news: Any) -> UpdateResult:
outputs = self.pull(props=_news)
return UpdateResult(outs=outputs)
def diff(self, _id: str, _olds: Any, _news: Any) -> DiffResult:
# Always assume that the container image has changed so that we run
# docker pull to see if a newer image is available
return DiffResult(changes=True)
def check(self, _olds: Any, news: Any) -> CheckResult:
failures = BaseProvider._check_for_required_params(news, IngressControllerImagePullerProvider.REQUIRED_PROPS)
try:
DockerImageName.from_name(news['image_name'])
except DockerImageNameError as e:
failures.append(CheckFailure(property_='image_name', reason=str(e)))
return CheckResult(inputs=news, failures=failures)