Skip to content

Commit 81c9fa4

Browse files
committed
Enable TLS profile configuration for TektonResult
Activate the TLS profile infrastructure for the Results component: - extension.go: Add injectTLSConfig transformer that injects TLS_MIN_VERSION and TLS_CIPHER_SUITES env vars into the Results API deployment based on cluster APIServer configuration. TLS config is fetched once during Transformers() call, not per-resource. - controller.go: Set up APIServer watch to trigger reconciliation when the cluster TLS security profile changes. The Results API deployment will automatically pick up TLS configuration from the OpenShift APIServer resource and update when it changes.
1 parent 06d7b5c commit 81c9fa4

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

pkg/reconciler/openshift/tektonresult/controller.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,41 @@ package tektonresult
1919
import (
2020
"context"
2121

22+
tektonResultInformer "github.com/tektoncd/operator/pkg/client/injection/informers/operator/v1alpha1/tektonresult"
23+
"github.com/tektoncd/operator/pkg/reconciler/common"
24+
occommon "github.com/tektoncd/operator/pkg/reconciler/openshift/common"
2225
k8s_ctrl "github.com/tektoncd/operator/pkg/reconciler/kubernetes/tektonresult"
2326
"knative.dev/pkg/configmap"
2427
"knative.dev/pkg/controller"
28+
"knative.dev/pkg/injection"
29+
"knative.dev/pkg/logging"
2530
)
2631

2732
// NewController initializes the controller and is called by the generated code
2833
// Registers eventhandlers to enqueue events
2934
func NewController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
30-
return k8s_ctrl.NewExtendedController(OpenShiftExtension)(ctx, cmw)
35+
return NewExtendedController(OpenShiftExtension)(ctx, cmw)
36+
}
37+
38+
// NewExtendedController wraps the base Kubernetes controller and adds OpenShift-specific watches
39+
func NewExtendedController(generator common.ExtensionGenerator) func(context.Context, configmap.Watcher) *controller.Impl {
40+
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
41+
logger := logging.FromContext(ctx)
42+
43+
// Create the base Kubernetes controller with OpenShift extension
44+
impl := k8s_ctrl.NewExtendedController(generator)(ctx, cmw)
45+
46+
// Setup OpenShift APIServer watch for TLS profile changes.
47+
// This will trigger reconciliation when the cluster TLS policy changes.
48+
// Errors are logged by SetupAPIServerTLSWatch; we don't fail controller startup.
49+
restConfig := injection.GetConfig(ctx)
50+
lister := tektonResultInformer.Get(ctx).Lister()
51+
listerAdapter := occommon.TektonResultListerAdapter{Lister: lister}
52+
53+
if err := occommon.SetupAPIServerTLSWatch(ctx, restConfig, impl, listerAdapter, "TektonResult"); err == nil {
54+
logger.Info("APIServer TLS profile watch enabled")
55+
}
56+
57+
return impl
58+
}
3159
}

pkg/reconciler/openshift/tektonresult/extension.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
corev1 "k8s.io/api/core/v1"
3333
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3434
k8sruntime "k8s.io/apimachinery/pkg/runtime"
35+
"k8s.io/client-go/rest"
36+
"knative.dev/pkg/injection"
3537
"knative.dev/pkg/logging"
3638
)
3739

@@ -71,11 +73,16 @@ func OpenShiftExtension(ctx context.Context) common.Extension {
7173
logger.Fatalf("Failed to fetch logs RBAC manifest: %v", err)
7274
}
7375

76+
// Get the rest.Config from the context for accessing OpenShift APIServer
77+
restConfig := injection.GetConfig(ctx)
78+
7479
ext := &openshiftExtension{
7580
installerSetClient: client.NewInstallerSetClient(operatorclient.Get(ctx).OperatorV1alpha1().TektonInstallerSets(),
7681
version, "results-ext", v1alpha1.KindTektonResult, nil),
7782
routeManifest: routeManifest,
7883
logsRBACManifest: logsRBACManifest,
84+
restConfig: restConfig,
85+
ctx: ctx,
7986
}
8087
return ext
8188
}
@@ -84,12 +91,15 @@ type openshiftExtension struct {
8491
installerSetClient *client.InstallerSetClient
8592
routeManifest *mf.Manifest
8693
logsRBACManifest *mf.Manifest
94+
restConfig *rest.Config
95+
ctx context.Context
8796
}
8897

