Skip to content

Commit

Permalink
New CLI Flag --devel To Include Development/Prerelease Versions of …
Browse files Browse the repository at this point in the history
…Charts (#139)

* Include devel Flag for Toggling Dev Chart Versions

The flag `--devel` for enabling/disabling dev versions
of charts in following endpoints:
1. /api/helm/repositories/kafka-operator
2. /api/helm/repositories/versions
3. /api/helm/repositories/latestver

Signed-off-by: Bhargav Ravuri <[email protected]>

* Run Tests on Devel Flag Related Changes

Signed-off-by: Bhargav Ravuri <[email protected]>

---------

Signed-off-by: Bhargav Ravuri <[email protected]>
  • Loading branch information
Bhargav-InfraCloud authored Feb 11, 2023
1 parent 61b67f8 commit 6a4ca79
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 55 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/komodorio/helm-dashboard
go 1.18

require (
github.com/Masterminds/semver/v3 v3.2.0
github.com/eko/gocache/v3 v3.1.2
github.com/gin-gonic/gin v1.8.1
github.com/hashicorp/go-version v1.6.0
Expand All @@ -17,20 +18,21 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.4.0
helm.sh/helm/v3 v3.11.1
k8s.io/api v0.26.0
k8s.io/apimachinery v0.26.0
k8s.io/cli-runtime v0.26.0
k8s.io/client-go v0.26.0
k8s.io/kubectl v0.26.0
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/Masterminds/squirrel v1.5.3 // indirect
github.com/XiaoMi/pegasus-go-client v0.0.0-20210427083443-f3b6b08bc4c2 // indirect
Expand Down Expand Up @@ -149,13 +151,11 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gotest.tools/v3 v3.4.0
k8s.io/apiextensions-apiserver v0.26.0 // indirect
k8s.io/apiserver v0.26.0 // indirect
k8s.io/component-base v0.26.0 // indirect
k8s.io/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d
oras.land/oras-go v1.2.2 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/kustomize/api v0.12.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type options struct {
BindHost string `long:"bind" description:"Host binding to start server (default: localhost)"` // default should be printed but not assigned as the precedence: flag > env > default
Port uint `short:"p" long:"port" description:"Port to start server on" default:"8080"`
Namespace string `short:"n" long:"namespace" description:"Namespace for HELM operations"`
Devel bool `long:"devel" description:"Include development versions of charts"`
}

func main() {
Expand All @@ -50,6 +51,7 @@ func main() {
Address: fmt.Sprintf("%s:%d", opts.BindHost, opts.Port),
Debug: opts.Verbose,
NoTracking: opts.NoTracking,
Devel: opts.Devel,
}

ctx, cancel := context.WithCancel(context.Background())
Expand Down
29 changes: 15 additions & 14 deletions pkg/dashboard/api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package dashboard

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"

"github.com/gin-gonic/gin"
"github.com/komodorio/helm-dashboard/pkg/dashboard/handlers"
"github.com/komodorio/helm-dashboard/pkg/dashboard/objects"
Expand All @@ -13,14 +22,6 @@ import (
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/storage"
"helm.sh/helm/v3/pkg/storage/driver"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
)

var inMemStorage *storage.Storage
Expand Down Expand Up @@ -111,7 +112,7 @@ func TestConfigureRoutes(t *testing.T) {

// Required arguements for route configuration
abortWeb := func() {}
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig)
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig, false)

if err != nil {
t.Fatal(err)
Expand All @@ -131,7 +132,7 @@ func TestContextSetter(t *testing.T) {
con := GetTestGinContext(w)

// Required arguements
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig)
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig, false)

if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -161,7 +162,7 @@ func TestNewRouter(t *testing.T) {

// Required arguemnets
abortWeb := func() {}
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig)
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig, false)

if err != nil {
t.Fatal(err)
Expand All @@ -183,7 +184,7 @@ func TestConfigureScanners(t *testing.T) {
}

// Required arguemnets
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig)
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig, false)

if err != nil {
t.Fatal(err)
Expand All @@ -206,7 +207,7 @@ func TestConfigureKubectls(t *testing.T) {
}

// Required arguemnets
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig)
data, err := objects.NewDataLayer([]string{"TestSpace"}, "T-1", NewHelmConfig, false)

