Skip to content

Commit 591d559

Browse files
author
femb
committed
Feat: output artifacts to json
1 parent 1a7c982 commit 591d559

File tree

4 files changed

+164
-4
lines changed

4 files changed

+164
-4
lines changed

internal/bootstrap/viper.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ type ImportConfigSection struct {
2626
Enabled bool `yaml:"enabled"`
2727
Architecture *string `yaml:"architecture"`
2828
ReplaceRegistryReferences bool `yaml:"replaceRegistryReferences"`
29-
Copacetic struct {
29+
Artifacts struct {
30+
Enabled bool `yaml:"enabled"`
31+
Folder string `yaml:"folder"`
32+
} `yaml:"artifacts"`
33+
Copacetic struct {
3034
Enabled bool `yaml:"enabled"`
3135
IgnoreErrors bool `yaml:"ignoreErrors"`
3236
Buildkitd struct {
@@ -139,6 +143,7 @@ func LoadViperConfiguration() (*viper.Viper, error) {
139143
viper.SetDefault("verbose", false)
140144
viper.SetDefault("update", false)
141145
viper.SetDefault("k8s_version", "1.31.1")
146+
viper.SetDefault("import.artifacts.enabled", false)
142147

143148
// Unmarshal registries config section
144149
conf := config{}
@@ -274,7 +279,15 @@ copacetic:
274279
`
275280
return nil, xerrors.Errorf("You have enabled copacetic patching but did not specify the path to the tars output folder'. Please add the value and try again\nExample:\n%s", s)
276281
}
277-
282+
if importConf.Import.Artifacts.Enabled && importConf.Import.Artifacts.Folder == "" {
283+
s := `
284+
import:
285+
artifacts:
286+
enabled: true
287+
folder: /workspace/.out/artifacts <---
288+
`
289+
return nil, xerrors.Errorf("You have enabled artifacts output but did not specify the output path. Please add the value and try again...\nExample config:\n%s", s)
290+
}
278291
}
279292

280293
viper.Set("importConfig", importConf)

internal/program.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/ChristofferNissen/helmper/internal/bootstrap"
1616
"github.com/ChristofferNissen/helmper/pkg/copa"
1717
mySign "github.com/ChristofferNissen/helmper/pkg/cosign"
18+
"github.com/ChristofferNissen/helmper/pkg/exportArtifacts"
1819
"github.com/ChristofferNissen/helmper/pkg/flow"
1920
"github.com/ChristofferNissen/helmper/pkg/helm"
2021
"github.com/ChristofferNissen/helmper/pkg/image"
@@ -260,8 +261,7 @@ func program(ctx context.Context, _ []string, viper *viper.Viper, settings *cli.
260261
}
261262
vo.Report.Render()
262263
so := mySign.SignOption{
263-
Data: imgs,
264-
264+
Data: imgs,
265265
KeyRef: importConfig.Import.Cosign.KeyRef,
266266
KeyRefPass: *importConfig.Import.Cosign.KeyRefPass,
267267
AllowInsecure: importConfig.Import.Cosign.AllowInsecure,
@@ -271,6 +271,17 @@ func program(ctx context.Context, _ []string, viper *viper.Viper, settings *cli.
271271
return err
272272
}
273273
}
274+
// Step 7: Export and output artifacts
274275

276+
if importConfig.Import.Artifacts.Enabled {
277+
eo := exportArtifacts.ExportOption{
278+
Data: mImgs,
279+
Data2: mCharts,
280+
}
281+
_, _, err = eo.Run(context.WithoutCancel(ctx))
282+
if err != nil {
283+
return err
284+
}
285+
}
275286
return nil
276287
}

pkg/exportArtifacts/main.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package exportArtifacts
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log/slog"
8+
"os"
9+
"strings"
10+
11+
"github.com/ChristofferNissen/helmper/pkg/helm"
12+
"github.com/ChristofferNissen/helmper/pkg/image"
13+
"github.com/ChristofferNissen/helmper/pkg/registry"
14+
)
15+
16+
type ExportOption struct {
17+
Data helm.RegistryImageStatus // image data
18+
Data2 map[*registry.Registry]map[*helm.Chart]bool // chart data
19+
ChartData helm.ChartData
20+
//RegistryImage map[*registry.Registry]map[*image.Image]bool
21+
RegistryImage map[*helm.Chart]map[*image.Image][]bool
22+
}
23+
24+
type ChartArtifact struct {
25+
ChartOverview string `json:"chart_overview"`
26+
ChartName string `json:"chart_name"`
27+
Repository string `json:"repository"`
28+
Registry string `json:"registry"`
29+
ChartVersion string `json:"chart_version"`
30+
Import bool `json:"import"`
31+
}
32+
33+
type ImageArtifact struct {
34+
ImageOverview string `json:"image_overview"`
35+
ImageName string `json:"image_name"`
36+
ImageTag string `json:"image_tag"`
37+
Repository string `json:"repository"`
38+
Import bool `json:"import"`
39+
}
40+
41+
func (eo *ExportOption) Run(ctx context.Context) ([]string, []string, error) {
42+
imgOverview := []string{}
43+
chartOverview := []string{}
44+
chartDataOverview := []string{}
45+
46+
// Collect image data
47+
for reg, imgs := range eo.Data {
48+
for img := range imgs {
49+
overview := fmt.Sprintf("Registry: %s, Image: %s, Repository: %s, Tag: %s",
50+
reg.GetName(),
51+
img.String(), img.Repository, img.Tag)
52+
fmt.Println("Registry - data 1:", reg)
53+
fmt.Println("Image - data 1:", imgs)
54+
imgOverview = append(imgOverview, overview)
55+
}
56+
}
57+
// Collect chart data
58+
for reg, charts := range eo.Data2 {
59+
for chart := range charts {
60+
overview := fmt.Sprintf("Registry: %s, Chart: %s, Version: %s",
61+
reg.Name, chart.Name, chart.Version)
62+
fmt.Println("Registry - data 2:", reg)
63+
fmt.Println("Chart - data 2:", chart)
64+
chartPath := fmt.Sprintf("charts/%s", chart.Name)
65+
fmt.Println(chartPath)
66+
chartOverview = append(chartOverview, overview)
67+
}
68+
}
69+
70+
// Collect chart data (2)
71+
for reg, charts := range eo.RegistryImage {
72+
// for chart := range charts {
73+
overview := fmt.Sprintf("Reg: %s, Charts: %s",
74+
reg, charts)
75+
chartDataOverview = append(chartDataOverview, overview)
76+
// }
77+
}
78+
79+
// Convert to JSON (IMAGES) ("Registry: %s, Image: %s, Repository: %s, Tag: %s"
80+
artifacts := []ImageArtifact{}
81+
for _, overview := range imgOverview {
82+
ia := ImageArtifact{
83+
ImageOverview: overview,
84+
ImageName: strings.Split(overview, ", ")[1],
85+
Repository: strings.Split(overview, ", ")[2],
86+
ImageTag: strings.Split(overview, ", ")[3],
87+
}
88+
artifacts = append(artifacts, ia)
89+
}
90+
chartArtifacts := []ChartArtifact{}
91+
// Convert to JSON (CHARTS) ("Registry: %s, Chart: %s, Version: %s"
92+
for _, overview := range chartOverview {
93+
ca := ChartArtifact{
94+
ChartOverview: overview,
95+
Registry: strings.Split(overview, ", ")[0],
96+
ChartName: strings.Split(overview, ", ")[1],
97+
ChartVersion: strings.Split(overview, ", ")[2],
98+
}
99+
chartArtifacts = append(chartArtifacts, ca)
100+
}
101+
102+
chartData := []ChartArtifact{}
103+
// Convert to JSON (CHARTS) ("Registry: %s, Chart: %s, Version: %s"
104+
for _, overview := range chartDataOverview {
105+
cd := ChartArtifact{
106+
ChartOverview: overview,
107+
}
108+
chartData = append(chartData, cd)
109+
}
110+
111+
exportData := struct {
112+
Images []ImageArtifact `json:"images"`
113+
Charts []ChartArtifact `json:"charts"`
114+
Data []ChartArtifact `json:"chart_data"`
115+
}{
116+
Images: artifacts,
117+
Charts: chartArtifacts,
118+
Data: chartData,
119+
}
120+
121+
jsonData, err := json.MarshalIndent(exportData, "", " ")
122+
if err != nil {
123+
return nil, nil, err
124+
}
125+
126+
// Write JSON to file
127+
err = os.WriteFile("artifacts.json", jsonData, 0644)
128+
if err != nil {
129+
return nil, nil, err
130+
}
131+
132+
slog.Info("Exported artifacts to artifacts.json")
133+
return imgOverview, chartOverview, nil
134+
}

pkg/exportArtifacts/main_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package exportArtifacts
2+

0 commit comments

Comments
 (0)