Skip to content

Commit

Permalink
[helm3] Return deleted time if exists (#1436)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Martinez Gotor authored Jan 13, 2020
1 parent 691135c commit 0800ec4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
17 changes: 14 additions & 3 deletions cmd/kubeops/internal/handler/dashboardcompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
package handler

import (
"fmt"
"strings"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/any"
"github.com/golang/protobuf/ptypes/timestamp"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
h3chart "helm.sh/helm/v3/pkg/chart"
Expand All @@ -16,16 +19,24 @@ import (
h2 "k8s.io/helm/pkg/proto/hapi/release"
)

func newDashboardCompatibleRelease(h3r h3.Release) h2.Release {
func newDashboardCompatibleRelease(h3r h3.Release) (h2.Release, error) {
var deleted *timestamp.Timestamp
if !h3r.Info.Deleted.IsZero() {
var err error
deleted, err = ptypes.TimestampProto(h3r.Info.Deleted.Time)
if err != nil {
return h2.Release{}, fmt.Errorf("Failed to parse deletion time %v", err)
}
}
return h2.Release{
Name: h3r.Name,
Info: &h2.Info{Status: compatibleStatus(*h3r.Info)},
Info: &h2.Info{Status: compatibleStatus(*h3r.Info), Deleted: deleted},
Chart: compatibleChart(*h3r.Chart),
Config: compatibleConfig(h3r),
Manifest: h3r.Manifest,
Version: int32(h3r.Version),
Namespace: h3r.Namespace,
}
}, nil
}

func compatibleChart(h3c h3chart.Chart) *h2chart.Chart {
Expand Down
5 changes: 4 additions & 1 deletion cmd/kubeops/internal/handler/dashboardcompat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ func TestNewDashboardCompatibleRelease(t *testing.T) {
}
}()
// Perform conversion
compatibleH3rls := newDashboardCompatibleRelease(test.Helm3Release)
compatibleH3rls, err := newDashboardCompatibleRelease(test.Helm3Release)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Marshall both: Compatible H3Release and H2Release
h3Marshalled := test.MarshallingFunction(compatibleH3rls)
t.Logf("Marshalled Helm 3 Release %s", h3Marshalled)
Expand Down
15 changes: 12 additions & 3 deletions cmd/kubeops/internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ func upgradeRelease(cfg Config, w http.ResponseWriter, req *http.Request, params
response.NewErrorResponse(handlerutil.ErrorCode(err), err.Error()).Write(w)
return
}
response.NewDataResponse(newDashboardCompatibleRelease(*rel)).Write(w)

compatRelease, err := newDashboardCompatibleRelease(*rel)
if err != nil {
response.NewErrorResponse(handlerutil.ErrorCode(err), err.Error()).Write(w)
return
}
response.NewDataResponse(compatRelease).Write(w)
}

// GetRelease returns a release
Expand All @@ -180,7 +184,12 @@ func GetRelease(cfg Config, w http.ResponseWriter, req *http.Request, params han
response.NewErrorResponse(handlerutil.ErrorCode(err), err.Error()).Write(w)
return
}
response.NewDataResponse(newDashboardCompatibleRelease(*release)).Write(w)
compatRelease, err := newDashboardCompatibleRelease(*release)
if err != nil {
response.NewErrorResponse(handlerutil.ErrorCode(err), err.Error()).Write(w)
return
}
response.NewDataResponse(compatRelease).Write(w)
}

// DeleteRelease deletes a release
Expand Down
31 changes: 30 additions & 1 deletion cmd/kubeops/internal/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strings"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/kubeapps/kubeapps/pkg/auth"
Expand All @@ -19,12 +20,17 @@ import (
kubefake "helm.sh/helm/v3/pkg/kube/fake"
"helm.sh/helm/v3/pkg/storage"
"helm.sh/helm/v3/pkg/storage/driver"
helmTime "helm.sh/helm/v3/pkg/time"

"helm.sh/helm/v3/pkg/release"
)

const defaultListLimit = 256

var (
testingTime, _ = helmTime.Parse(time.RFC3339, "1977-09-02T22:04:05Z")
)

// newConfigFixture returns a Config with fake clients
// and memory storage.
func newConfigFixture(t *testing.T) *Config {
Expand Down Expand Up @@ -191,6 +197,25 @@ func TestActions(t *testing.T) {
},
ResponseBody: `{"data":{"name":"foobar","info":{"status":{"code":1}},"chart":{"metadata":{"name":"foo"},"values":{"raw":"{}\n"}},"config":{"raw":"{}\n"},"version":1,"namespace":"default"}}`,
},
{
// Scenario params
Description: "Get a deleted release",
ExistingReleases: []release.Release{
createRelease("foo", "foobar", "default", 1, release.StatusUninstalled),
},
DisableAuth: true,
// Request params
RequestBody: "",
RequestQuery: "",
Action: "get",
Params: map[string]string{"namespace": "default", "releaseName": "foobar"},
// Expected result
StatusCode: 200,
RemainingReleases: []release.Release{
createRelease("foo", "foobar", "default", 1, release.StatusUninstalled),
},
ResponseBody: `{"data":{"name":"foobar","info":{"status":{"code":2},"deleted":{"seconds":242085845}},"chart":{"metadata":{"name":"foo"},"values":{"raw":"{}\n"}},"config":{"raw":"{}\n"},"version":1,"namespace":"default"}}`,
},
{
// Scenario params
Description: "Delete and purge a simple release with purge=1",
Expand Down Expand Up @@ -305,11 +330,15 @@ func derefReleases(storage *storage.Storage) []release.Release {
}

func createRelease(chartName, name, namespace string, version int, status release.Status) release.Release {
deleted := helmTime.Time{}
if status == release.StatusUninstalled {
deleted = testingTime
}
return release.Release{
Name: name,
Namespace: namespace,
Version: version,
Info: &release.Info{Status: status},
Info: &release.Info{Status: status, Deleted: deleted},
Chart: &chart.Chart{
Metadata: &chart.Metadata{
Name: chartName,
Expand Down

0 comments on commit 0800ec4

Please sign in to comment.