|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "helm.sh/helm/v3/pkg/getter" |
| 10 | + "helm.sh/helm/v3/pkg/repo" |
| 11 | + |
| 12 | + "github.com/operator-framework/operator-registry/alpha/declcfg" |
| 13 | + "github.com/operator-framework/operator-registry/alpha/property" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + if err := rootCmd().Execute(); err != nil { |
| 18 | + os.Exit(1) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func rootCmd() *cobra.Command { |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "chartrepo-to-catalog", |
| 25 | + Args: cobra.ExactArgs(1), |
| 26 | + Short: "opm render, but from a Helm chart repo", |
| 27 | + Run: func(cmd *cobra.Command, args []string) { |
| 28 | + repoURL := args[0] |
| 29 | + repoEntry := repo.Entry{ |
| 30 | + URL: repoURL, |
| 31 | + } |
| 32 | + |
| 33 | + providers := getter.Providers{ |
| 34 | + getter.Provider{ |
| 35 | + Schemes: []string{"http", "https"}, |
| 36 | + New: getter.NewHTTPGetter, |
| 37 | + }, |
| 38 | + getter.Provider{ |
| 39 | + Schemes: []string{"oci"}, |
| 40 | + New: getter.NewOCIGetter, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + repoClient, err := repo.NewChartRepository(&repoEntry, providers) |
| 45 | + if err != nil { |
| 46 | + cmd.PrintErr(err) |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + tmpDir, err := os.MkdirTemp("", "chartrepo-to-catalog") |
| 51 | + if err != nil { |
| 52 | + cmd.PrintErr(err) |
| 53 | + os.Exit(1) |
| 54 | + } |
| 55 | + defer os.RemoveAll(tmpDir) |
| 56 | + |
| 57 | + repoClient.CachePath = tmpDir |
| 58 | + |
| 59 | + indexFilePath, err := repoClient.DownloadIndexFile() |
| 60 | + if err != nil { |
| 61 | + cmd.PrintErr(err) |
| 62 | + os.Exit(1) |
| 63 | + } |
| 64 | + idx, err := repo.LoadIndexFile(indexFilePath) |
| 65 | + if err != nil { |
| 66 | + cmd.PrintErr(err) |
| 67 | + os.Exit(1) |
| 68 | + } |
| 69 | + |
| 70 | + fbc := declcfg.DeclarativeConfig{} |
| 71 | + for name, entries := range idx.Entries { |
| 72 | + if len(entries) == 0 { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + pkg := declcfg.Package{ |
| 77 | + Schema: "olm.package", |
| 78 | + Name: name, |
| 79 | + Description: entries[0].Description, |
| 80 | + DefaultChannel: "default", |
| 81 | + } |
| 82 | + ch := declcfg.Channel{ |
| 83 | + Schema: "olm.channel", |
| 84 | + Package: name, |
| 85 | + Name: "default", |
| 86 | + Entries: mapSlice(entries, func(i int, entry *repo.ChartVersion) declcfg.ChannelEntry { |
| 87 | + replaces := "" |
| 88 | + if i < len(entries)-1 { |
| 89 | + replaces = bundleNameFromEntry(name, entries[i+1]) |
| 90 | + } |
| 91 | + return declcfg.ChannelEntry{ |
| 92 | + Name: bundleNameFromEntry(name, entry), |
| 93 | + Replaces: replaces, |
| 94 | + } |
| 95 | + }), |
| 96 | + } |
| 97 | + bundles := mapSlice(entries, func(_ int, entry *repo.ChartVersion) declcfg.Bundle { |
| 98 | + return declcfg.Bundle{ |
| 99 | + Schema: "olm.bundle", |
| 100 | + Package: name, |
| 101 | + Name: bundleNameFromEntry(name, entry), |
| 102 | + Image: fmt.Sprintf("%s/%s", repoURL, entry.URLs[0]), |
| 103 | + Properties: []property.Property{ |
| 104 | + property.MustBuildPackage(name, strings.TrimPrefix(entry.Version, "v")), |
| 105 | + }, |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + fbc.Packages = append(fbc.Packages, pkg) |
| 110 | + fbc.Channels = append(fbc.Channels, ch) |
| 111 | + fbc.Bundles = append(fbc.Bundles, bundles...) |
| 112 | + } |
| 113 | + if err := declcfg.WriteYAML(fbc, cmd.OutOrStdout()); err != nil { |
| 114 | + cmd.PrintErr(err) |
| 115 | + os.Exit(1) |
| 116 | + } |
| 117 | + }, |
| 118 | + } |
| 119 | + return cmd |
| 120 | +} |
| 121 | + |
| 122 | +func bundleNameFromEntry(name string, entry *repo.ChartVersion) string { |
| 123 | + return fmt.Sprintf("%s.v%s", name, strings.TrimPrefix(entry.Version, "v")) |
| 124 | +} |
| 125 | + |
| 126 | +func mapSlice[T any, U any](s []T, f func(int, T) U) []U { |
| 127 | + out := make([]U, len(s)) |
| 128 | + for i, v := range s { |
| 129 | + out[i] = f(i, v) |
| 130 | + } |
| 131 | + return out |
| 132 | +} |
0 commit comments