Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
luzfcb committed Dec 21, 2024
1 parent 0a81ac8 commit 581a923
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ LABEL maintainer="mail@sobolevn.me"
LABEL vendor="wemake.services"

# Our own tool:
ENV DISL_VERSION='2.0.0'
ENV DISL_VERSION='2.1.0'

RUN apk add --no-cache bash docker
RUN pip3 install "docker-image-size-limit==$DISL_VERSION"
COPY . /docker-image-size-limit
RUN #pip3 install "docker-image-size-limit==$DISL_VERSION"
RUN pip3 install /docker-image-size-limit/ && rm -rfv /docker-image-size-limit

COPY ./scripts/entrypoint.sh /
RUN chmod +x /entrypoint.sh
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ $ disl your-image-name:label 300MiB --max-layers=5
# ok!
```

Add `--current` or `-c` flag to show the current size your image:

```bash
$ disl your-image-name:label 300MiB --current
your-image-name:label size is 414.4 MiB
your-image-name:label exceeds 300MiB limit by 114.4 MiB

$ disl your-image-name:label 300MiB --max-layers=5 --current
your-image-name:label size is 414.4 MiB
your-image-name:label exceeds 300MiB limit by 114.4 MiB
your-image-name:label exceeds 5 maximum layers by 2

```



## Options

Expand Down Expand Up @@ -84,6 +99,10 @@ You can also use this check as a [GitHub Action](https://github.com/marketplace/
with:
image: "$YOUR_IMAGE_NAME"
size: "$YOUR_SIZE_LIMIT"
# optional fields:
max_layers: 5
show_current_size: false
dry_run: false
```
Here's [an example](https://github.com/wemake-services/docker-image-size-limit/actions?query=workflow%3Adisl).
Expand All @@ -105,6 +124,9 @@ Then you can use it like so:
docker run -v /var/run/docker.sock:/var/run/docker.sock --rm \
-e INPUT_IMAGE="$YOUR_IMAGE_NAME" \
-e INPUT_SIZE="$YOUR_SIZE_LIMIT" \
-e INPUT_MAX_LAYERS="$YOUR_MAX_LAYERS" \
-e INPUT_SHOW_CURRENT_SIZE="true" \
-e INPUT_DRY_RUN="true" \
wemakeservices/docker-image-size-limit
```

Expand Down
12 changes: 11 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# We also define metadata here:
# See: https://help.github.com/en/articles/metadata-syntax-for-github-actions

name: 'docker-image-size-limit'
name: 'docker-image-size-limit2'
description: 'Runs docker-image-size-limit as a GitHub Action'
branding:
icon: 'check'
Expand All @@ -21,6 +21,14 @@ inputs:
description: 'The maximum number of layers in the image'
required: false
default: -1
show_current_size:
description: 'Show the current size of the image'
required: false
default: 'false'
dry_run:
description: 'Do not fail the action even if docker image size/layers exceed size and max_layers'
required: false
default: 'false'
outputs:
size:
description: 'The output of docker-image-size-limit run'
Expand All @@ -32,3 +40,5 @@ runs:
- ${{ inputs.image }}
- ${{ inputs.size }}
- ${{ inputs.layers }}
- ${{ inputs.show_current_size }}
- ${{ inputs.dry_run }}
28 changes: 25 additions & 3 deletions docker_image_size_limit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ def main() -> NoReturn:
"""Main CLI entrypoint."""
client = docker.from_env()
arguments = _parse_args()
extra_size, extra_layers = _check_image(
extra_size, extra_layers, image_current_size = _check_image(
client,
image=arguments.image,
max_size=arguments.max_size,
max_layers=arguments.max_layers,
)
if arguments.current_size:
print('{0} current size is {1}'.format( # noqa: WPS421
arguments.image,
format_size(image_current_size, binary=True),
))

exit_code = 0
if extra_size > 0:
Expand All @@ -41,6 +46,8 @@ def main() -> NoReturn:
extra_layers,
))
exit_code = 1
if arguments.dry_run:
exit_code = 0
sys.exit(exit_code)


Expand All @@ -49,14 +56,15 @@ def _check_image(
image: str,
max_size: str,
max_layers: int,
) -> Tuple[int, int]:
) -> Tuple[int, int, int]:
image_info = client.images.get(image)
image_current_size: int = image_info.attrs['Size']
size_overflow = check_image_size(image_info, limit=max_size)
if max_layers > 0:
layers_overflow = check_image_layers(image_info, limit=max_layers)
else:
layers_overflow = 0
return size_overflow, layers_overflow
return size_overflow, layers_overflow, image_current_size


def check_image_size(
Expand Down Expand Up @@ -126,4 +134,18 @@ def _parse_args() -> argparse.Namespace:
help='Maximum number of image layers',
default=-1,
)
parser.add_argument(
'--current', '-c',
action='store_true',
help='Display the current size of the Docker image',
default=False,
dest='current_size'
)
parser.add_argument(
'--dry-run',
action='store_true',
help='Exit with 0 even if docker image size/layers exceed max_size and max-layers',
default=False,
dest='dry_run'
)
return parser.parse_args()
5 changes: 5 additions & 0 deletions docker_image_size_limit/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from docker_image_size_limit import main


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "docker-image-size-limit"
version = "2.0.0"
version = "2.1.0"
description = ""
license = "MIT"
authors = [
Expand Down
17 changes: 16 additions & 1 deletion scripts/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@

# Default values:
: "${INPUT_MAX_LAYERS:=-1}"
: "${INPUT_SHOW_CURRENT_SIZE:=false}"
: "${INPUT_DRY_RUN:=false}"

# Diagnostic output:
echo "Using image: $INPUT_IMAGE"
echo "Size limit: $INPUT_SIZE"
echo "Max layers: $INPUT_MAX_LAYERS"
echo "Show Current Size: $INPUT_SHOW_CURRENT_SIZE"
echo "Dry Run: $INPUT_DRY_RUN"
echo 'disl --version:'
disl --version
echo '================================='
echo

SHOW_CURRENT_SIZE=""
if [ "$INPUT_SHOW_CURRENT_SIZE" = "true" ]; then
SHOW_CURRENT_SIZE="--current"
fi

DRY_RUN=""
if [ "$INPUT_DRY_RUN" = "true" ]; then
DRY_RUN="--dry-run"
fi


# Runs disl:
output=$(disl "$INPUT_IMAGE" "$INPUT_SIZE" --max-layers="$INPUT_MAX_LAYERS")
output=$(disl "$INPUT_IMAGE" "$INPUT_SIZE" --max-layers="$INPUT_MAX_LAYERS" "$SHOW_CURRENT_SIZE" "$DRY_RUN")
status="$?"

# Sets the output variable for Github Action API:
Expand Down

0 comments on commit 581a923

Please sign in to comment.