Skip to content

Commit

Permalink
push all images to registry. verified harbor core image is working as…
Browse files Browse the repository at this point in the history
… expected
  • Loading branch information
Vad1mo committed Nov 30, 2024
1 parent e11e143 commit ca8d7f1
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 48 deletions.
150 changes: 113 additions & 37 deletions .dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,25 @@ const (
GORELEASER_VERSION = "v2.3.2"
)

type Package string
type Platform string

var (
SupportedPlatforms = []string{"linux/arm64", "linux/amd64"}
packages = []string{"core", "jobservice", "registryctl", "cmd/exporter", "cmd/standalone-db-migrator"}
targetPlatforms = []Platform{"linux/arm64", "linux/amd64"}
packages = []Package{"core", "jobservice", "registryctl", "cmd/exporter", "cmd/standalone-db-migrator"}
//packages = []string{"core", "jobservice"}
)

type BuildMetadata struct {
Package string
Package Package
BinaryPath string
Container *dagger.Container
Platform string
Platform Platform
}

func New(
// Local or remote directory with source code, defaults to "./"
// +optional
// +defaultPath="./"
// +optional
// +defaultPath="./"
source *dagger.Directory,
) *Harbor {
return &Harbor{Source: source}
Expand All @@ -41,17 +43,90 @@ type Harbor struct {
Source *dagger.Directory
}

func (m *Harbor) ExportAllImages(ctx context.Context) (string, error) {
metdata := m.buildAllImages(ctx)
for _, meta := range metdata {
export, err := meta.Container.Export(ctx, fmt.Sprintf("bin/container/%s/%s.tgz", meta.Platform, meta.Package))
export, err := meta.Container.AsTarball(ctx, fmt.Sprintf("bin/container/%s/%s.tgz", meta.Platform, meta.Package))
println(export)
func (m *Harbor) PublishImageAllImages(
ctx context.Context,
registry, registryUsername string,
imageTags []string,
registryPassword *dagger.Secret) string {

allImages := m.buildAllImages(ctx)

for i, tag := range imageTags {
imageTags[i] = strings.TrimSpace(tag)
if strings.HasPrefix(imageTags[i], "v") {
imageTags[i] = strings.TrimPrefix(imageTags[i], "v")
}
}
fmt.Printf("provided tags: %s\n", imageTags)

platformVariantsContainer := make(map[Package][]*dagger.Container)
for _, meta := range allImages {
platformVariantsContainer[meta.Package] = append(platformVariantsContainer[meta.Package], meta.Container)
}

for pkg, imgs := range platformVariantsContainer {
for _, imageTag := range imageTags {
_, err := dag.Container().WithRegistryAuth(registry, registryUsername, registryPassword).Publish(ctx,
fmt.Sprintf("%s/%s/%s:%s", registry, "harbor", pkg, imageTag),
dagger.ContainerPublishOpts{PlatformVariants: imgs},
)
if err != nil {
panic(err)
}
}
}
return "allImageAddrs"
}

func (m *Harbor) PublishImage(
ctx context.Context,
registry, registryUsername string,
imageTags []string,
registryPassword *dagger.Secret) []string {

releaseImages := []*dagger.Container{}

for i, tag := range imageTags {
imageTags[i] = strings.TrimSpace(tag)
if strings.HasPrefix(imageTags[i], "v") {
imageTags[i] = strings.TrimPrefix(imageTags[i], "v")
}
}
fmt.Printf("provided tags: %s\n", imageTags)

for _, platform := range targetPlatforms {
for _, pkg := range packages {
build := m.buildImage(ctx, platform, pkg)
if strings.HasPrefix(string(platform), "linux") {
releaseImages = append(releaseImages, build.Container)
}
}
}

imageAddrs := []string{}
for _, imageTag := range imageTags {
addr, err := dag.Container().WithRegistryAuth(registry, registryUsername, registryPassword).
Publish(ctx,
fmt.Sprintf("%s/%s/harbor-cli:%s", registry, "harbor-cli", imageTag),
dagger.ContainerPublishOpts{PlatformVariants: releaseImages},
)

if err != nil {
return "", err
panic(err)
}
fmt.Printf("Published image address: %s\n", addr)
imageAddrs = append(imageAddrs, addr)
}
return imageAddrs
}

func (m *Harbor) ExportAllImages(ctx context.Context) *dagger.Directory {
metdata := m.buildAllImages(ctx)
artifacts := dag.Directory()
for _, meta := range metdata {
artifacts = artifacts.WithFile(fmt.Sprintf("containers/%s/%s.tgz", meta.Platform, meta.Package), meta.Container.AsTarball())
}
return "bin/container", nil
return artifacts
}

func (m *Harbor) BuildAllImages(ctx context.Context) []*dagger.Container {
Expand All @@ -65,7 +140,7 @@ func (m *Harbor) BuildAllImages(ctx context.Context) []*dagger.Container {

func (m *Harbor) buildAllImages(ctx context.Context) []*BuildMetadata {
var buildMetadata []*BuildMetadata
for _, platform := range SupportedPlatforms {
for _, platform := range targetPlatforms {
for _, pkg := range packages {
img := m.BuildImage(ctx, platform, pkg)
buildMetadata = append(buildMetadata, &BuildMetadata{
Expand All @@ -75,22 +150,26 @@ func (m *Harbor) buildAllImages(ctx context.Context) []*BuildMetadata {
Platform: platform,
})
}
// build portal
}
return buildMetadata
}

func (m *Harbor) BuildImage(ctx context.Context, platform string, pkg string) *dagger.Container {
return m.buildImage(ctx, platform, pkg).Container
func (m *Harbor) BuildImage(ctx context.Context, platform Platform, pkg Package) *dagger.Container {
buildMtd := m.buildImage(ctx, platform, pkg)
if pkg == "core" {
buildMtd.Container = buildMtd.Container.WithDirectory("/migrations", m.Source.Directory("make/migrations"))
}
return buildMtd.Container

}

func (m *Harbor) buildImage(ctx context.Context, platform string, pkg string) *BuildMetadata {
build := m.buildBinary(ctx, platform, pkg)
img := dag.Container(dagger.ContainerOpts{Platform: dagger.Platform(platform)}).
WithFile("/"+pkg, build.Container.File(build.BinaryPath)).
WithEntrypoint([]string{"/" + pkg})
build.Container = img
return build
func (m *Harbor) buildImage(ctx context.Context, platform Platform, pkg Package) *BuildMetadata {
buildMtd := m.buildBinary(ctx, platform, pkg)
img := dag.Container(dagger.ContainerOpts{Platform: dagger.Platform(string(platform))}).
WithFile("/"+string(pkg), buildMtd.Container.File(buildMtd.BinaryPath)).
WithEntrypoint([]string{"/" + string(pkg)})
buildMtd.Container = img
return buildMtd
}

func (m *Harbor) BuildAllBinaries(ctx context.Context) *dagger.Directory {
Expand All @@ -100,29 +179,27 @@ func (m *Harbor) BuildAllBinaries(ctx context.Context) *dagger.Directory {
output = output.WithFile(build.BinaryPath, build.Container.File(build.BinaryPath))
}
return output

}

func (m *Harbor) buildAllBinaries(ctx context.Context) []*BuildMetadata {
var buildContainers []*BuildMetadata
for _, platform := range SupportedPlatforms {
for _, platform := range targetPlatforms {
for _, pkg := range packages {
buildContainer := m.buildBinary(ctx, platform, pkg)
buildContainers = append(buildContainers, buildContainer)
}
// build portal
}
return buildContainers
}

func (m *Harbor) BuildBinary(ctx context.Context, platform string, pkg string) *dagger.File {
func (m *Harbor) BuildBinary(ctx context.Context, platform Platform, pkg Package) *dagger.File {
build := m.buildBinary(ctx, platform, pkg)
return build.Container.File(build.BinaryPath)
}

func (m *Harbor) buildBinary(ctx context.Context, platform string, pkg string) *BuildMetadata {
func (m *Harbor) buildBinary(ctx context.Context, platform Platform, pkg Package) *BuildMetadata {

os, arch, err := parsePlatform(platform)
os, arch, err := parsePlatform(string(platform))
if err != nil {
log.Fatalf("Error parsing platform: %v", err)
}
Expand All @@ -135,7 +212,7 @@ func (m *Harbor) buildBinary(ctx context.Context, platform string, pkg string) *
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
WithMountedCache("/go/build-cache", dag.CacheVolume("go-build-"+GO_VERSION)).
WithEnvVariable("GOCACHE", "/go/build-cache").
WithMountedDirectory("/harbor", m.Source). // Ensure the source directory with go.mod is mounted
WithMountedDirectory("/harbor", m.Source).
WithWorkdir("/harbor/src/").
WithEnvVariable("GOOS", os).
WithEnvVariable("GOARCH", arch).
Expand All @@ -150,10 +227,9 @@ func (m *Harbor) buildBinary(ctx context.Context, platform string, pkg string) *
}
}

func (m *Harbor) buildPortal(ctx context.Context, platform string, pkg string) *dagger.Directory {
func (m *Harbor) buildPortal(ctx context.Context, platform Platform, pkg Package) *dagger.Directory {
fmt.Println("🛠️ Building Harbor Core...")
// Define the path for the binary output
os, arch, err := parsePlatform(platform)
os, arch, err := parsePlatform(string(platform))

if err != nil {
log.Fatalf("Error parsing platform: %v", err)
Expand All @@ -167,7 +243,7 @@ func (m *Harbor) buildPortal(ctx context.Context, platform string, pkg string) *
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
WithMountedCache("/go/build-cache", dag.CacheVolume("go-build-"+GO_VERSION)).
WithEnvVariable("GOCACHE", "/go/build-cache").
WithMountedDirectory("/harbor", m.Source). // Ensure the source directory with go.mod is mounted
WithMountedDirectory("/harbor", m.Source).
WithWorkdir("/harbor").
WithEnvVariable("GOOS", os).
WithEnvVariable("GOARCH", arch).
Expand Down
4 changes: 2 additions & 2 deletions dagger.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "harbor",
"engineVersion": "v0.14.0",
"sdk": "go",
"source": ".dagger",
"engineVersion": "v0.13.6"
"source": ".dagger"
}
1 change: 1 addition & 0 deletions src/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import (
pkguser "github.com/goharbor/harbor/src/pkg/user"
"github.com/goharbor/harbor/src/pkg/version"
"github.com/goharbor/harbor/src/server"
_ "golang.org/x/crypto/x509roots/fallback"
)

const (
Expand Down
11 changes: 7 additions & 4 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ require (
github.com/go-asn1-ber/asn1-ber v1.5.7
github.com/go-ldap/ldap/v3 v3.4.6
github.com/go-openapi/errors v0.22.0
github.com/go-openapi/loads v0.22.0 // indirect
github.com/go-openapi/loads v0.22.0
github.com/go-openapi/runtime v0.28.0
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0
github.com/go-openapi/strfmt v0.23.0
github.com/go-openapi/swag v0.23.0
github.com/go-openapi/validate v0.24.0 // indirect
github.com/go-openapi/validate v0.24.0
github.com/go-redis/redis/v8 v8.11.4
github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8
github.com/gocraft/work v0.5.1
Expand Down Expand Up @@ -78,7 +78,10 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require github.com/prometheus/client_model v0.6.1
require (
github.com/prometheus/client_model v0.6.1
golang.org/x/crypto/x509roots/fallback v0.0.0-20241127184453-8c4e668694cc
)

require (
cloud.google.com/go/compute/metadata v0.3.0 // indirect
Expand Down
7 changes: 2 additions & 5 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,13 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 h1:Kk6a4nehpJ3UuJRqlA3JxYxBZEqCeOmATOvrbT4p9RA=
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4=
github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190726115642-cd293c93fd97 h1:bNE5ID4C3YOkROfvBjXJUG53gyb+8az3TQN02LqnGBk=
github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190726115642-cd293c93fd97/go.mod h1:myCDvQSzCW+wB1WAlocEru4wMGJxy+vlxHdhegi1CDQ=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1193 h1:C5LuIDWuQlugv30EBsSLKFF6jdtrqoVH84nYCdVYTC4=
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1193/go.mod h1:pUKYbK5JQ+1Dfxk80P0qxGqe5dkxDoabbZS7zOcouyA=
github.com/aliyun/aliyun-oss-go-sdk v0.0.0-20190307165228-86c17b95fcd5/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc=
github.com/beego/beego/v2 v2.2.1 h1:5RatpEOKnw6sm76hj6lQvEFi4Tco+E21VQomnVB7NsA=
github.com/beego/beego/v2 v2.2.1/go.mod h1:X4hHhM2AXn0hN2tbyz5X/PD7v5JUdE4IihZApiljpNA=
github.com/beego/i18n v0.0.0-20140604031826-e87155e8f0c0 h1:fQaDnUQvBXHHQdGBu9hz8nPznB4BeiPQokvmQVjmNEw=
Expand Down Expand Up @@ -605,6 +601,8 @@ golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/crypto/x509roots/fallback v0.0.0-20241127184453-8c4e668694cc h1:N2DXFnxni8U4KZ7CcK6kicRat3uowReUjOJxwYTxQv8=
golang.org/x/crypto/x509roots/fallback v0.0.0-20241127184453-8c4e668694cc/go.mod h1:kNa9WdvYnzFwC79zRpLRMJbdEFlhyM5RPFBBZp/wWH8=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
Expand Down Expand Up @@ -711,7 +709,6 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down

0 comments on commit ca8d7f1

Please sign in to comment.