Skip to content

Commit

Permalink
fix: workaround panic in the kubelet service controller
Browse files Browse the repository at this point in the history
The traceback:

```
user: warning: [2022-12-02T17:31:09.496341098Z]: [talos] controller failed {"component": "controller-runtime", "controller": "k8s.KubeletServiceController", "error": "controller \x5c"k8s.KubeletServiceController\x5c" panicked: runtime error: invalid memory address or nil pointer dereference\x5cn\x5cngoroutine 308 [running]:\x5cnruntime/debug.Stack()\x5cn\x5ct/toolchain/go/src/runtime/debug/stack.go:24 +0x65\x5cngithub.com/cosi-project/runtime/pkg/controller/runtime.(*adapter).runOnce.func2()\x5cn\x5ct/.cache/mod/github.com/cosi-project/runtime@v0.1.1/pkg/controller/runtime/adapter.go:403 +0x5d\x5cnpanic({0x2b7b600, 0x536c7c0})\x5cn\x5ct/toolchain/go/src/runtime/panic.go:884 +0x212\x5cngithub.com/talos-systems/talos/internal/app/machined/pkg/controllers/k8s.updateKubeconfig(0xc0000d49b0?)\x5cn\x5ct/src/internal/app/machined/pkg/controllers/k8s/kubelet_service.go:302 +0xb8\x5cngithub.com/talos-systems/talos/internal/app/machined/pkg/controllers/k8s.(*KubeletServiceController).Run(0xc000956030, {0x389f7c0, 0xc000808040}, {0x38bce60, 0xc0000dfa80}, 0x0?)\x5cn\x5ct/s...
```

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
(cherry picked from commit 82e8c9e)
  • Loading branch information
smira committed Dec 20, 2022
1 parent 05430b9 commit 426fd28
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions internal/app/machined/pkg/controllers/k8s/kubelet_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (ctrl *KubeletServiceController) Run(ctx context.Context, r controller.Runt
}
}

err = updateKubeconfig(secretSpec.Endpoint)
err = updateKubeconfig(logger, secretSpec.Endpoint)
if err != nil {
return err
}
Expand Down Expand Up @@ -289,7 +289,7 @@ func (ctrl *KubeletServiceController) writeConfig(cfgSpec *k8s.KubeletSpecSpec)
}

// updateKubeconfig updates the kubeconfig of kubelet with the given endpoint if it exists.
func updateKubeconfig(newEndpoint *url.URL) error {
func updateKubeconfig(logger *zap.Logger, newEndpoint *url.URL) error {
config, err := clientcmd.LoadFromFile(constants.KubeletKubeconfig)
if errors.Is(err, os.ErrNotExist) {
return nil
Expand All @@ -299,7 +299,23 @@ func updateKubeconfig(newEndpoint *url.URL) error {
return err
}

cluster := config.Clusters[config.Contexts[config.CurrentContext].Cluster]
context := config.Contexts[config.CurrentContext]
if context == nil {
// this should never happen, but we can't fix kubeconfig if it is malformed
logger.Error("kubeconfig is missing current context", zap.String("context", config.CurrentContext))

return nil
}

cluster := config.Clusters[context.Cluster]

if cluster == nil {
// this should never happen, but we can't fix kubeconfig if it is malformed
logger.Error("kubeconfig is missing cluster", zap.String("context", config.CurrentContext), zap.String("cluster", context.Cluster))

return nil
}

if cluster.Server == newEndpoint.String() {
return nil
}
Expand Down

0 comments on commit 426fd28

Please sign in to comment.