Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding helm service endpoints to console #3826

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
4 changes: 2 additions & 2 deletions cmd/bridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/openshift/console/pkg/auth"
"github.com/openshift/console/pkg/bridge"
"github.com/openshift/console/pkg/crypto"
"github.com/openshift/console/pkg/helm"
"github.com/openshift/console/pkg/helm/chartproxy"
"github.com/openshift/console/pkg/proxy"
"github.com/openshift/console/pkg/server"
"github.com/openshift/console/pkg/serverconfig"
Expand Down Expand Up @@ -109,7 +109,7 @@ func main() {

fLoadTestFactor := fs.Int("load-test-factor", 0, "DEV ONLY. The factor used to multiply k8s API list responses for load testing purposes.")

helmConfig := helm.RegisterFlags(fs)
helmConfig := chartproxy.RegisterFlags(fs)

if err := fs.Parse(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
Expand Down
30 changes: 18 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ go 1.12

require (
github.com/coreos/dex v2.3.0+incompatible
github.com/coreos/go-oidc v0.0.0-20180117170138-065b426bd416
github.com/coreos/pkg v0.0.0-20150728231633-7373797ee9cd
github.com/gorilla/websocket v0.0.0-20180201015256-4ac909741dfa
github.com/pquerna/cachecontrol v0.0.0-20160421231612-c97913dcbd76 // indirect
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/net v0.0.0-20190311183353-d8887717615a // indirect
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 // indirect
google.golang.org/genproto v0.0.0-20190508193815-b515fa19cec8 // indirect
google.golang.org/grpc v1.19.0
gopkg.in/square/go-jose.v2 v2.0.1 // indirect
gopkg.in/yaml.v2 v2.2.1
github.com/coreos/go-oidc v2.1.0+incompatible
github.com/coreos/pkg v0.0.0-20180108230652-97fdf19511ea
github.com/gorilla/websocket v1.4.0
github.com/pquerna/cachecontrol v0.0.0-20180517163645-1555304b9b35 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
google.golang.org/grpc v1.24.0
gopkg.in/square/go-jose.v2 v2.4.1 // indirect
gopkg.in/yaml.v2 v2.2.4
helm.sh/helm/v3 v3.0.1
k8s.io/cli-runtime v0.0.0-20191016114015-74ad18325ed5
k8s.io/client-go v0.0.0-20191016111102-bec269661e48
k8s.io/klog v1.0.0
)

replace (
github.com/Azure/go-autorest/autorest => github.com/Azure/go-autorest/autorest v0.9.0
github.com/docker/docker => github.com/moby/moby v0.7.3-0.20190826074503-38ab9da00309
)
579 changes: 567 additions & 12 deletions go.sum

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions pkg/helm/actions/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package actions

import (
"net/http"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
"k8s.io/klog"
)

var settings = cli.New()

type configFlagsWithTransport struct {
*genericclioptions.ConfigFlags
Transport *http.RoundTripper
}

func (c configFlagsWithTransport) ToRESTConfig() (*rest.Config, error) {
return &rest.Config{
Host: *c.APIServer,
BearerToken: *c.BearerToken,
Transport: *c.Transport,
}, nil
}

func GetActionConfigurations(host, ns, token string, transport *http.RoundTripper) *action.Configuration {

confFlags := &configFlagsWithTransport{
ConfigFlags: &genericclioptions.ConfigFlags{
APIServer: &host,
BearerToken: &token,
Namespace: &ns,
},
Transport: transport,
}
conf := new(action.Configuration)
conf.Init(confFlags, ns, "secrets", klog.Infof)

return conf
}
34 changes: 34 additions & 0 deletions pkg/helm/actions/install_chart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package actions

import (
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/release"
)

func InstallChart(ns, name, url string, vals map[string]interface{}, conf *action.Configuration) (*release.Release, error) {
cmd := action.NewInstall(conf)

name, chart, err := cmd.NameAndChart([]string{name, url})
if err != nil {
return nil, err
}
cmd.ReleaseName = name

cp, err := cmd.ChartPathOptions.LocateChart(chart, settings)
if err != nil {
return nil, err
}

ch, err := loader.Load(cp)
if err != nil {
return nil, err
}

cmd.Namespace = ns
release, err := cmd.Run(ch, vals)
if err != nil {
return nil, err
}
return release, nil
}
71 changes: 71 additions & 0 deletions pkg/helm/actions/install_chart_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package actions

akashshinde marked this conversation as resolved.
Show resolved Hide resolved
import (
"io/ioutil"
"testing"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chartutil"
kubefake "helm.sh/helm/v3/pkg/kube/fake"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage"
"helm.sh/helm/v3/pkg/storage/driver"
)

func TestInstallChart(t *testing.T) {
tests := []struct {
releaseName string
chartPath string
chartName string
chartVersion string
}{
{
releaseName: "valid chart path",
chartPath: "../testdata/influxdb-3.0.2.tgz",
chartName: "influxdb",
chartVersion: "3.0.2",
},
{
releaseName: "invalid chart path",
chartPath: "../testdata/influxdb-3.0.1.tgz",
chartName: "influxdb",
chartVersion: "3.0.2",
},
}
for _, tt := range tests {
t.Run(tt.releaseName, func(t *testing.T) {
store := storage.Init(driver.NewMemory())
actionConfig := &action.Configuration{
Releases: store,
KubeClient: &kubefake.PrintingKubeClient{Out: ioutil.Discard},
Capabilities: chartutil.DefaultCapabilities,
Log: func(format string, v ...interface{}) {},
}
rel, err := InstallChart("test-namespace", "test", tt.chartPath, nil, actionConfig)
if tt.releaseName == "valid chart path" {
if err != nil {
t.Error("Error occurred while installing chartPath")
}
if rel.Name != "test" {
t.Error("Release releaseName isn't matching")
}
if rel.Namespace != "test-namespace" {
t.Error("Namespace releaseName isn't matching")
}
if rel.Info.Status != release.StatusDeployed {
t.Error("Chart status should be deployed")
}
if rel.Chart.Metadata.Name != tt.chartName {
t.Error("Chart name mismatch")
}
if rel.Chart.Metadata.Version != tt.chartVersion {
t.Error("Chart version mismatch")
}
} else if tt.releaseName == "invalid chart path" {
if err == nil {
t.Error("Should fail to parse while locating invalid chart")
}
}
})
}
akashshinde marked this conversation as resolved.
Show resolved Hide resolved
}
20 changes: 20 additions & 0 deletions pkg/helm/actions/list_releases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package actions

import (
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/release"
)

func ListReleases(conf *action.Configuration) ([]*release.Release, error) {
cmd := action.NewList(conf)

releases, err := cmd.Run()
if err != nil {
return nil, err
}
if releases == nil {
rs := make([]*release.Release, 0)
return rs, nil
}
return releases, nil
}
75 changes: 75 additions & 0 deletions pkg/helm/actions/list_releases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package actions

import (
"io/ioutil"
"testing"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
kubefake "helm.sh/helm/v3/pkg/kube/fake"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage"
"helm.sh/helm/v3/pkg/storage/driver"
"helm.sh/helm/v3/pkg/time"
)

func TestListReleases(t *testing.T) {
tests := []struct {
name string
release release.Release
}{
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to have a positive & negative test?

name: "list valid releases",
release: release.Release{
Name: "test",
Namespace: "test-namespace",
Info: &release.Info{
FirstDeployed: time.Time{},
Status: "deployed",
},
Chart: &chart.Chart{
Metadata: &chart.Metadata{
Name: "influxdb",
Version: "3.0.2",
},
},
},
},
}
for _, tt := range tests {
store := storage.Init(driver.NewMemory())
t.Run(tt.name, func(t *testing.T) {
// create fake release
err := store.Create(&tt.release)
actionConfig := &action.Configuration{
Releases: store,
KubeClient: &kubefake.PrintingKubeClient{Out: ioutil.Discard},
Capabilities: chartutil.DefaultCapabilities,
Log: func(format string, v ...interface{}) {},
}
rels, err := ListReleases(actionConfig)
if err != nil {
t.Error("Error occurred while installing chartPath")
}
if len(rels) <= 0 {
t.Error("Release list should contain 1 release")
}
if rels[0].Name != "test" {
t.Error("Release releaseName isn't matching")
}
if rels[0].Namespace != "test-namespace" {
t.Error("Namespace rels[0]easeName isn't matching")
}
if rels[0].Info.Status != release.StatusDeployed {
t.Error("Chart status should be deployed")
}
if rels[0].Chart.Metadata.Name != "influxdb" {
t.Error("Chart name mismatch")
}
if rels[0].Chart.Metadata.Version != "3.0.2" {
t.Error("Chart version mismatch")
}
akashshinde marked this conversation as resolved.
Show resolved Hide resolved
})
}
}