This commit is contained in:
Quentin 2023-05-03 10:52:34 +02:00
parent f2e3de4b5d
commit 40fd4646ce
Signed by: quentin
GPG key ID: E9602264D639FF68
9 changed files with 187 additions and 189 deletions

View file

@ -2,8 +2,8 @@ package main
import ( import (
"fmt" "fmt"
"os"
"git.deuxfleurs.fr/deuxfleurs/albatros/cmd" "git.deuxfleurs.fr/deuxfleurs/albatros/cmd"
"os"
) )
func main() { func main() {

View file

@ -1,10 +1,10 @@
package cmd package cmd
import ( import (
"crypto/sha256"
"errors"
"context" "context"
"crypto/sha256"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -13,17 +13,17 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/containers/image/v5/transports/alltransports"
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/copy" "github.com/containers/image/v5/copy"
"github.com/containers/image/v5/signature"
"github.com/containers/image/v5/transports/alltransports"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gocloud.dev/blob" "gocloud.dev/blob"
_ "gocloud.dev/blob/s3blob" _ "gocloud.dev/blob/s3blob"
) )
var pctx *signature.PolicyContext var pctx *signature.PolicyContext
const distributionPrefix = "v2"
const distributionPrefix = "v2"
//--- //---
//--- Image converter //--- Image converter
@ -33,6 +33,7 @@ type OCIImageManifest struct {
Config OCIRef `json:"config"` Config OCIRef `json:"config"`
Layers []OCIRef `json:layers"` Layers []OCIRef `json:layers"`
} }
func LoadOCIImageManifest(path string) (*OCIImageManifest, error) { func LoadOCIImageManifest(path string) (*OCIImageManifest, error) {
fd, err := os.Open(path) fd, err := os.Open(path)
if err != nil { if err != nil {
@ -71,8 +72,9 @@ type OCIImageIndex struct {
MediaType string `json:"mediaType"` MediaType string `json:"mediaType"`
Manifests []OCIRef `json:"manifests"` Manifests []OCIRef `json:"manifests"`
} }
func NewOCIImageIndex(o *OCIMultiArch) (OCIImageIndex, error) { func NewOCIImageIndex(o *OCIMultiArch) (OCIImageIndex, error) {
idx := OCIImageIndex { idx := OCIImageIndex{
SchemaVersion: 2, SchemaVersion: 2,
MediaType: "application/vnd.oci.image.index.v1+json", MediaType: "application/vnd.oci.image.index.v1+json",
Manifests: []OCIRef{}, Manifests: []OCIRef{},
@ -130,6 +132,7 @@ func (i OCIImageIndex) WriteTo(root string) error {
type OCILayout struct { type OCILayout struct {
ImageLayoutVersion string `json:"imageLayoutVersion"` ImageLayoutVersion string `json:"imageLayoutVersion"`
} }
func NewOCILayout() OCILayout { func NewOCILayout() OCILayout {
return OCILayout{"1.0.0"} return OCILayout{"1.0.0"}
} }
@ -151,8 +154,9 @@ type OCISystemImage struct {
arch string arch string
index *OCIImageIndex index *OCIImageIndex
} }
func NewOCISystemImage(path, os, arch string) (OCISystemImage, error) { func NewOCISystemImage(path, os, arch string) (OCISystemImage, error) {
si := OCISystemImage { si := OCISystemImage{
path: path, path: path,
os: os, os: os,
arch: arch, arch: arch,
@ -171,7 +175,7 @@ func NewOCISystemImage(path, os, arch string) (OCISystemImage, error) {
} }
// Enrich manifest // Enrich manifest
si.index.Manifests[0].Platform = &OCIPlatform { si.index.Manifests[0].Platform = &OCIPlatform{
Architecture: arch, Architecture: arch,
OS: os, OS: os,
} }
@ -201,7 +205,7 @@ func NewOCIMultiArch(nametag string) (*OCIMultiArch, error) {
return nil, err return nil, err
} }
return &OCIMultiArch { return &OCIMultiArch{
Name: ntspl[0], Name: ntspl[0],
Tag: ntspl[1], Tag: ntspl[1],
path: tmp, path: tmp,
@ -252,7 +256,6 @@ func (o *OCIMultiArch) LoadFromDockerArchives(path string) error {
return err return err
} }
// Convert the docker archive to an oci image // Convert the docker archive to an oci image
_, err = copy.Image(context.Background(), pctx, dstRef, srcRef, &copy.Options{}) _, err = copy.Image(context.Background(), pctx, dstRef, srcRef, &copy.Options{})
if err != nil { if err != nil {
@ -288,7 +291,6 @@ func (o *OCIMultiArch) MergeSystemImages() error {
return err return err
} }
// Create the oci-layout file // Create the oci-layout file
err = NewOCILayout().WriteTo(multiArchRoot) err = NewOCILayout().WriteTo(multiArchRoot)
if err != nil { if err != nil {
@ -308,7 +310,6 @@ func (o *OCIMultiArch) MergeSystemImages() error {
o.multi = &idx o.multi = &idx
fmt.Printf("-> index.json\n") fmt.Printf("-> index.json\n")
// Copy blobs // Copy blobs
for _, img := range o.images { for _, img := range o.images {
blobCounter := 0 blobCounter := 0
@ -462,7 +463,6 @@ func (o *OCIMultiArch) UploadImageS3(buck *blob.Bucket) error {
return nil return nil
} }
type StaticRegistryManager struct { type StaticRegistryManager struct {
name string name string
buck *blob.Bucket buck *blob.Bucket

View file

@ -44,7 +44,6 @@ type Manifest struct {
Tags []string `json:"tags"` Tags []string `json:"tags"`
} }
//--- //---
//--- Collect data on the filesystem //--- Collect data on the filesystem
type Platform struct { type Platform struct {
@ -83,8 +82,7 @@ func CollectPlatforms(path string) ([]Platform, error) {
filenames = append(filenames, f.Name()) filenames = append(filenames, f.Name())
} }
plat := Platform{
plat := Platform {
OS: osDir.Name(), OS: osDir.Name(),
Arch: archDir.Name(), Arch: archDir.Name(),
Root: root, Root: root,
@ -98,7 +96,7 @@ func CollectPlatforms(path string) ([]Platform, error) {
} }
func (p *Platform) BuildRegistryPlatform() RegistryPlatform { func (p *Platform) BuildRegistryPlatform() RegistryPlatform {
return RegistryPlatform { return RegistryPlatform{
Architecture: p.Arch, Architecture: p.Arch,
OS: p.OS, OS: p.OS,
} }
@ -140,7 +138,7 @@ func NewArtifact(nametag, path string) (Artifact, error) {
} }
func (a *Artifact) UpdateManifest() Manifest { func (a *Artifact) UpdateManifest() Manifest {
return Manifest { return Manifest{
Name: a.Name, Name: a.Name,
Tags: []string{a.Tag}, //@FIXME we must fetch the other tags of the repo Tags: []string{a.Tag}, //@FIXME we must fetch the other tags of the repo
} }
@ -149,7 +147,7 @@ func (a *Artifact) UpdateManifest() Manifest {
func (a *Artifact) BuildTag() Tag { func (a *Artifact) BuildTag() Tag {
t := Tag{Flavors: []Flavor{}} t := Tag{Flavors: []Flavor{}}
for _, p := range a.Platforms { for _, p := range a.Platforms {
f := Flavor { f := Flavor{
Resources: p.BuildResources(), Resources: p.BuildResources(),
Platform: p.BuildRegistryPlatform(), Platform: p.BuildRegistryPlatform(),
} }
@ -212,6 +210,7 @@ type BucketUploader struct {
reader io.ReadCloser reader io.ReadCloser
options *blob.WriterOptions options *blob.WriterOptions
} }
func NewUploadFromFS(buck *blob.Bucket, path string) (*BucketUploader, error) { func NewUploadFromFS(buck *blob.Bucket, path string) (*BucketUploader, error) {
fd, err := os.Open(path) fd, err := os.Open(path)
if err != nil { if err != nil {

View file

@ -1,18 +1,17 @@
package pkg package pkg
import ( import (
"log"
consul "github.com/hashicorp/consul/api" consul "github.com/hashicorp/consul/api"
nomad "github.com/hashicorp/nomad/api" nomad "github.com/hashicorp/nomad/api"
"log"
) )
type Cluster struct { type Cluster struct {
Nomad *nomad.Client Nomad *nomad.Client
Consul *consul.Client Consul *consul.Client
} }
func NewCluster(conf* Config) Cluster { func NewCluster(conf *Config) Cluster {
cluster := Cluster{} cluster := Cluster{}
// Init Nomad // Init Nomad

View file

@ -1,9 +1,9 @@
package pkg package pkg
import ( import (
"log"
"fmt" "fmt"
"github.com/caarlos0/env/v7" "github.com/caarlos0/env/v7"
"log"
) )
type Config struct { type Config struct {

View file

@ -4,10 +4,10 @@ import (
b64 "encoding/base64" b64 "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"golang.org/x/exp/slices"
"io" "io"
"log" "log"
"net/http" "net/http"
"golang.org/x/exp/slices"
"strings" "strings"
nomad "github.com/hashicorp/nomad/api" nomad "github.com/hashicorp/nomad/api"
@ -101,7 +101,7 @@ func (h HookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Created job %s for %s\n", dres.DispatchedJobID, notifInfo) log.Printf("Created job %s for %s\n", dres.DispatchedJobID, notifInfo)
// Start a lifecycle observer to update gitea status // Start a lifecycle observer to update gitea status
j := Job { j := Job{
Cluster: h.Cluster, Cluster: h.Cluster,
Config: h.Config, Config: h.Config,
Notification: &notification, Notification: &notification,

View file

@ -1,9 +1,9 @@
package pkg package pkg
import ( import (
"log"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
nomad "github.com/hashicorp/nomad/api" nomad "github.com/hashicorp/nomad/api"
"log"
) )
type Job struct { type Job struct {