Skip to content

Instantly share code, notes, and snippets.

@tegioz
Last active August 19, 2020 19:25
Show Gist options
  • Save tegioz/6c81dc7f2d77c66ae3c1b513e5aba7f1 to your computer and use it in GitHub Desktop.
Save tegioz/6c81dc7f2d77c66ae3c1b513e5aba7f1 to your computer and use it in GitHub Desktop.
Script to import repositories in Helm Hub not present in Artifact Hub
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/artifacthub/hub/internal/hub"
"github.com/artifacthub/hub/internal/repo"
"github.com/artifacthub/hub/internal/util"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
)
const (
helmReposURL = "https://raw.githubusercontent.com/helm/hub/master/repos.yaml"
)
var (
orgName = flag.String("org-name", "", "Name of the organization which will own the imported repositories")
userID = flag.String("user-id", "", "ID of the user who will register the repositories (must belong to org)")
)
func main() {
flag.Parse()
if *orgName == "" || *userID == "" {
log.Fatal().Msg("org name and user id must be provided")
}
// Setup some internal services
cfg, err := util.SetupConfig("helm-import")
if err != nil {
log.Fatal().Err(err).Msg("configuration setup failed")
}
if err := util.SetupLogger(cfg, nil); err != nil {
log.Fatal().Err(err).Msg("logger setup failed")
}
db, err := util.SetupDB(cfg)
if err != nil {
log.Fatal().Err(err).Msg("database setup failed")
}
repositoryManager := repo.NewManager(cfg, db)
// Get repos available in Artifact Hub
artifactHubRepos, err := repositoryManager.GetAll(context.Background())
if err != nil {
log.Fatal().Err(err).Msg("error getting repos available in Artifact Hub")
}
// Get repos available in Helm Hub
helmHubRepos, err := getHelmHubRepos()
if err != nil {
log.Fatal().Err(err).Msg("error getting repos available in Helm Hub")
}
// Register repos in Helm Hub not present in Artifact Hub
var reposToRegister []*hub.Repository
for _, helmHubRepo := range helmHubRepos {
if !isHelmRepoInArtifactHub(artifactHubRepos, helmHubRepo) {
reposToRegister = append(reposToRegister, &hub.Repository{
Kind: hub.Helm,
Name: helmHubRepo.Name,
URL: helmHubRepo.URL,
})
}
}
ctx := context.WithValue(context.Background(), hub.UserIDKey, *userID)
for _, r := range reposToRegister {
log.Debug().Str("repo", r.Name).Str("url", r.URL).Msg("registering repo in Artifact Hub")
if err := repositoryManager.Add(ctx, *orgName, r); err != nil {
log.Warn().
Err(err).
Str("repo", r.Name).
Str("url", r.URL).
Msg("error registering repo in Artifact Hub")
}
}
}
func getHelmHubRepos() ([]*HelmRepo, error) {
resp, err := http.Get(helmReposURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var repos *HelmRepos
if err = yaml.Unmarshal(data, &repos); err != nil {
return nil, err
}
return repos.Repositories, nil
}
func isHelmRepoInArtifactHub(artifactHubRepos []*hub.Repository, repo *HelmRepo) bool {
helmRepoName := strings.ToLower(repo.Name)
helmRepoURL := strings.TrimSuffix(repo.URL, "/")
for _, artifactHubRepo := range artifactHubRepos {
if strings.ToLower(artifactHubRepo.Name) == helmRepoName {
return true
}
if strings.TrimSuffix(artifactHubRepo.URL, "/") == helmRepoURL {
return true
}
}
return false
}
type HelmRepos struct {
Repositories []*HelmRepo `yaml:"repositories"`
}
type HelmRepo struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment