Skip to content

Commit

Permalink
add: cloudflared protocol option (#135)
Browse files Browse the repository at this point in the history
Co-authored-by: omega.zeng <[email protected]>
  • Loading branch information
omegazeng and omega.zeng authored Oct 17, 2024
1 parent be107bd commit 649d5af
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
15 changes: 9 additions & 6 deletions cmd/cloudflare-tunnel-ingress-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ type rootCmdFlags struct {
cloudflareAccountId string
cloudflareTunnelName string
namespace string
cloudflaredProtocol string
}

func main() {
var rootLogger = stdr.NewWithOptions(log.New(os.Stderr, "", log.LstdFlags), stdr.Options{LogCaller: stdr.All})

options := rootCmdFlags{
logger: rootLogger.WithName("main"),
ingressClass: "cloudflare-tunnel",
controllerClass: "strrl.dev/cloudflare-tunnel-ingress-controller",
logLevel: 0,
namespace: "default",
logger: rootLogger.WithName("main"),
ingressClass: "cloudflare-tunnel",
controllerClass: "strrl.dev/cloudflare-tunnel-ingress-controller",
logLevel: 0,
namespace: "default",
cloudflaredProtocol: "quic",
}

crlog.SetLogger(rootLogger.WithName("controller-runtime"))
Expand Down Expand Up @@ -100,7 +102,7 @@ func main() {
case <-done:
return
case _ = <-ticker.C:
err := controller.CreateOrUpdateControlledCloudflared(ctx, mgr.GetClient(), tunnelClient, options.namespace)
err := controller.CreateOrUpdateControlledCloudflared(ctx, mgr.GetClient(), tunnelClient, options.namespace, options.cloudflaredProtocol)
if err != nil {
logger.WithName("controlled-cloudflared").Error(err, "create controlled cloudflared")
}
Expand All @@ -120,6 +122,7 @@ func main() {
rootCommand.PersistentFlags().StringVar(&options.cloudflareAccountId, "cloudflare-account-id", options.cloudflareAccountId, "cloudflare account id")
rootCommand.PersistentFlags().StringVar(&options.cloudflareTunnelName, "cloudflare-tunnel-name", options.cloudflareTunnelName, "cloudflare tunnel name")
rootCommand.PersistentFlags().StringVar(&options.namespace, "namespace", options.namespace, "namespace to execute cloudflared connector")
rootCommand.PersistentFlags().StringVar(&options.cloudflaredProtocol, "cloudflared-protocol", options.cloudflaredProtocol, "cloudflared protocol")

err := rootCommand.Execute()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ spec:
- --cloudflare-account-id=$(CLOUDFLARE_ACCOUNT_ID)
- --cloudflare-tunnel-name=$(CLOUDFLARE_TUNNEL_NAME)
- --namespace=$(NAMESPACE)
- --cloudflared-protocol={{ .Values.cloudflared.protocol }}
env:
- name: CLOUDFLARE_API_TOKEN
valueFrom:
Expand Down
19 changes: 11 additions & 8 deletions helm/cloudflare-tunnel-ingress-controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ serviceAccount:

podAnnotations: {}

podSecurityContext: {}
podSecurityContext:
{}
# fsGroup: 2000

securityContext: {}
securityContext:
{}
# capabilities:
# drop:
# - ALL
Expand All @@ -58,12 +60,12 @@ resources:
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi

nodeSelector: {}

Expand All @@ -77,3 +79,4 @@ cloudflared:
pullPolicy: IfNotPresent
tag: latest
replicaCount: 1
protocol: quic
9 changes: 6 additions & 3 deletions pkg/controller/controlled-cloudflared-connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func CreateOrUpdateControlledCloudflared(
kubeClient client.Client,
tunnelClient cloudflarecontroller.TunnelClientInterface,
namespace string,
protocol string,
) error {
logger := log.FromContext(ctx)
list := appsv1.DeploymentList{}
Expand Down Expand Up @@ -62,7 +63,7 @@ func CreateOrUpdateControlledCloudflared(
return errors.Wrap(err, "fetch tunnel token")
}

updatedDeployment := cloudflaredConnectDeploymentTemplating(token, namespace, int32(desiredReplicas))
updatedDeployment := cloudflaredConnectDeploymentTemplating(protocol, token, namespace, int32(desiredReplicas))
existingDeployment.Spec = updatedDeployment.Spec
err = kubeClient.Update(ctx, existingDeployment)
if err != nil {
Expand All @@ -84,7 +85,7 @@ func CreateOrUpdateControlledCloudflared(
return errors.Wrap(err, "invalid replica count")
}

deployment := cloudflaredConnectDeploymentTemplating(token, namespace, int32(replicas))
deployment := cloudflaredConnectDeploymentTemplating(protocol, token, namespace, int32(replicas))
err = kubeClient.Create(ctx, deployment)
if err != nil {
return errors.Wrap(err, "create controlled-cloudflared-connector deployment")
Expand All @@ -93,7 +94,7 @@ func CreateOrUpdateControlledCloudflared(
return nil
}

func cloudflaredConnectDeploymentTemplating(token string, namespace string, replicas int32) *appsv1.Deployment {
func cloudflaredConnectDeploymentTemplating(protocol string, token string, namespace string, replicas int32) *appsv1.Deployment {
appName := "controlled-cloudflared-connector"
image := os.Getenv("CLOUDFLARED_IMAGE")
pullPolicy := os.Getenv("CLOUDFLARED_IMAGE_PULL_POLICY")
Expand Down Expand Up @@ -131,6 +132,8 @@ func cloudflaredConnectDeploymentTemplating(token string, namespace string, repl
ImagePullPolicy: v1.PullPolicy(pullPolicy),
Command: []string{
"cloudflared",
"--protocol",
protocol,
"--no-autoupdate",
"tunnel",
"--metrics",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ var _ = Describe("CreateOrUpdateControlledCloudflared", func() {
},
}

protocol := "quic"

// Act
err = controller.CreateOrUpdateControlledCloudflared(ctx, kubeClient, mockTunnelClient, ns)
err = controller.CreateOrUpdateControlledCloudflared(ctx, kubeClient, mockTunnelClient, ns, protocol)
Expect(err).NotTo(HaveOccurred())

// Assert
Expand Down Expand Up @@ -101,16 +103,18 @@ var _ = Describe("CreateOrUpdateControlledCloudflared", func() {
},
}

protocol := "quic"

// Create initial deployment
err = controller.CreateOrUpdateControlledCloudflared(ctx, kubeClient, mockTunnelClient, ns)
err = controller.CreateOrUpdateControlledCloudflared(ctx, kubeClient, mockTunnelClient, ns, protocol)
Expect(err).NotTo(HaveOccurred())

// Change environment variables
os.Setenv("CLOUDFLARED_REPLICA_COUNT", "3")
os.Setenv("CLOUDFLARED_IMAGE", "cloudflare/cloudflared:2022.3.0")

// Act
err = controller.CreateOrUpdateControlledCloudflared(ctx, kubeClient, mockTunnelClient, ns)
err = controller.CreateOrUpdateControlledCloudflared(ctx, kubeClient, mockTunnelClient, ns, protocol)
Expect(err).NotTo(HaveOccurred())

// Assert
Expand Down

0 comments on commit 649d5af

Please sign in to comment.