8998
func (oe openshiftExtension) Transformers(comp v1alpha1.TektonComponent) []mf.Transformer {
9099
instance := comp.(*v1alpha1.TektonResult)
100+
logger := logging.FromContext(oe.ctx)
91101

92-
return []mf.Transformer{
102+
transformers := []mf.Transformer{
93103
occommon.RemoveRunAsUser(),
94104
occommon.RemoveRunAsGroup(),
95105
occommon.ApplyCABundlesToDeployment,
@@ -101,6 +111,16 @@ func (oe openshiftExtension) Transformers(comp v1alpha1.TektonComponent) []mf.Tr
101111
injectResultsAPIServiceCACert(instance.Spec.ResultsAPIProperties),
102112
injectPostgresUpgradeSupport(),
103113
}
114+
115+
// Fetch TLS configuration once and create the transformer if available
116+
tlsEnvVars, err := occommon.GetTLSEnvVarsFromAPIServer(oe.ctx, oe.restConfig)
117+
if err != nil {
118+
logger.Warnf("Failed to get TLS configuration from APIServer: %v", err)
119+
} else if tlsEnvVars != nil {
120+
transformers = append(transformers, injectTLSConfig(tlsEnvVars))
121+
}
122+
123+
return transformers
104124
}
105125

106126
func (oe *openshiftExtension) PreReconcile(ctx context.Context, tc v1alpha1.TektonComponent) error {
@@ -470,3 +490,69 @@ func injectPostgresUpgradeSupport() mf.Transformer {
470490
return nil
471491
}
472492
}
493+
494+
// injectTLSConfig injects the TLS configuration as environment variables into the Results API deployment
495+
func injectTLSConfig(tlsEnvVars *occommon.TLSEnvVars) mf.Transformer {
496+
return func(u *unstructured.Unstructured) error {
497+
if u.GetKind() != "Deployment" || u.GetName() != deploymentAPI {
498+
return nil
499+
}
500+
501+
d := &appsv1.Deployment{}
502+
if err := k8sruntime.DefaultUnstructuredConverter.FromUnstructured(u.Object, d); err != nil {
503+
return err
504+
}
505+
506+
for i, container := range d.Spec.Template.Spec.Containers {
507+
if container.Name != apiContainerName {
508+
continue
509+
}
510+
511+
envVars := []corev1.EnvVar{}
512+
if tlsEnvVars.MinVersion != "" {
513+
envVars = append(envVars, corev1.EnvVar{
514+
Name: occommon.TLSMinVersionEnvVar,
515+
Value: tlsEnvVars.MinVersion,
516+
})
517+
}
518+
if tlsEnvVars.CipherSuites != "" {
519+
envVars = append(envVars, corev1.EnvVar{
520+
Name: occommon.TLSCipherSuitesEnvVar,
521+
Value: tlsEnvVars.CipherSuites,
522+
})
523+
}
524+
// CurvePreferences will be populated once openshift/api#2583 is merged
525+
if tlsEnvVars.CurvePreferences != "" {
526+
envVars = append(envVars, corev1.EnvVar{
527+
Name: occommon.TLSCurvePreferencesEnvVar,
528+
Value: tlsEnvVars.CurvePreferences,
529+
})
530+
}
531+
532+
// Merge with existing env vars
533+
existingEnv := container.Env
534+
for _, newEnv := range envVars {
535+
found := false
536+
for j, existing := range existingEnv {
537+
if existing.Name == newEnv.Name {
538+
existingEnv[j] = newEnv
539+
found = true
540+
break
541+
}
542+
}
543+
if !found {
544+
existingEnv = append(existingEnv, newEnv)
545+
}
546+
}
547+
d.Spec.Template.Spec.Containers[i].Env = existingEnv
548+
break
549+
}
550+
551+
uObj, err := k8sruntime.DefaultUnstructuredConverter.ToUnstructured(d)
552+
if err != nil {
553+
return err
554+
}
555+
u.SetUnstructuredContent(uObj)
556+
return nil
557+
}
558+
}

0 commit comments

Comments
 (0)