mirror of
https://github.com/komodorio/helm-dashboard.git
synced 2026-03-24 11:48:04 +00:00
* Object model with self-sufficient binary (#131) * Code cosmetics * Experimenting with object model and direct HELM usage * Experiment with object model * replacing the kubectl * Progressing * Save the progress * Able to start with migration in mind * Migrated two pieces * List releases via Helm * Forgotten field * Cristallized the problem of ctx switcher * Reworked to multi-context * Rollback is also new style * More migration * Refactoring * Describe via code * Bye-bye kubectl binary * Eliminate more old code * Refactor a bit * Merges * No binaries in dockerfile * Commit * Progress with getting the data * Learned the thing about get * One field less * Sstart with repos * Repo add * repo remove * Repos! Icons! * Simplified access to data * Ver listing works * Ver check works * Caching and values * fixup * Done with repos * Working on install * Install work-ish * Fix UI failing on install * Upgrade flow works * Fix image building * Remove outdated test file * Move files around * REfactorings * Cosmetics * Test for cache control (#151) * Files import formatted * Added go-test tools * Added test for no-cache header * added changes * test for cache behaviour of app * test for static route (#153) * Tests: route configuration & context setter (#154) * Test for route configuration * Test for context setter middleware * implemented changes * Restore coverage profile Fixes #156 * Cosmetics * Test for `NewRouter` function (#157) * Test for `configureScanners` (#158) * Test for `configureKubectls` (#163) * Test for repository loading (#169) - Created `repos_test.go` - Test: `Load()` of Repositories * Build all PRs * Fixes failing test (#171) * Fixes failing test - Fixes failing test of repo loading * handles error for * Did some changes * Test for listing of repos (#173) - and did some code formatting Signed-off-by: OmAxiani0 <aximaniom@gmail.com> Signed-off-by: OmAxiani0 <aximaniom@gmail.com> * Test for adding repo (#175) - Modified the `repositories.yml` file Signed-off-by: OmAxiani0 <aximaniom@gmail.com> Signed-off-by: OmAxiani0 <aximaniom@gmail.com> * Test for deleting the repository (#176) * Test for deleting the repository - Also added cleanup function for `TestAdd` * Fixes failing test * Add auto labeler for PR's (#174) * Add auto labeler for PR's * Add all file under .github/workflow to 'ci' label Co-authored-by: Harshit Mehta <harshitm@nvidia.com> * Test for getting repository (#177) * Add github workflow for auto PR labeling (#181) Co-authored-by: Harshit Mehta <harshitm@nvidia.com> * Stub compilation * Fixes around installing * More complex test * Using object model to execute helm test (#191) * Expand test * More test * Coverage * Add mutex for operations * Rectore cluster detection code * Change receiver to pointer * Support multiple namespaces * Cosmetics * Update repos periodically * fix tests * Fix error display * Allow reconfiguring chart without repo * mute linter * Cosmetics * Failing approach to parse manifests Relates to #30 * Report the error properly * ✅ Add test for dashboard/objects/data.go NewDataLayer (#199) * Fix problem of wrong namespace * Added unit tests for releases (#204) * Rework API routes (#197) * Bootstrap OpenAPI doc * Renaming some routes * Listing namespaces * k8s part of things * Repositories section * Document scanners API * One more API call * Progress * Reworked install flow * History endpoint * Textual info section * Resources endpoint * Rollback endpoint * Rollback endpoint * Unit tests * Cleanup * Forgotten tags * Fix tests * TODOs * Rework manifest scanning * add hasTests flag * Adding more information on UI for helm test API response (#195) * Hide test button when no tests Fixes #115 Improves #195 --------- Signed-off-by: OmAxiani0 <aximaniom@gmail.com> Co-authored-by: Om Aximani <75031769+OmAximani0@users.noreply.github.com> Co-authored-by: Harshit Mehta <hdm23061993@gmail.com> Co-authored-by: Harshit Mehta <harshitm@nvidia.com> Co-authored-by: Todd Turner <todd@toddtee.sh> Co-authored-by: arvindsundararajan98 <109727359+arvindsundararajan98@users.noreply.github.com>
218 lines
5.5 KiB
Go
218 lines
5.5 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/joomcode/errorx"
|
|
"github.com/komodorio/helm-dashboard/pkg/dashboard/objects"
|
|
"github.com/komodorio/helm-dashboard/pkg/dashboard/subproc"
|
|
"helm.sh/helm/v3/pkg/action"
|
|
"helm.sh/helm/v3/pkg/cli"
|
|
"helm.sh/helm/v3/pkg/registry"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/hashicorp/go-version"
|
|
"github.com/komodorio/helm-dashboard/pkg/dashboard/scanners"
|
|
"github.com/komodorio/helm-dashboard/pkg/dashboard/utils"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type Server struct {
|
|
Version string
|
|
Namespaces []string
|
|
Address string
|
|
Debug bool
|
|
NoTracking bool
|
|
}
|
|
|
|
func (s *Server) StartServer(ctx context.Context, cancel context.CancelFunc) (string, utils.ControlChan, error) {
|
|
data, err := objects.NewDataLayer(s.Namespaces, s.Version, NewHelmConfig)
|
|
if err != nil {
|
|
return "", nil, errorx.Decorate(err, "Failed to create data layer")
|
|
}
|
|
|
|
isDevModeWithAnalytics := os.Getenv("HD_DEV_ANALYTICS") == "true"
|
|
data.StatusInfo.Analytics = (!s.NoTracking && s.Version != "0.0.0") || isDevModeWithAnalytics
|
|
|
|
err = s.detectClusterMode(data)
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
go checkUpgrade(data.StatusInfo)
|
|
|
|
discoverScanners(data)
|
|
|
|
go data.PeriodicTasks(ctx)
|
|
|
|
api := NewRouter(cancel, data, s.Debug)
|
|
done := s.startBackgroundServer(api, ctx)
|
|
|
|
return "http://" + s.Address, done, nil
|
|
}
|
|
|
|
func (s *Server) detectClusterMode(data *objects.DataLayer) error {
|
|
data.StatusInfo.ClusterMode = os.Getenv("HD_CLUSTER_MODE") != ""
|
|
if data.StatusInfo.ClusterMode {
|
|
return nil
|
|
}
|
|
|
|
ctxs, err := data.ListContexts()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(ctxs) == 0 {
|
|
log.Infof("Got no kubectl config contexts, will attempt to detect if we're inside cluster...")
|
|
app, err := data.AppForCtx("")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ns, err := app.K8s.GetNameSpaces()
|
|
if err != nil { // no point in continuing without kubectl context and k8s connection
|
|
return err
|
|
}
|
|
log.Debugf("Got %d namespaces listed", len(ns.Items))
|
|
data.StatusInfo.ClusterMode = true
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (s *Server) startBackgroundServer(routes *gin.Engine, ctx context.Context) utils.ControlChan {
|
|
done := make(utils.ControlChan)
|
|
server := &http.Server{
|
|
Addr: s.Address,
|
|
Handler: routes,
|
|
}
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
err := server.Shutdown(context.Background())
|
|
if err != nil {
|
|
log.Warnf("Had problems shutting down the server: %s", err)
|
|
}
|
|
log.Infof("Web server has been shut down.")
|
|
}()
|
|
|
|
go func() {
|
|
err := server.ListenAndServe()
|
|
if err != nil && err != http.ErrServerClosed {
|
|
log.Warnf("Looks like port is busy for %s, checking if it's us...", s.Address)
|
|
if s.itIsUs() {
|
|
log.Infof("Yes, it's another instance of us. Just reuse it.")
|
|
} else {
|
|
panic(err)
|
|
}
|
|
}
|
|
done <- struct{}{}
|
|
}()
|
|
|
|
return done
|
|
}
|
|
|
|
func (s *Server) itIsUs() bool {
|
|
url := fmt.Sprintf("http://%s/status", s.Address)
|
|
var myClient = &http.Client{
|
|
Timeout: 5 * time.Second,
|
|
}
|
|
r, err := myClient.Get(url)
|
|
if err != nil {
|
|
log.Debugf("It's not us on %s: %s", s.Address, err)
|
|
return false
|
|
}
|
|
defer r.Body.Close()
|
|
|
|
return strings.HasPrefix(r.Header.Get("X-Application-Name"), "Helm Dashboard")
|
|
}
|
|
|
|
func discoverScanners(data *objects.DataLayer) {
|
|
potential := []subproc.Scanner{
|
|
&scanners.Checkov{Data: data},
|
|
&scanners.Trivy{Data: data},
|
|
}
|
|
|
|
data.Scanners = []subproc.Scanner{}
|
|
for _, scanner := range potential {
|
|
if scanner.Test() {
|
|
data.Scanners = append(data.Scanners, scanner)
|
|
}
|
|
}
|
|
}
|
|
|
|
func checkUpgrade(d *objects.StatusInfo) { // TODO: check it once an hour
|
|
url := "https://api.github.com/repos/komodorio/helm-dashboard/releases/latest"
|
|
type GHRelease struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
var myClient = &http.Client{Timeout: 5 * time.Second}
|
|
r, err := myClient.Get(url)
|
|
if err != nil {
|
|
log.Warnf("Failed to check for new version: %s", err)
|
|
return
|
|
}
|
|
defer r.Body.Close()
|
|
|
|
target := new(GHRelease)
|
|
err = json.NewDecoder(r.Body).Decode(target)
|
|
if err != nil {
|
|
log.Warnf("Failed to decode new release version: %s", err)
|
|
return
|
|
}
|
|
d.LatestVer = target.Name
|
|
|
|
v1, err := version.NewVersion(d.CurVer)
|
|
if err != nil {
|
|
log.Warnf("Failed to parse CurVer: %s", err)
|
|
v1 = &version.Version{}
|
|
}
|
|
|
|
v2, err := version.NewVersion(d.LatestVer)
|
|
if err != nil {
|
|
log.Warnf("Failed to parse RepoLatestVer: %s", err)
|
|
} else {
|
|
if v1.LessThan(v2) {
|
|
log.Warnf("Newer Helm Dashboard version is available: %s", d.LatestVer)
|
|
log.Warnf("Upgrade instructions: https://github.com/komodorio/helm-dashboard#installing")
|
|
} else {
|
|
log.Debugf("Got latest version from GH: %s", d.LatestVer)
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewHelmConfig(origSettings *cli.EnvSettings, ns string) (*action.Configuration, error) {
|
|
// TODO: cache it into map
|
|
// TODO: I feel there should be more elegant way to organize this code
|
|
actionConfig := new(action.Configuration)
|
|
|
|
settings := cli.New()
|
|
settings.KubeContext = origSettings.KubeContext
|
|
settings.SetNamespace(ns) // important for RESTClientGetter to have correct namespace
|
|
|
|
registryClient, err := registry.NewClient(
|
|
registry.ClientOptDebug(false),
|
|
registry.ClientOptEnableCache(true),
|
|
//registry.ClientOptWriter(out),
|
|
registry.ClientOptCredentialsFile(settings.RegistryConfig),
|
|
)
|
|
if err != nil {
|
|
return nil, errorx.Decorate(err, "failed to crete helm config object")
|
|
}
|
|
actionConfig.RegistryClient = registryClient
|
|
|
|
helmDriver := os.Getenv("HELM_DRIVER")
|
|
if err := actionConfig.Init(
|
|
settings.RESTClientGetter(),
|
|
ns,
|
|
helmDriver, log.Debugf); err != nil {
|
|
return nil, errorx.Decorate(err, "failed to init Helm action config")
|
|
}
|
|
|
|
return actionConfig, nil
|
|
}
|