Skip to content

Commit a108654

Browse files
committed
merge transparent & tag => annotations
Signed-off-by: Gang Liu <gang.liu@daocloud.io>
1 parent d4cb7ea commit a108654

6 files changed

Lines changed: 61 additions & 72 deletions

File tree

client.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"reflect"
1111
"strings"
1212

13-
"k8s.io/apimachinery/pkg/api/errors"
14-
1513
"github.com/spf13/pflag"
1614
"helm.sh/helm/v3/pkg/action"
1715
"helm.sh/helm/v3/pkg/chart"
@@ -25,6 +23,7 @@ import (
2523
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2624
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
2725
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
26+
"k8s.io/apimachinery/pkg/api/errors"
2827
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2928
"k8s.io/apimachinery/pkg/util/yaml"
3029
"k8s.io/cli-runtime/pkg/genericclioptions"
@@ -274,20 +273,25 @@ func (c *HelmClient) ListReleases(opts ListOptions) ([]*release.Release, error)
274273
filterByName = true
275274
filter = strings.ToLower(opts.Filter)
276275
}
277-
for _, v := range rels {
278-
if filterByName && !strings.Contains(strings.ToLower(v.Name), filter) {
279-
continue
280-
}
281276

282-
t, ok := v.Config[walkAroundCustomTagKey]
283-
if !ok {
277+
// TODO: go routine it ?
278+
for _, rel := range rels {
279+
if filterByName && !strings.Contains(strings.ToLower(rel.Name), filter) {
284280
continue
285281
}
286-
tag := strings.TrimSpace(t.(string))
287-
if tag != opts.Selector {
288-
continue
282+
283+
for key, val := range opts.Selectors {
284+
sel, ok := rel.Config[key]
285+
if !ok {
286+
continue
287+
}
288+
289+
if strings.TrimSpace(sel.(string)) != val {
290+
continue
291+
}
289292
}
290-
rr = append(rr, v)
293+
294+
rr = append(rr, rel)
291295
}
292296
return rr, nil
293297
}
@@ -302,24 +306,28 @@ func (c *HelmClient) GetRelease(name string) (*release.Release, error) {
302306
return c.getRelease(name)
303307
}
304308

305-
// Transparent returns the transparent value if not existed, returns ""
306-
func (c *HelmClient) Transparent(releaseName string) string {
307-
r, err := c.getRelease(releaseName)
308-
if err != nil {
309-
return ""
310-
}
311-
return c.TransparentWithRelease(r)
309+
func toAnnotationKey(origin string) string {
310+
return fmt.Sprintf(fmtAnnoationKey, origin)
312311
}
313312

314-
// TransparentWithRelease returns the transparent value bind to the release if not existed, returns ""
315-
func (c *HelmClient) TransparentWithRelease(rel *release.Release) string {
316-
if v, ok := rel.Config[transparentKey]; !ok {
313+
// AnnotationWithRelease get the annoation from release by key
314+
func (c *HelmClient) AnnotationWithRelease(rel *release.Release, key string) string {
315+
if v, ok := rel.Config[toAnnotationKey(key)]; !ok {
317316
return ""
318317
} else {
319318
return v.(string)
320319
}
321320
}
322321

322+
// Annotation get the Annotation with the key from the release
323+
func (c *HelmClient) Annotation(releaseName, key string) string {
324+
r, err := c.getRelease(releaseName)
325+
if err != nil {
326+
return ""
327+
}
328+
return c.AnnotationWithRelease(r, key)
329+
}
330+
323331
// RollbackRelease implicitly rolls back a release to the last revision.
324332
func (c *HelmClient) RollbackRelease(spec *ChartSpec) error {
325333
return c.rollbackRelease(spec)

interface.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ type Client interface {
3434
ListReleaseHistory(name string, max int) ([]*release.Release, error)
3535
GetChart(chartName string, chartPathOptions *action.ChartPathOptions) (*chart.Chart, string, error)
3636

37-
// Transparent returns the transparent in the release, if not existed, returns ""
38-
Transparent(name string) string
39-
40-
// TransparentWithRelease returns the transparent value bind to the release if not existed, returns ""
41-
TransparentWithRelease(rel *release.Release) string
37+
// AnnotationWithRelease returns the annotation value bind to the key in the release if not existed, returns ""
38+
AnnotationWithRelease(rel *release.Release, key string) string
4239
}
4340

4441
type RollBack interface {

mock/interface.go

Lines changed: 14 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ type ListOptions struct {
99
Namespace string
1010
States action.ListStates
1111

12-
// label.Selector
13-
Selector string
12+
//label.Selector
13+
Selectors map[string]string
1414

1515
// name filter case-insensitive
1616
Filter string

spec.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"sigs.k8s.io/yaml"
55
)
66

7-
// transparentKey represents the key for save transparent values
8-
const transparentKey = "transparent__go-helm-client"
7+
const fmtAnnoationKey = "g0-he1m-c1ient__%s"
98

10-
// walkAroundCustomTagKey walkaround & wait for https://github.com/helm/helm/issues/11049
11-
const walkAroundCustomTagKey = "walk-around-custom-tag__go-helm-client"
9+
// common annoation's key
10+
const (
11+
// walkaround & wait for https://github.com/helm/helm/issues/11049
12+
ManagedBy = "manged-by"
13+
14+
// the user defined transparent values, will retrieve in future
15+
Transparent = "transparent"
16+
)
1217

1318
// GetValuesMap returns the mapped out values of a chart
1419
func (spec *ChartSpec) GetValuesMap() (map[string]interface{}, error) {
@@ -19,11 +24,8 @@ func (spec *ChartSpec) GetValuesMap() (map[string]interface{}, error) {
1924
return nil, err
2025
}
2126

22-
if spec.Transparent != "" {
23-
values[transparentKey] = spec.Transparent
24-
}
25-
if spec.Tag != "" {
26-
values[walkAroundCustomTagKey] = spec.Tag
27+
for k, v := range spec.Annotations {
28+
values[toAnnotationKey(k)] = v
2729
}
2830

2931
return values, nil

types.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,9 @@ type ChartSpec struct {
8282
// +optional
8383
ValuesYaml string `json:"valuesYaml,omitempty"`
8484

85-
// Transparent the user defined transparent values, will retrieve in future
85+
// Annotations save the all user defined data
8686
// +optional
87-
Transparent string `json:"transparent,omitempty"`
88-
89-
// Tag the tempoary walkaround & wait for https://github.com/helm/helm/pull/10533
90-
// +optional
91-
Tag string `json:"tag,omitempty"`
87+
Annotations map[string]string
9288

9389
// Version of the chart release.
9490
// +optional

0 commit comments

Comments
 (0)