@@ -7,10 +7,9 @@ You may obtain a copy of the License at
7
7
http://www.apache.org/licenses/LICENSE-2.0
8
8
9
9
Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
10
+ distributed under the 4r is distributed on an "AS IS" BASIS,
11
11
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
12
+ elimitations under the License.wr
14
13
*/
15
14
16
15
package controllers
@@ -34,7 +33,6 @@ import (
34
33
"github.com/go-logr/logr"
35
34
"github.com/pkg/errors"
36
35
"github.com/prometheus/common/log"
37
- "gopkg.in/src-d/go-git.v4"
38
36
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
39
37
"helm.sh/helm/pkg/repo"
40
38
corev1 "k8s.io/api/core/v1"
@@ -46,11 +44,18 @@ import (
46
44
"sigs.k8s.io/controller-runtime/pkg/client"
47
45
"sigs.k8s.io/controller-runtime/pkg/controller"
48
46
"sigs.k8s.io/yaml"
47
+
48
+ "gopkg.in/src-d/go-git.v4"
49
49
)
50
50
51
51
var (
52
52
// annotation to record a ctr's last sync at timestamp. This was intend to avoid sync chartrepo too frequency
53
53
LastSyncAt = "cpaas.io/last-sync-at"
54
+
55
+ transCfg = & http.Transport {
56
+ TLSClientConfig : & tls.Config {InsecureSkipVerify : true }, // ignore expired SSL certificates
57
+ }
58
+ httpClient = & http.Client {Timeout : 30 * time .Second , Transport : transCfg }
54
59
)
55
60
56
61
// ChartRepoReconciler reconciles a ChartRepo object
@@ -81,6 +86,13 @@ func (r *ChartRepoReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
81
86
var cr v1beta1.ChartRepo
82
87
if err := r .Get (ctx , req .NamespacedName , & cr ); err != nil {
83
88
log .Error (err , "unable to fetch chartrepo" )
89
+ if isNotFound (err ) {
90
+ log .Info ("chart repo not found when processing, conside it deleted, try to delete charts" )
91
+ if err := r .cleanupLocalRepo (req .NamespacedName .Name ); err != nil {
92
+ log .Error (err , "cleanup for chartrepo error" )
93
+ return ctrl.Result {}, err
94
+ }
95
+ }
84
96
return ctrl.Result {}, ignoreNotFound (err )
85
97
}
86
98
@@ -353,6 +365,58 @@ func (r *ChartRepoReconciler) GetSecretData(cr *v1beta1.ChartRepo, ctx context.C
353
365
return nil , nil
354
366
}
355
367
368
+ // cleanupLocalRepo will remove all the charts after the ctr was removed
369
+ func (r * ChartRepoReconciler ) cleanupLocalRepo (name string ) error {
370
+ log := r .Log .WithValues ("chartrepo" , name )
371
+ url := generateLocalRepoURL (name )
372
+ link := url + "/index.yaml"
373
+
374
+ req , err := http .NewRequest ("GET" , link , nil )
375
+ if err != nil {
376
+ return err
377
+ }
378
+
379
+ index , err := getRepoIndexFile (req )
380
+ if err != nil {
381
+ return err
382
+ }
383
+
384
+ for name , versions := range index .Entries {
385
+ n := strings .ToLower (name )
386
+ for _ , version := range versions {
387
+ path := fmt .Sprintf ("%s/api/charts/%s/%s" , url , n , version .Version )
388
+ req , _ := http .NewRequest ("DELETE" , path , nil )
389
+ resp , err := httpClient .Do (req )
390
+ if err != nil {
391
+ log .Error (err , "delete chart version error" , "chart" , n , "version" , version .Version )
392
+ } else {
393
+ log .Info ("delete chart version" , "chart" , n , "version" , version .Version , "resp_code" , resp .Status )
394
+ }
395
+ }
396
+
397
+ }
398
+ return nil
399
+ }
400
+
401
+ func getRepoIndexFile (req * http.Request ) (* repo.IndexFile , error ) {
402
+ // Get the data
403
+ resp , err := httpClient .Do (req )
404
+ if err != nil {
405
+ return nil , err
406
+ }
407
+ defer resp .Body .Close ()
408
+
409
+ if resp .StatusCode != 200 {
410
+ return nil , errors .Errorf ("failed to fetch %s : %s" , req .URL .String (), resp .Status )
411
+ }
412
+
413
+ buf := bytes .NewBuffer (nil )
414
+ _ , err = io .Copy (buf , resp .Body )
415
+ body := buf .Bytes ()
416
+
417
+ return loadIndex (body )
418
+ }
419
+
356
420
// DownloadIndexFile fetches the index from a repository.
357
421
func (r * ChartRepoReconciler ) GetIndex (cr * v1beta1.ChartRepo , ctx context.Context ) (* repo.IndexFile , error ) {
358
422
var username string
@@ -378,32 +442,15 @@ func (r *ChartRepoReconciler) GetIndex(cr *v1beta1.ChartRepo, ctx context.Contex
378
442
}
379
443
380
444
link := strings .TrimSuffix (cr .Spec .URL , "/" ) + "/index.yaml"
381
- transCfg := & http.Transport {
382
- TLSClientConfig : & tls.Config {InsecureSkipVerify : true }, // ignore expired SSL certificates
383
- }
384
- c := & http.Client {Timeout : 30 * time .Second , Transport : transCfg }
385
445
req , err := http .NewRequest ("GET" , link , nil )
386
-
387
- if username != "" && password != "" {
388
- req .SetBasicAuth (username , password )
389
- }
390
-
391
- // Get the data
392
- resp , err := c .Do (req )
393
446
if err != nil {
394
447
return nil , err
395
448
}
396
- defer resp .Body .Close ()
397
-
398
- if resp .StatusCode != 200 {
399
- return nil , errors .Errorf ("failed to fetch %s : %s" , link , resp .Status )
449
+ if username != "" && password != "" {
450
+ req .SetBasicAuth (username , password )
400
451
}
401
452
402
- buf := bytes .NewBuffer (nil )
403
- _ , err = io .Copy (buf , resp .Body )
404
- body := buf .Bytes ()
405
-
406
- return loadIndex (body )
453
+ return getRepoIndexFile (req )
407
454
408
455
}
409
456
@@ -436,7 +483,7 @@ func (r *ChartRepoReconciler) syncCharts(cr *v1beta1.ChartRepo, ctx context.Cont
436
483
return err
437
484
}
438
485
// this may causes bugs
439
- for name , _ := range index .Entries {
486
+ for name := range index .Entries {
440
487
checked [strings .ToLower (name )] = true
441
488
}
442
489
@@ -573,12 +620,17 @@ func generateChartResource(versions repo.ChartVersions, name string, cr *v1beta1
573
620
574
621
}
575
622
623
+ // generateLocalRepoURL generte a repo url for local repo
624
+ func generateLocalRepoURL (name string ) string {
625
+ return "http://captain-chartmuseum:8080/" + name
626
+ }
627
+
576
628
func (r * ChartRepoReconciler ) updateChartRepoURL (ctx context.Context , cr * v1beta1.ChartRepo ) error {
577
629
old := cr .DeepCopy ()
578
630
mp := client .MergeFrom (old .DeepCopy ())
579
631
580
632
if old .Spec .URL == "" {
581
- old .Spec .URL = "http://captain-chartmuseum:8080/" + cr .GetName ()
633
+ old .Spec .URL = generateLocalRepoURL ( cr .GetName () )
582
634
}
583
635
584
636
// save to origin object
0 commit comments