Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/source/image/image.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ image.inspectschema module
:undoc-members:
:show-inheritance:

image.linter module
-------------------

.. automodule:: image.linter
:members:
:undoc-members:
:show-inheritance:

image.manifest module
---------------------

Expand Down
32 changes: 32 additions & 0 deletions examples/image-lint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
######
# Hack
#
# Make sibling modules visible to this nested executable
import os, sys
sys.path.insert(
0,
os.path.dirname(
os.path.dirname(
os.path.realpath(__file__)
)
)
)
# End Hack
######

from image.auth import AUTH
from image.containerimage import ContainerImage
from image.linter import *
from lint.config import LinterConfig

# Initialize a ContainerImage given a tag reference
my_image = ContainerImage("registry.k8s.io/pause:3.5")

# Initialize the linter and linter config
linter = ContainerImageLinter()
config = DEFAULT_CONTAINER_IMAGE_LINTER_CONFIG

# Lint the container image and print results
results = linter.lint(my_image, config=config, auth=AUTH)
for result in results:
print(result)
1 change: 1 addition & 0 deletions image/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import image.auth
import image.byteunit
import image.cli.cli
import image.client
import image.config
import image.configschema
Expand Down
144 changes: 144 additions & 0 deletions image/cli/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import argparse
import json
import sys
from image.auth import AUTH_FILE_PATH_DEFAULT
from image.containerimage import ContainerImage
from image.linter import ContainerImageLinter, DEFAULT_CONTAINER_IMAGE_LINTER_CONFIG
from lint.config import LinterConfig
from lint.status import LintStatus
from typing import Union

def inspect(
ref: str,
auth: str=AUTH_FILE_PATH_DEFAULT
):
"""
Inspect a container image
"""
try:
# Initialize the image
image = ContainerImage(ref)

# Load the auth
img_auth = {}
with open(auth, 'r') as auth_file:
img_auth = json.load(auth_file)

# Inspect the image
print(image.inspect(auth=img_auth))
except Exception as e:
print(
f"{type(e).__name__} while linting image: {str(e)}"
)
sys.exit(1)

def lint(
ref: str,
config: Union[str, None],
auth: str=AUTH_FILE_PATH_DEFAULT
):
"""
Lint a container image using the ContainerImageLinter
"""
try:
# Initialize the image
image = ContainerImage(ref)

# Load the auth
img_auth = {}
with open(auth, 'r') as auth_file:
img_auth = json.load(auth_file)

# Load the config
img_config = DEFAULT_CONTAINER_IMAGE_LINTER_CONFIG
if config is not None:
with open(config, 'r') as config_file:
img_config = LinterConfig(json.load(config_file))

# Initialize the linter and lint the image
linter = ContainerImageLinter()
results = linter.lint(image, config=img_config, auth=img_auth)
errors = [
result for result in results if result.status == LintStatus.ERROR
]
warnings = [
result for result in results if result.status == LintStatus.WARNING
]

# Print the results
print(f"Encountered {len(errors)} errors and {len(warnings)} warnings")
for result in errors:
print(f"- {result}")
for result in warnings:
print(f"- {result}")
if len(errors) > 0:
sys.exit(1)
except Exception as e:
print(
f"{type(e).__name__} while linting image: {str(e)}"
)
sys.exit(1)

def main(argv=None):
"""
Main command executed on entry in the containerimage-py CLI
"""
# Initialize the root command and subcommand parsers
parser = argparse.ArgumentParser(
prog="containerimage-py",
description="A CLI for interacting with container images and " + \
"container image registries based on the containerimage-py " + \
"python library."
)
parser.add_argument(
'--auth',
dest='auth',
required=False,
default=AUTH_FILE_PATH_DEFAULT,
help='A path to an image pull secret with access to your registry'
)
subparsers = parser.add_subparsers(dest='command', help='Available commands')

# Initialize the inspect subcommand
inspect_subcommand = subparsers.add_parser('inspect', help='Inspect a container image')
inspect_subcommand.add_argument(
'image',
type=str,
help='The container image reference to inspect'
)

# Initialize the lint subcommand
lint_subcommand = subparsers.add_parser('lint', help='Lint a container image')
lint_subcommand.add_argument(
'image',
type=str,
help='The container image reference to lint'
)
lint_subcommand.add_argument(
'--config',
dest='config',
required=False,
default=None,
help='A path to a linter config file for linting the container image'
)

# Execute the subcommand
args = parser.parse_args(argv)
if args.command == "lint":
lint(
ref=args.image,
config=args.config,
auth=args.auth
)
elif args.command == "inspect":
inspect(
ref=args.image,
auth=args.auth
)
else:
print(f"Unknown command: {args.command}")
parser.print_help()
sys.exit(1)

if __name__ == "__main__":
main()
Loading
Loading