-
Notifications
You must be signed in to change notification settings - Fork 173
/
dev.go
178 lines (145 loc) · 5.09 KB
/
dev.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/defenseunicorns/pkg/helpers/v2"
"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/lint"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/types"
"github.com/fatih/color"
)
// DevDeploy creates + deploys a package in one shot
func (p *Packager) DevDeploy(ctx context.Context) error {
config.CommonOptions.Confirm = true
p.cfg.CreateOpts.SkipSBOM = !p.cfg.CreateOpts.NoYOLO
cwd, err := os.Getwd()
if err != nil {
return err
}
if err := os.Chdir(p.cfg.CreateOpts.BaseDir); err != nil {
return fmt.Errorf("unable to access directory %q: %w", p.cfg.CreateOpts.BaseDir, err)
}
pc := creator.NewPackageCreator(p.cfg.CreateOpts, cwd)
if err := helpers.CreatePathAndCopy(layout.ZarfYAML, p.layout.ZarfYAML); err != nil {
return err
}
p.cfg.Pkg, p.warnings, err = pc.LoadPackageDefinition(ctx, p.layout)
if err != nil {
return err
}
filter := filters.Combine(
filters.ByLocalOS(runtime.GOOS),
filters.ForDeploy(p.cfg.PkgOpts.OptionalComponents, false),
)
p.cfg.Pkg.Components, err = filter.Apply(p.cfg.Pkg)
if err != nil {
return err
}
if err := p.cfg.Pkg.Validate(); err != nil {
return fmt.Errorf("unable to validate package: %w", err)
}
if err := p.populatePackageVariableConfig(); err != nil {
return fmt.Errorf("unable to set the active variables: %w", err)
}
// If building in yolo mode, strip out all images and repos
if !p.cfg.CreateOpts.NoYOLO {
for idx := range p.cfg.Pkg.Components {
p.cfg.Pkg.Components[idx].Images = []string{}
p.cfg.Pkg.Components[idx].Repos = []string{}
}
}
if err := pc.Assemble(ctx, p.layout, p.cfg.Pkg.Components, p.cfg.Pkg.Metadata.Architecture); err != nil {
return err
}
message.HeaderInfof("📦 PACKAGE DEPLOY %s", p.cfg.Pkg.Metadata.Name)
p.connectStrings = make(types.ConnectStrings)
if !p.cfg.CreateOpts.NoYOLO {
p.cfg.Pkg.Metadata.YOLO = true
} else {
p.hpaModified = false
// Reset registry HPA scale down whether an error occurs or not
defer p.resetRegistryHPA(ctx)
}
// Get a list of all the components we are deploying and actually deploy them
deployedComponents, err := p.deployComponents(ctx)
if err != nil {
return err
}
if len(deployedComponents) == 0 {
message.Warn("No components were selected for deployment. Inspect the package to view the available components and select components interactively or by name with \"--components\"")
}
// Notify all the things about the successful deployment
message.Successf("Zarf dev deployment complete")
message.HorizontalRule()
message.Title("Next steps:", "")
message.ZarfCommand("package inspect %s", p.cfg.Pkg.Metadata.Name)
// cd back
return os.Chdir(cwd)
}
// Lint ensures a package is valid & follows suggested conventions
func (p *Packager) Lint(ctx context.Context) error {
if err := os.Chdir(p.cfg.CreateOpts.BaseDir); err != nil {
return fmt.Errorf("unable to access directory %q: %w", p.cfg.CreateOpts.BaseDir, err)
}
if err := utils.ReadYaml(layout.ZarfYAML, &p.cfg.Pkg); err != nil {
return err
}
findings, err := lint.Validate(ctx, p.cfg.Pkg, p.cfg.CreateOpts)
if err != nil {
return fmt.Errorf("linting failed: %w", err)
}
if len(findings) == 0 {
message.Successf("0 findings for %q", p.cfg.Pkg.Metadata.Name)
return nil
}
mapOfFindingsByPath := lint.GroupFindingsByPath(findings, types.SevWarn, p.cfg.Pkg.Metadata.Name)
header := []string{"Type", "Path", "Message"}
for _, findings := range mapOfFindingsByPath {
lintData := [][]string{}
for _, finding := range findings {
lintData = append(lintData, []string{
colorWrapSev(finding.Severity),
message.ColorWrap(finding.YqPath, color.FgCyan),
itemizedDescription(finding.Description, finding.Item),
})
}
var packagePathFromUser string
if helpers.IsOCIURL(findings[0].PackagePathOverride) {
packagePathFromUser = findings[0].PackagePathOverride
} else {
packagePathFromUser = filepath.Join(p.cfg.CreateOpts.BaseDir, findings[0].PackagePathOverride)
}
message.Notef("Linting package %q at %s", findings[0].PackageNameOverride, packagePathFromUser)
message.Table(header, lintData)
}
if lint.HasSeverity(findings, types.SevErr) {
return errors.New("errors during lint")
}
return nil
}
func itemizedDescription(description string, item string) string {
if item == "" {
return description
}
return fmt.Sprintf("%s - %s", description, item)
}
func colorWrapSev(s types.Severity) string {
if s == types.SevErr {
return message.ColorWrap("Error", color.FgRed)
} else if s == types.SevWarn {
return message.ColorWrap("Warning", color.FgYellow)
}
return "unknown"
}