forked from zoosky/helmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (90 loc) · 2.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"code.cloudfoundry.org/lager"
"github.com/monostream/helmi/pkg/broker"
"github.com/monostream/helmi/pkg/catalog"
"github.com/monostream/helmi/pkg/helm"
)
func main() {
logger := lager.NewLogger("helmi")
logger.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
logger.RegisterSink(lager.NewWriterSink(os.Stderr, lager.ERROR))
// expects a JSON map in the form of "name":"http://url" pairs
err := parseHelmReposFromJSON(getEnv("REPOSITORY_URLS", "{}"))
if err != nil {
log.Fatal(err)
}
catalogSource := getEnv("CATALOG_URL", "./catalog")
c, err := catalog.New(catalogSource)
if err != nil {
log.Fatal("Failed to parse catalog. Did you set CATALOG_URL correctly? Error:", err)
}
err = verifyChartVersions(c)
if err != nil {
log.Fatal(err)
}
addr := ":" + getEnv("PORT", "5000")
user := os.Getenv("USERNAME")
pass := os.Getenv("PASSWORD")
if user == "" || pass == "" {
log.Println("Username and/or password not specified, authentication will be disabled!")
}
config := broker.Config{
Username: user,
Password: pass,
Address: addr,
}
b := broker.NewBroker(c, config, logger)
b.Run()
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func parseHelmReposFromJSON(helmReposJSON string) error {
var helmRepos map[string]string
err := json.Unmarshal([]byte(helmReposJSON), &helmRepos)
if err != nil {
return err
}
for repo, url := range helmRepos {
err := helm.RepoAdd(repo, url)
if err != nil {
return fmt.Errorf("failed to update repository %s: %s", repo, err)
}
}
return helm.RepoUpdate()
}
func verifyChartVersions(catalog *catalog.Catalog) error {
charts, err := helm.ListCharts()
if err != nil {
return err
}
for _, service := range catalog.Services() {
for _, plan := range service.Plans {
chartName := service.Chart
chartVersion := service.ChartVersion
if len(plan.Chart) > 0 {
chartName = plan.Chart
}
if len(plan.ChartVersion) > 0 {
chartVersion = plan.ChartVersion
}
if chart, ok := charts[chartName]; ok {
if !strings.EqualFold(chart.ChartVersion, chartVersion) {
fmt.Println(fmt.Sprintf("Outdated Chart %v: %v <> %v", chartName, chartVersion, chart.ChartVersion))
}
} else {
fmt.Println(fmt.Sprintf("Missing Chart %v", chartName))
}
}
}
return nil
}