diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index efcbd8bf9..fff3bac61 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -21,6 +21,7 @@ import ( "net/http" "os" "sync" + "time" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -266,6 +267,7 @@ func (a *Agent) Stop() { defer a.stopLock.Unlock() if a.stopC != nil { + a.clearOwnConfigStatus() close(a.stopC) <-a.doneC a.stopC = nil @@ -633,6 +635,31 @@ func (a *Agent) updateConfig(cfg metav1.Object) { a.configure(cfg) } +// clearOwnConfigStatus nulls this node's Status.nodes entry on the current +// config resource so a clean shutdown does not leave a permanent orphan. +func (a *Agent) clearOwnConfigStatus() { + if a.cfgIf == nil || a.nodeName == "" || a.hasLocalConfig() || a.currentCfg == nil { + return + } + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + ns := a.namespace + name := a.currentCfg.GetName() + node := a.nodeName + + data, pt, err := cfgapi.NodeStatusPatch(node, nil) + if err == nil { + err = a.cfgIf.PatchStatus(ctx, ns, name, pt, data, metav1.PatchOptions{}) + } + if err != nil { + log.Errorf("failed to clear own config status on %s/%s: %v", ns, name, err) + return + } + log.Infof("cleared own config status on %s/%s", ns, name) +} + func (a *Agent) patchConfigStatus(prev, curr metav1.Object, errors error) { if a.cfgIf == nil { return