Skip to content

cmd/k8s-operator: operator can create subnetrouter - #9505

Merged
irbekrm merged 5 commits into
mainfrom
irbekrm/subnetrouter
Dec 14, 2023
Merged

irbekrm merged 5 commits into
mainfrom
irbekrm/subnetrouter

Conversation

@irbekrm

@irbekrm irbekrm commented Sep 21, 2023 •

Copy link
Copy Markdown
Contributor

This PR makes it possible to deploy a Tailscale subnet router in a Kuberntes cluster to expose range(s) of CIDR(s) accessible from within the cluster to tailnet. This could be either cluster Pod/Service CIDR ranges or routes to other private network segments that can be accessed from within the cluster, such as routes to private cloud networks that can be accessed from Kubernetes deployed on that cloud and should be made accessible to tailnet users without exposing them to the internet.

It introduces a new cluster-scoped Kubernetes custom resource Connector. Connector is intended to be used to allow users to define additional components that the Tailscale operator should create, such as subnet routers, exit nodes etc. Currently the only supported component type is subnet router. There will be 1:1 mapping between a Connector custom resource and a component to be deployed (so 1 Connector can only contain a definition for 1 subnet router, exit node etc).

Connector functionality is currently off by default. Users who wish to use it, will have to install the Connector CRD and set ENABLE_CONNECTOR env var on operator manifest to true (or set .values.enableConnector to true if using Helm).

User flow

With auto-approval mechanism

  1. Edit ACLs: create a new ACL tag and, make the operator owner of it and make it able to advertize the routes you want to expose without requiring manual approval. (As an alternative to creating a new ACL tag you could also make the default tag:k8s ACL tag that operator tags all nodes that it creates able to advertize routes without manual approval).
  2. Run kubectl apply -f ./cmd/k8s-operator/deploy/crds to install the Connector CRD
  3. Deploy the operator setting ENABLE_CONNECTOR env var to "true"
  4. Create a Connector resource:
apiVersion: tailscale.io/v1alpha1
kind: Connector
metadata:
  name: foo
spec:
  subnetRouter:
    routes:
    - "10.40.0.0/14" // example pod CIDR range
    - "10.44.0.0/20" // example Service CIDR range
    tags:
    - "tag:subnet" // replace with the created ACL tag
    hostname: "pods-subnetrouter"
  1. Wait for the operator to update the Connector status:
 $ kubectl get connector foo
NAME         SUBNETROUTES   STATUS
foo   10.40.0.0/14,10.44.0.0/20   SubnetRouterCreated
  1. You should now be able to access anything running on the advertized route from any tailnet node (see instructions for Linux)

With manual approval

  1. Deploy the operator
  2. Create a Connector resource:
apiVersion: tailscale.io/v1alpha1
kind: Connector
metadata:
  name: foo
spec:
  subnetRouter:
    routes:
    - "10.40.0.0/14" // replace with whatever CIDR you want to expose
  1. Wait for the operator to update the Connector status
  2. Go to admin panel and manually approve routes for the added subnet router.

Next steps

  • add CRD to the Helm chart so that users can install the Connector CRD and the operator with a single helm command

/cc @maisem

@irbekrm
irbekrm marked this pull request as draft September 21, 2023 19:04
@@ -0,0 +1,89 @@
apiVersion: apiextensions.k8s.io/v1

@irbekrm irbekrm Sep 21, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autogenerated from Go types in kube/apis/v1alpha1/types_connector.go.

@@ -0,0 +1,54 @@
// Copyright (c) Tailscale Inc & AUTHORS

@irbekrm irbekrm Sep 21, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is all boilerplate

@irbekrm
irbekrm force-pushed the irbekrm/subnetrouter branch 12 times, most recently from 0652d24 to 8c997b9 Compare September 25, 2023 15:23
@irbekrm irbekrm changed the title WIP: subnetrouter for kube subnetrouter for kube Sep 25, 2023
@irbekrm
irbekrm requested a review from maisem September 25, 2023 15:36
@irbekrm
irbekrm marked this pull request as ready for review September 25, 2023 15:37
Comment thread kube/apis/v1alpha1/types_connector.go Resolved
@irbekrm irbekrm changed the title subnetrouter for kube cmd/k8s-operator: operator can create subnetrouter Oct 3, 2023
@irbekrm
irbekrm force-pushed the irbekrm/subnetrouter branch 3 times, most recently from 25dfc79 to 2239a73 Compare November 30, 2023 16:16
@@ -0,0 +1,129 @@
apiVersion: apiextensions.k8s.io/v1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to figure out the best way how to add this to the Helm chart. Probably will opt for a similar mechanism to how it was done for cert-manager because the Helm CRD management is still suboptimal
cert-manager/cert-manager#2775
https://helm.sh/docs/chart_best_practices/custom_resource_definitions/#method-2-separate-charts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update this part once #10436 merges.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, since the Connector functionality is now opt-in, it should be ok to merge this PR as is and add the Helm chart CRD generation in a separate step.

