Skip to content

Commit

Permalink
Add helm flag to download chart and images listed in values.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ccleouf66 committed Nov 18, 2020
1 parent 7b7b7d3 commit fa01447
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
46 changes: 38 additions & 8 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"

"rkd/containers"
"rkd/git"
Expand All @@ -24,9 +26,9 @@ var (
// DownloadCommand return downlaod cli command
func DownloadCommand() cli.Command {
DownloadFlags := []cli.Flag{
cli.StringFlag{
cli.StringSliceFlag{
Name: "helm",
Usage: "Download a helm chart list.\n rkd download --helm",
Usage: "Download a helm chart list.\nrkd download --helm https://REPO_URL/REPO_NAME/CHART_NAME \nrkd download --helm https://charts.bitnami.com/bitnami/postgresql-ha",
},
cli.StringSliceFlag{
Name: "image",
Expand Down Expand Up @@ -67,6 +69,7 @@ func DownloadDataPack(c *cli.Context) {
}
helpers.CreateDestDir(dest)

// Images
if len(c.StringSlice("image")) > 0 {
var destImg string

Expand All @@ -78,19 +81,46 @@ func DownloadDataPack(c *cli.Context) {
containers.DownloadImage(c.StringSlice("image"), destImg)
}

// Helm
if len(c.StringSlice("helm")) > 0 {
for _, el := range c.StringSlice("helm") {
s := strings.Split(el, "/")
chartName := s[len(s)-1]
repoName := s[len(s)-2]
repoURL := strings.Join(s[:len(s)-1], "/")
chartDest := fmt.Sprintf("%s/%s", dest, chartName)

fmt.Printf("Getting %s chart\n", chartName)

helm.RepoAdd(repoName, repoURL)
helm.RepoUpdate()
chartPath := helm.DownloadChart(repoName, chartName, "", chartDest)

// Get chart image list
imgList, err := helm.GetChartImages(chartPath)
if err != nil {
log.Printf("Err getting image list of chart %s.\n%s\n", chartName, err)
continue
}

// Downlaod container images
destImg := fmt.Sprintf("%s/%s-images.tar", chartDest, chartName)
err = containers.DownloadImage(imgList, destImg)
if err != nil {
log.Printf("%s\n", err)
}
}
}

// Rancher
if c.String("rancher") != "" {
fmt.Printf("Getting Rancher %s\n", c.String("rancher"))
GetRancherHelmChart(c.String("rancher"), dest)
GetRancherImages(c.String("rancher"), dest)
}

// if c.String("helm") != "" {
// fmt.Printf("Getting Rancher chart %s\n", c.String("helm"))
// GetRancherHelmChart(c.String("helm"))
// }

// If no flag provided, download latest chart and images
if c.String("rancher") == "" && c.String("image") == "" {
if c.String("rancher") == "" && len(c.StringSlice("image")) == 0 && len(c.StringSlice("helm")) == 0 {
GetRancherHelmChart("latest", dest)
GetRancherImages("latest", dest)
}
Expand Down
32 changes: 20 additions & 12 deletions containers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containers
import (
"context"
"fmt"
"log"
"os"
"strings"

Expand All @@ -15,23 +16,26 @@ import (
)

// DownloadImage download docker images from src and create docker-archive
func DownloadImage(imgList []string, dest string) {
func DownloadImage(imgList []string, dest string) error {

// Contexts
defaultPolicy, err := signature.NewPolicyFromFile("policy.json")
if err != nil {
fmt.Printf("default policy err: %s\n", err)
log.Printf("Default policy err.\n")
return err
}
policyContext, err := signature.NewPolicyContext(defaultPolicy)
if err != nil {
fmt.Printf("Policy context err: %s\n", err)
log.Printf("Policy context err.\n")
return err
}
defer policyContext.Destroy()

// Create new dest archive
aw, err := archive.NewWriter(nil, dest)
if err != nil {
fmt.Printf("%s\n", err)
log.Printf("Error when initializing destination archive.\n")
return err
}
defer aw.Close()

Expand All @@ -41,24 +45,26 @@ func DownloadImage(imgList []string, dest string) {
imgRef := fmt.Sprintf("%s%s", "docker://", img)
srcRef, err := alltransports.ParseImageName(imgRef)
if err != nil {
fmt.Printf("%s\n", err)
log.Printf("Error when parsing image name for %s", img)
return err
}

////////// Dest
imgNamed, err := reference.ParseDockerRef(img)
if err != nil {
fmt.Printf("%s\n", err)
log.Printf("Error when parsing image reference for %s", img)
return err
}

imgNameTagged, err := reference.WithTag(imgNamed, getImgTag(img))
if err != nil {
fmt.Printf("%s\n", err)
log.Printf("Error when parsing image reference and tag for %s", img)
return err
}

// Create dest ref
destRef, err := aw.NewReference(imgNameTagged)
if err != nil {
fmt.Printf("%s\n", err)
log.Printf("Error when creating new image reference for %s", img)
return err
}
//////////

Expand All @@ -69,15 +75,17 @@ func DownloadImage(imgList []string, dest string) {
}

// Download and create tar
fmt.Printf("Copy %s to %s\n", imgRef, dest)
fmt.Printf("Copy %s to %s\n", img, dest)
_, err = copy.Image(context.Background(), policyContext, destRef, srcRef, &copy.Options{
ReportWriter: os.Stdout,
SourceCtx: sysCtx,
})
if err != nil {
fmt.Printf("%s\n", err)
log.Printf("Error when downloading image %s", img)
return err
}
}
return nil
}

func getImgTag(imgStr string) string {
Expand Down
6 changes: 5 additions & 1 deletion helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func RepoUpdate() {
}

// DownloadChart download a chart from public repo to local folder
func DownloadChart(repo string, chart string, version string, dest string) {
func DownloadChart(repo string, chart string, version string, dest string) (chartPath string) {

settings := cli.New()

Expand Down Expand Up @@ -162,6 +162,8 @@ func DownloadChart(repo string, chart string, version string, dest string) {
}

fmt.Printf("Chart downloaded to %s\n", path)

return path
}

func debug(format string, v ...interface{}) {
Expand Down Expand Up @@ -216,6 +218,8 @@ func GetChartImagesFromValues(values map[string]interface{}) ([]string, error) {
if img.Registry != "" {
imgStr = fmt.Sprintf("%s/%s", img.Registry, imgStr)
}
// remove \n to prevent of reference format error
imgStr = strings.TrimSuffix(imgStr, "\n")
imgList = append(imgList, imgStr)
}
} else {
Expand Down

0 comments on commit fa01447

Please sign in to comment.