Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete svc failure #2125

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions api/controller/apigateway/api_gateway_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,17 +502,45 @@ func (g Struct) DeleteTCPRoute(w http.ResponseWriter, r *http.Request) {
tenant := r.Context().Value(ctxutil.ContextKey("tenant")).(*dbmodel.Tenants)
name := chi.URLParam(r, "name")

// Split name by "-" to get alias and port
parts := strings.Split(name, "-")
if len(parts) != 2 {
httputil.ReturnBcodeError(r, w, bcode.ErrRouteDelete)
return
}
serviceAlias := parts[0]
port := parts[1]

// Create label selector
labelSelector := fmt.Sprintf("outer=true,port=%s,service_alias=%s", port, serviceAlias)

k := k8s.Default().Clientset.CoreV1()
err := k.Services(tenant.Namespace).Delete(r.Context(), name, v1.DeleteOptions{})
// List services with the specified labels
services, err := k.Services(tenant.Namespace).List(r.Context(), v1.ListOptions{
LabelSelector: labelSelector,
})
if err != nil {
if errors.IsNotFound(err) {
httputil.ReturnSuccess(r, w, name)
} else {
logrus.Errorf("delete route error %s", err.Error())
logrus.Errorf("list services error %s", err.Error())
httputil.ReturnBcodeError(r, w, bcode.ErrRouteDelete)
return
}

if len(services.Items) == 0 {
// No services found with these labels
httputil.ReturnSuccess(r, w, name)
return
}

// Delete all matching services
for _, svc := range services.Items {
err := k.Services(tenant.Namespace).Delete(r.Context(), svc.Name, v1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
logrus.Errorf("delete service %s error %s", svc.Name, err.Error())
httputil.ReturnBcodeError(r, w, bcode.ErrRouteDelete)
return
}
return
}

httputil.ReturnSuccess(r, w, name)
}

Expand Down
Loading