def test_writedockerfile_without_docker_cmd(): _, tmp_file = tempfile.mkstemp() dockerfile.write_dockerfile( destination=tmp_file, docker_command=None, path_prefix="/pre", base_image="foo_bar") actual = open(tmp_file, 'r').read() expected = """FROM foo_bar WORKDIR /pre ENV FAIRING_RUNTIME 1 RUN if [ -e requirements.txt ];then pip install --no-cache -r requirements.txt; fi COPY /pre /pre""" assert actual == expected
def build(self): dockerfile_path = dockerfile.write_dockerfile( dockerfile_path=self.dockerfile_path, path_prefix=self.preprocessor.path_prefix, base_image=self.base_image) self.preprocessor.output_map[dockerfile_path] = 'Dockerfile' context_path, context_hash = self.preprocessor.context_tar_gz() self.image_tag = self.full_image_name(context_hash) self.context_source.prepare(context_path) labels = {'fairing-builder': 'kaniko'} pod_spec = self.context_source.generate_pod_spec( self.image_tag, self.push) for fn in self.pod_spec_mutators: fn(self.manager, pod_spec, self.namespace) build_pod = client.V1Pod(api_version="v1", kind="Pod", metadata=client.V1ObjectMeta( generate_name="fairing-builder-", labels=labels, namespace=self.namespace, ), spec=pod_spec) created_pod = client. \ CoreV1Api(). \ create_namespaced_pod("default", build_pod) self.manager.log(name=created_pod.metadata.name, namespace=created_pod.metadata.namespace, selectors=labels) # clean up created pod and secret self.context_source.cleanup() client.CoreV1Api().delete_namespaced_pod( created_pod.metadata.name, created_pod.metadata.namespace, client.V1DeleteOptions())
def _build(self): docker_command = self.preprocessor.get_command() logger.warning("Docker command: {}".format(docker_command)) if not docker_command: logger.warning( "Not setting a command for the output docker image.") install_reqs_before_copy = self.preprocessor.is_requirements_txt_file_present( ) dockerfile_path = dockerfile.write_dockerfile( docker_command=docker_command, dockerfile_path=self.dockerfile_path, path_prefix=self.preprocessor.path_prefix, base_image=self.base_image, install_reqs_before_copy=install_reqs_before_copy) self.preprocessor.output_map[dockerfile_path] = 'Dockerfile' context_file, context_hash = self.preprocessor.context_tar_gz() self.image_tag = self.full_image_name(context_hash) logger.warn('Building docker image {}...'.format(self.image_tag)) with open(context_file, 'rb') as fileobj: bld = self.docker_client.build(path='.', custom_context=True, fileobj=fileobj, tag=self.image_tag, encoding='utf-8') for line in bld: self._process_stream(line)
def test_writedockerfile_with_early_install_reqs(): _, tmp_file = tempfile.mkstemp() dockerfile.write_dockerfile( destination=tmp_file, docker_command=["python", "main.py"], path_prefix="/pre", base_image="foo_bar", install_reqs_before_copy=True) actual = open(tmp_file, 'r').read() expected = """FROM foo_bar WORKDIR /pre ENV FAIRING_RUNTIME 1 COPY /pre/requirements.txt /pre RUN if [ -e requirements.txt ];then pip install --no-cache -r requirements.txt; fi COPY /pre /pre CMD python main.py""" assert actual == expected
def build(self): logging.info("Building image using cluster builder.") install_reqs_before_copy = self.preprocessor.is_requirements_txt_file_present() dockerfile_path = dockerfile.write_dockerfile( dockerfile_path=self.dockerfile_path, path_prefix=self.preprocessor.path_prefix, base_image=self.base_image, install_reqs_before_copy=install_reqs_before_copy ) self.preprocessor.output_map[dockerfile_path] = 'Dockerfile' context_path, context_hash = self.preprocessor.context_tar_gz() self.image_tag = self.full_image_name(context_hash) self.context_source.prepare(context_path) labels = {'fairing-builder': 'kaniko'} labels['fairing-build-id'] = str(uuid.uuid1()) pod_spec = self.context_source.generate_pod_spec( self.image_tag, self.push) for fn in self.pod_spec_mutators: fn(self.manager, pod_spec, self.namespace) build_pod = client.V1Pod( api_version="v1", kind="Pod", metadata=client.V1ObjectMeta( generate_name="fairing-builder-", labels=labels, namespace=self.namespace, annotations={"sidecar.istio.io/inject": "false"}, ), spec=pod_spec ) created_pod = client. \ CoreV1Api(). \ create_namespaced_pod(self.namespace, build_pod) self.manager.log( name=created_pod.metadata.name, namespace=created_pod.metadata.namespace, selectors=labels, container="kaniko") # clean up created pod and secret self.context_source.cleanup() client.CoreV1Api().delete_namespaced_pod( created_pod.metadata.name, created_pod.metadata.namespace, body=client.V1DeleteOptions())
def execute(self): dockerfile.write_dockerfile(dockerfile_path=self.dockerfile_path, base_image=self.base_image) self.copy_src_to_mount_point() self.build_and_push()