Skip to content

Commit

Permalink
[backport] feature: delete codebase when gateway is deleted (#394)
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Yang <[email protected]>
  • Loading branch information
reaver-flomesh committed Oct 18, 2024
1 parent 333297a commit 98b4bff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pkg/controllers/gateway/v1/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}.String())
r.mutex.Unlock()

if _, err := r.fctx.RepoClient.DeleteCodebase(utils.GatewayCodebasePath(req.Namespace, req.Name)); err != nil {
log.Warn().Msgf("Failed to delete codebase of gateway %s/%s: %s", req.Namespace, req.Name, err)
}

return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
Expand All @@ -165,6 +169,10 @@ func (r *gatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
delete(r.activeGateways, client.ObjectKeyFromObject(gateway).String())
r.mutex.Unlock()

if _, err := r.fctx.RepoClient.DeleteCodebase(utils.GatewayCodebasePath(gateway.Namespace, gateway.Name)); err != nil {
log.Warn().Msgf("Failed to delete codebase of gateway %s/%s: %s", gateway.Namespace, gateway.Name, err)
}

return ctrl.Result{}, nil
}

Expand Down
24 changes: 22 additions & 2 deletions pkg/repo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,26 @@ func (p *PipyRepoClient) upsertFile(path string, content interface{}) error {
return fmt.Errorf(errstr, resp.StatusCode(), resp.Status())
}

// DeleteCodebase delete codebase
func (p *PipyRepoClient) DeleteCodebase(path string) (success bool, err error) {
var resp *resty.Response

resp, err = p.httpClient.R().
Delete(fullRepoApiPath(path))

if err == nil {
if resp.IsSuccess() {
success = true
return
}
err = fmt.Errorf("error happened while deleting codebase[%s], reason: %s", path, resp.Status())
return
}

log.Err(err).Msgf("error happened while deleting codebase[%s]", path)
return
}

// deleteFile delete codebase file
func (p *PipyRepoClient) deleteFile(path string) (success bool, err error) {
var resp *resty.Response
Expand All @@ -228,11 +248,11 @@ func (p *PipyRepoClient) deleteFile(path string) (success bool, err error) {
success = true
return
}
err = fmt.Errorf("error happened while deleting codebase[%s], reason: %s", path, resp.Status())
err = fmt.Errorf("error happened while deleting file %q, reason: %s", path, resp.Status())
return
}

log.Err(err).Msgf("error happened while deleting codebase[%s]", path)
log.Err(err).Msgf("error happened while deleting file %q", path)
return
}

Expand Down

0 comments on commit 98b4bff

Please sign in to comment.