watcher.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // 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.
  14. package watcher
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. identity_api "yunion.io/x/onecloud/pkg/apis/identity"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/consts"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/syncman"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. "yunion.io/x/onecloud/pkg/mcclient/informer"
  26. "yunion.io/x/onecloud/pkg/util/ctx"
  27. )
  28. type SInformerSyncManager struct {
  29. syncman.SSyncManager
  30. resourceManager informer.IResourceManager
  31. done bool
  32. }
  33. func (manager *SInformerSyncManager) OnAdd(obj *jsonutils.JSONDict) {
  34. log.Infof("[CREATED]: \n%s", obj.String())
  35. if manager.NeedSync(obj) {
  36. manager.SyncOnce(false, false)
  37. }
  38. }
  39. func (manager *SInformerSyncManager) OnUpdate(oldObj, newObj *jsonutils.JSONDict) {
  40. log.Infof("[UPDATED]: \n[NEW]: %s\n[OLD]: %s", newObj.String(), oldObj.String())
  41. if manager.NeedSync(oldObj) || manager.NeedSync(newObj) {
  42. manager.SyncOnce(false, false)
  43. }
  44. }
  45. func (manager *SInformerSyncManager) OnDelete(obj *jsonutils.JSONDict) {
  46. log.Infof("[DELETED]: \n%s", obj.String())
  47. if manager.NeedSync(obj) {
  48. manager.SyncOnce(false, false)
  49. }
  50. }
  51. func (manager *SInformerSyncManager) OnServiceCatalogChange(catalog mcclient.IServiceCatalog) {
  52. if manager.done {
  53. return
  54. }
  55. url, _ := mcclient.CatalogGetServiceURL(catalog, apis.SERVICE_TYPE_ETCD, consts.GetRegion(), "", identity_api.EndpointInterfaceInternal)
  56. if len(url) == 0 {
  57. log.Debugf("[%s] OnServiceCatalogChange: no etcd internal url found, retry", manager.Name())
  58. return
  59. }
  60. err := manager.startWatcher()
  61. if err != nil {
  62. log.Errorf("[%s] watching resource errror %s", manager.Name(), err)
  63. return
  64. }
  65. manager.done = true
  66. }
  67. func (manager *SInformerSyncManager) StartWatching(resMan informer.IResourceManager) {
  68. manager.resourceManager = resMan
  69. auth.RegisterCatalogListener(manager)
  70. }
  71. func (manager *SInformerSyncManager) startWatcher() error {
  72. log.Infof("[%s] Start resource informer watcher for %s", manager.Name(), manager.resourceManager.GetKeyword())
  73. ctx := ctx.CtxWithTime()
  74. s := auth.GetAdminSession(ctx, consts.GetRegion())
  75. informer.NewWatchManagerBySessionBg(s, func(watchMan *informer.SWatchManager) error {
  76. if err := watchMan.For(manager.resourceManager).AddEventHandler(ctx, manager); err != nil {
  77. return errors.Wrapf(err, "watch resource %s", manager.resourceManager.GetKeyword())
  78. }
  79. return nil
  80. })
  81. return nil
  82. }