| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 1 | package installer |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| gio | f884341 | 2024-05-22 16:38:05 +0400 | [diff] [blame^] | 5 | "io" |
| 6 | "io/fs" |
| 7 | "path/filepath" |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 8 | |
| gio | f884341 | 2024-05-22 16:38:05 +0400 | [diff] [blame^] | 9 | "github.com/giolekva/pcloud/core/installer/soft" |
| 10 | |
| 11 | "github.com/go-git/go-billy/v5/memfs" |
| 12 | "github.com/go-git/go-billy/v5/util" |
| 13 | "github.com/go-git/go-git/v5" |
| 14 | "github.com/go-git/go-git/v5/plumbing" |
| 15 | "github.com/go-git/go-git/v5/storage/memory" |
| Giorgi Lekveishvili | 7c42760 | 2024-01-04 00:13:55 +0400 | [diff] [blame] | 16 | "helm.sh/helm/v3/pkg/action" |
| 17 | "helm.sh/helm/v3/pkg/kube" |
| 18 | ) |
| 19 | |
| 20 | type ActionConfigFactory struct { |
| 21 | kubeConfigPath string |
| 22 | } |
| 23 | |
| 24 | func NewActionConfigFactory(kubeConfigPath string) ActionConfigFactory { |
| 25 | return ActionConfigFactory{kubeConfigPath} |
| 26 | } |
| 27 | |
| 28 | func (f ActionConfigFactory) New(namespace string) (*action.Configuration, error) { |
| 29 | config := new(action.Configuration) |
| 30 | if err := config.Init( |
| 31 | kube.GetConfig(f.kubeConfigPath, "", namespace), |
| 32 | namespace, |
| 33 | "", |
| 34 | func(fmtString string, args ...any) { |
| 35 | fmt.Printf(fmtString, args...) |
| 36 | fmt.Println() |
| 37 | }, |
| 38 | ); err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | return config, nil |
| 42 | } |
| gio | f884341 | 2024-05-22 16:38:05 +0400 | [diff] [blame^] | 43 | |
| 44 | type HelmFetcher interface { |
| 45 | Pull(chart HelmChartGitRepo, rfs soft.RepoFS, root string) error |
| 46 | } |
| 47 | |
| 48 | type gitHelmFetcher struct{} |
| 49 | |
| 50 | func NewGitHelmFetcher() *gitHelmFetcher { |
| 51 | return &gitHelmFetcher{} |
| 52 | } |
| 53 | |
| 54 | func (f *gitHelmFetcher) Pull(chart HelmChartGitRepo, rfs soft.RepoFS, root string) error { |
| 55 | ref := fmt.Sprintf("refs/heads/%s", chart.Branch) |
| 56 | r, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{ |
| 57 | URL: chart.Address, |
| 58 | ReferenceName: plumbing.ReferenceName(ref), |
| 59 | SingleBranch: true, |
| 60 | Depth: 1, |
| 61 | }) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | wt, err := r.Worktree() |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | wtFS, err := wt.Filesystem.Chroot(chart.Path) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | return util.Walk(wtFS, "/", func(path string, info fs.FileInfo, err error) error { |
| 74 | if info.IsDir() { |
| 75 | return nil |
| 76 | } |
| 77 | inp, err := wtFS.Open(path) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | out, err := rfs.Writer(filepath.Join(root, path)) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | _, err = io.Copy(out, inp) |
| 86 | return err |
| 87 | }) |
| 88 | } |