Skip to content

Commit d4a1b47

Browse files
authored
fix: add log (#2417)
Signed-off-by: 张启航 <[email protected]>
1 parent 8e7b4be commit d4a1b47

File tree

3 files changed

+102
-15
lines changed

3 files changed

+102
-15
lines changed

worker/appm/conversion/conversion.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func InitCacheOperatorManaged(appID string) *v1.OperatorManaged {
128128
// InitCacheAppService init cache app service.
129129
// if store manager receive a kube model belong with service and not find in store,will create
130130
func InitCacheAppService(dbm db.Manager, serviceID, creatorID string) (*v1.AppService, error) {
131+
logrus.Infof("[InitCacheAppService] Initializing AppService for serviceID=%s, creatorID=%s", serviceID, creatorID)
131132
appService := &v1.AppService{
132133
AppServiceBase: v1.AppServiceBase{
133134
ServiceID: serviceID,
@@ -139,18 +140,27 @@ func InitCacheAppService(dbm db.Manager, serviceID, creatorID string) (*v1.AppSe
139140
}
140141

141142
// setup governance mode
143+
logrus.Debugf("[InitCacheAppService] Querying application info for service: %s", serviceID)
142144
app, err := dbm.ApplicationDao().GetByServiceID(serviceID)
143145
if err != nil && err != bcode.ErrApplicationNotFound {
144-
return nil, fmt.Errorf("get app based on service id(%s)", serviceID)
146+
logrus.Errorf("[InitCacheAppService] Failed to get app for service %s: %v", serviceID, err)
147+
return nil, fmt.Errorf("get app based on service id(%s): %v", serviceID, err)
145148
}
146149
if app != nil {
150+
logrus.Infof("[InitCacheAppService] Found application for service %s: appID=%s, governanceMode=%s",
151+
serviceID, app.AppID, app.GovernanceMode)
147152
appService.AppServiceBase.GovernanceMode = app.GovernanceMode
148153
appService.AppServiceBase.K8sApp = app.K8sApp
154+
} else {
155+
logrus.Debugf("[InitCacheAppService] No application found for service %s (this is OK)", serviceID)
149156
}
150157

158+
logrus.Debugf("[InitCacheAppService] Loading tenant service base info for service: %s", serviceID)
151159
if err := TenantServiceBase(appService, dbm); err != nil {
160+
logrus.Errorf("[InitCacheAppService] TenantServiceBase failed for service %s: %v", serviceID, err)
152161
return nil, err
153162
}
154163

164+
logrus.Infof("[InitCacheAppService] Successfully initialized AppService for service: %s", serviceID)
155165
return appService, nil
156166
}

worker/appm/conversion/service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,26 @@ func int32Ptr(i int) *int32 {
9999

100100
// TenantServiceBase conv tenant service base info
101101
func TenantServiceBase(as *v1.AppService, dbmanager db.Manager) error {
102+
logrus.Debugf("[TenantServiceBase] Querying tenant_service table for serviceID: %s", as.ServiceID)
102103
tenantService, err := dbmanager.TenantServiceDao().GetServiceByID(as.ServiceID)
103104
if err != nil {
104105
if err == gorm.ErrRecordNotFound {
106+
logrus.Warnf("[TenantServiceBase] Service %s not found in database (ErrRecordNotFound)", as.ServiceID)
105107
return ErrServiceNotFound
106108
}
109+
logrus.Errorf("[TenantServiceBase] Database error querying service %s: %v", as.ServiceID, err)
107110
return fmt.Errorf("error getting service base info by serviceID(%s) %s", as.ServiceID, err.Error())
108111
}
112+
logrus.Infof("[TenantServiceBase] Found service in DB: serviceID=%s, alias=%s, kind=%s, tenantID=%s, deployVersion=%s",
113+
as.ServiceID, tenantService.ServiceAlias, tenantService.Kind, tenantService.TenantID, tenantService.DeployVersion)
109114
as.ServiceKind = dbmodel.ServiceKind(tenantService.Kind)
115+
logrus.Debugf("[TenantServiceBase] Querying tenant info for tenantID: %s", tenantService.TenantID)
110116
tenant, err := dbmanager.TenantDao().GetTenantByUUID(tenantService.TenantID)
111117
if err != nil {
118+
logrus.Errorf("[TenantServiceBase] Failed to get tenant %s: %v", tenantService.TenantID, err)
112119
return fmt.Errorf("get tenant info failure %s", err.Error())
113120
}
121+
logrus.Debugf("[TenantServiceBase] Found tenant: %s", tenant.Name)
114122
as.TenantID = tenantService.TenantID
115123
if as.DeployVersion == "" {
116124
as.DeployVersion = tenantService.DeployVersion
@@ -127,15 +135,18 @@ func TenantServiceBase(as *v1.AppService, dbmanager db.Manager) error {
127135
}
128136
as.TenantName = tenant.Name
129137
if err := initTenant(as, tenant); err != nil {
138+
logrus.Errorf("[TenantServiceBase] Failed to init tenant for service %s: %v", as.ServiceID, err)
130139
return fmt.Errorf("conversion tenant info failure %s", err.Error())
131140
}
132141
if tenantService.Kind == dbmodel.ServiceKindThirdParty.String() {
142+
logrus.Debugf("[TenantServiceBase] Service %s is third-party, loading discovery config", as.ServiceID)
133143
disCfg, _ := dbmanager.ThirdPartySvcDiscoveryCfgDao().GetByServiceID(as.ServiceID)
134144
as.SetDiscoveryCfg(disCfg)
135145
return nil
136146
}
137147

138148
if tenantService.Kind == dbmodel.ServiceKindCustom.String() {
149+
logrus.Debugf("[TenantServiceBase] Service %s is custom component", as.ServiceID)
139150
return nil
140151
}
141152
label, _ := dbmanager.TenantServiceLabelDao().GetLabelByNodeSelectorKey(as.ServiceID, "windows")

worker/appm/store/store.go

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
760793
func (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.
10531098
func (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

10591107
func (a *appRuntimeStore) RegistOperatorManaged(app *v1.OperatorManaged) {
@@ -1080,9 +1128,11 @@ func (a *appRuntimeStore) DeleteAppServiceByKey(key v1.CacheKey) {
10801128

10811129
func (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

12301280
func (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

Comments
 (0)