Skip to content

Commit

Permalink
Merge pull request #14226 from mlschroe/master
Browse files Browse the repository at this point in the history
[backend] BSContar: split normalize_layer from create_layer_data
  • Loading branch information
mlschroe committed Apr 25, 2023
2 parents bdf5abd + 5664974 commit 73a6ddf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
22 changes: 16 additions & 6 deletions src/backend/BSContar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,14 @@ sub create_config_data {
return $config_data;
}

sub create_layer_data {
sub normalize_layer {
my ($layer_ent, $oci, $comp, $newcomp, $lcomp) = @_;
$comp ||= detect_entry_compression($layer_ent);
$lcomp = $comp unless defined $lcomp;
$comp = 'zstd' if $comp && $comp =~ /^zstd:chunked/;
$comp = detect_entry_compression($layer_ent) unless defined $comp;
$newcomp ||= $oci && $comp eq 'zstd' ? 'zstd' : 'gzip';
$newcomp = $comp if $layer_ent->{'mimetype'}; # do not change the compression if the mime type is already set
return ($layer_ent, $lcomp) if $newcomp eq 'zstd' && $comp eq $newcomp && $lcomp && $lcomp =~ /^zstd:chunked/;
return ($layer_ent, $comp) if $layer_ent->{'mimetype'}; # do not change the compression if the mime type is already set
if ($comp ne $newcomp) {
if ($comp) {
print "recompressing $layer_ent->{'name'}... ";
Expand All @@ -459,14 +462,21 @@ sub create_layer_data {
}
$layer_ent = compress_entry($layer_ent, $comp, $newcomp);
print "done.\n";
undef $lcomp;
}
return ($layer_ent, $newcomp);
}

sub create_layer_data {
my ($layer_ent, $oci, $comp) = @_;
my $lcomp = $comp;
$comp = 'zstd' if $comp && $comp =~ /^zstd:chunked/;
$comp = detect_entry_compression($layer_ent) unless defined $comp;
my $layer_data = {
'mediaType' => $layer_ent->{'mimetype'} || ($oci ? ($newcomp eq 'zstd' ? $mt_oci_layer_zstd : $mt_oci_layer_gzip) : $mt_docker_layer_gzip),
'mediaType' => $layer_ent->{'mimetype'} || ($oci ? ($comp eq 'zstd' ? $mt_oci_layer_zstd : $mt_oci_layer_gzip) : $mt_docker_layer_gzip),
'size' => $layer_ent->{'size'},
'digest' => $layer_ent->{'blobid'} || blobid_entry($layer_ent),
};
if ($newcomp eq 'zstd' && $lcomp && $lcomp =~ /^zstd:chunked/) {
if ($comp eq 'zstd' && $lcomp && $lcomp =~ /^zstd:chunked/) {
my @c = split(',', $lcomp);
$layer_data->{'annotations'}->{'io.github.containers.zstd-chunked.manifest-position'} = $c[1] if $c[1];
$layer_data->{'annotations'}->{'io.github.containers.zstd-chunked.manifest-checksum'} = $c[2] if $c[2];
Expand Down
7 changes: 2 additions & 5 deletions src/backend/BSPublisher/Container.pm
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,8 @@ sub create_container_dist_info {
my $layer_ent = $tar{$layer_file};
die("file $layer_file not included in tar\n") unless $layer_ent;
my $lcomp = shift @layer_comp;
my $comp;
$comp = 'gzip' if $lcomp && $lcomp eq 'gzip';
$comp = 'zstd' if $lcomp && ($lcomp eq 'zstd' || $lcomp =~ /^zstd:/);
my $layer_data;
($layer_ent, $layer_data) = BSContar::create_layer_data($layer_ent, $oci, $comp, undef, $lcomp);
($layer_ent, $lcomp) = BSContar::normalize_layer($layer_ent, $oci, $lcomp);
my $layer_data = BSContar::create_layer_data($layer_ent, $oci, $lcomp);
push @layer_data, $layer_data;
}
my $mani = BSContar::create_dist_manifest_data($config_data, \@layer_data, $oci);
Expand Down
6 changes: 2 additions & 4 deletions src/backend/BSPublisher/Registry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,8 @@ sub push_containers {
}
my $layer_ent = $tar{$layer_file};
die("File $layer_file not included in tar\n") unless $layer_ent;
my $comp = 'gzip';
$comp = 'zstd' if $lcomp && ($lcomp eq 'zstd' || $lcomp =~ /^zstd:/);
my $layer_data;
($layer_ent, $layer_data) = BSContar::create_layer_data($layer_ent, $oci, $comp, $comp, $lcomp);
$lcomp = 'gzip' unless $oci && $lcomp && ($lcomp eq 'zstd' || $lcomp =~ /^zstd:/);
my $layer_data = BSContar::create_layer_data($layer_ent, $oci, $lcomp);
push @layer_data, $layer_data;
$layer_datas{$layer_file} = $layer_data;

Expand Down
6 changes: 3 additions & 3 deletions src/backend/bs_regpush
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,10 @@ for my $tarfile (@tarfiles) {
}
my $layer_ent = $tar{$layer_file};
die("File $layer_file not included in tar\n") unless $layer_ent;

# normalize layer
($layer_ent, $lcomp) = BSContar::normalize_layer($layer_ent, $oci, undef, undef, $lcomp);
# create layer data
my $layer_data;
($layer_ent, $layer_data) = BSContar::create_layer_data($layer_ent, $oci, undef, undef, $lcomp);
my $layer_data = BSContar::create_layer_data($layer_ent, $oci, $lcomp);
push @layer_data, $layer_data;
$layer_datas{$layer_file} = $layer_data;

Expand Down

0 comments on commit 73a6ddf

Please sign in to comment.