1919
2020import cn .polarismesh .polaris .sync .common .rest .HostAndPort ;
2121import cn .polarismesh .polaris .sync .common .rest .RestOperator ;
22+ import cn .polarismesh .polaris .sync .common .utils .CommonUtils ;
23+ import cn .polarismesh .polaris .sync .common .utils .DefaultValues ;
2224import cn .polarismesh .polaris .sync .extension .Health ;
2325import cn .polarismesh .polaris .sync .extension .ResourceEndpoint ;
2426import cn .polarismesh .polaris .sync .extension .ResourceType ;
2527import cn .polarismesh .polaris .sync .extension .registry .RegistryCenter ;
2628import cn .polarismesh .polaris .sync .extension .registry .RegistryInitRequest ;
2729import cn .polarismesh .polaris .sync .extension .registry .Service ;
2830import cn .polarismesh .polaris .sync .extension .registry .WatchEvent ;
29- import cn .polarismesh .polaris .sync .common .utils .CommonUtils ;
30- import cn .polarismesh .polaris .sync .common .utils .DefaultValues ;
3131import cn .polarismesh .polaris .sync .extension .utils .ResponseUtils ;
3232import cn .polarismesh .polaris .sync .extension .utils .StatusCodes ;
3333import cn .polarismesh .polaris .sync .model .pb .ModelProto ;
5151import com .tencent .polaris .client .pb .ServiceProto ;
5252import com .tencent .polaris .client .pb .ServiceProto .Instance .Builder ;
5353import com .tencent .polaris .client .pb .ServiceProto .Namespace ;
54-
5554import java .lang .reflect .Field ;
5655import java .util .ArrayList ;
5756import java .util .Collection ;
@@ -276,7 +275,8 @@ public DiscoverResponse listInstances(Service service, ModelProto.Group group) {
276275 return builder .build ();
277276 }
278277
279- private List <ServiceProto .Instance > convertNacosInstances (Service service , List <Instance > instances , ModelProto .Group group ) {
278+ private List <ServiceProto .Instance > convertNacosInstances (Service service , List <Instance > instances ,
279+ ModelProto .Group group ) {
280280 Map <String , String > filters = (null == group ? null : group .getMetadataMap ());
281281 List <ServiceProto .Instance > polarisInstances = new ArrayList <>();
282282 for (Instance instance : instances ) {
@@ -394,6 +394,11 @@ public void updateServices(Collection<Service> services) {
394394 return ;
395395 }
396396 }
397+ if (!authResponse .isGlobalAdmin ()) {
398+ LOG .warn ("[Nacos][Registry] current user is not nacos global admin, ignore create nacos namespace, {}" ,
399+ registryEndpoint .getAuthorization ());
400+ return ;
401+ }
397402 //2. 查询命名空间是否已经创建
398403 List <NacosNamespace > nacosNamespaces = new ArrayList <>();
399404 DiscoverResponse discoverResponse = NacosRestUtils
@@ -420,7 +425,8 @@ public void updateGroups(Service service, Collection<ModelProto.Group> groups) {
420425 }
421426
422427 @ Override
423- public void updateInstances (Service service , ModelProto .Group group , Collection <ServiceProto .Instance > srcInstances ) {
428+ public void updateInstances (Service service , ModelProto .Group group ,
429+ Collection <ServiceProto .Instance > srcInstances ) {
424430 ResourceEndpoint registryEndpoint = registryInitRequest .getResourceEndpoint ();
425431 List <Instance > allInstances = queryNacosInstances (service , registryEndpoint .getName ());
426432 if (null == allInstances ) {
@@ -458,7 +464,8 @@ public void updateInstances(Service service, ModelProto.Group group, Collection<
458464 hasExistsInstances = true ;
459465 //比较是否存在不一致
460466 if (!instanceEquals (srcInstance , destInstance )) {
461- targetsToUpdate .put (srcAddress , toNacosInstancePendingSync (srcInstance , destInstance .getInstanceId ()));
467+ targetsToUpdate .put (srcAddress ,
468+ toNacosInstancePendingSync (srcInstance , destInstance .getInstanceId ()));
462469 }
463470 targetsExists .put (srcAddress , toNacosInstancePendingSync (srcInstance , destInstance .getInstanceId ()));
464471 }
@@ -485,7 +492,8 @@ public void updateInstances(Service service, ModelProto.Group group, Collection<
485492 //1. 有1-N个实例需要删除
486493 //2. 存在新增+存量
487494 if (!targetsToDelete .isEmpty ()) {
488- LOG .info ("[Nacos] targets pending to delete are {}, group {}" , targetsToDelete .keySet (), group .getName ());
495+ LOG .info ("[Nacos] targets pending to delete are {}, group {}" , targetsToDelete .keySet (),
496+ group .getName ());
489497 deregisterInstance ("delete" , namingService , service .getService (), targetsToDelete .values ().iterator ()
490498 .next ());
491499 targetDeleteCount += targetsToDelete .size ();
@@ -505,7 +513,7 @@ public void updateInstances(Service service, ModelProto.Group group, Collection<
505513 }
506514 registerInstances ("create" , namingService , service .getService (), instances );
507515 targetAddCount += targetsToCreate .size ();
508- } else {
516+ } else {
509517 if (deleted ) {
510518 //前面已经删除了,则把存量重新注册一遍
511519 List <Instance > instances = new ArrayList <>(targetsExists .values ());
@@ -515,7 +523,8 @@ public void updateInstances(Service service, ModelProto.Group group, Collection<
515523 } else if (!targetsToUpdate .isEmpty ()) {
516524 //前面已经删除了,则把存量重新注册一遍
517525 List <Instance > instances = new ArrayList <>(targetsToUpdate .values ());
518- LOG .info ("[Nacos] targets pending to update are {}, group {}" , targetsToUpdate .keySet (), group .getName ());
526+ LOG .info ("[Nacos] targets pending to update are {}, group {}" , targetsToUpdate .keySet (),
527+ group .getName ());
519528 registerInstances ("update" , namingService , service .getService (), instances );
520529 targetPatchCount += targetsToUpdate .size ();
521530 }
@@ -533,8 +542,10 @@ private void registerInstances(String operation, NamingService namingService,
533542 } catch (NacosException e ) {
534543 if (StringUtils .contains (e .getMessage (), "RequestHandler Not Found" )) {
535544 Field field = ReflectionUtils .findField (NacosNamingService .class , "clientProxy" );
536- NamingClientProxyDelegate proxy = (NamingClientProxyDelegate ) ReflectionUtils .getField (field , namingService );
537- NamingClientProxy httpProxy = (NamingClientProxy ) ReflectionUtils .getField (ReflectionUtils .findField (NamingClientProxyDelegate .class , "httpClientProxy" ), proxy );
545+ NamingClientProxyDelegate proxy = (NamingClientProxyDelegate ) ReflectionUtils .getField (field ,
546+ namingService );
547+ NamingClientProxy httpProxy = (NamingClientProxy ) ReflectionUtils .getField (
548+ ReflectionUtils .findField (NamingClientProxyDelegate .class , "httpClientProxy" ), proxy );
538549 for (Instance instance : instances ) {
539550 try {
540551 httpProxy .registerService (values [1 ], values [0 ], instance );
0 commit comments