@@ -403,32 +403,50 @@ func (a *appRuntimeStore) OnAdd(obj interface{}) {
403403 createrID := deployment .Labels ["creater_id" ]
404404 migrator := deployment .Labels ["migrator" ]
405405 appID := deployment .Labels ["app_id" ]
406+ logrus .Infof ("[OnAdd-Deployment] Detected deployment: name=%s, namespace=%s, serviceID=%s, version=%s, createrID=%s" ,
407+ deployment .Name , deployment .Namespace , serviceID , version , createrID )
406408 if serviceID != "" && version != "" && createrID != "" {
409+ logrus .Infof ("[OnAdd-Deployment] Processing deployment for service: %s" , serviceID )
407410 appservice , err := a .getAppService (serviceID , version , createrID , true )
408- if err == conversion .ErrServiceNotFound {
409- a .k8sClient .Clientset .AppsV1 ().Deployments (deployment .Namespace ).Delete (context .Background (), deployment .Name , metav1.DeleteOptions {})
411+ if err != nil {
412+ logrus .Errorf ("[OnAdd-Deployment] Error getting AppService for %s: %v" , serviceID , err )
413+ if err == conversion .ErrServiceNotFound {
414+ logrus .Warnf ("[OnAdd-Deployment] Service %s not found in DB, deleting deployment: %s" , serviceID , deployment .Name )
415+ a .k8sClient .Clientset .AppsV1 ().Deployments (deployment .Namespace ).Delete (context .Background (), deployment .Name , metav1.DeleteOptions {})
416+ } else {
417+ logrus .Errorf ("[OnAdd-Deployment] Non-ErrServiceNotFound error for service %s, deployment will remain unmanaged" , serviceID )
418+ }
410419 }
411420 if appservice != nil {
421+ logrus .Infof ("[OnAdd-Deployment] Setting deployment for AppService: %s" , serviceID )
412422 appservice .SetDeployment (deployment )
413423 if migrator == "rainbond" {
414424 label := "service_id=" + serviceID
415425 pods , _ := a .k8sClient .Clientset .CoreV1 ().Pods (deployment .Namespace ).List (context .Background (), metav1.ListOptions {LabelSelector : label })
416426 if pods != nil {
427+ logrus .Infof ("[OnAdd-Deployment] Found %d pods for service %s, adding to AppService" , len (pods .Items ), serviceID )
417428 for _ , pod := range pods .Items {
418429 pod := pod
419430 appservice .SetPods (& pod )
420431 }
421432 }
422433 }
434+ logrus .Infof ("[OnAdd-Deployment] Successfully processed deployment for service: %s" , serviceID )
423435 return
436+ } else {
437+ logrus .Warnf ("[OnAdd-Deployment] AppService is nil for service %s after getAppService, deployment unmanaged" , serviceID )
424438 }
425439
426440 } else if deployment .OwnerReferences != nil && appID != "" {
441+ logrus .Debugf ("[OnAdd-Deployment] Deployment has OwnerReferences, processing as operator managed: appID=%s" , appID )
427442 operatorManaged := a .getOperatorManaged (appID )
428443 if operatorManaged != nil {
429444 operatorManaged .SetDeployment (deployment )
430445 return
431446 }
447+ } else {
448+ logrus .Warnf ("[OnAdd-Deployment] Deployment %s missing required labels: serviceID=%s, version=%s, createrID=%s" ,
449+ deployment .Name , serviceID , version , createrID )
432450 }
433451 }
434452 if job , ok := obj .(* batchv1.Job ); ok {
@@ -525,26 +543,41 @@ func (a *appRuntimeStore) OnAdd(obj interface{}) {
525543 createrID := statefulset .Labels ["creater_id" ]
526544 migrator := statefulset .Labels ["migrator" ]
527545 appID := statefulset .Labels ["app_id" ]
546+ logrus .Infof ("[OnAdd-StatefulSet] Detected statefulset: name=%s, namespace=%s, serviceID=%s, version=%s, createrID=%s" ,
547+ statefulset .Name , statefulset .Namespace , serviceID , version , createrID )
528548 if serviceID != "" && version != "" && createrID != "" {
549+ logrus .Infof ("[OnAdd-StatefulSet] Processing statefulset for service: %s" , serviceID )
529550 appservice , err := a .getAppService (serviceID , version , createrID , true )
530- if err == conversion .ErrServiceNotFound {
531- a .k8sClient .Clientset .AppsV1 ().StatefulSets (statefulset .Namespace ).Delete (context .Background (), statefulset .Name , metav1.DeleteOptions {})
551+ if err != nil {
552+ logrus .Errorf ("[OnAdd-StatefulSet] Error getting AppService for %s: %v" , serviceID , err )
553+ if err == conversion .ErrServiceNotFound {
554+ logrus .Warnf ("[OnAdd-StatefulSet] Service %s not found in DB, deleting statefulset: %s" , serviceID , statefulset .Name )
555+ a .k8sClient .Clientset .AppsV1 ().StatefulSets (statefulset .Namespace ).Delete (context .Background (), statefulset .Name , metav1.DeleteOptions {})
556+ } else {
557+ logrus .Errorf ("[OnAdd-StatefulSet] Non-ErrServiceNotFound error for service %s, statefulset will remain unmanaged" , serviceID )
558+ }
532559 }
533560 if appservice != nil {
561+ logrus .Infof ("[OnAdd-StatefulSet] Setting statefulset for AppService: %s" , serviceID )
534562 appservice .SetStatefulSet (statefulset )
535563 if migrator == "rainbond" {
536564 label := "service_id=" + serviceID
537565 pods , _ := a .k8sClient .Clientset .CoreV1 ().Pods (statefulset .Namespace ).List (context .Background (), metav1.ListOptions {LabelSelector : label })
538566 if pods != nil {
567+ logrus .Infof ("[OnAdd-StatefulSet] Found %d pods for service %s, adding to AppService" , len (pods .Items ), serviceID )
539568 for _ , pod := range pods .Items {
540569 pod := pod
541570 appservice .SetPods (& pod )
542571 }
543572 }
544573 }
574+ logrus .Infof ("[OnAdd-StatefulSet] Successfully processed statefulset for service: %s" , serviceID )
545575 return
576+ } else {
577+ logrus .Warnf ("[OnAdd-StatefulSet] AppService is nil for service %s after getAppService, statefulset unmanaged" , serviceID )
546578 }
547579 } else if statefulset .OwnerReferences != nil && appID != "" {
580+ logrus .Debugf ("[OnAdd-StatefulSet] StatefulSet has OwnerReferences, processing as operator managed: appID=%s" , appID )
548581 operatorManaged := a .getOperatorManaged (appID )
549582 if operatorManaged != nil {
550583 operatorManaged .SetStatefulSet (statefulset )
@@ -758,16 +791,28 @@ func (a *appRuntimeStore) OnAdd(obj interface{}) {
758791
759792// getAppService if creator is true, will create new app service where not found in store
760793func (a * appRuntimeStore ) getAppService (serviceID , version , createrID string , creator bool ) (* v1.AppService , error ) {
794+ logrus .Debugf ("[getAppService] Looking for service: %s (version=%s, createrID=%s, creator=%v)" ,
795+ serviceID , version , createrID , creator )
761796 var appservice * v1.AppService
762797 appservice = a .GetAppService (serviceID )
763- if appservice == nil && creator {
764- var err error
765- appservice , err = conversion .InitCacheAppService (a .dbmanager , serviceID , createrID )
766- if err != nil {
767- logrus .Infof ("init cache app service %s failure:%s " , serviceID , err .Error ())
768- return nil , err
798+ if appservice == nil {
799+ logrus .Infof ("[getAppService] AppService not found in cache for service: %s, creator=%v" , serviceID , creator )
800+ if creator {
801+ logrus .Infof ("[getAppService] Creating new AppService for service: %s" , serviceID )
802+ var err error
803+ appservice , err = conversion .InitCacheAppService (a .dbmanager , serviceID , createrID )
804+ if err != nil {
805+ logrus .Errorf ("[getAppService] Failed to init cache app service %s: %v" , serviceID , err )
806+ return nil , err
807+ }
808+ logrus .Infof ("[getAppService] Successfully created AppService for service: %s, registering to cache" , serviceID )
809+ a .RegistAppService (appservice )
810+ logrus .Infof ("[getAppService] AppService registered to cache for service: %s" , serviceID )
811+ } else {
812+ logrus .Debugf ("[getAppService] creator=false, not creating AppService for service: %s" , serviceID )
769813 }
770- a .RegistAppService (appservice )
814+ } else {
815+ logrus .Debugf ("[getAppService] AppService found in cache for service: %s" , serviceID )
771816 }
772817 return appservice , nil
773818}
@@ -1051,9 +1096,12 @@ func (a *appRuntimeStore) OnDeletes(objs ...interface{}) {
10511096
10521097// RegistAppService regist a app model to store.
10531098func (a * appRuntimeStore ) RegistAppService (app * v1.AppService ) {
1054- a .appServices .Store (v1 .GetCacheKeyOnlyServiceID (app .ServiceID ), app )
1099+ cacheKey := v1 .GetCacheKeyOnlyServiceID (app .ServiceID )
1100+ logrus .Infof ("[RegistAppService] Registering AppService to cache: serviceID=%s, alias=%s, cacheKey=%s" ,
1101+ app .ServiceID , app .ServiceAlias , cacheKey )
1102+ a .appServices .Store (cacheKey , app )
10551103 a .appCount ++
1056- logrus .Debugf ( "current have %d app after add \n " , a .appCount )
1104+ logrus .Infof ( "[RegistAppService] AppService registered successfully. Total apps in cache: %d " , a .appCount )
10571105}
10581106
10591107func (a * appRuntimeStore ) RegistOperatorManaged (app * v1.OperatorManaged ) {
@@ -1080,9 +1128,11 @@ func (a *appRuntimeStore) DeleteAppServiceByKey(key v1.CacheKey) {
10801128
10811129func (a * appRuntimeStore ) GetAppService (serviceID string ) * v1.AppService {
10821130 key := v1 .GetCacheKeyOnlyServiceID (serviceID )
1131+ logrus .Debugf ("[GetAppService] Looking up cache for serviceID=%s, cacheKey=%s" , serviceID , key )
10831132 app , ok := a .appServices .Load (key )
10841133 if ok {
10851134 appService := app .(* v1.AppService )
1135+ logrus .Debugf ("[GetAppService] Cache hit for serviceID=%s, alias=%s" , serviceID , appService .ServiceAlias )
10861136 return appService
10871137 }
10881138 return nil
@@ -1228,25 +1278,41 @@ func (a *appRuntimeStore) GetAllAppServices() (apps []*v1.AppService) {
12281278}
12291279
12301280func (a * appRuntimeStore ) GetAppServiceStatus (serviceID string ) string {
1281+ logrus .Debugf ("[GetAppServiceStatus] Query status for service: %s" , serviceID )
12311282 app := a .GetAppService (serviceID )
12321283 if app == nil {
1233- component , _ := a .dbmanager .TenantServiceDao ().GetServiceByID (serviceID )
1284+ logrus .Warnf ("[GetAppServiceStatus] AppService not found in cache for service: %s, checking database" , serviceID )
1285+ component , err := a .dbmanager .TenantServiceDao ().GetServiceByID (serviceID )
1286+ if err != nil {
1287+ logrus .Errorf ("[GetAppServiceStatus] Failed to query database for service %s: %v" , serviceID , err )
1288+ }
12341289 if component == nil {
1290+ logrus .Warnf ("[GetAppServiceStatus] Service %s not found in database, returning UNKNOW" , serviceID )
12351291 return v1 .UNKNOW
12361292 }
1293+ logrus .Infof ("[GetAppServiceStatus] Service %s found in DB: kind=%s, alias=%s, tenant=%s" ,
1294+ serviceID , component .Kind , component .ServiceAlias , component .TenantID )
12371295 if component .Kind == model .ServiceKindThirdParty .String () {
1296+ logrus .Infof ("[GetAppServiceStatus] Service %s is third-party component, returning CLOSED" , serviceID )
12381297 return v1 .CLOSED
12391298 }
12401299 versions , err := a .dbmanager .VersionInfoDao ().GetVersionByServiceID (serviceID )
12411300 if (err != nil && err == gorm .ErrRecordNotFound ) || len (versions ) == 0 {
1301+ logrus .Warnf ("[GetAppServiceStatus] No version found for service %s, returning UNDEPLOY" , serviceID )
12421302 return v1 .UNDEPLOY
12431303 }
1304+ logrus .Warnf ("[GetAppServiceStatus] Service %s exists in DB with %d versions but not in cache, returning CLOSED" ,
1305+ serviceID , len (versions ))
12441306 return v1 .CLOSED
12451307 }
1308+ logrus .Debugf ("[GetAppServiceStatus] AppService found in cache for service: %s" , serviceID )
12461309 status := app .GetServiceStatus ()
1310+ logrus .Debugf ("[GetAppServiceStatus] Service %s status from AppService: %s" , serviceID , status )
12471311 if status == v1 .UNKNOW {
1312+ logrus .Infof ("[GetAppServiceStatus] Status is UNKNOW, updating AppService for service: %s" , serviceID )
12481313 app := a .UpdateGetAppService (serviceID )
12491314 if app == nil {
1315+ logrus .Warnf ("[GetAppServiceStatus] UpdateGetAppService returned nil for service: %s" , serviceID )
12501316 versions , err := a .dbmanager .VersionInfoDao ().GetVersionByServiceID (serviceID )
12511317 if (err != nil && err == gorm .ErrRecordNotFound ) || len (versions ) == 0 {
12521318 return v1 .UNDEPLOY
0 commit comments