Skip to content

cmd/k8s-operator: truncate long StatefulSet name prefixes - #10343

Merged
irbekrm merged 1 commit into
mainfrom
irbekrm/longnames
Nov 21, 2023
Merged

irbekrm merged 1 commit into
mainfrom
irbekrm/longnames

Conversation

@irbekrm

@irbekrm irbekrm commented Nov 20, 2023 •

Copy link
Copy Markdown
Contributor

This PR fixes a bug where exposing a Service with a name longer than 43 chars fails at StatefulSet creation, due to a Kubernetes bug kubernetes/kubernetes#64023, see #10284 .

We use Kubernetes name generation to create a name for Services in front of proxy StatefulSets and use that to name the StatefulSet. (Name generation currently ensures that the generated name is 63 chars max). StatefulSet Pods get a controller-revision-hash label applied with a value of the StatefulSet name + revision hash of (10 chars for the hash + a hyphen). As the max allowed label value lenghth is 63 chars this does not work in cases where the generated StatefulSetNames are longer than 52 chars. The generated name consists of a base that we provide in form ts-<name-of-the-exposed-service>- and a suffix of, currently 5 random chars.

This PR fixes the issue by pre-calculating the StatefulSet name that will be used and, if it's longer than 52 chars, cutting the excess characters off the base.

This is not a breaking change as the cut only applies to Service names that wouldn't have successfully worked and does not introduce a change in behaviour as we are already using name generation which cuts long names. (We should ensure that in docs we encourage the use of labels to identify child resources).

To verify the fix:

  1. Deploy the operator from this PR
  2. Apply a Service with a name longer than 43 chars:
apiVersion: v1
kind: Service
metadata:
  name: oidhexl9o832hcbhyg4uz6o0s7u9uae54h5k8ofs9xbo // 44 chars
spec:
  ports:
  - port: 80
  selector:
    app: test
  loadBalancerClass: tailscale
  type: LoadBalancer
  1. Observe that a StatefulSet for the ingress proxy has been successfully created in tailscale namespace.

This fix will also work in case of Ingress resources with very long names.

/cc @Shaked

Updates #10284

@irbekrm
irbekrm force-pushed the irbekrm/longnames branch 2 times, most recently from f3c412d to 2dd7074 Compare November 21, 2023 08:47
@irbekrm
irbekrm requested a review from knyar November 21, 2023 09:01
Comment thread cmd/k8s-operator/sts.go Resolved
Kubernetes can generate StatefulSet names that are too long and result in invalid Pod revision hash label values.
Calculate whether a StatefulSet name generated for a Service or Ingress
will be too long and if so, truncate it.

Updates #10284

Signed-off-by: Irbe Krumina <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants