@@ -25,6 +25,7 @@ import (
25
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26
26
"k8s.io/apimachinery/pkg/runtime"
27
27
"k8s.io/apimachinery/pkg/runtime/schema"
28
+ "k8s.io/apimachinery/pkg/types"
28
29
"k8s.io/apimachinery/pkg/util/yaml"
29
30
"k8s.io/utils/ptr"
30
31
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -53,6 +54,11 @@ type Deployer struct {
53
54
type ControlPlaneInfo struct {
54
55
XdsHost string
55
56
XdsPort int32
57
+ // The data in this struct is static, so is a good place to keep track of if mtls is enabled
58
+ // and a bad place to store the actual mtls secret data
59
+ GlooMtlsEnabled bool
60
+ // We could lookup the pod namespace from the env, but it's cleaner to pass it in
61
+ Namespace string
56
62
}
57
63
58
64
type AwsInfo struct {
@@ -103,17 +109,21 @@ func (d *Deployer) GetGvksToWatch(ctx context.Context) ([]schema.GroupVersionKin
103
109
// as we only care about the GVKs of the rendered resources)
104
110
// - the minimal values that render all the proxy resources (HPA is not included because it's not
105
111
// fully integrated/working at the moment)
112
+ // - a flag to indicate whether mtls is enabled, so we can render the secret if needed
106
113
//
107
114
// Note: another option is to hardcode the GVKs here, but rendering the helm chart is a
108
115
// _slightly_ more dynamic way of getting the GVKs. It isn't a perfect solution since if
109
116
// we add more resources to the helm chart that are gated by a flag, we may forget to
110
117
// update the values here to enable them.
118
+ // Currently the only resource that is gated by a flag is the mtls secret.
119
+
111
120
emptyGw := & api.Gateway {
112
121
ObjectMeta : metav1.ObjectMeta {
113
122
Name : "default" ,
114
123
Namespace : "default" ,
115
124
},
116
125
}
126
+
117
127
// TODO(Law): these must be set explicitly as we don't have defaults for them
118
128
// and the internal template isn't robust enough.
119
129
// This should be empty eventually -- the template must be resilient against nil-pointers
@@ -124,13 +134,20 @@ func (d *Deployer) GetGvksToWatch(ctx context.Context) ([]schema.GroupVersionKin
124
134
"enabled" : false ,
125
135
},
126
136
"image" : map [string ]any {},
137
+ // Render the secret based on the mtls flag so we can watch it.
138
+ // This is an exception to the "TODO" above as this is not protection against nil-pointers,
139
+ // it is determining which resources to render based on ControlPlane configuration.
140
+ "glooMtls" : map [string ]any {
141
+ "renderSecret" : d .inputs .ControlPlane .GlooMtlsEnabled ,
142
+ },
127
143
},
128
144
}
129
145
130
146
objs , err := d .renderChartToObjects (emptyGw , vals )
131
147
if err != nil {
132
148
return nil , err
133
149
}
150
+
134
151
var ret []schema.GroupVersionKind
135
152
for _ , obj := range objs {
136
153
gvk := obj .GetObjectKind ().GroupVersionKind ()
@@ -255,7 +272,7 @@ func (d *Deployer) getGatewayClassFromGateway(ctx context.Context, gw *api.Gatew
255
272
return gwc , nil
256
273
}
257
274
258
- func (d * Deployer ) getValues (gw * api.Gateway , gwParam * v1alpha1.GatewayParameters ) (* helmConfig , error ) {
275
+ func (d * Deployer ) getValues (ctx context. Context , gw * api.Gateway , gwParam * v1alpha1.GatewayParameters ) (* helmConfig , error ) {
259
276
// construct the default values
260
277
vals := & helmConfig {
261
278
Gateway : & helmGateway {
@@ -361,9 +378,61 @@ func (d *Deployer) getValues(gw *api.Gateway, gwParam *v1alpha1.GatewayParameter
361
378
362
379
gateway .Stats = getStatsValues (statsConfig )
363
380
381
+ // mtls values
382
+ gateway .GlooMtls , err = d .getHelmMtlsConfig (ctx )
383
+ if err != nil {
384
+ return nil , err
385
+ }
386
+
364
387
return vals , nil
365
388
}
366
389
390
+ func (d * Deployer ) getHelmMtlsConfig (ctx context.Context ) (* helmMtlsConfig , error ) {
391
+
392
+ if ! d .inputs .ControlPlane .GlooMtlsEnabled {
393
+ return & helmMtlsConfig {
394
+ Enabled : ptr .To (false ),
395
+ }, nil
396
+ }
397
+
398
+ helmTls , err := d .getHelmTlsSecretData (ctx )
399
+
400
+ if err != nil {
401
+ return nil , err
402
+ }
403
+
404
+ return & helmMtlsConfig {
405
+ Enabled : ptr .To (true ),
406
+ TlsSecret : helmTls ,
407
+ }, nil
408
+ }
409
+
410
+ // getHelmTlsSecretData builds a helmTls object built from the gloo-mtls-certs secret data, which it fetches
411
+ // This function does not check if mtls is enabled, and a missing secret will return an error via getGlooMtlsCertsSecret
412
+ func (d * Deployer ) getHelmTlsSecretData (ctx context.Context ) (* helmTlsSecretData , error ) {
413
+
414
+ mtlsSecret := & corev1.Secret {}
415
+ mtlsSecretNns := types.NamespacedName {
416
+ Name : wellknown .GlooMtlsCertName ,
417
+ Namespace : d .inputs .ControlPlane .Namespace ,
418
+ }
419
+ err := d .cli .Get (ctx , mtlsSecretNns , mtlsSecret )
420
+
421
+ if err != nil {
422
+ return nil , eris .Wrap (err , "failed to get gloo mtls secret" )
423
+ }
424
+
425
+ if mtlsSecret .Type != corev1 .SecretTypeTLS {
426
+ return nil , eris .New (fmt .Sprintf ("unexpected secret type, expected %s and got %s" , corev1 .SecretTypeTLS , mtlsSecret .Type ))
427
+ }
428
+
429
+ return & helmTlsSecretData {
430
+ TlsCert : mtlsSecret .Data [corev1 .TLSCertKey ],
431
+ TlsKey : mtlsSecret .Data [corev1 .TLSPrivateKeyKey ],
432
+ CaCert : mtlsSecret .Data [corev1 .ServiceAccountRootCAKey ],
433
+ }, nil
434
+ }
435
+
367
436
// Render relies on a `helm install` to render the Chart with the injected values
368
437
// It returns the list of Objects that are rendered, and an optional error if rendering failed,
369
438
// or converting the rendered manifests to objects failed.
@@ -392,6 +461,7 @@ func (d *Deployer) Render(name, ns string, vals map[string]any) ([]client.Object
392
461
if err != nil {
393
462
return nil , fmt .Errorf ("failed to convert helm manifest yaml to objects for gateway %s.%s: %w" , ns , name , err )
394
463
}
464
+
395
465
return objs , nil
396
466
}
397
467
@@ -405,6 +475,8 @@ func (d *Deployer) Render(name, ns string, vals map[string]any) ([]client.Object
405
475
//
406
476
// * returns the objects to be deployed by the caller
407
477
func (d * Deployer ) GetObjsToDeploy (ctx context.Context , gw * api.Gateway ) ([]client.Object , error ) {
478
+ logger := log .FromContext (ctx )
479
+
408
480
gwParam , err := d .getGatewayParametersForGateway (ctx , gw )
409
481
if err != nil {
410
482
return nil , err
@@ -414,9 +486,7 @@ func (d *Deployer) GetObjsToDeploy(ctx context.Context, gw *api.Gateway) ([]clie
414
486
return nil , nil
415
487
}
416
488
417
- logger := log .FromContext (ctx )
418
-
419
- vals , err := d .getValues (gw , gwParam )
489
+ vals , err := d .getValues (ctx , gw , gwParam )
420
490
if err != nil {
421
491
return nil , fmt .Errorf ("failed to get values to render objects for gateway %s.%s: %w" , gw .GetNamespace (), gw .GetName (), err )
422
492
}
0 commit comments