forked from opskumu/helm-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (109 loc) · 2.53 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
118
119
120
121
122
123
124
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/golang/glog"
"github.com/spf13/pflag"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/repo"
)
type HelmConfig struct {
UploadPath string `yaml:"uploadPath"`
HelmRepos []*repo.Entry `yaml:"helmRepos"`
}
var (
settings = cli.New()
defaultUploadPath = "/data/charts"
helmConfig = &HelmConfig{}
)
func main() {
var (
listenHost string
listenPort string
config string
)
err := flag.Set("logtostderr", "true")
if err != nil {
glog.Fatalln(err)
}
pflag.CommandLine.StringVar(&listenHost, "addr", "0.0.0.0", "server listen addr")
pflag.CommandLine.StringVar(&listenPort, "port", "8080", "server listen port")
pflag.CommandLine.StringVar(&config, "config", "config.yaml", "helm wrapper config")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
settings.AddFlags(pflag.CommandLine)
pflag.Parse()
defer glog.Flush()
// read config
_, err = readRepoConfig()
if err != nil {
glog.Fatalln(err)
}
// touch blank file
_, err = os.Stat(settings.RepositoryConfig)
if err != nil {
if os.IsNotExist(err) {
var f repo.File
if err := f.WriteFile(settings.RepositoryConfig, 0644); err != nil {
glog.Fatalln(err)
}
} else {
glog.Fatalln(err)
}
}
// init repos
err = rebuildRepos()
if err != nil {
glog.Fatalln(err)
}
// upload chart path
if helmConfig.UploadPath == "" {
helmConfig.UploadPath = defaultUploadPath
} else {
if !filepath.IsAbs(helmConfig.UploadPath) {
glog.Fatalln("charts upload path is not absolute")
}
}
_, err = os.Stat(helmConfig.UploadPath)
if err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(helmConfig.UploadPath, 0755)
if err != nil {
glog.Fatalln(err)
}
} else {
glog.Fatalln(err)
}
}
// router
router := gin.Default()
router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Welcome helm wrapper server")
})
// register router
RegisterRouter(router)
srv := &http.Server{
Addr: fmt.Sprintf("%s:%s", listenHost, listenPort),
Handler: router,
}
glog.Infof("listen: %s", srv.Addr)
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
glog.Fatalf("listen: %s\n", err)
}
}()
quit := make(chan os.Signal, 2)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
glog.Infoln("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
}