-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
210 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"sort" | ||
|
||
embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-kinds/apis/v1beta1" | ||
"github.com/replicatedhq/embedded-cluster-kinds/types" | ||
"github.com/replicatedhq/embedded-cluster/pkg/helm" | ||
"github.com/replicatedhq/embedded-cluster/pkg/utils" | ||
"github.com/urfave/cli/v2" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var metadataCommand = &cli.Command{ | ||
Name: "metadata", | ||
Usage: "Perform operations on the version-metadata.json file", | ||
Subcommands: []*cli.Command{ | ||
metadataListImagesCommand, | ||
metadataExtractHelmChartImagesCommand, | ||
}, | ||
} | ||
|
||
var metadataListImagesCommand = &cli.Command{ | ||
Name: "extract-images", | ||
Usage: "List images", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "metadata-path", | ||
Usage: "Path to the metadata file", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
metadata, err := readMetadataFromFile(c.String("metadata-path")) | ||
if err != nil { | ||
return fmt.Errorf("failed to read metadata from file: %w", err) | ||
} | ||
|
||
for _, image := range metadata.Images { | ||
fmt.Println(image) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
var metadataExtractHelmChartImagesCommand = &cli.Command{ | ||
Name: "extract-images", | ||
Usage: "Extract images from Helm charts", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "metadata-path", | ||
Usage: "Path to the metadata file", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
metadata, err := readMetadataFromFile(c.String("metadata-path")) | ||
if err != nil { | ||
return fmt.Errorf("failed to read metadata from file: %w", err) | ||
} | ||
|
||
repos := metadata.Configs.Repositories | ||
charts := metadata.Configs.Charts | ||
for _, chart := range metadata.BuiltinConfigs { | ||
repos = append(repos, chart.Repositories...) | ||
charts = append(charts, chart.Charts...) | ||
} | ||
|
||
images, err := extractImagesFromHelmExtensions(repos, charts) | ||
if err != nil { | ||
return fmt.Errorf("failed to extract images from helm extensions: %w", err) | ||
} | ||
|
||
for _, image := range images { | ||
fmt.Println(image) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
func readMetadataFromFile(path string) (*types.ReleaseMetadata, error) { | ||
b, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("read file: %w", err) | ||
} | ||
|
||
var metadata types.ReleaseMetadata | ||
if err := json.Unmarshal(b, &metadata); err != nil { | ||
return nil, fmt.Errorf("unmarshal metadata: %w", err) | ||
} | ||
|
||
return &metadata, nil | ||
} | ||
|
||
func extractImagesFromHelmExtensions(repos []embeddedclusterv1beta1.Repository, charts []embeddedclusterv1beta1.Chart) ([]string, error) { | ||
hcli, err := helm.NewHelm(helm.HelmOptions{ | ||
K0sVersion: "1.29.6", // TODO | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("create helm client: %w", err) | ||
} | ||
defer hcli.Close() | ||
|
||
var images []string | ||
for _, ext := range charts { | ||
ims, err := extractImagesFromChart(hcli, ext) | ||
if err != nil { | ||
return nil, fmt.Errorf("extract images from chart %s: %w", ext.Name, err) | ||
} | ||
images = append(images, ims...) | ||
} | ||
images = utils.UniqueStringSlice(images) | ||
sort.Strings(images) | ||
return images, nil | ||
} | ||
|
||
func extractImagesFromChart(hcli *helm.Helm, chart embeddedclusterv1beta1.Chart) ([]string, error) { | ||
values := map[string]interface{}{} | ||
if chart.Values != "" { | ||
err := yaml.Unmarshal([]byte(chart.Values), &values) | ||
if err != nil { | ||
return nil, fmt.Errorf("unmarshal values: %w", err) | ||
} | ||
} | ||
|
||
return helm.ExtractImagesFromOCIChart(hcli, chart.ChartName, chart.Name, chart.Version, values) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.