Skip to content

Commit

Permalink
chore: add log for syncManifest delete upstream (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
lingsamuel committed Jul 8, 2022
1 parent 374f865 commit 62e0ea2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/ingress/apisix_route.go
Expand Up @@ -375,7 +375,9 @@ func (c *apisixRouteController) onAdd(obj interface{}) {
return
}
log.Debugw("ApisixRoute add event arrived",
zap.Any("object", obj))
zap.String("key", key),
zap.Any("object", obj),
)

ar := kube.MustNewApisixRoute(obj)
c.workqueue.Add(&types.Event{
Expand Down Expand Up @@ -404,8 +406,9 @@ func (c *apisixRouteController) onUpdate(oldObj, newObj interface{}) {
return
}
log.Debugw("ApisixRoute update event arrived",
zap.Any("new object", curr),
zap.Any("old object", prev),
zap.String("key", key),
zap.Any("new object", oldObj),
zap.Any("old object", newObj),
)
c.workqueue.Add(&types.Event{
Type: types.EventUpdate,
Expand Down Expand Up @@ -437,6 +440,7 @@ func (c *apisixRouteController) onDelete(obj interface{}) {
return
}
log.Debugw("ApisixRoute delete event arrived",
zap.String("key", key),
zap.Any("final state", ar),
)
c.workqueue.Add(&types.Event{
Expand Down
53 changes: 53 additions & 0 deletions pkg/ingress/utils/manifest.go
Expand Up @@ -222,6 +222,10 @@ func SyncManifests(ctx context.Context, apisix apisix.APISIX, clusterName string
}
for _, r := range deleted.Routes {
if err := apisix.Cluster(clusterName).Route().Delete(ctx, r); err != nil {
log.Warnw("failed to delete route, this may affect upstream deletions",
zap.Error(err),
zap.Any("route", r),
)
merr = multierror.Append(merr, err)
}
}
Expand All @@ -240,6 +244,55 @@ func SyncManifests(ctx context.Context, apisix apisix.APISIX, clusterName string
zap.String("upstream_id", u.ID),
zap.String("upstream_name", u.Name),
)

if log.Level() <= zap.DebugLevel {
// this could also happen when the route is synced(deleted) in another syncManifest call,
// but arrives later than this
// So log the deleted routes in this call to see if it's true
if len(deleted.Routes) == 0 {
log.Debugw("syncManifest deletes upstream but doesn't delete any routes")
} else {
found := false

for _, r := range deleted.Routes {
if r.UpstreamId == u.ID {
found = true
log.Debugw("a deleted route is referencing upstream",
zap.Any("route", r),
)
}
}
if !found {
log.Debugw("no any deleted route is referencing this upstream",
zap.String("upstream_id", u.ID),
)
}
}

// try to find which route is referencing the upstream
routes, err := apisix.Cluster(clusterName).Route().List(ctx)
if err != nil {
log.Debugw("try to find referencing routes, but failed to list",
zap.Error(err),
)
}

found := false
for _, r := range routes {
if r.UpstreamId == u.ID {
found = true
log.Debugw("route is referencing upstream",
zap.Any("route", r),
)
}
}
if !found {
log.Debugw("failed to find a route that references the upstream",
zap.String("upstream_id", u.ID),
zap.Any("routes", routes),
)
}
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/log/default_logger.go
Expand Up @@ -33,6 +33,11 @@ func init() {
DefaultLogger = l
}

// Level returns the DefaultLogger log level
func Level() zapcore.Level {
return DefaultLogger.Level()
}

// Debug uses the fmt.Sprint to construct and log a message using the DefaultLogger.
func Debug(args ...interface{}) {
DefaultLogger.Debug(args...)
Expand Down
5 changes: 5 additions & 0 deletions pkg/log/logger.go
Expand Up @@ -43,6 +43,11 @@ type Logger struct {
level zapcore.Level
}

// Level returns the log level
func (logger *Logger) Level() zapcore.Level {
return logger.level
}

func (logger *Logger) write(level zapcore.Level, message string, fields []zapcore.Field) {
e := zapcore.Entry{
Level: level,
Expand Down

0 comments on commit 62e0ea2

Please sign in to comment.