Files
tailscale/cmd/k8s-operator/nodeport-service-ports.go
T
Will Norris 3ec5be3f51 all: remove AUTHORS file and references to it
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>
2026-01-23 15:49:45 -08:00

204 lines
5.6 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package main
import (
"context"
"fmt"
"math/rand/v2"
"regexp"
"sort"
"strconv"
"strings"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
k8soperator "tailscale.com/k8s-operator"
tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
"tailscale.com/kube/kubetypes"
)
const (
tailscaledPortMax = 65535
tailscaledPortMin = 1024
testSvcName = "test-node-port-range"
invalidSvcNodePort = 777777
)
// getServicesNodePortRange is a hacky function that attempts to determine Service NodePort range by
// creating a deliberately invalid Service with a NodePort that is too large and parsing the returned
// validation error. Returns nil if unable to determine port range.
// https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport
func getServicesNodePortRange(ctx context.Context, c client.Client, tsNamespace string, logger *zap.SugaredLogger) *tsapi.PortRange {
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: testSvcName,
Namespace: tsNamespace,
Labels: map[string]string{
kubetypes.LabelManaged: "true",
},
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeNodePort,
Ports: []corev1.ServicePort{
{
Name: testSvcName,
Port: 8080,
TargetPort: intstr.FromInt32(8080),
Protocol: corev1.ProtocolUDP,
NodePort: invalidSvcNodePort,
},
},
},
}
// NOTE(ChaosInTheCRD): ideally this would be a server side dry-run but could not get it working
err := c.Create(ctx, svc)
if err == nil {
return nil
}
if validPorts := getServicesNodePortRangeFromErr(err.Error()); validPorts != "" {
pr, err := parseServicesNodePortRange(validPorts)
if err != nil {
logger.Debugf("failed to parse NodePort range set for Kubernetes Cluster: %w", err)
return nil
}
return pr
}
return nil
}
func getServicesNodePortRangeFromErr(err string) string {
reg := regexp.MustCompile(`\d{1,5}-\d{1,5}`)
matches := reg.FindAllString(err, -1)
if len(matches) != 1 {
return ""
}
return matches[0]
}
// parseServicesNodePortRange converts the `ValidPorts` string field in the Kubernetes PortAllocator error and converts it to
// PortRange
func parseServicesNodePortRange(p string) (*tsapi.PortRange, error) {
parts := strings.Split(p, "-")
s, err := strconv.ParseUint(parts[0], 10, 16)
if err != nil {
return nil, fmt.Errorf("failed to parse string as uint16: %w", err)
}
var e uint64
switch len(parts) {
case 1:
e = uint64(s)
case 2:
e, err = strconv.ParseUint(parts[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("failed to parse string as uint16: %w", err)
}
default:
return nil, fmt.Errorf("failed to parse port range %q", p)
}
portRange := &tsapi.PortRange{Port: uint16(s), EndPort: uint16(e)}
if !portRange.IsValid() {
return nil, fmt.Errorf("port range %q is not valid", portRange.String())
}
return portRange, nil
}
// validateNodePortRanges checks that the port range specified is valid. It also ensures that the specified ranges
// lie within the NodePort Service port range specified for the Kubernetes API Server.
func validateNodePortRanges(ctx context.Context, c client.Client, kubeRange *tsapi.PortRange, pc *tsapi.ProxyClass) error {
if pc.Spec.StaticEndpoints == nil {
return nil
}
portRanges := pc.Spec.StaticEndpoints.NodePort.Ports
if kubeRange != nil {
for _, pr := range portRanges {
if !kubeRange.Contains(pr.Port) || (pr.EndPort != 0 && !kubeRange.Contains(pr.EndPort)) {
return fmt.Errorf("range %q is not within Cluster configured range %q", pr.String(), kubeRange.String())
}
}
}
for _, r := range portRanges {
if !r.IsValid() {
return fmt.Errorf("port range %q is invalid", r.String())
}
}
// TODO(ChaosInTheCRD): if a ProxyClass that made another invalid (due to port range clash) is deleted,
// the invalid ProxyClass doesn't get reconciled on, and therefore will not go valid. We should fix this.
proxyClassRanges, err := getPortsForProxyClasses(ctx, c)
if err != nil {
return fmt.Errorf("failed to get port ranges for ProxyClasses: %w", err)
}
for _, r := range portRanges {
for pcName, pcr := range proxyClassRanges {
if pcName == pc.Name {
continue
}
if pcr.ClashesWith(r) {
return fmt.Errorf("port ranges for ProxyClass %q clash with existing ProxyClass %q", pc.Name, pcName)
}
}
}
if len(portRanges) == 1 {
return nil
}
sort.Slice(portRanges, func(i, j int) bool {
return portRanges[i].Port < portRanges[j].Port
})
for i := 1; i < len(portRanges); i++ {
prev := portRanges[i-1]
curr := portRanges[i]
if curr.Port <= prev.Port || curr.Port <= prev.EndPort {
return fmt.Errorf("overlapping ranges: %q and %q", prev.String(), curr.String())
}
}
return nil
}
// getPortsForProxyClasses gets the port ranges for all the other existing ProxyClasses
func getPortsForProxyClasses(ctx context.Context, c client.Client) (map[string]tsapi.PortRanges, error) {
pcs := new(tsapi.ProxyClassList)
err := c.List(ctx, pcs)
if err != nil {
return nil, fmt.Errorf("failed to list ProxyClasses: %w", err)
}
portRanges := make(map[string]tsapi.PortRanges)
for _, i := range pcs.Items {
if !k8soperator.ProxyClassIsReady(&i) {
continue
}
if se := i.Spec.StaticEndpoints; se != nil && se.NodePort != nil {
portRanges[i.Name] = se.NodePort.Ports
}
}
return portRanges, nil
}
func getRandomPort() uint16 {
return uint16(rand.IntN(tailscaledPortMax-tailscaledPortMin+1) + tailscaledPortMin)
}