mirror of
https://github.com/tailscale/tailscale.git
synced 2026-05-05 20:26:47 +02:00
This file was never truly necessary and has never actually been used in the history of Tailscale's open source releases. A Brief History of AUTHORS files --- The AUTHORS file was a pattern developed at Google, originally for Chromium, then adopted by Go and a bunch of other projects. The problem was that Chromium originally had a copyright line only recognizing Google as the copyright holder. Because Google (and most open source projects) do not require copyright assignemnt for contributions, each contributor maintains their copyright. Some large corporate contributors then tried to add their own name to the copyright line in the LICENSE file or in file headers. This quickly becomes unwieldy, and puts a tremendous burden on anyone building on top of Chromium, since the license requires that they keep all copyright lines intact. The compromise was to create an AUTHORS file that would list all of the copyright holders. The LICENSE file and source file headers would then include that list by reference, listing the copyright holder as "The Chromium Authors". This also become cumbersome to simply keep the file up to date with a high rate of new contributors. Plus it's not always obvious who the copyright holder is. Sometimes it is the individual making the contribution, but many times it may be their employer. There is no way for the proejct maintainer to know. Eventually, Google changed their policy to no longer recommend trying to keep the AUTHORS file up to date proactively, and instead to only add to it when requested: https://opensource.google/docs/releasing/authors. They are also clear that: > Adding contributors to the AUTHORS file is entirely within the > project's discretion and has no implications for copyright ownership. It was primarily added to appease a small number of large contributors that insisted that they be recognized as copyright holders (which was entirely their right to do). But it's not truly necessary, and not even the most accurate way of identifying contributors and/or copyright holders. In practice, we've never added anyone to our AUTHORS file. It only lists Tailscale, so it's not really serving any purpose. It also causes confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header in other open source repos which don't actually have an AUTHORS file, so it's ambiguous what that means. Instead, we just acknowledge that the contributors to Tailscale (whoever they are) are copyright holders for their individual contributions. We also have the benefit of using the DCO (developercertificate.org) which provides some additional certification of their right to make the contribution. The source file changes were purely mechanical with: git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g' Updates #cleanup Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
328 lines
11 KiB
Go
328 lines
11 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build !plan9
|
|
|
|
// Package tailnet provides reconciliation logic for the Tailnet custom resource definition. It is responsible for
|
|
// ensuring the referenced OAuth credentials are valid and have the required scopes to be able to generate authentication
|
|
// keys, manage devices & manage VIP services.
|
|
package tailnet
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
"golang.org/x/oauth2"
|
|
"golang.org/x/oauth2/clientcredentials"
|
|
corev1 "k8s.io/api/core/v1"
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"sigs.k8s.io/controller-runtime/pkg/builder"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/manager"
|
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
|
|
|
"tailscale.com/internal/client/tailscale"
|
|
"tailscale.com/ipn"
|
|
operatorutils "tailscale.com/k8s-operator"
|
|
tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
|
|
"tailscale.com/k8s-operator/reconciler"
|
|
"tailscale.com/kube/kubetypes"
|
|
"tailscale.com/tstime"
|
|
"tailscale.com/util/clientmetric"
|
|
"tailscale.com/util/set"
|
|
)
|
|
|
|
type (
|
|
// The Reconciler type is a reconcile.TypedReconciler implementation used to manage the reconciliation of
|
|
// Tailnet custom resources.
|
|
Reconciler struct {
|
|
client.Client
|
|
|
|
tailscaleNamespace string
|
|
clock tstime.Clock
|
|
logger *zap.SugaredLogger
|
|
clientFunc func(*tsapi.Tailnet, *corev1.Secret) TailscaleClient
|
|
|
|
// Metrics related fields
|
|
mu sync.Mutex
|
|
tailnets set.Slice[types.UID]
|
|
}
|
|
|
|
// The ReconcilerOptions type contains configuration values for the Reconciler.
|
|
ReconcilerOptions struct {
|
|
// The client for interacting with the Kubernetes API.
|
|
Client client.Client
|
|
// The namespace the operator is installed in. This reconciler expects Tailnet OAuth credentials to be stored
|
|
// in Secret resources within this namespace.
|
|
TailscaleNamespace string
|
|
// Controls which clock to use for performing time-based functions. This is typically modified for use
|
|
// in tests.
|
|
Clock tstime.Clock
|
|
// The logger to use for this Reconciler.
|
|
Logger *zap.SugaredLogger
|
|
// ClientFunc is a function that takes tailscale credentials and returns an implementation for the Tailscale
|
|
// HTTP API. This should generally be nil unless needed for testing.
|
|
ClientFunc func(*tsapi.Tailnet, *corev1.Secret) TailscaleClient
|
|
}
|
|
|
|
// The TailscaleClient interface describes types that interact with the Tailscale HTTP API.
|
|
TailscaleClient interface {
|
|
Devices(context.Context, *tailscale.DeviceFieldsOpts) ([]*tailscale.Device, error)
|
|
Keys(ctx context.Context) ([]string, error)
|
|
ListVIPServices(ctx context.Context) (*tailscale.VIPServiceList, error)
|
|
}
|
|
)
|
|
|
|
const reconcilerName = "tailnet-reconciler"
|
|
|
|
// NewReconciler returns a new instance of the Reconciler type. It watches specifically for changes to Tailnet custom
|
|
// resources. The ReconcilerOptions can be used to modify the behaviour of the Reconciler.
|
|
func NewReconciler(options ReconcilerOptions) *Reconciler {
|
|
return &Reconciler{
|
|
Client: options.Client,
|
|
tailscaleNamespace: options.TailscaleNamespace,
|
|
clock: options.Clock,
|
|
logger: options.Logger.Named(reconcilerName),
|
|
clientFunc: options.ClientFunc,
|
|
}
|
|
}
|
|
|
|
// Register the Reconciler onto the given manager.Manager implementation.
|
|
func (r *Reconciler) Register(mgr manager.Manager) error {
|
|
return builder.
|
|
ControllerManagedBy(mgr).
|
|
For(&tsapi.Tailnet{}).
|
|
Named(reconcilerName).
|
|
Complete(r)
|
|
}
|
|
|
|
var (
|
|
// gaugeTailnetResources tracks the overall number of Tailnet resources currently managed by this operator instance.
|
|
gaugeTailnetResources = clientmetric.NewGauge(kubetypes.MetricTailnetCount)
|
|
)
|
|
|
|
// Reconcile is invoked when a change occurs to Tailnet resources within the cluster. On create/update, the Tailnet
|
|
// resource is validated ensuring that the specified Secret exists and contains valid OAuth credentials that have
|
|
// required permissions to perform all necessary functions by the operator.
|
|
func (r *Reconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
|
|
var tailnet tsapi.Tailnet
|
|
err := r.Get(ctx, req.NamespacedName, &tailnet)
|
|
switch {
|
|
case apierrors.IsNotFound(err):
|
|
return reconcile.Result{}, nil
|
|
case err != nil:
|
|
return reconcile.Result{}, fmt.Errorf("failed to get Tailnet %q: %w", req.NamespacedName, err)
|
|
}
|
|
|
|
if !tailnet.DeletionTimestamp.IsZero() {
|
|
return r.delete(ctx, &tailnet)
|
|
}
|
|
|
|
return r.createOrUpdate(ctx, &tailnet)
|
|
}
|
|
|
|
func (r *Reconciler) delete(ctx context.Context, tailnet *tsapi.Tailnet) (reconcile.Result, error) {
|
|
reconciler.RemoveFinalizer(tailnet)
|
|
if err := r.Update(ctx, tailnet); err != nil {
|
|
return reconcile.Result{}, fmt.Errorf("failed to remove finalizer from Tailnet %q: %w", tailnet.Name, err)
|
|
}
|
|
|
|
r.mu.Lock()
|
|
r.tailnets.Remove(tailnet.UID)
|
|
r.mu.Unlock()
|
|
gaugeTailnetResources.Set(int64(r.tailnets.Len()))
|
|
|
|
return reconcile.Result{}, nil
|
|
}
|
|
|
|
// Constants for condition reasons.
|
|
const (
|
|
ReasonInvalidOAuth = "InvalidOAuth"
|
|
ReasonInvalidSecret = "InvalidSecret"
|
|
ReasonValid = "TailnetValid"
|
|
)
|
|
|
|
func (r *Reconciler) createOrUpdate(ctx context.Context, tailnet *tsapi.Tailnet) (reconcile.Result, error) {
|
|
r.mu.Lock()
|
|
r.tailnets.Add(tailnet.UID)
|
|
r.mu.Unlock()
|
|
gaugeTailnetResources.Set(int64(r.tailnets.Len()))
|
|
|
|
name := types.NamespacedName{Name: tailnet.Spec.Credentials.SecretName, Namespace: r.tailscaleNamespace}
|
|
|
|
var secret corev1.Secret
|
|
err := r.Get(ctx, name, &secret)
|
|
|
|
// The referenced Secret does not exist within the tailscale namespace, so we'll mark the Tailnet as not ready
|
|
// for use.
|
|
if apierrors.IsNotFound(err) {
|
|
operatorutils.SetTailnetCondition(
|
|
tailnet,
|
|
tsapi.TailnetReady,
|
|
metav1.ConditionFalse,
|
|
ReasonInvalidSecret,
|
|
fmt.Sprintf("referenced secret %q does not exist in namespace %q", name.Name, r.tailscaleNamespace),
|
|
r.clock,
|
|
r.logger,
|
|
)
|
|
|
|
if err = r.Status().Update(ctx, tailnet); err != nil {
|
|
return reconcile.Result{}, fmt.Errorf("failed to update Tailnet status for %q: %w", tailnet.Name, err)
|
|
}
|
|
|
|
return reconcile.Result{}, nil
|
|
}
|
|
|
|
if err != nil {
|
|
return reconcile.Result{}, fmt.Errorf("failed to get secret %q: %w", name, err)
|
|
}
|
|
|
|
// We first ensure that the referenced secret contains the required fields. Otherwise, we set the Tailnet as
|
|
// invalid. The operator will not allow the use of this Tailnet while it is in an invalid state.
|
|
if ok := r.ensureSecret(tailnet, &secret); !ok {
|
|
if err = r.Status().Update(ctx, tailnet); err != nil {
|
|
return reconcile.Result{}, fmt.Errorf("failed to update Tailnet status for %q: %w", tailnet.Name, err)
|
|
}
|
|
|
|
return reconcile.Result{RequeueAfter: time.Minute / 2}, nil
|
|
}
|
|
|
|
tsClient := r.createClient(ctx, tailnet, &secret)
|
|
|
|
// Second, we ensure the OAuth credentials supplied in the secret are valid and have the required scopes to access
|
|
// the various API endpoints required by the operator.
|
|
if ok := r.ensurePermissions(ctx, tsClient, tailnet); !ok {
|
|
if err = r.Status().Update(ctx, tailnet); err != nil {
|
|
return reconcile.Result{}, fmt.Errorf("failed to update Tailnet status for %q: %w", tailnet.Name, err)
|
|
}
|
|
|
|
// We provide a requeue duration here as a user will likely want to go and modify their scopes and come back.
|
|
// This should save them having to delete and recreate the resource.
|
|
return reconcile.Result{RequeueAfter: time.Minute / 2}, nil
|
|
}
|
|
|
|
operatorutils.SetTailnetCondition(
|
|
tailnet,
|
|
tsapi.TailnetReady,
|
|
metav1.ConditionTrue,
|
|
ReasonValid,
|
|
ReasonValid,
|
|
r.clock,
|
|
r.logger,
|
|
)
|
|
|
|
if err = r.Status().Update(ctx, tailnet); err != nil {
|
|
return reconcile.Result{}, fmt.Errorf("failed to update Tailnet status for %q: %w", tailnet.Name, err)
|
|
}
|
|
|
|
reconciler.SetFinalizer(tailnet)
|
|
if err = r.Update(ctx, tailnet); err != nil {
|
|
return reconcile.Result{}, fmt.Errorf("failed to add finalizer to Tailnet %q: %w", tailnet.Name, err)
|
|
}
|
|
|
|
return reconcile.Result{}, nil
|
|
}
|
|
|
|
// Constants for OAuth credential fields within the Secret referenced by the Tailnet.
|
|
const (
|
|
clientIDKey = "client_id"
|
|
clientSecretKey = "client_secret"
|
|
)
|
|
|
|
func (r *Reconciler) createClient(ctx context.Context, tailnet *tsapi.Tailnet, secret *corev1.Secret) TailscaleClient {
|
|
if r.clientFunc != nil {
|
|
return r.clientFunc(tailnet, secret)
|
|
}
|
|
|
|
baseURL := ipn.DefaultControlURL
|
|
if tailnet.Spec.LoginURL != "" {
|
|
baseURL = tailnet.Spec.LoginURL
|
|
}
|
|
|
|
credentials := clientcredentials.Config{
|
|
ClientID: string(secret.Data[clientIDKey]),
|
|
ClientSecret: string(secret.Data[clientSecretKey]),
|
|
TokenURL: baseURL + "/api/v2/oauth/token",
|
|
}
|
|
|
|
source := credentials.TokenSource(ctx)
|
|
httpClient := oauth2.NewClient(ctx, source)
|
|
|
|
tsClient := tailscale.NewClient("-", nil)
|
|
tsClient.UserAgent = "tailscale-k8s-operator"
|
|
tsClient.HTTPClient = httpClient
|
|
tsClient.BaseURL = baseURL
|
|
|
|
return tsClient
|
|
}
|
|
|
|
func (r *Reconciler) ensurePermissions(ctx context.Context, tsClient TailscaleClient, tailnet *tsapi.Tailnet) bool {
|
|
// Perform basic list requests here to confirm that the OAuth credentials referenced on the Tailnet resource
|
|
// can perform the basic operations required for the operator to function. This has a caveat of only performing
|
|
// read actions, as we don't want to create arbitrary keys and VIP services. However, it will catch when a user
|
|
// has completely forgotten an entire scope that's required.
|
|
var errs error
|
|
if _, err := tsClient.Devices(ctx, nil); err != nil {
|
|
errs = errors.Join(errs, fmt.Errorf("failed to list devices: %w", err))
|
|
}
|
|
|
|
if _, err := tsClient.Keys(ctx); err != nil {
|
|
errs = errors.Join(errs, fmt.Errorf("failed to list auth keys: %w", err))
|
|
}
|
|
|
|
if _, err := tsClient.ListVIPServices(ctx); err != nil {
|
|
errs = errors.Join(errs, fmt.Errorf("failed to list tailscale services: %w", err))
|
|
}
|
|
|
|
if errs != nil {
|
|
operatorutils.SetTailnetCondition(
|
|
tailnet,
|
|
tsapi.TailnetReady,
|
|
metav1.ConditionFalse,
|
|
ReasonInvalidOAuth,
|
|
errs.Error(),
|
|
r.clock,
|
|
r.logger,
|
|
)
|
|
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (r *Reconciler) ensureSecret(tailnet *tsapi.Tailnet, secret *corev1.Secret) bool {
|
|
var message string
|
|
|
|
switch {
|
|
case len(secret.Data) == 0:
|
|
message = fmt.Sprintf("Secret %q is empty", secret.Name)
|
|
case len(secret.Data[clientIDKey]) == 0:
|
|
message = fmt.Sprintf("Secret %q is missing the client_id field", secret.Name)
|
|
case len(secret.Data[clientSecretKey]) == 0:
|
|
message = fmt.Sprintf("Secret %q is missing the client_secret field", secret.Name)
|
|
}
|
|
|
|
if message == "" {
|
|
return true
|
|
}
|
|
|
|
operatorutils.SetTailnetCondition(
|
|
tailnet,
|
|
tsapi.TailnetReady,
|
|
metav1.ConditionFalse,
|
|
ReasonInvalidSecret,
|
|
message,
|
|
r.clock,
|
|
r.logger,
|
|
)
|
|
|
|
return false
|
|
}
|