@@ -808,6 +808,90 @@ func (g Struct) GetCertManager(w http.ResponseWriter, r *http.Request) {
808808 httputil .ReturnSuccess (r , w , certInfoList )
809809}
810810
811+ func (g Struct ) DeleteCertManager (w http.ResponseWriter , r * http.Request ) {
812+ // 从上下文中获取租户信息
813+ tenant := r .Context ().Value (ctxutil .ContextKey ("tenant" )).(* dbmodel.Tenants )
814+
815+ // 解析请求参数
816+ var req struct {
817+ RouteName string `json:"route_name"`
818+ }
819+ if err := httputil .ReadEntity (r , & req ); err != nil {
820+ httputil .ReturnError (r , w , 400 , err .Error ())
821+ return
822+ }
823+
824+ // 验证路由名称
825+ if req .RouteName == "" {
826+ httputil .ReturnError (r , w , 400 , "route_name is required" )
827+ return
828+ }
829+
830+ req .RouteName = removeLeadingDigits (req .RouteName )
831+
832+ // 删除 Certificate 资源
833+ scheme := runtime .NewScheme ()
834+ _ = cmapi .AddToScheme (scheme )
835+ kubeConfig := config .GetConfigOrDie ()
836+ k8sClient , err := client .New (kubeConfig , client.Options {Scheme : scheme })
837+ if err != nil {
838+ logrus .Errorf ("failed to create k8s client: %v" , err )
839+ httputil .ReturnError (r , w , 500 , fmt .Sprintf ("failed to create k8s client: %v" , err ))
840+ return
841+ }
842+
843+ // 删除 Certificate
844+ cert := & cmapi.Certificate {
845+ ObjectMeta : v1.ObjectMeta {
846+ Name : req .RouteName ,
847+ Namespace : tenant .Namespace ,
848+ },
849+ }
850+ err = k8sClient .Delete (r .Context (), cert )
851+ if err != nil && ! errors .IsNotFound (err ) {
852+ logrus .Errorf ("delete certificate error: %v" , err )
853+ httputil .ReturnError (r , w , 500 , fmt .Sprintf ("delete certificate error: %v" , err ))
854+ return
855+ }
856+
857+ // 删除 ApisixTls 资源
858+ c := k8s .Default ().ApiSixClient .ApisixV2 ()
859+ err = c .ApisixTlses (tenant .Namespace ).Delete (r .Context (), req .RouteName , v1.DeleteOptions {})
860+ if err != nil && ! errors .IsNotFound (err ) {
861+ logrus .Errorf ("delete apisix tls error: %v" , err )
862+ httputil .ReturnError (r , w , 500 , fmt .Sprintf ("delete apisix tls error: %v" , err ))
863+ return
864+ }
865+
866+ // 更新 ApisixRoute,移除 cert-manager 标签
867+ route , err := c .ApisixRoutes (tenant .Namespace ).Get (r .Context (), req .RouteName , v1.GetOptions {})
868+ if err != nil {
869+ if errors .IsNotFound (err ) {
870+ // 如果路由不存在,返回成功
871+ httputil .ReturnSuccess (r , w , nil )
872+ return
873+ }
874+ logrus .Errorf ("get apisix route error: %v" , err )
875+ httputil .ReturnError (r , w , 500 , fmt .Sprintf ("get apisix route error: %v" , err ))
876+ return
877+ }
878+
879+ // 移除 cert-manager 标签
880+ if route .Labels != nil {
881+ delete (route .Labels , "cert-manager-enabled" )
882+
883+ // 更新路由
884+ _ , err = c .ApisixRoutes (tenant .Namespace ).Update (r .Context (), route , v1.UpdateOptions {})
885+ if err != nil {
886+ logrus .Errorf ("update apisix route error: %v" , err )
887+ httputil .ReturnError (r , w , 500 , fmt .Sprintf ("update apisix route error: %v" , err ))
888+ return
889+ }
890+ }
891+
892+ httputil .ReturnSuccess (r , w , nil )
893+ }
894+
811895// extractBaseName 从 Challenge 名称中提取基础名称
812896func extractBaseName (challengeName string ) string {
813897 // 按照 "-" 分割
0 commit comments