Skip to content

Commit 7c4c9fb

Browse files
authored
Merge pull request #81 from alauda/local-repo-delete
Also delete charts when delete local repo
2 parents 566b833 + 10c31c7 commit 7c4c9fb

File tree

2 files changed

+83
-26
lines changed

2 files changed

+83
-26
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ int-test:
2929
manager: generate fmt vet
3030
go build -o bin/manager main.go
3131

32+
build: manager
33+
3234
# Run against the configured Kubernetes cluster in ~/.kube/config
3335
run: generate fmt vet manifests
3436
go run ./main.go
@@ -84,6 +86,9 @@ docker-build: test
8486
docker build -t captain-cert-init -f Dockerfile.init .
8587
docker build . -t ${IMG}
8688

89+
90+
docker: docker-build
91+
8792
multi-build:
8893
bash arch.sh
8994

controllers/chartrepo_controller.go

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ You may obtain a copy of the License at
77
http://www.apache.org/licenses/LICENSE-2.0
88
99
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,
1111
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
1413
*/
1514

1615
package controllers
@@ -34,7 +33,6 @@ import (
3433
"github.com/go-logr/logr"
3534
"github.com/pkg/errors"
3635
"github.com/prometheus/common/log"
37-
"gopkg.in/src-d/go-git.v4"
3836
githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
3937
"helm.sh/helm/pkg/repo"
4038
corev1 "k8s.io/api/core/v1"
@@ -46,11 +44,18 @@ import (
4644
"sigs.k8s.io/controller-runtime/pkg/client"
4745
"sigs.k8s.io/controller-runtime/pkg/controller"
4846
"sigs.k8s.io/yaml"
47+
48+
"gopkg.in/src-d/go-git.v4"
4949
)
5050

5151
var (
5252
// annotation to record a ctr's last sync at timestamp. This was intend to avoid sync chartrepo too frequency
5353
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}
5459
)
5560

5661
// ChartRepoReconciler reconciles a ChartRepo object
@@ -81,6 +86,13 @@ func (r *ChartRepoReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
8186
var cr v1beta1.ChartRepo
8287
if err := r.Get(ctx, req.NamespacedName, &cr); err != nil {
8388
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+
}
8496
return ctrl.Result{}, ignoreNotFound(err)
8597
}
8698

@@ -353,6 +365,58 @@ func (r *ChartRepoReconciler) GetSecretData(cr *v1beta1.ChartRepo, ctx context.C
353365
return nil, nil
354366
}
355367

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+
356420
// DownloadIndexFile fetches the index from a repository.
357421
func (r *ChartRepoReconciler) GetIndex(cr *v1beta1.ChartRepo, ctx context.Context) (*repo.IndexFile, error) {
358422
var username string
@@ -378,32 +442,15 @@ func (r *ChartRepoReconciler) GetIndex(cr *v1beta1.ChartRepo, ctx context.Contex
378442
}
379443

380444
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}
385445
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)
393446
if err != nil {
394447
return nil, err
395448
}
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)
400451
}
401452

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)
407454

408455
}
409456

@@ -436,7 +483,7 @@ func (r *ChartRepoReconciler) syncCharts(cr *v1beta1.ChartRepo, ctx context.Cont
436483
return err
437484
}
438485
// this may causes bugs
439-
for name, _ := range index.Entries {
486+
for name := range index.Entries {
440487
checked[strings.ToLower(name)] = true
441488
}
442489

@@ -573,12 +620,17 @@ func generateChartResource(versions repo.ChartVersions, name string, cr *v1beta1
573620

574621
}
575622

623+
// generateLocalRepoURL generte a repo url for local repo
624+
func generateLocalRepoURL(name string) string {
625+
return "http://captain-chartmuseum:8080/" + name
626+
}
627+
576628
func (r *ChartRepoReconciler) updateChartRepoURL(ctx context.Context, cr *v1beta1.ChartRepo) error {
577629
old := cr.DeepCopy()
578630
mp := client.MergeFrom(old.DeepCopy())
579631

580632
if old.Spec.URL == "" {
581-
old.Spec.URL = "http://captain-chartmuseum:8080/" + cr.GetName()
633+
old.Spec.URL = generateLocalRepoURL(cr.GetName())
582634
}
583635

584636
// save to origin object

0 commit comments

Comments
 (0)