Comment thread kube/apis/v1alpha1/types_connector.go Resolved
@irbekrm
irbekrm force-pushed the irbekrm/subnetrouter branch from 2239a73 to 416df07 Compare December 1, 2023 19:07
Comment thread kube/apis/v1alpha1/types_connector.go Resolved
Comment thread cmd/k8s-operator/operator.go Resolved
Comment thread cmd/k8s-operator/connector.go Resolved
Comment thread kube/apis/v1alpha1/types_connector.go Resolved
@irbekrm
irbekrm marked this pull request as draft December 6, 2023 18:26
@irbekrm
irbekrm force-pushed the irbekrm/subnetrouter branch 2 times, most recently from 5542fc3 to ea30974 Compare December 7, 2023 10:14
@irbekrm
irbekrm marked this pull request as ready for review December 7, 2023 10:31
@irbekrm
irbekrm requested a review from knyar December 7, 2023 10:32
Comment thread cmd/k8s-operator/connector.go Resolved
Comment thread cmd/k8s-operator/connector.go Resolved
Comment thread cmd/k8s-operator/connector.go Resolved
Comment thread cmd/k8s-operator/connector.go Resolved
priorityClassName = defaultEnv("PROXY_PRIORITY_CLASS_NAME", "")
tags = defaultEnv("PROXY_TAGS", "tag:k8s")
tsFirewallMode = defaultEnv("PROXY_FIREWALL_MODE", "")
tsEnableConnector = defaultBool("ENABLE_CONNECTOR", false)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why disable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am actually not very opinionated on this- we discussed this with @knyar before.
Disabling would allow folks to not install the Connector CRD in the cluster if they don't need it and they might prefer that for some reasons (a large resource? being generally cautious of additional CRDs?). However, none of those are very strong reasons.
It would make it slightly easier for us to get this PR in and deal with updating helm packaging mechanism to include the CRD in after we finally have published helm charts without breaking everyone who is currently installing the operator by helm install ./cmd/k8s-operator .. . This is because I am planning to add the CRD to the chart at package time rather than make it a helm template which doesn't work nicely with the codegen yaml generator.

Comment thread cmd/tailscaled/depaware.txt Resolved
Comment thread kube/conditions.go Resolved
… Connector kube CRD.

Connector allows users to configure the Tailscale operator
to deploy a subnet router to expose cluster CIDRs or
other CIDRs available from within the cluster
to their tailnet.

Also adds various CRD related machinery to
generate CRD YAML, deep copy implementations etc.

Engineers will now have to run
'make kube-generate-all` after changing kube files
to ensure that all generated files are up to date.

Updates #502

Signed-off-by: Irbe Krumina <[email protected]>
@irbekrm
irbekrm force-pushed the irbekrm/subnetrouter branch from ea30974 to 8dbd9ff Compare December 7, 2023 19:38
@irbekrm

irbekrm commented Dec 7, 2023 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review @maisem I have restructured it so that the Connector API lives in ./k8s-operator. It would have been possible to put it in ./cmd/k8s-operator too, but I put it into a separate package thinking that this will likely be imported by folks who will want to programmatically create Connector resources, so a library type of package that is not under ./cmd seemed to make more sense.

I still left the Connector disabled by default, happy to enable it as well, I don't feel strongly either way (cc @knyar )

Reconcile Connector resources, create/delete subnetrouter resources in response to changes to Connector(s).

