Skip to content

Commit 568bcdb

Browse files
committed
add CA to Otel exporter on startup
1 parent ca43a0e commit 568bcdb

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

cmd/nginx-ingress/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,18 @@ func main() {
182182
if err != nil {
183183
logEventAndExit(ctx, eventRecorder, pod, secretErrorReason, err)
184184
}
185+
185186
globalConfigurationValidator := createGlobalConfigurationValidator()
186187

187188
mustProcessGlobalConfiguration(ctx)
188189

189190
cfgParams := configs.NewDefaultConfigParams(ctx, *nginxPlus)
190191
cfgParams = processConfigMaps(kubeClient, cfgParams, nginxManager, templateExecutor, eventRecorder)
191192

193+
if err := processOtelTrustedCertSecret(kubeClient, nginxManager, cfgParams, controllerNamespace); err != nil {
194+
logEventAndExit(ctx, eventRecorder, pod, secretErrorReason, err)
195+
}
196+
192197
staticCfgParams := &configs.StaticConfigParams{
193198
DisableIPV6: *disableIPV6,
194199
DefaultHTTPListenerPort: *defaultHTTPListenerPort,
@@ -385,6 +390,23 @@ func processMgmtTrustedCertSecret(kubeClient *kubernetes.Clientset, nginxManager
385390
return nil
386391
}
387392

393+
func processOtelTrustedCertSecret(kubeClient *kubernetes.Clientset, nginxManager nginx.Manager, cfgParams *configs.ConfigParams, controllerNamespace string) error {
394+
if cfgParams.MainOtelExporterTrustedCA == "" {
395+
return nil
396+
}
397+
398+
trustedCertSecretNsName := controllerNamespace + "/" + cfgParams.MainOtelExporterTrustedCA
399+
400+
secret, err := getAndValidateSecret(kubeClient, trustedCertSecretNsName, secrets.SecretTypeCA)
401+
if err != nil {
402+
return fmt.Errorf("error trying to get the trusted cert secret %v: %w", trustedCertSecretNsName, err)
403+
}
404+
405+
caBytes, _ := configs.GenerateCAFileContent(secret)
406+
nginxManager.CreateSecret(fmt.Sprintf("%s-%s-%s", controllerNamespace, cfgParams.MainOtelExporterTrustedCA, configs.CACrtKey), caBytes, nginx.ReadWriteOnlyFileMode)
407+
return nil
408+
}
409+
388410
func mustCreateConfigAndKubeClient(ctx context.Context) (*rest.Config, *kubernetes.Clientset) {
389411
l := nl.LoggerFromContext(ctx)
390412
var config *rest.Config

internal/configs/configmaps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ func GenerateNginxMainConfig(staticCfgParams *StaticConfigParams, config *Config
10121012
MainOtelLoadModule: config.MainOtelLoadModule,
10131013
MainOtelGlobalTraceEnabled: config.MainOtelGlobalTraceEnabled,
10141014
MainOtelExporterEndpoint: config.MainOtelExporterEndpoint,
1015-
MainOtelExporterTrustedCA: config.MainOtelExporterTrustedCA,
1015+
MainOtelExporterTrustedCA: fmt.Sprintf("%s-%s-%s", os.Getenv("POD_NAMESPACE"), config.MainOtelExporterTrustedCA, CACrtKey),
10161016
MainOtelExporterHeaderName: config.MainOtelExporterHeaderName,
10171017
MainOtelExporterHeaderValue: config.MainOtelExporterHeaderValue,
10181018
MainOtelServiceName: config.MainOtelServiceName,

internal/configs/version1/nginx-plus.tmpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ http {
146146
ssl_dhparam {{.SSLDHParam}};
147147
{{- end}}
148148

149-
{{- if .MainOtelLoadModule}}
149+
{{- if .MainOtelLoadModule }}
150150
otel_exporter {
151-
endpoint {{ .MainOtelExporterEndpoint}};
152-
{{ if and .MainOtelExporterHeaderName .MainOtelExporterHeaderValue }}
151+
endpoint {{ .MainOtelExporterEndpoint }};
152+
{{- if and .MainOtelExporterHeaderName .MainOtelExporterHeaderValue }}
153153
header {{ .MainOtelExporterHeaderName }} "{{ .MainOtelExporterHeaderValue }}";
154154
{{- end }}
155-
{{ if .MainOtelExporterTrustedCA}}
156-
# trusted_certificate <path>;
155+
{{- if .MainOtelExporterTrustedCA }}
156+
trusted_certificate /etc/nginx/secrets/{{ .MainOtelExporterTrustedCA }};
157157
{{- end }}
158158
}
159159

internal/configs/version1/nginx.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,14 @@ http {
108108
ssl_dhparam {{.SSLDHParam}};
109109
{{- end}}
110110

111-
{{- if .MainOtelLoadModule}}
111+
{{- if .MainOtelLoadModule }}
112112
otel_exporter {
113-
endpoint {{ .MainOtelExporterEndpoint}};
113+
endpoint {{ .MainOtelExporterEndpoint }};
114114
{{- if and .MainOtelExporterHeaderName .MainOtelExporterHeaderValue }}
115115
header {{ .MainOtelExporterHeaderName }} "{{ .MainOtelExporterHeaderValue }}";
116116
{{- end }}
117-
{{- if .MainOtelExporterTrustedCA}}
118-
# trusted_certificate <path>;
117+
{{- if .MainOtelExporterTrustedCA }}
118+
trusted_certificate /etc/nginx/secrets/{{ .MainOtelExporterTrustedCA }};
119119
{{- end }}
120120
}
121121

0 commit comments

Comments
 (0)