9
9
"github.com/alauda/captain/pkg/util"
10
10
"github.com/alauda/helm-crds/pkg/apis/app/v1alpha1"
11
11
"helm.sh/helm/pkg/repo"
12
- apierrors "k8s.io/apimachinery/pkg/api/errors"
13
12
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
13
"k8s.io/apimachinery/pkg/runtime/schema"
15
14
"k8s.io/apimachinery/pkg/types"
@@ -103,32 +102,42 @@ func (c *Controller) syncChartRepo(obj interface{}) {
103
102
104
103
// createCharts create charts resource for a repo
105
104
func (c * Controller ) createCharts (cr * v1alpha1.ChartRepo ) error {
105
+ checked := map [string ]bool {}
106
106
index , err := helm .GetChartsForRepo (cr .GetName ())
107
107
if err != nil {
108
108
return err
109
109
}
110
+ for name , _ := range index .Entries {
111
+ checked [name ] = true
112
+ }
110
113
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
+ }
112
126
113
- options := v1.GetOptions {}
114
127
for name , versions := range index .Entries {
115
- checked [name ] = true
116
128
chart := generateChartResource (versions , name , cr )
117
129
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 {
128
134
return err
129
135
}
136
+ continue
130
137
}
131
138
139
+ old := existCharts [name ]
140
+
132
141
if compareChart (old , chart ) {
133
142
chart .SetResourceVersion (old .GetResourceVersion ())
134
143
_ , err = c .appClientSet .AppV1alpha1 ().Charts (cr .GetNamespace ()).Update (chart )
@@ -139,15 +148,7 @@ func (c *Controller) createCharts(cr *v1alpha1.ChartRepo) error {
139
148
140
149
}
141
150
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 {
151
152
if ! checked [name ] {
152
153
err := c .appClientSet .AppV1alpha1 ().Charts (cr .GetNamespace ()).Delete (item .GetName (), & v1.DeleteOptions {})
153
154
if err != nil {
@@ -163,7 +164,7 @@ func (c *Controller) createCharts(cr *v1alpha1.ChartRepo) error {
163
164
}
164
165
165
166
// 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 {
167
168
if len (old .Spec .Versions ) != len (new .Spec .Versions ) {
168
169
return true
169
170
}
0 commit comments