Signed-off-by: Irbe Krumina <[email protected]>
@irbekrm
irbekrm force-pushed the irbekrm/subnetrouter branch from 8dbd9ff to 77dddf8 Compare December 12, 2023 07:14
Connector reconciler will not be started unless
ENABLE_CONNECTOR env var is set to true.
This means that users who don't want to use the alpha
Connector custom resource don't have to install the Connector
CRD to their cluster.
For users who do want to use it the flow is:
- install the CRD
- install the operator (via Helm chart or using static manifests).
For Helm users set .values.enableConnector to true, for static
manifest users, set ENABLE_CONNECTOR to true in the static manifest.

Signed-off-by: Irbe Krumina <[email protected]>
@irbekrm
irbekrm force-pushed the irbekrm/subnetrouter branch from 77dddf8 to 56935da Compare December 12, 2023 07:23

@maisem maisem left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minor nits.

Comment thread cmd/k8s-operator/connector.go Resolved
Comment thread cmd/k8s-operator/connector.go Resolved
Comment thread cmd/k8s-operator/connector.go Outdated
Message: fmt.Sprintf(messageSubnetRouterCreated, cidrsS),
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you return here then you can unindent the block below too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will set subnetrouter as required and remove the block below altogether. When we start supporting more component types, we can anyway not support removing one component definition from a Connector and adding another.

Comment thread cmd/k8s-operator/connector.go Outdated
// that will change in the future - so run the cleanup and provision
// conditionally already now.
if cn.Spec.SubnetRouter == nil {
return true, nil

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will result in always returning true from line 171, which means the cleanup won't ever happen but we will consider it did.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to set the subnet router def as required in the CRD spec, so it won't be possible to apply a Connector without a subnet router or routes defined. Then we can remove this check altogether.

Comment thread cmd/k8s-operator/operator.go Resolved
Comment thread cmd/k8s-operator/operator.go Resolved
Comment thread k8s-operator/apis/doc.go Resolved
Comment thread cmd/k8s-operator/connector.go Outdated
if cn.Spec.SubnetRouter != nil && len(cn.Spec.SubnetRouter.Routes) > 0 {
var sb strings.Builder
sb.WriteString(string(cn.Spec.SubnetRouter.Routes[0]))
for _, r := range cn.Spec.SubnetRouter.Routes[1:] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

control (or tailscaled, i forget which) rejects routes that have pfx.Masked() != pfx, we might want to check that here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to eventually be able to put this into the validation at apply time, but CEL does not have IP parsing, not sure if this would be too expensive for them to ever do it.

Comment thread cmd/k8s-operator/operator.go Outdated
//go:generate go run tailscale.com/cmd/k8s-operator/generate

// Generate Connector CustomResourceDefinition yaml from its Go types.
//go:generate go run sigs.k8s.io/controller-tools/cmd/controller-gen crd schemapatch:manifests=./deploy/crds output:dir=./deploy/crds paths=../../k8s-operator/apis/...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think because of this go:generate line you don't need an additional .github action. but fine as is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The github action is to test that the generated code is up to date, without it we could still push a PR with yamls out of date wrt the Go types- or you meant something else?

Signed-off-by: Irbe Krumina <[email protected]>
@irbekrm
irbekrm merged commit 1a08ea5 into main Dec 14, 2023
@irbekrm
irbekrm deleted the irbekrm/subnetrouter branch December 14, 2023 13:52
chen8945 pushed a commit to Ckid-Home/tailscale that referenced this pull request Jul 31, 2024
* k8s-operator,cmd/k8s-operator,Makefile,scripts,.github/workflows: add Connector kube CRD.

Connector CRD allows users to configure the Tailscale Kubernetes operator
to deploy a subnet router to expose cluster CIDRs or
other CIDRs available from within the cluster
to their tailnet.

Also adds various CRD related machinery to
generate CRD YAML, deep copy implementations etc.

Engineers will now have to run
'make kube-generate-all` after changing kube files
to ensure that all generated files are up to date.

* cmd/k8s-operator,k8s-operator: reconcile Connector resources

Reconcile Connector resources, create/delete subnetrouter resources in response to changes to Connector(s).

Connector reconciler will not be started unless
ENABLE_CONNECTOR env var is set to true.
This means that users who don't want to use the alpha
Connector custom resource don't have to install the Connector
CRD to their cluster.
For users who do want to use it the flow is:
- install the CRD
- install the operator (via Helm chart or using static manifests).
For Helm users set .values.enableConnector to true, for static
manifest users, set ENABLE_CONNECTOR to true in the static manifest.

Updates tailscale#502


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.

3 participants