-
Notifications
You must be signed in to change notification settings - Fork 173
/
publish.go
123 lines (104 loc) · 3.94 KB
/
publish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors
// Package packager contains functions for interacting with, managing and deploying Zarf packages.
package packager
import (
"context"
"fmt"
"os"
"strings"
"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/defenseunicorns/pkg/oci"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/packager/creator"
"github.com/defenseunicorns/zarf/src/pkg/packager/filters"
"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/zoci"
"github.com/defenseunicorns/zarf/src/types"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
// Publish publishes the package to a registry
func (p *Packager) Publish(ctx context.Context) (err error) {
_, isOCISource := p.source.(*sources.OCISource)
if isOCISource && p.cfg.PublishOpts.SigningKeyPath == "" {
// oci --> oci is a special case, where we will use oci.CopyPackage so that we can transfer the package
// w/o layers touching the filesystem
srcRemote := p.source.(*sources.OCISource).Remote
parts := strings.Split(srcRemote.Repo().Reference.Repository, "/")
packageName := parts[len(parts)-1]
p.cfg.PublishOpts.PackageDestination = p.cfg.PublishOpts.PackageDestination + "/" + packageName
arch := config.GetArch()
dstRemote, err := zoci.NewRemote(p.cfg.PublishOpts.PackageDestination, oci.PlatformForArch(arch))
if err != nil {
return err
}
return zoci.CopyPackage(ctx, srcRemote, dstRemote, config.CommonOptions.OCIConcurrency)
}
if p.cfg.CreateOpts.IsSkeleton {
if err := os.Chdir(p.cfg.CreateOpts.BaseDir); err != nil {
return fmt.Errorf("unable to access directory %q: %w", p.cfg.CreateOpts.BaseDir, err)
}
sc := creator.NewSkeletonCreator(p.cfg.CreateOpts, p.cfg.PublishOpts)
if err := helpers.CreatePathAndCopy(layout.ZarfYAML, p.layout.ZarfYAML); err != nil {
return err
}
p.cfg.Pkg, p.warnings, err = sc.LoadPackageDefinition(ctx, p.layout)
if err != nil {
return err
}
if err := sc.Assemble(ctx, p.layout, p.cfg.Pkg.Components, ""); err != nil {
return err
}
if err := sc.Output(ctx, p.layout, &p.cfg.Pkg); err != nil {
return err
}
} else {
filter := filters.Empty()
p.cfg.Pkg, p.warnings, err = p.source.LoadPackage(ctx, p.layout, filter, false)
if err != nil {
return fmt.Errorf("unable to load the package: %w", err)
}
// Sign the package if a key has been provided
if err := p.layout.SignPackage(p.cfg.PublishOpts.SigningKeyPath, p.cfg.PublishOpts.SigningKeyPassword, !config.CommonOptions.Confirm); err != nil {
return err
}
}
// Get a reference to the registry for this package
ref, err := zoci.ReferenceFromMetadata(p.cfg.PublishOpts.PackageDestination, &p.cfg.Pkg.Metadata, &p.cfg.Pkg.Build)
if err != nil {
return err
}
var platform ocispec.Platform
if p.cfg.CreateOpts.IsSkeleton {
platform = zoci.PlatformForSkeleton()
} else {
platform = oci.PlatformForArch(p.cfg.Pkg.Build.Architecture)
}
remote, err := zoci.NewRemote(ref, platform)
if err != nil {
return err
}
message.HeaderInfof("📦 PACKAGE PUBLISH %s:%s", p.cfg.Pkg.Metadata.Name, ref)
// Publish the package/skeleton to the registry
if err := remote.PublishPackage(ctx, &p.cfg.Pkg, p.layout, config.CommonOptions.OCIConcurrency); err != nil {
return err
}
if p.cfg.CreateOpts.IsSkeleton {
message.Title("How to import components from this skeleton:", "")
ex := []types.ZarfComponent{}
for _, c := range p.cfg.Pkg.Components {
ex = append(ex, types.ZarfComponent{
Name: fmt.Sprintf("import-%s", c.Name),
Import: types.ZarfComponentImport{
ComponentName: c.Name,
URL: helpers.OCIURLPrefix + remote.Repo().Reference.String(),
},
})
}
utils.ColorPrintYAML(ex, nil, true)
}
return nil
}