This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from chainguard-dev/production-images
Production Images Update
- Loading branch information
Showing
15 changed files
with
348 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Command\Build; | ||
|
||
use App\ImageCollection; | ||
use Autodocs\DataFeed\JsonDataFeed; | ||
use Autodocs\Exception\NotFoundException; | ||
use autodocs\Service\AutodocsService; | ||
use Minicli\Command\CommandController; | ||
use Exception; | ||
use TypeError; | ||
|
||
class DatafeedsController extends CommandController | ||
{ | ||
/** | ||
* @throws NotFoundException | ||
*/ | ||
public function handle(): void | ||
{ | ||
/** @var AutodocsService $autodocs */ | ||
$autodocs = $this->getApp()->autodocs; | ||
|
||
try { | ||
$imagesDevList = $autodocs->getDataFeed('images-tags-dev.json'); | ||
$imagesProdList = $autodocs->getDataFeed('images-tags-prod.json'); | ||
} catch (Exception $exception) { | ||
$this->error("Error: ".$exception->getMessage()); | ||
return; | ||
} catch (TypeError $error) { | ||
$this->error("Error: ".$error->getMessage()); | ||
return; | ||
} | ||
|
||
$cacheDev = $this->getApp()->autodocs->config['cache_dir'].'/images-dev'; | ||
$cacheProd = $this->getApp()->autodocs->config['cache_dir'].'/images-prod'; | ||
|
||
$images = new ImageCollection(); | ||
//all dev images are in the prod list, the opposite is not true. use prod images as source of truth | ||
foreach ($imagesProdList->json as $imageInfo) { | ||
$image = $images->get($imageInfo['repo']['name'], true); | ||
$image->tagsProd = $imageInfo['tags']; | ||
if (array_key_exists('readme', $imageInfo['repo'])) { | ||
$image->readmeProd = $imageInfo['repo']['readme']; | ||
} | ||
$image->variants = $this->getImageConfigs($image->getName(), $cacheProd); | ||
$images->add($image); | ||
$autodocs->storage->saveFile($autodocs->config['cache_dir'].'/datafeeds/'.$image->name.".json", $image->getJson()); | ||
} | ||
//get complimentary information about public tags, readme | ||
foreach ($imagesDevList->json as $imageInfo) { | ||
$image = $images->get($imageInfo['repo']['name'], true); | ||
$image->tagsDev = $imageInfo['tags']; | ||
if (array_key_exists('readme', $imageInfo['repo'])) { | ||
$image->readmeDev = $imageInfo['repo']['readme']; | ||
} | ||
$images->add($image); | ||
$autodocs->storage->saveFile($autodocs->config['cache_dir'].'/datafeeds/'.$image->name.".json", $image->getJson()); | ||
} | ||
|
||
$this->success("Finished building image Datafeeds based on cached data."); | ||
} | ||
|
||
/** | ||
* @throws NotFoundException | ||
*/ | ||
public function getImageConfigs(string $image, string $cachePath): array | ||
{ | ||
$variantsList = []; | ||
|
||
foreach (glob($cachePath.'/*.json') as $imageConfig) { | ||
if (str_starts_with(basename($imageConfig), $image.'.latest')) { | ||
|
||
list($imageName, $variantName, $extension) = explode('.', basename($imageConfig)); | ||
try { | ||
$dataFeed = new JsonDataFeed(); | ||
$dataFeed->loadFile($imageConfig); | ||
$variantsList[$variantName] = $dataFeed->json; | ||
} catch (TypeError $e) { | ||
//json might have issues. skip | ||
} | ||
} | ||
} | ||
|
||
return $variantsList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
use Autodocs\DataFeed\JsonDataFeed; | ||
use Autodocs\Exception\NotFoundException; | ||
use Autodocs\Mark; | ||
|
||
class Image | ||
{ | ||
public string $name; | ||
|
||
public string $readmeDev = ""; | ||
public string $readmeProd = ""; | ||
public array $tagsDev = []; | ||
public array $tagsProd = []; | ||
public array $variants = []; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getJson(): string | ||
{ | ||
return json_encode([ | ||
'name' => $this->name, | ||
'tagsDev' => $this->tagsDev, | ||
'tagsProd' => $this->tagsProd, | ||
'readmeDev' => $this->readmeDev, | ||
'readmeProd' => $this->readmeProd, | ||
'variants' => $this->variants, | ||
]); | ||
} | ||
|
||
public function getReadme(string $fallback): string | ||
{ | ||
if ("" === $this->readmeDev || empty($this->readmeDev)) { | ||
if ("" === $this->readmeProd || empty($this->readmeProd)) { | ||
return $fallback; | ||
} | ||
return $this->readmeProd; | ||
} | ||
return $this->readmeDev; | ||
} | ||
|
||
public function getRegistryTagsTable(): string | ||
{ | ||
$tagsDev = $tagsProd = []; | ||
foreach ($this->tagsDev as $tag) { | ||
$tagsDev[] = $tag['name']; | ||
} | ||
foreach ($this->tagsProd as $tag) { | ||
$tagsProd[] = $tag['name']; | ||
} | ||
$rows = [ | ||
['`cgr.dev/chainguard`', count($tagsDev) ? implode(', ', $tagsDev) : "No public tags are available for this image."], | ||
['`cgr.dev/chainguard-private`', count($tagsProd) ? implode(', ', $tagsProd) : "No production tags are available for this image."] | ||
]; | ||
|
||
return Mark::table($rows, ['Registry', 'Tags']); | ||
} | ||
|
||
/** | ||
* @throws NotFoundException | ||
*/ | ||
public static function loadFromDatafeed(string $datafeed): Image | ||
{ | ||
$imageDatafeed = new JsonDataFeed(); | ||
$imageDatafeed->loadFile($datafeed); | ||
$image = new Image($imageDatafeed->json['name']); | ||
|
||
$image->tagsDev = $imageDatafeed->json['tagsDev']; | ||
$image->tagsProd = $imageDatafeed->json['tagsProd']; | ||
$image->readmeDev = $imageDatafeed->json['readmeDev']; | ||
$image->readmeProd = $imageDatafeed->json['readmeProd']; | ||
$image->variants = $imageDatafeed->json['variants']; | ||
|
||
return $image; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
class ImageCollection | ||
{ | ||
protected array $images = []; | ||
|
||
public function add(Image $image): void | ||
{ | ||
$this->images[] = $image; | ||
} | ||
|
||
public function addImages(array $images): void | ||
{ | ||
foreach ($images as $image) { | ||
$this->add($image); | ||
} | ||
} | ||
|
||
public function getImages(): array | ||
{ | ||
return $this->images; | ||
} | ||
|
||
public function get(string $imageName, $createNew = false): ?Image | ||
{ | ||
/** @var Image $image */ | ||
foreach ($this->images as $image) { | ||
if ($image->getName() === $imageName) { | ||
return $image; | ||
} | ||
} | ||
|
||
if ($createNew) { | ||
return new Image($imageName); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.