@@ -4,12 +4,15 @@ import (
4
4
"context"
5
5
"errors"
6
6
7
- "github.com/cloudflare/cloudflare-go"
7
+ "github.com/cloudflare/cloudflare-go/v2"
8
+ "github.com/cloudflare/cloudflare-go/v2/shared"
9
+ "github.com/cloudflare/cloudflare-go/v2/zero_trust"
8
10
apps "k8s.io/api/apps/v1"
9
11
core "k8s.io/api/core/v1"
10
12
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
13
"k8s.io/apimachinery/pkg/runtime"
12
14
"k8s.io/apimachinery/pkg/types"
15
+ "k8s.io/apimachinery/pkg/util/intstr"
13
16
ctrl "sigs.k8s.io/controller-runtime"
14
17
"sigs.k8s.io/controller-runtime/pkg/client"
15
18
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -109,18 +112,19 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
109
112
log .Error (err , "Failed to delete Deployment" )
110
113
}
111
114
112
- tunnel , tunnelInfo , err := api .ListTunnels (ctx , account , cloudflare.TunnelListParams {
113
- IsDeleted : cloudflare .BoolPtr (false ),
114
- Name : gateway .Name ,
115
+ tunnel , err := api .ZeroTrust .Tunnels .List (ctx , zero_trust.TunnelListParams {
116
+ AccountID : cloudflare .String (account ),
117
+ IsDeleted : cloudflare .Bool (false ),
118
+ Name : cloudflare .String (gateway .Name ),
115
119
})
116
120
if err != nil {
117
121
log .Error (err , "Failed to get tunnel from Cloudflare API" )
118
122
return ctrl.Result {}, err
119
123
}
120
124
121
- if tunnelInfo . Count > 0 {
125
+ if len ( tunnel . Result ) > 0 {
122
126
log .Info ("Deleting Tunnel" )
123
- if err := api .DeleteTunnel (ctx , account , tunnel [0 ].ID ); err != nil {
127
+ if _ , err := api .ZeroTrust . Tunnels . Delete (ctx , tunnel . Result [0 ].ID , zero_trust. TunnelDeleteParams {} ); err != nil {
124
128
log .Error (err , "Failed to delete tunnel Deployment" )
125
129
return ctrl.Result {}, err
126
130
}
@@ -150,25 +154,30 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
150
154
return ctrl.Result {}, nil
151
155
}
152
156
153
- tunnels , info , err := api .ListTunnels (ctx , account , cloudflare.TunnelListParams {IsDeleted : cloudflare .BoolPtr (false ), Name : gateway .Name })
157
+ tunnels , err := api .ZeroTrust .Tunnels .List (ctx , zero_trust.TunnelListParams {
158
+ AccountID : cloudflare .String (account ),
159
+ IsDeleted : cloudflare .Bool (false ),
160
+ Name : cloudflare .String (gateway .Name ),
161
+ })
154
162
if err != nil {
155
163
log .Error (err , "Failed to get Tunnel from Cloudflare API" )
156
164
return ctrl.Result {}, err
157
165
}
158
166
159
- tunnel := cloudflare. Tunnel {}
160
- if info . Count == 0 {
167
+ tunnelID := ""
168
+ if len ( tunnels . Result ) == 0 {
161
169
log .Info ("Creating tunnel" )
162
170
// secret is required, despite optional in docs and seemingly only needed for ConfigSrc=local
163
- tunnel , err = api .CreateTunnel (ctx , account , cloudflare. TunnelCreateParams {
164
- Name : gateway . Name ,
165
- ConfigSrc : " cloudflare" ,
166
- Secret : "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg=" ,
171
+ tunnel , err : = api .ZeroTrust . Tunnels . New (ctx , zero_trust. TunnelNewParams {
172
+ AccountID : cloudflare . String ( account ) ,
173
+ Name : cloudflare . String ( gateway . Name ) ,
174
+ TunnelSecret : cloudflare . String ( "AQIDBAUGBwgBAgMEBQYHCAECAwQFBgcIAQIDBAUGBwg=" ) ,
167
175
})
168
176
if err != nil {
169
177
log .Error (err , "Failed to create tunnel" )
170
178
return ctrl.Result {}, err
171
179
}
180
+ tunnelID = tunnel .ID
172
181
} else {
173
182
// patch unsupported with api_token
174
183
// if tunnels[0].Name != gateway.Name {
@@ -181,47 +190,60 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
181
190
// }
182
191
// }
183
192
log .Info ("Tunnel exists" )
184
- tunnel = tunnels [0 ]
193
+ tunnelID = tunnels . Result [0 ]. ID
185
194
}
186
195
187
- token , err := api .GetTunnelToken (ctx , account , tunnel .ID )
196
+ res , err := api .ZeroTrust .Tunnels .Token .Get (ctx , tunnelID , zero_trust.TunnelTokenGetParams {
197
+ AccountID : cloudflare .String (account ),
198
+ })
188
199
if err != nil {
189
200
log .Error (err , "Failed to get tunnel token" )
190
201
return ctrl.Result {}, err
191
202
}
203
+ token := string ((* res ).(shared.UnionString ))
192
204
193
- if err := r .Get (ctx , types.NamespacedName {
194
- Namespace : gateway .Namespace ,
195
- Name : gateway .Name ,
196
- }, & apps.Deployment {}); err == nil {
197
- log .Info ("Tunnel deployment exists" )
198
- return ctrl.Result {}, nil
199
- }
200
-
201
- labels := map [string ]string {"cfargotunnel.com/name" : gateway .Name }
202
- deployment := apps.Deployment {
205
+ deployment := & apps.Deployment {
203
206
ObjectMeta : v1.ObjectMeta {
204
207
Namespace : gateway .Namespace ,
205
208
Name : gateway .Name ,
206
209
},
207
- Spec : apps.DeploymentSpec {
210
+ }
211
+ if _ , err := controllerutil .CreateOrUpdate (ctx , r .Client , deployment , func () error {
212
+ labels := map [string ]string {"cfargotunnel.com/name" : gateway .Name }
213
+ deployment .Spec = apps.DeploymentSpec {
208
214
Selector : & v1.LabelSelector {MatchLabels : labels },
209
215
Template : core.PodTemplateSpec {
210
216
ObjectMeta : v1.ObjectMeta {Labels : labels },
211
217
Spec : core.PodSpec {Containers : []core.Container {{
212
- Name : "main" ,
218
+ Name : "main" ,
213
219
// renovate: datasource=docker
214
220
Image : "cloudflare/cloudflared:2024.5.0" ,
215
- Args : []string {"tunnel" , "--no-autoupdate" , "run" , "--token" , token },
221
+ Args : []string {"tunnel" , "--no-autoupdate" , "--metrics" , "0.0.0.0:2000" , "run" , "--token" , token },
222
+ LivenessProbe : & core.Probe {
223
+ FailureThreshold : 5 ,
224
+ InitialDelaySeconds : 10 ,
225
+ PeriodSeconds : 10 ,
226
+ ProbeHandler : core.ProbeHandler {
227
+ HTTPGet : & core.HTTPGetAction {
228
+ Path : "/ready" ,
229
+ Port : intstr .FromInt (2000 ),
230
+ },
231
+ },
232
+ },
216
233
}}},
217
234
},
218
- },
219
- }
220
-
221
- if err := r .Create (ctx , & deployment ); err != nil {
222
- log .Error (err , "Failed to create tunnel deployment" )
235
+ Strategy : apps.DeploymentStrategy {
236
+ RollingUpdate : & apps.RollingUpdateDeployment {
237
+ MaxUnavailable : & intstr.IntOrString {IntVal : 0 },
238
+ },
239
+ },
240
+ }
241
+ return nil
242
+ }); err != nil {
243
+ log .Error (err , "Failed to reconcile tunnel deployment" )
223
244
return ctrl.Result {}, err
224
245
}
246
+ log .Info ("Reconciled tunnel deployment" )
225
247
226
248
return ctrl.Result {}, nil
227
249
}
0 commit comments