Skip to content

Commit

Permalink
build/image: build the storage-operator image
Browse files Browse the repository at this point in the history
Add a new image to be built: the container image for the Storage
operator.
Since it's very similar to a LocalImage, except for the building part
(we use the operator-sdk command instead of Docker directly, because we
need to compile the Go code), we introduce a new class (OperatorImage)
that inherits from LocalImage and overload the `_do_build` method.

Note that we take care of enabling Go module and compiling from the
`storage-operator` directory.

Refs: #1410
Signed-off-by: Sylvain Laperche <[email protected]>
  • Loading branch information
slaperche-scality committed Jul 15, 2019
1 parent 7a34d8a commit 30b0d50
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .pylint-dict
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ epel
filename
init
io
Kubernetes
linter
metadata
mkdir
Expand All @@ -33,6 +34,7 @@ rpmspec
repo
rpmlint
scality
sdk
skopeo
sls
timestamp
Expand Down
11 changes: 6 additions & 5 deletions buildchain/buildchain/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@
class ExtCommand(enum.Enum):
"""External commands used by the build chain."""

GIT = os.getenv('GIT_BIN', 'git')
HARDLINK = os.getenv('HARDLINK_BIN', 'hardlink')
MKISOFS = os.getenv('MKISOFS_BIN', 'mkisofs')
SKOPEO = os.getenv('SKOPEO_BIN', 'skopeo')
VAGRANT = os.getenv('VAGRANT_BIN', 'vagrant')
GIT = os.getenv('GIT_BIN', 'git')
HARDLINK = os.getenv('HARDLINK_BIN', 'hardlink')
MKISOFS = os.getenv('MKISOFS_BIN', 'mkisofs')
SKOPEO = os.getenv('SKOPEO_BIN', 'skopeo')
VAGRANT = os.getenv('VAGRANT_BIN', 'vagrant')
OPERATOR_SDK = os.getenv('OPERATOR_SDK_BIN', 'operator-sdk')

@property
def command_name(self) -> str:
Expand Down
9 changes: 8 additions & 1 deletion buildchain/buildchain/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pathlib import Path
import subprocess
from typing import Optional
from typing import Optional, FrozenSet

from buildchain import ROOT # Re-export ROOT through this module.
from buildchain import config
Expand Down Expand Up @@ -39,6 +39,8 @@
STATIC_CONTAINER_REGISTRY : Path = Path(
ROOT, 'buildchain/static-container-registry'
)
# Path to the storage-operator source directory.
STORAGE_OPERATOR_ROOT : Path = ROOT/'storage-operator'

# }}}
# Vagrant parameters {{{
Expand Down Expand Up @@ -103,3 +105,8 @@ def git_ref() -> Optional[str]:
GIT_REF = git_ref()

# }}}

STORAGE_OPERATOR_SOURCES : FrozenSet[Path] = frozenset([
filepath for filepath in STORAGE_OPERATOR_ROOT.rglob('*.go')
if 'vendor' not in str(filepath.parent)
])
7 changes: 7 additions & 0 deletions buildchain/buildchain/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ def show() -> str:
},
task_dep=['_image_mkdir_root'],
),
targets.OperatorImage(
name='storage-operator',
version='latest',
destination=constants.ISO_IMAGE_ROOT,
task_dep=['_image_mkdir_root'],
file_dep=list(constants.STORAGE_OPERATOR_SOURCES)
),
)


Expand Down
1 change: 1 addition & 0 deletions buildchain/buildchain/targets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from buildchain.targets.directory import Mkdir
from buildchain.targets.file_tree import FileTree
from buildchain.targets.local_image import LocalImage
from buildchain.targets.operator_image import OperatorImage
from buildchain.targets.package import Package
from buildchain.targets.remote_image import RemoteImage
from buildchain.targets.repository import Repository
Expand Down
54 changes: 54 additions & 0 deletions buildchain/buildchain/targets/operator_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# coding: utf-8


"""Provides container image construction for Kubernetes Operator.
Those are very similar to the locally built images, except that they are build
by the `operator-sdk` command (which also takes care of compiling the Go code)
instead of calling Docker directly.
"""


from pathlib import Path
import shlex
from typing import Any, List

import doit # type: ignore

from buildchain import config
from buildchain import constants
from buildchain import types

from . import local_image


class OperatorImage(local_image.LocalImage):
"""A locally built container image for a Kubernetes Operator."""
def __init__(
self, name: str, version: str, destination: Path, **kwargs: Any
):
"""Initialize an operator container image.
Arguments:
name: image name
version: image version
destination: where to save the result
Keyword Arguments:
They are passed to `Target` init method.
"""
dockerfile = constants.ROOT/name/'build'/'Dockerfile'
kwargs.setdefault('task_dep', []).append('check_for:operator-sdk')
super().__init__(
name=name, version=version, dockerfile=dockerfile,
destination=destination, save_on_disk=True, build_args=None,
**kwargs
)

def _do_build(self) -> List[types.Action]:
"""Return the actions used to build the image."""
cwd = constants.ROOT/self.name
cmd = ' '.join(map(shlex.quote, [
config.ExtCommand.OPERATOR_SDK.value, 'build', self.tag
]))
return [doit.action.CmdAction(cmd, cwd=cwd)]

0 comments on commit 30b0d50

Please sign in to comment.