2023-01-27 13:37:20 -08:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-02-28 13:08:45 -08:00
|
|
|
|
|
|
|
// Package kubestore contains an ipn.StateStore implementation using Kubernetes Secrets.
|
|
|
|
package kubestore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-29 17:03:48 +01:00
|
|
|
"fmt"
|
2024-11-19 13:07:19 +00:00
|
|
|
"log"
|
2023-03-29 13:51:53 -07:00
|
|
|
"net"
|
2024-05-31 14:39:38 -04:00
|
|
|
"os"
|
2023-03-23 11:35:09 -07:00
|
|
|
"strings"
|
2022-02-28 13:08:45 -08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/ipn"
|
2024-10-26 09:33:47 -05:00
|
|
|
"tailscale.com/ipn/store/mem"
|
2024-09-08 22:57:29 +03:00
|
|
|
"tailscale.com/kube/kubeapi"
|
|
|
|
"tailscale.com/kube/kubeclient"
|
2022-02-28 13:08:45 -08:00
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
2024-11-19 13:07:19 +00:00
|
|
|
const (
|
|
|
|
// timeout is the timeout for a single state update that includes calls to the API server to write or read a
|
|
|
|
// state Secret and emit an Event.
|
|
|
|
timeout = 30 * time.Second
|
|
|
|
|
|
|
|
reasonTailscaleStateUpdated = "TailscaledStateUpdated"
|
|
|
|
reasonTailscaleStateLoaded = "TailscaleStateLoaded"
|
|
|
|
reasonTailscaleStateUpdateFailed = "TailscaleStateUpdateFailed"
|
|
|
|
reasonTailscaleStateLoadFailed = "TailscaleStateLoadFailed"
|
|
|
|
eventTypeWarning = "Warning"
|
|
|
|
eventTypeNormal = "Normal"
|
|
|
|
)
|
2024-10-26 09:33:47 -05:00
|
|
|
|
2022-02-28 13:08:45 -08:00
|
|
|
// Store is an ipn.StateStore that uses a Kubernetes Secret for persistence.
|
|
|
|
type Store struct {
|
2024-09-08 21:06:07 +03:00
|
|
|
client kubeclient.Client
|
cmd/k8s-operator,ipn/store/kubestore: patch secrets instead of updating
We would call Update on the secret, but that was racey and would occasionaly
fail. Instead use patch whenever we can.
Fixes errors like
```
boot: 2023/08/29 01:03:53 failed to set serve config: sending serve config: updating config: writing ServeConfig to StateStore: Operation cannot be fulfilled on secrets "ts-webdav-kfrzv-0": the object has been modified; please apply your changes to the latest version and try again
{"level":"error","ts":"2023-08-29T01:03:48Z","msg":"Reconciler error","controller":"ingress","controllerGroup":"networking.k8s.io","controllerKind":"Ingress","Ingress":{"name":"webdav","namespace":"default"},"namespace":"default","name":"webdav","reconcileID":"96f5cfed-7782-4834-9b75-b0950fd563ed","error":"failed to provision: failed to create or get API key secret: Operation cannot be fulfilled on secrets \"ts-webdav-kfrzv-0\": the object has been modified; please apply your changes to the latest version and try again","stacktrace":"sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:324\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:265\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:226"}
```
Updates #502
Updates #7895
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-08-29 12:43:22 -07:00
|
|
|
canPatch bool
|
2022-02-28 13:08:45 -08:00
|
|
|
secretName string
|
2024-10-26 09:33:47 -05:00
|
|
|
|
|
|
|
// memory holds the latest tailscale state. Writes write state to a kube Secret and memory, Reads read from
|
|
|
|
// memory.
|
|
|
|
memory mem.Store
|
2022-02-28 13:08:45 -08:00
|
|
|
}
|
|
|
|
|
2024-10-26 09:33:47 -05:00
|
|
|
// New returns a new Store that persists to the named Secret.
|
2022-02-28 13:08:45 -08:00
|
|
|
func New(_ logger.Logf, secretName string) (*Store, error) {
|
2024-11-19 13:07:19 +00:00
|
|
|
c, err := kubeclient.New("tailscale-state-store")
|
2022-02-28 13:08:45 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-31 14:39:38 -04:00
|
|
|
if os.Getenv("TS_KUBERNETES_READ_API_SERVER_ADDRESS_FROM_ENV") == "true" {
|
|
|
|
// Derive the API server address from the environment variables
|
|
|
|
c.SetURL(fmt.Sprintf("https://%s:%s", os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT_HTTPS")))
|
|
|
|
}
|
2024-04-29 17:03:48 +01:00
|
|
|
canPatch, _, err := c.CheckSecretPermissions(context.Background(), secretName)
|
cmd/k8s-operator,ipn/store/kubestore: patch secrets instead of updating
We would call Update on the secret, but that was racey and would occasionaly
fail. Instead use patch whenever we can.
Fixes errors like
```
boot: 2023/08/29 01:03:53 failed to set serve config: sending serve config: updating config: writing ServeConfig to StateStore: Operation cannot be fulfilled on secrets "ts-webdav-kfrzv-0": the object has been modified; please apply your changes to the latest version and try again
{"level":"error","ts":"2023-08-29T01:03:48Z","msg":"Reconciler error","controller":"ingress","controllerGroup":"networking.k8s.io","controllerKind":"Ingress","Ingress":{"name":"webdav","namespace":"default"},"namespace":"default","name":"webdav","reconcileID":"96f5cfed-7782-4834-9b75-b0950fd563ed","error":"failed to provision: failed to create or get API key secret: Operation cannot be fulfilled on secrets \"ts-webdav-kfrzv-0\": the object has been modified; please apply your changes to the latest version and try again","stacktrace":"sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:324\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:265\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:226"}
```
Updates #502
Updates #7895
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-08-29 12:43:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-10-26 09:33:47 -05:00
|
|
|
s := &Store{
|
2022-02-28 13:08:45 -08:00
|
|
|
client: c,
|
cmd/k8s-operator,ipn/store/kubestore: patch secrets instead of updating
We would call Update on the secret, but that was racey and would occasionaly
fail. Instead use patch whenever we can.
Fixes errors like
```
boot: 2023/08/29 01:03:53 failed to set serve config: sending serve config: updating config: writing ServeConfig to StateStore: Operation cannot be fulfilled on secrets "ts-webdav-kfrzv-0": the object has been modified; please apply your changes to the latest version and try again
{"level":"error","ts":"2023-08-29T01:03:48Z","msg":"Reconciler error","controller":"ingress","controllerGroup":"networking.k8s.io","controllerKind":"Ingress","Ingress":{"name":"webdav","namespace":"default"},"namespace":"default","name":"webdav","reconcileID":"96f5cfed-7782-4834-9b75-b0950fd563ed","error":"failed to provision: failed to create or get API key secret: Operation cannot be fulfilled on secrets \"ts-webdav-kfrzv-0\": the object has been modified; please apply your changes to the latest version and try again","stacktrace":"sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:324\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:265\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:226"}
```
Updates #502
Updates #7895
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-08-29 12:43:22 -07:00
|
|
|
canPatch: canPatch,
|
2022-02-28 13:08:45 -08:00
|
|
|
secretName: secretName,
|
2024-10-26 09:33:47 -05:00
|
|
|
}
|
|
|
|
// Load latest state from kube Secret if it already exists.
|
2024-10-27 10:54:38 -05:00
|
|
|
if err := s.loadState(); err != nil && err != ipn.ErrStateNotExist {
|
2024-10-26 09:33:47 -05:00
|
|
|
return nil, fmt.Errorf("error loading state from kube Secret: %w", err)
|
|
|
|
}
|
|
|
|
return s, nil
|
2022-02-28 13:08:45 -08:00
|
|
|
}
|
|
|
|
|
2023-03-29 13:51:53 -07:00
|
|
|
func (s *Store) SetDialer(d func(ctx context.Context, network, address string) (net.Conn, error)) {
|
|
|
|
s.client.SetDialer(d)
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:08:45 -08:00
|
|
|
func (s *Store) String() string { return "kube.Store" }
|
|
|
|
|
|
|
|
// ReadState implements the StateStore interface.
|
|
|
|
func (s *Store) ReadState(id ipn.StateKey) ([]byte, error) {
|
2024-10-26 09:33:47 -05:00
|
|
|
return s.memory.ReadState(ipn.StateKey(sanitizeKey(id)))
|
2023-03-23 11:35:09 -07:00
|
|
|
}
|
|
|
|
|
2022-02-28 13:08:45 -08:00
|
|
|
// WriteState implements the StateStore interface.
|
2024-10-26 09:33:47 -05:00
|
|
|
func (s *Store) WriteState(id ipn.StateKey, bs []byte) (err error) {
|
2024-11-19 13:07:19 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
2024-10-26 09:33:47 -05:00
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
s.memory.WriteState(ipn.StateKey(sanitizeKey(id)), bs)
|
|
|
|
}
|
2024-11-19 13:07:19 +00:00
|
|
|
if err != nil {
|
|
|
|
if err := s.client.Event(ctx, eventTypeWarning, reasonTailscaleStateUpdateFailed, err.Error()); err != nil {
|
|
|
|
log.Printf("kubestore: error creating tailscaled state update Event: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := s.client.Event(ctx, eventTypeNormal, reasonTailscaleStateUpdated, "Successfully updated tailscaled state Secret"); err != nil {
|
|
|
|
log.Printf("kubestore: error creating tailscaled state Event: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cancel()
|
2024-10-26 09:33:47 -05:00
|
|
|
}()
|
2022-02-28 13:08:45 -08:00
|
|
|
|
|
|
|
secret, err := s.client.GetSecret(ctx, s.secretName)
|
|
|
|
if err != nil {
|
2024-09-08 21:06:07 +03:00
|
|
|
if kubeclient.IsNotFoundErr(err) {
|
|
|
|
return s.client.CreateSecret(ctx, &kubeapi.Secret{
|
|
|
|
TypeMeta: kubeapi.TypeMeta{
|
2022-02-28 13:08:45 -08:00
|
|
|
APIVersion: "v1",
|
|
|
|
Kind: "Secret",
|
|
|
|
},
|
2024-09-08 21:06:07 +03:00
|
|
|
ObjectMeta: kubeapi.ObjectMeta{
|
2022-02-28 13:08:45 -08:00
|
|
|
Name: s.secretName,
|
|
|
|
},
|
|
|
|
Data: map[string][]byte{
|
2023-03-23 11:35:09 -07:00
|
|
|
sanitizeKey(id): bs,
|
2022-02-28 13:08:45 -08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
cmd/k8s-operator,ipn/store/kubestore: patch secrets instead of updating
We would call Update on the secret, but that was racey and would occasionaly
fail. Instead use patch whenever we can.
Fixes errors like
```
boot: 2023/08/29 01:03:53 failed to set serve config: sending serve config: updating config: writing ServeConfig to StateStore: Operation cannot be fulfilled on secrets "ts-webdav-kfrzv-0": the object has been modified; please apply your changes to the latest version and try again
{"level":"error","ts":"2023-08-29T01:03:48Z","msg":"Reconciler error","controller":"ingress","controllerGroup":"networking.k8s.io","controllerKind":"Ingress","Ingress":{"name":"webdav","namespace":"default"},"namespace":"default","name":"webdav","reconcileID":"96f5cfed-7782-4834-9b75-b0950fd563ed","error":"failed to provision: failed to create or get API key secret: Operation cannot be fulfilled on secrets \"ts-webdav-kfrzv-0\": the object has been modified; please apply your changes to the latest version and try again","stacktrace":"sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:324\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:265\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:226"}
```
Updates #502
Updates #7895
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-08-29 12:43:22 -07:00
|
|
|
if s.canPatch {
|
2024-04-29 17:03:48 +01:00
|
|
|
if len(secret.Data) == 0 { // if user has pre-created a blank Secret
|
2024-09-08 21:06:07 +03:00
|
|
|
m := []kubeclient.JSONPatch{
|
2024-04-29 17:03:48 +01:00
|
|
|
{
|
|
|
|
Op: "add",
|
|
|
|
Path: "/data",
|
|
|
|
Value: map[string][]byte{sanitizeKey(id): bs},
|
|
|
|
},
|
|
|
|
}
|
2024-11-19 13:07:19 +00:00
|
|
|
if err := s.client.JSONPatchResource(ctx, s.secretName, kubeclient.TypeSecrets, m); err != nil {
|
2024-04-29 17:03:48 +01:00
|
|
|
return fmt.Errorf("error patching Secret %s with a /data field: %v", s.secretName, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-08 21:06:07 +03:00
|
|
|
m := []kubeclient.JSONPatch{
|
cmd/k8s-operator,ipn/store/kubestore: patch secrets instead of updating
We would call Update on the secret, but that was racey and would occasionaly
fail. Instead use patch whenever we can.
Fixes errors like
```
boot: 2023/08/29 01:03:53 failed to set serve config: sending serve config: updating config: writing ServeConfig to StateStore: Operation cannot be fulfilled on secrets "ts-webdav-kfrzv-0": the object has been modified; please apply your changes to the latest version and try again
{"level":"error","ts":"2023-08-29T01:03:48Z","msg":"Reconciler error","controller":"ingress","controllerGroup":"networking.k8s.io","controllerKind":"Ingress","Ingress":{"name":"webdav","namespace":"default"},"namespace":"default","name":"webdav","reconcileID":"96f5cfed-7782-4834-9b75-b0950fd563ed","error":"failed to provision: failed to create or get API key secret: Operation cannot be fulfilled on secrets \"ts-webdav-kfrzv-0\": the object has been modified; please apply your changes to the latest version and try again","stacktrace":"sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:324\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:265\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:226"}
```
Updates #502
Updates #7895
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-08-29 12:43:22 -07:00
|
|
|
{
|
|
|
|
Op: "add",
|
|
|
|
Path: "/data/" + sanitizeKey(id),
|
|
|
|
Value: bs,
|
|
|
|
},
|
|
|
|
}
|
2024-11-19 13:07:19 +00:00
|
|
|
if err := s.client.JSONPatchResource(ctx, s.secretName, kubeclient.TypeSecrets, m); err != nil {
|
|
|
|
return fmt.Errorf("error patching Secret %s with /data/%s field: %v", s.secretName, sanitizeKey(id), err)
|
cmd/k8s-operator,ipn/store/kubestore: patch secrets instead of updating
We would call Update on the secret, but that was racey and would occasionaly
fail. Instead use patch whenever we can.
Fixes errors like
```
boot: 2023/08/29 01:03:53 failed to set serve config: sending serve config: updating config: writing ServeConfig to StateStore: Operation cannot be fulfilled on secrets "ts-webdav-kfrzv-0": the object has been modified; please apply your changes to the latest version and try again
{"level":"error","ts":"2023-08-29T01:03:48Z","msg":"Reconciler error","controller":"ingress","controllerGroup":"networking.k8s.io","controllerKind":"Ingress","Ingress":{"name":"webdav","namespace":"default"},"namespace":"default","name":"webdav","reconcileID":"96f5cfed-7782-4834-9b75-b0950fd563ed","error":"failed to provision: failed to create or get API key secret: Operation cannot be fulfilled on secrets \"ts-webdav-kfrzv-0\": the object has been modified; please apply your changes to the latest version and try again","stacktrace":"sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:324\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:265\nsigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2\n\tsigs.k8s.io/controller-runtime@v0.15.0/pkg/internal/controller/controller.go:226"}
```
Updates #502
Updates #7895
Signed-off-by: Maisem Ali <maisem@tailscale.com>
2023-08-29 12:43:22 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-23 11:35:09 -07:00
|
|
|
secret.Data[sanitizeKey(id)] = bs
|
2022-02-28 13:08:45 -08:00
|
|
|
if err := s.client.UpdateSecret(ctx, secret); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2024-10-26 09:33:47 -05:00
|
|
|
|
2024-11-19 13:07:19 +00:00
|
|
|
func (s *Store) loadState() (err error) {
|
2024-10-26 09:33:47 -05:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
secret, err := s.client.GetSecret(ctx, s.secretName)
|
|
|
|
if err != nil {
|
|
|
|
if st, ok := err.(*kubeapi.Status); ok && st.Code == 404 {
|
|
|
|
return ipn.ErrStateNotExist
|
|
|
|
}
|
2024-11-19 13:07:19 +00:00
|
|
|
if err := s.client.Event(ctx, eventTypeWarning, reasonTailscaleStateLoadFailed, err.Error()); err != nil {
|
|
|
|
log.Printf("kubestore: error creating Event: %v", err)
|
|
|
|
}
|
2024-10-26 09:33:47 -05:00
|
|
|
return err
|
|
|
|
}
|
2024-11-19 13:07:19 +00:00
|
|
|
if err := s.client.Event(ctx, eventTypeNormal, reasonTailscaleStateLoaded, "Successfully loaded tailscaled state from Secret"); err != nil {
|
|
|
|
log.Printf("kubestore: error creating Event: %v", err)
|
|
|
|
}
|
2024-10-26 09:33:47 -05:00
|
|
|
s.memory.LoadFromMap(secret.Data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sanitizeKey(k ipn.StateKey) string {
|
|
|
|
// The only valid characters in a Kubernetes secret key are alphanumeric, -,
|
|
|
|
// _, and .
|
|
|
|
return strings.Map(func(r rune) rune {
|
|
|
|
if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '-' || r == '_' || r == '.' {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return '_'
|
|
|
|
}, string(k))
|
|
|
|
}
|