提交 40ebeccf 编写于 作者: J Jeff 提交者: zryfish

delete orphan resources where service deleted

上级 273aa010
...@@ -88,7 +88,8 @@ func AddControllers(mgr manager.Manager, cfg *rest.Config, stopCh <-chan struct{ ...@@ -88,7 +88,8 @@ func AddControllers(mgr manager.Manager, cfg *rest.Config, stopCh <-chan struct{
informerFactory.Core().V1().Services(), informerFactory.Core().V1().Services(),
servicemeshInformer.Servicemesh().V1alpha2().ServicePolicies(), servicemeshInformer.Servicemesh().V1alpha2().ServicePolicies(),
kubeClient, kubeClient,
istioclient) istioclient,
servicemeshclient)
apController := application.NewApplicationController(informerFactory.Core().V1().Services(), apController := application.NewApplicationController(informerFactory.Core().V1().Services(),
informerFactory.Apps().V1().Deployments(), informerFactory.Apps().V1().Deployments(),
......
...@@ -149,7 +149,7 @@ func NewApplicationController(serviceInformer coreinformers.ServiceInformer, ...@@ -149,7 +149,7 @@ func NewApplicationController(serviceInformer coreinformers.ServiceInformer,
} }
func (v *ApplicationController) Start(stopCh <-chan struct{}) error { func (v *ApplicationController) Start(stopCh <-chan struct{}) error {
v.Run(5, stopCh) v.Run(2, stopCh)
return nil return nil
} }
......
...@@ -34,6 +34,7 @@ import ( ...@@ -34,6 +34,7 @@ import (
"k8s.io/client-go/util/workqueue" "k8s.io/client-go/util/workqueue"
"time" "time"
servicemeshclient "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
servicemeshinformers "kubesphere.io/kubesphere/pkg/client/informers/externalversions/servicemesh/v1alpha2" servicemeshinformers "kubesphere.io/kubesphere/pkg/client/informers/externalversions/servicemesh/v1alpha2"
servicemeshlisters "kubesphere.io/kubesphere/pkg/client/listers/servicemesh/v1alpha2" servicemeshlisters "kubesphere.io/kubesphere/pkg/client/listers/servicemesh/v1alpha2"
) )
...@@ -53,6 +54,7 @@ type DestinationRuleController struct { ...@@ -53,6 +54,7 @@ type DestinationRuleController struct {
client clientset.Interface client clientset.Interface
destinationRuleClient istioclientset.Interface destinationRuleClient istioclientset.Interface
servicemeshClient servicemeshclient.Interface
eventBroadcaster record.EventBroadcaster eventBroadcaster record.EventBroadcaster
eventRecorder record.EventRecorder eventRecorder record.EventRecorder
...@@ -79,7 +81,8 @@ func NewDestinationRuleController(deploymentInformer informersv1.DeploymentInfor ...@@ -79,7 +81,8 @@ func NewDestinationRuleController(deploymentInformer informersv1.DeploymentInfor
serviceInformer coreinformers.ServiceInformer, serviceInformer coreinformers.ServiceInformer,
servicePolicyInformer servicemeshinformers.ServicePolicyInformer, servicePolicyInformer servicemeshinformers.ServicePolicyInformer,
client clientset.Interface, client clientset.Interface,
destinationRuleClient istioclientset.Interface) *DestinationRuleController { destinationRuleClient istioclientset.Interface,
servicemeshClient servicemeshclient.Interface) *DestinationRuleController {
broadcaster := record.NewBroadcaster() broadcaster := record.NewBroadcaster()
broadcaster.StartLogging(func(format string, args ...interface{}) { broadcaster.StartLogging(func(format string, args ...interface{}) {
...@@ -95,6 +98,7 @@ func NewDestinationRuleController(deploymentInformer informersv1.DeploymentInfor ...@@ -95,6 +98,7 @@ func NewDestinationRuleController(deploymentInformer informersv1.DeploymentInfor
v := &DestinationRuleController{ v := &DestinationRuleController{
client: client, client: client,
destinationRuleClient: destinationRuleClient, destinationRuleClient: destinationRuleClient,
servicemeshClient: servicemeshClient,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "destinationrule"), queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "destinationrule"),
workerLoopPeriod: time.Second, workerLoopPeriod: time.Second,
} }
...@@ -211,12 +215,20 @@ func (v *DestinationRuleController) syncService(key string) error { ...@@ -211,12 +215,20 @@ func (v *DestinationRuleController) syncService(key string) error {
service, err := v.serviceLister.Services(namespace).Get(name) service, err := v.serviceLister.Services(namespace).Get(name)
if err != nil { if err != nil {
// Delete the corresponding destinationrule if there is any, as the service has been deleted. // delete the corresponding destinationrule if there is any, as the service has been deleted.
err = v.destinationRuleClient.NetworkingV1alpha3().DestinationRules(namespace).Delete(name, nil) err = v.destinationRuleClient.NetworkingV1alpha3().DestinationRules(namespace).Delete(name, nil)
if err != nil && !errors.IsNotFound(err) { if err != nil && !errors.IsNotFound(err) {
log.Error(err, "delete destination rule failed", "namespace", namespace, "name", name) log.Error(err, "delete destination rule failed", "namespace", namespace, "name", name)
return err return err
} }
// delete orphan service policy if there is any
err = v.servicemeshClient.ServicemeshV1alpha2().ServicePolicies(namespace).Delete(name, nil)
if err != nil && !errors.IsNotFound(err) {
log.Error(err, "delete orphan service policy failed", "namespace", namespace, "name", name)
return err
}
return nil return nil
} }
...@@ -332,9 +344,9 @@ func (v *DestinationRuleController) syncService(key string) error { ...@@ -332,9 +344,9 @@ func (v *DestinationRuleController) syncService(key string) error {
} }
if createDestinationRule { if createDestinationRule {
newDestinationRule, err = v.destinationRuleClient.NetworkingV1alpha3().DestinationRules(namespace).Create(newDestinationRule) _, err = v.destinationRuleClient.NetworkingV1alpha3().DestinationRules(namespace).Create(newDestinationRule)
} else { } else {
newDestinationRule, err = v.destinationRuleClient.NetworkingV1alpha3().DestinationRules(namespace).Update(newDestinationRule) _, err = v.destinationRuleClient.NetworkingV1alpha3().DestinationRules(namespace).Update(newDestinationRule)
} }
if err != nil { if err != nil {
......
...@@ -231,6 +231,14 @@ func (v *VirtualServiceController) syncService(key string) error { ...@@ -231,6 +231,14 @@ func (v *VirtualServiceController) syncService(key string) error {
log.Error(err, "delete orphan virtualservice failed", "namespace", namespace, "name", service.Name) log.Error(err, "delete orphan virtualservice failed", "namespace", namespace, "name", service.Name)
return err return err
} }
// delete the orphan strategy if there is any
err = v.servicemeshClient.ServicemeshV1alpha2().Strategies(namespace).Delete(name, nil)
if err != nil && !errors.IsNotFound(err) {
log.Error(err, "delete orphan strategy failed", "namespace", namespace, "name", service.Name)
return err
}
return nil return nil
} }
log.Error(err, "get service failed", "namespace", namespace, "name", name) log.Error(err, "get service failed", "namespace", namespace, "name", name)
...@@ -385,9 +393,9 @@ func (v *VirtualServiceController) syncService(key string) error { ...@@ -385,9 +393,9 @@ func (v *VirtualServiceController) syncService(key string) error {
} }
if createVirtualService { if createVirtualService {
newVirtualService, err = v.virtualServiceClient.NetworkingV1alpha3().VirtualServices(namespace).Create(newVirtualService) _, err = v.virtualServiceClient.NetworkingV1alpha3().VirtualServices(namespace).Create(newVirtualService)
} else { } else {
newVirtualService, err = v.virtualServiceClient.NetworkingV1alpha3().VirtualServices(namespace).Update(newVirtualService) _, err = v.virtualServiceClient.NetworkingV1alpha3().VirtualServices(namespace).Update(newVirtualService)
} }
if err != nil { if err != nil {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册