fix(cms-deploy): preserve Compose-owned image labels
This commit is contained in:
@@ -43141,15 +43141,27 @@ CMS_BUILD_BASE_ERROR_CODES = frozenset((
|
||||
'cms-base-json-invalid','cms-base-output-limit','cms-base-image-mismatch',
|
||||
'cms-base-parent-invalid','cms-base-parent-cycle','cms-base-parent-depth',
|
||||
'cms-base-parent-layers','cms-base-parent-platform','cms-base-inventory-format',
|
||||
'cms-base-inventory-limit','cms-base-missing','cms-base-ambiguous',
|
||||
'cms-base-inventory-limit','cms-base-missing','cms-base-ambiguous','cms-base-build-labels',
|
||||
))
|
||||
CMS_BASE_REJECTION_CODES = frozenset(('layers','platform','base-shape','inherited-config','labels','environment','node-version'))
|
||||
|
||||
|
||||
def cms_base_public_diagnostic(value):
|
||||
# Public counts only, never arbitrary keys/values from image inspection.
|
||||
if not isinstance(value,dict) or set(value)!={'parentCount','nodeCount','rejected'}:return None
|
||||
if any(type(value[k]) is not int or not 0<=value[k]<=384 for k in ('parentCount','nodeCount')):return None
|
||||
rejected=value['rejected']
|
||||
if not isinstance(rejected,dict) or not set(rejected)<=CMS_BASE_REJECTION_CODES:return None
|
||||
if any(type(v) is not int or not 0<=v<=384 for v in rejected.values()):return None
|
||||
return {'parentCount':value['parentCount'],'nodeCount':value['nodeCount'],'rejected':dict(rejected)}
|
||||
|
||||
|
||||
class CmsBuildBaseError(DeployError):
|
||||
def __init__(self, code):
|
||||
def __init__(self, code, diagnostic=None):
|
||||
if code not in CMS_BUILD_BASE_ERROR_CODES:
|
||||
raise ValueError('Unknown CMS base error code')
|
||||
self.code=code
|
||||
self.diagnostic=cms_base_public_diagnostic(diagnostic)
|
||||
super().__init__(code)
|
||||
|
||||
|
||||
@@ -43180,7 +43192,35 @@ def cms_inspect_image(identity):
|
||||
return rows[0]
|
||||
|
||||
|
||||
def cms_node_base_matches(image, accepted):
|
||||
def cms_image_build_labels(config):
|
||||
labels=config.get('Labels')
|
||||
if labels is None:return {}
|
||||
if not isinstance(labels,dict) or any(not isinstance(k,str) or not isinstance(v,str) for k,v in labels.items()):
|
||||
raise CmsBuildBaseError('cms-base-build-labels')
|
||||
owned={k:v for k,v in labels.items() if k.startswith('com.docker.compose.')}
|
||||
if not owned:return {}
|
||||
prefix='com.docker.compose.'
|
||||
if (set(owned)!={prefix+'project',prefix+'service',prefix+'version'}
|
||||
or owned[prefix+'project']!='dc-cms' or owned[prefix+'service']!='cms-app'
|
||||
or len(owned[prefix+'version'])>80
|
||||
or not re.fullmatch(r'[0-9]+\.[0-9]+\.[0-9]+(?:[-+][A-Za-z0-9.-]+)?',owned[prefix+'version'])):
|
||||
raise CmsBuildBaseError('cms-base-build-labels')
|
||||
return owned
|
||||
|
||||
|
||||
def cms_base_labels_match(config, accepted):
|
||||
# Compose's BuildKit path adds exactly project/service/version to the app
|
||||
# image, not its Node base. Reproduce these labels; never ignore drift.
|
||||
try:build_labels=cms_image_build_labels(accepted)
|
||||
except CmsBuildBaseError:return False
|
||||
base=config.get('Labels');old=accepted.get('Labels')
|
||||
if base==old:return True
|
||||
if not build_labels or (base is not None and not isinstance(base,dict)):return False
|
||||
base=base or {}
|
||||
return not set(base)&set(build_labels) and {**base,**build_labels}==old
|
||||
|
||||
|
||||
def cms_node_base_rejection(image, accepted):
|
||||
# Prove filesystem ancestry AND the exact config inherited by the fixed
|
||||
# Dockerfile. Neither a mutable tag nor a matching NODE_VERSION is proof.
|
||||
layers=(image.get('RootFS') or {}).get('Layers')
|
||||
@@ -43190,27 +43230,39 @@ def cms_node_base_matches(image, accepted):
|
||||
or not isinstance(layers,list) or not isinstance(original,list) or not layers
|
||||
or len(layers)>=len(original) or original[:len(layers)]!=layers
|
||||
or any(not re.fullmatch('sha256:[a-f0-9]{64}',str(v)) for v in layers)):
|
||||
return False
|
||||
return 'layers'
|
||||
if any(image.get(key)!=accepted.get(key) for key in ('Os','Architecture','Variant')):
|
||||
return False
|
||||
return 'platform'
|
||||
config=image.get('Config') or {};old=accepted.get('Config') or {}
|
||||
if (image.get('Os')!='linux' or config.get('Cmd')!=['node']
|
||||
or config.get('WorkingDir') not in ('',None)
|
||||
or config.get('OnBuild') not in (None,[]) or config.get('Volumes') not in (None,{})
|
||||
or config.get('ExposedPorts') not in (None,{})):
|
||||
return False
|
||||
for key in ('User','Entrypoint','Shell','StopSignal','Healthcheck','Labels'):
|
||||
if config.get(key)!=old.get(key):return False
|
||||
return 'base-shape'
|
||||
for key in ('User','Entrypoint','Shell','StopSignal','Healthcheck'):
|
||||
if config.get(key)!=old.get(key):return 'inherited-config'
|
||||
if not cms_base_labels_match(config,old):return 'labels'
|
||||
try:
|
||||
env=cms_image_env(config);old_env=cms_image_env(old)
|
||||
except DeployError:
|
||||
return False
|
||||
if not re.fullmatch(r'24\.[0-9]+\.[0-9]+',env.get('NODE_VERSION','')):return False
|
||||
return {**env,'HOST':'0.0.0.0','PORT':'8090'}==old_env
|
||||
return 'environment'
|
||||
if not re.fullmatch(r'24\.[0-9]+\.[0-9]+',env.get('NODE_VERSION','')):return 'node-version'
|
||||
if {**env,'HOST':'0.0.0.0','PORT':'8090'}!=old_env:return 'environment'
|
||||
return None
|
||||
|
||||
|
||||
def cms_node_base_matches(image, accepted):
|
||||
return cms_node_base_rejection(image,accepted) is None
|
||||
|
||||
|
||||
def cms_resolve_build_base(runtime):
|
||||
accepted=cms_inspect_image(runtime['imageId'])
|
||||
diagnostic={'parentCount':0,'nodeCount':0,'rejected':{}}
|
||||
def matches(image,kind):
|
||||
diagnostic[kind]+=1
|
||||
reason=cms_node_base_rejection(image,accepted)
|
||||
if reason is not None:diagnostic['rejected'][reason]=diagnostic['rejected'].get(reason,0)+1
|
||||
return reason is None
|
||||
# Legacy builds retain immutable Parent edges, including untagged bases.
|
||||
# Follow this app's ancestry, never enumerate another component's cache.
|
||||
current=accepted;seen={accepted['Id']};base=None
|
||||
@@ -43231,7 +43283,7 @@ def cms_resolve_build_base(runtime):
|
||||
raise CmsBuildBaseError('cms-base-parent-layers')
|
||||
if any(image.get(k)!=current.get(k) for k in ('Os','Architecture','Variant')):
|
||||
raise CmsBuildBaseError('cms-base-parent-platform')
|
||||
if cms_node_base_matches(image,accepted):
|
||||
if matches(image,'parentCount'):
|
||||
base=image;break
|
||||
current=image
|
||||
else:
|
||||
@@ -43246,13 +43298,13 @@ def cms_resolve_build_base(runtime):
|
||||
if len(rows)>256:raise CmsBuildBaseError('cms-base-inventory-limit')
|
||||
if any(not isinstance(v,str) or not re.fullmatch('sha256:[a-f0-9]{64}',v) for v in rows):
|
||||
raise CmsBuildBaseError('cms-base-inventory-format')
|
||||
matches=[]
|
||||
found=[]
|
||||
for identity in sorted(set(rows)):
|
||||
image=cms_inspect_image(identity)
|
||||
if cms_node_base_matches(image,accepted):matches.append(image)
|
||||
if not matches:raise CmsBuildBaseError('cms-base-missing')
|
||||
if len(matches)!=1:raise CmsBuildBaseError('cms-base-ambiguous')
|
||||
base=matches[0];method='node-cache'
|
||||
if matches(image,'nodeCount'):found.append(image)
|
||||
if not found:raise CmsBuildBaseError('cms-base-missing',diagnostic)
|
||||
if len(found)!=1:raise CmsBuildBaseError('cms-base-ambiguous',diagnostic)
|
||||
base=found[0];method='node-cache'
|
||||
proof={'imageId':base['Id'],'acceptedImageId':accepted['Id'],
|
||||
'layersSha256':cms_audit_digest(base['RootFS']['Layers']),
|
||||
'configSha256':cms_audit_digest(base['Config'])}
|
||||
@@ -43301,7 +43353,9 @@ def build_cms_release(before):
|
||||
(context/'Dockerfile').write_bytes(('FROM '+base['Id']+'\n').encode()+dockerfile.split(b'\n',1)[1])
|
||||
# Docker 24's legacy builder accepts a local image ID in FROM. BuildKit
|
||||
# interprets sha256:<id> as a registry repository and must not be used.
|
||||
subprocess.run([str(DOCKER),'build','--pull=false','--file',str(context/'Dockerfile'),'--tag',before['runtime']['imageRef'],str(context)],
|
||||
labels=cms_image_build_labels(accepted.get('Config') or {})
|
||||
label_args=[item for key,value in sorted(labels.items()) for item in ('--label',key+'='+value)]
|
||||
subprocess.run([str(DOCKER),'build','--pull=false',*label_args,'--file',str(context/'Dockerfile'),'--tag',before['runtime']['imageRef'],str(context)],
|
||||
check=True,env={**os.environ,'DOCKER_BUILDKIT':'0'})
|
||||
image=docker_json(['image','inspect',before['runtime']['imageRef']],'CMS built image')
|
||||
if not isinstance(image,list) or len(image)!=1:die('CMS built image is ambiguous')
|
||||
@@ -44036,6 +44090,8 @@ def cms_autodeploy_error_status(error, policy_errors):
|
||||
# private, even if a caller mutates the exception's message or code.
|
||||
if isinstance(error,CmsBuildBaseError) and error.code in CMS_BUILD_BASE_ERROR_CODES:
|
||||
result['reason']=error.code
|
||||
diagnostic=cms_base_public_diagnostic(error.diagnostic)
|
||||
if diagnostic is not None:result['baseLookup']=diagnostic
|
||||
return result
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user