if err != nil {
t.Fatal(err)
Expand All @@ -226,7 +227,7 @@ func TestConfigureKubectls(t *testing.T) {

func TestE2E(t *testing.T) {
// Initialize data layer
data, err := objects.NewDataLayer([]string{""}, "0.0.0-test", getFakeHelmConfig)
data, err := objects.NewDataLayer([]string{""}, "0.0.0-test", getFakeHelmConfig, false)
assert.NilError(t, err)

// Create a new router with the function
Expand Down
13 changes: 10 additions & 3 deletions pkg/dashboard/objects/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/joomcode/errorx"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"

// Import to initialize client auth plugins.
// From https://github.com/kubernetes/client-go/issues/242
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand All @@ -22,7 +23,7 @@ type Application struct {
Repositories *Repositories
}

func NewApplication(settings *cli.EnvSettings, helmConfig HelmNSConfigGetter, namespaces []string) (*Application, error) {
func NewApplication(settings *cli.EnvSettings, helmConfig HelmNSConfigGetter, namespaces []string, devel bool) (*Application, error) {
hc, err := helmConfig(settings.Namespace())
if err != nil {
return nil, errorx.Decorate(err, "failed to get helm config for namespace '%s'", "")
Expand All @@ -33,6 +34,11 @@ func NewApplication(settings *cli.EnvSettings, helmConfig HelmNSConfigGetter, na
return nil, errorx.Decorate(err, "failed to get k8s client")
}

semVerConstraint, err := versionConstaint(devel)
if err != nil {
return nil, errorx.Decorate(err, "failed to create semantic version constraint")
}

return &Application{
HelmConfig: helmConfig,
K8s: k8s,
Expand All @@ -42,8 +48,9 @@ func NewApplication(settings *cli.EnvSettings, helmConfig HelmNSConfigGetter, na
HelmConfig: helmConfig,
},
Repositories: &Repositories{
Settings: settings,
HelmConfig: hc,
Settings: settings,
HelmConfig: hc,
versionConstraint: semVerConstraint,
},
}, nil
}
9 changes: 6 additions & 3 deletions pkg/dashboard/objects/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"time"

"io"

"github.com/joomcode/errorx"
"github.com/komodorio/helm-dashboard/pkg/dashboard/subproc"
"github.com/pkg/errors"
Expand All @@ -15,7 +17,6 @@ import (
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/release"
"io"
v1 "k8s.io/apimachinery/pkg/apis/testapigroup/v1"
"k8s.io/client-go/tools/clientcmd"
)
Expand All @@ -30,6 +31,7 @@ type DataLayer struct {
ConfGen HelmConfigGetter
appPerContext map[string]*Application
appPerContextMx *sync.Mutex
devel bool
}

type StatusInfo struct {
Expand All @@ -40,7 +42,7 @@ type StatusInfo struct {
ClusterMode bool
}

func NewDataLayer(ns []string, ver string, cg HelmConfigGetter) (*DataLayer, error) {
func NewDataLayer(ns []string, ver string, cg HelmConfigGetter, devel bool) (*DataLayer, error) {
if cg == nil {
return nil, errors.New("HelmConfigGetter can't be nil")
}
Expand All @@ -56,6 +58,7 @@ func NewDataLayer(ns []string, ver string, cg HelmConfigGetter) (*DataLayer, err
ConfGen: cg,
appPerContext: map[string]*Application{},
appPerContextMx: new(sync.Mutex),
devel: devel,
}, nil
}

Expand Down Expand Up @@ -162,7 +165,7 @@ func (d *DataLayer) AppForCtx(ctx string) (*Application, error) {
return d.ConfGen(settings, ns)
}

a, err := NewApplication(settings, cfgGetter, d.Namespaces)
a, err := NewApplication(settings, cfgGetter, d.Namespaces, d.devel)
if err != nil {
return nil, errorx.Decorate(err, "Failed to create application for context '%s'", ctx)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/dashboard/objects/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ func TestNewDataLayer(t *testing.T) {
namespaces []string
version string
helmConfig HelmConfigGetter
devel bool
errorExpected bool
}{
{
name: "should return error when helm config is nil",
namespaces: []string{"namespace1", "namespace2"},
version: "1.0.0",
helmConfig: nil,
devel: false,
errorExpected: true,
},
{
Expand All @@ -34,12 +36,13 @@ func TestNewDataLayer(t *testing.T) {
helmConfig: func(sett *cli.EnvSettings, ns string) (*action.Configuration, error) {
return &action.Configuration{}, nil
},
devel: false,
errorExpected: false,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
dl, err := NewDataLayer(tt.namespaces, tt.version, tt.helmConfig)
dl, err := NewDataLayer(tt.namespaces, tt.version, tt.helmConfig, tt.devel)
if tt.errorExpected {
assert.Error(t, err, "Expected error but got nil")
} else {
Expand Down
Loading

0 comments on commit 6a4ca79

Please sign in to comment.