Skip to content

Commit 367156d

Browse files
committed
List before check charts
1 parent f367f58 commit 367156d

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

pkg/controller/chartrepo.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/alauda/captain/pkg/util"
1010
"github.com/alauda/helm-crds/pkg/apis/app/v1alpha1"
1111
"helm.sh/helm/pkg/repo"
12-
apierrors "k8s.io/apimachinery/pkg/api/errors"
1312
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1413
"k8s.io/apimachinery/pkg/runtime/schema"
1514
"k8s.io/apimachinery/pkg/types"
@@ -103,32 +102,42 @@ func (c *Controller) syncChartRepo(obj interface{}) {
103102

104103
// createCharts create charts resource for a repo
105104
func (c *Controller) createCharts(cr *v1alpha1.ChartRepo) error {
105+
checked := map[string]bool{}
106106
index, err := helm.GetChartsForRepo(cr.GetName())
107107
if err != nil {
108108
return err
109109
}
110+
for name, _ := range index.Entries {
111+
checked[name] = true
112+
}
110113

111-
checked := map[string]bool{}
114+
existCharts := map[string]v1alpha1.Chart{}
115+
listOptions := v1.ListOptions{
116+
LabelSelector: fmt.Sprintf("repo=%s", cr.GetName()),
117+
}
118+
charts, err := c.appClientSet.AppV1alpha1().Charts(cr.GetNamespace()).List(listOptions)
119+
if err != nil {
120+
return err
121+
}
122+
for _, item := range charts.Items {
123+
name := strings.Split(item.GetName(), ".")[0]
124+
existCharts[name] = item
125+
}
112126

113-
options := v1.GetOptions{}
114127
for name, versions := range index.Entries {
115-
checked[name] = true
116128
chart := generateChartResource(versions, name, cr)
117129

118-
old, err := c.appClientSet.AppV1alpha1().Charts(cr.GetNamespace()).Get(getChartName(cr.GetName(), name), options)
119-
if err != nil {
120-
if apierrors.IsNotFound(err) {
121-
klog.Infof("chart %s/%s not found, create", cr.GetName(), name)
122-
_, err = c.appClientSet.AppV1alpha1().Charts(cr.GetNamespace()).Create(chart)
123-
if err != nil {
124-
return err
125-
}
126-
continue
127-
} else {
130+
if _, ok := existCharts[name]; !ok {
131+
klog.Infof("chart %s/%s not found, create", cr.GetName(), name)
132+
_, err = c.appClientSet.AppV1alpha1().Charts(cr.GetNamespace()).Create(chart)
133+
if err != nil {
128134
return err
129135
}
136+
continue
130137
}
131138

139+
old := existCharts[name]
140+
132141
if compareChart(old, chart) {
133142
chart.SetResourceVersion(old.GetResourceVersion())
134143
_, err = c.appClientSet.AppV1alpha1().Charts(cr.GetNamespace()).Update(chart)
@@ -139,15 +148,7 @@ func (c *Controller) createCharts(cr *v1alpha1.ChartRepo) error {
139148

140149
}
141150

142-
listOptions := v1.ListOptions{
143-
LabelSelector: fmt.Sprintf("repo=%s", cr.GetName()),
144-
}
145-
charts, err := c.appClientSet.AppV1alpha1().Charts(cr.GetNamespace()).List(listOptions)
146-
if err != nil {
147-
return err
148-
}
149-
for _, item := range charts.Items {
150-
name := strings.Split(item.GetName(), ".")[0]
151+
for name, item := range existCharts {
151152
if !checked[name] {
152153
err := c.appClientSet.AppV1alpha1().Charts(cr.GetNamespace()).Delete(item.GetName(), &v1.DeleteOptions{})
153154
if err != nil {
@@ -163,7 +164,7 @@ func (c *Controller) createCharts(cr *v1alpha1.ChartRepo) error {
163164
}
164165

165166
// compareChart simply compare versions list length
166-
func compareChart(old *v1alpha1.Chart, new *v1alpha1.Chart) bool {
167+
func compareChart(old v1alpha1.Chart, new *v1alpha1.Chart) bool {
167168
if len(old.Spec.Versions) != len(new.Spec.Versions) {
168169
return true
169170
}

0 commit comments

Comments
 (0)