Force namespace via cmdline parameter (#53)

* Force name via cmdline parameter

* Use a library to parse CLI flags

* Use less env vars

* Document it
This commit is contained in:
Andrey Pokhilko
2022-10-28 12:39:19 +01:00
committed by GitHub
parent d9edcf2f48
commit 786bddc478
8 changed files with 93 additions and 30 deletions

View File

@@ -44,9 +44,9 @@ func contextSetter(data *subproc.DataLayer) gin.HandlerFunc {
}
}
func NewRouter(abortWeb utils.ControlChan, data *subproc.DataLayer) *gin.Engine {
func NewRouter(abortWeb utils.ControlChan, data *subproc.DataLayer, debug bool) *gin.Engine {
var api *gin.Engine
if os.Getenv("DEBUG") == "" {
if debug {
api = gin.New()
api.Use(gin.Recovery())
} else {

View File

@@ -11,11 +11,14 @@ import (
log "github.com/sirupsen/logrus"
"net/http"
"os"
"strconv"
"time"
)
func StartServer(version string) (string, utils.ControlChan) {
data := subproc.DataLayer{}
func StartServer(version string, port int, ns string, debug bool) (string, utils.ControlChan) {
data := subproc.DataLayer{
Namespace: ns,
}
err := data.CheckConnectivity()
if err != nil {
log.Errorf("Failed to check that Helm is operational, cannot continue. The error was: %s", err)
@@ -32,14 +35,10 @@ func StartServer(version string) (string, utils.ControlChan) {
address = "localhost"
}
if os.Getenv("HD_PORT") == "" {
address += ":8080" // TODO: better default port to clash less?
} else {
address += ":" + os.Getenv("HD_PORT")
}
address += ":" + strconv.Itoa(port)
abort := make(utils.ControlChan)
api := NewRouter(abort, &data)
api := NewRouter(abort, &data, debug)
done := startBackgroundServer(address, api, abort)
return "http://" + address, done

View File

@@ -26,6 +26,7 @@ type DataLayer struct {
Kubectl string
Scanners []Scanner
VersionInfo *VersionInfo
Namespace string
}
type VersionInfo struct {
@@ -34,7 +35,11 @@ type VersionInfo struct {
}
func (d *DataLayer) runCommand(cmd ...string) (string, error) {
log.Debugf("Starting command: %s", cmd)
for i, c := range cmd {
if c == "--namespace" && i < len(cmd) { // TODO: in case it's not found - add it?
d.forceNamespace(&cmd[i+1])
}
}
return utils.RunCommand(cmd, map[string]string{"HELM_KUBECONTEXT": d.KubeContext})
}
@@ -67,6 +72,12 @@ func (d *DataLayer) runCommandKubectl(cmd ...string) (string, error) {
return d.runCommand(cmd...)
}
func (d *DataLayer) forceNamespace(s *string) {
if d.Namespace != "" {
*s = d.Namespace
}
}
func (d *DataLayer) CheckConnectivity() error {
contexts, err := d.ListContexts()
if err != nil {
@@ -128,8 +139,16 @@ func (d *DataLayer) ListContexts() (res []KubeContext, err error) {
}
func (d *DataLayer) ListInstalled() (res []ReleaseElement, err error) {
cmd := []string{"ls", "--all", "--output", "json", "--time-format", time.RFC3339}
// TODO: filter by namespace
out, err := d.runCommandHelm("ls", "--all", "--all-namespaces", "--output", "json", "--time-format", time.RFC3339)
if d.Namespace == "" {
cmd = append(cmd, "--all-namespaces")
} else {
cmd = append(cmd, "--namespace", d.Namespace)
}
out, err := d.runCommandHelm(cmd...)
if err != nil {
return nil, err
}
@@ -218,6 +237,7 @@ func enrichRepoChartsWithInstalled(charts []*RepoChartElement, installed []Relea
pieces := strings.Split(chart.Name, "/")
if pieces[1] == c {
// TODO: there can be more than one
chart.InstalledNamespace = rel.Namespace
chart.InstalledName = rel.Name
}

View File

@@ -34,7 +34,7 @@ func TempFile(txt string) (string, func(), error) {
return "", nil, err
}
return file.Name(), func() { os.Remove(file.Name()) }, nil
return file.Name(), func() { _ = os.Remove(file.Name()) }, nil
}
type CmdError struct {
@@ -49,6 +49,7 @@ func (e CmdError) Error() string {
}
func RunCommand(cmd []string, env map[string]string) (string, error) {
log.Debugf("Starting command: %s", cmd)
prog := exec.Command(cmd[0], cmd[1:]...)
prog.Env = os.Environ()