Skip to content

Commit

Permalink
[backend] publisher: implement repotype 'helm'
Browse files Browse the repository at this point in the history
This generates a helm chart index from all published charts.
  • Loading branch information
mlschroe committed Aug 27, 2020
1 parent 5901f23 commit 24ba426
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/backend/BSPublisher/Helm.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ package BSPublisher::Helm;

use BSTar;
use BSUtil;
use JSON::XS ();

eval { require YAML::XS; $YAML::XS::LoadBlessed = 0; };
*YAML::XS::Dump = sub {die("YAML::XS is not available\n")} unless defined &YAML::XS::Dump;

use strict;

Expand Down Expand Up @@ -54,4 +58,49 @@ sub readhelminfo {
return $d;
}

# generate iso utc time
sub isodatetimez {
my ($t) = @_;
my @gt = gmtime($t || time());
return sprintf "%04d-%02d-%02dT%02d:%02d:%02dZ", $gt[5] + 1900, $gt[4] + 1, @gt[3,2,1,0];
}

# generate an index entry from the helminfo
sub mkindexentry {
my ($helminfo, $url) = @_;
return undef unless $helminfo->{'config_json'} && $helminfo->{'chart_sha256'};
my $chart;
eval { $chart = JSON::XS::decode_json($helminfo->{'config_json'}) };
return undef unless $chart && ref($chart) eq 'HASH';
return undef unless $chart->{'name'} eq $helminfo->{'name'}; # sanity
$chart->{'digest'} = $helminfo->{'chart_sha256'};
$chart->{'urls'} = [ $url ] if $url;
$chart->{'created'} = isodatetimez($helminfo->{'buildtime'});
return $chart;
}

# generate the index of all charts
sub mkindex {
my ($entries) = @_;
for my $ents (values %$entries) {
$ents = [ sort {$a->{'version'} cmp $b->{'version'}} @$ents ];
}
my $index = {
'apiVersion' => 'v1',
'entries' => $entries,
'generated' => isodatetimez(),
};
return $index;
}

sub toyaml {
my $yaml = YAML::XS::Dump($_[0]);
$yaml =~ s/^---\n//s;
return $yaml;
}

sub mkindex_yaml {
return toyaml(mkindex(@_));
}

1;
24 changes: 24 additions & 0 deletions src/backend/bs_publish
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,27 @@ sub createrepo_virtbuilder {
}
}

sub createrepo_helm {
my ($extrep, $projid, $repoid, $data, $options, $containers) = @_;
print " generating helm chart index\n";
my $downloadurl = BSUrlmapper::get_downloadurl("$projid/$repoid");
unlink("$extrep/index.yaml");
my %entries;
for my $p (sort keys %$containers) {
next unless ($containers->{$p}->{'type'} || '') eq 'helm';
my $helminfo = $containers->{$p};
my $entry = BSPublisher::Helm::mkindexentry($helminfo, $downloadurl ? "${downloadurl}$p" : undef);
push @{$entries{$helminfo->{'name'}}}, $entry if $entry;
}
my $index_yaml = BSPublisher::Helm::mkindex_yaml(\%entries);
writestr("$extrep/index.yaml", undef, $index_yaml);
}

sub deleterepo_helm {
my ($extrep, $projid) = @_;
unlink("$extrep/index.yaml");
}

sub createrepo_hdlist2 {
my ($extrep, $projid, $repoid, $data, $options) = @_;

Expand Down Expand Up @@ -2886,6 +2907,9 @@ sub publish {
} else {
deleterepo_zyppservice($extrep, $projid, $xrepoid, $data);
}
if ($repotype{'helm'}) {
createrepo_helm($extrep, $projid, $xrepoid, $data, $repotype{'helm'}, \%containers);
}

if ($patterntype{'ymp'}) {
createpatterns_ymp($extrep, $projid, $xrepoid, $data, $patterntype{'ymp'});
Expand Down

0 comments on commit 24ba426

Please sign in to comment.