@@ -6,10 +6,14 @@ import (
6
6
7
7
"github.com/securesign/operator/internal/controller/common/action"
8
8
"github.com/securesign/operator/internal/controller/common/utils"
9
+ "github.com/securesign/operator/internal/controller/common/utils/kubernetes"
10
+ "github.com/securesign/operator/internal/controller/common/utils/kubernetes/ensure"
9
11
"github.com/securesign/operator/internal/controller/constants"
10
12
"github.com/securesign/operator/internal/controller/labels"
11
13
"github.com/securesign/operator/internal/controller/trillian/actions"
12
14
trillianUtils "github.com/securesign/operator/internal/controller/trillian/utils"
15
+ "golang.org/x/exp/maps"
16
+ apps "k8s.io/api/apps/v1"
13
17
"k8s.io/apimachinery/pkg/api/meta"
14
18
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
19
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -36,60 +40,40 @@ func (i deployAction) CanHandle(_ context.Context, instance *rhtasv1alpha1.Trill
36
40
37
41
func (i deployAction ) Handle (ctx context.Context , instance * rhtasv1alpha1.Trillian ) * action.Result {
38
42
var (
39
- err error
40
- updated bool
43
+ err error
44
+ result controllerutil. OperationResult
41
45
)
42
46
43
47
labels := labels .For (actions .LogServerComponentName , actions .LogserverDeploymentName , instance .Name )
44
- server , err := trillianUtils .CreateLogServerDeployment (ctx , i .Client , instance , constants .TrillianServerImage , actions .LogserverDeploymentName , actions .RBACName , labels )
45
- if err != nil {
46
- return i .Failed (err )
47
- }
48
-
49
- caTrustRef := utils .TrustedCAAnnotationToReference (instance .Annotations )
50
- // override if spec.trustedCA is defined
51
- if instance .Spec .TrustedCA != nil {
52
- caTrustRef = instance .Spec .TrustedCA
53
- }
54
- err = utils .SetTrustedCA (& server .Spec .Template , caTrustRef )
55
-
56
- if err != nil {
57
- meta .SetStatusCondition (& instance .Status .Conditions , metav1.Condition {
58
- Type : actions .ServerCondition ,
59
- Status : metav1 .ConditionFalse ,
60
- Reason : constants .Failure ,
61
- Message : err .Error (),
62
- })
63
- meta .SetStatusCondition (& instance .Status .Conditions , metav1.Condition {
64
- Type : constants .Ready ,
65
- Status : metav1 .ConditionFalse ,
66
- Reason : constants .Failure ,
67
- Message : err .Error (),
68
- })
69
- return i .FailedWithStatusUpdate (ctx , fmt .Errorf ("could not create Trillian server: %w" , err ), instance )
70
- }
48
+ insCopy := instance .DeepCopy ()
71
49
72
- if err = controllerutil . SetControllerReference ( instance , server , i . Client . Scheme ()); err != nil {
73
- return i . Failed ( fmt . Errorf ( "could not set controller reference for server: %w" , err ) )
50
+ if insCopy . Spec . TrustedCA != nil {
51
+ insCopy . Spec . TrustedCA = utils . TrustedCAAnnotationToReference ( instance . Annotations )
74
52
}
75
53
76
- if updated , err = i .Ensure (ctx , server ); err != nil {
77
- meta .SetStatusCondition (& instance .Status .Conditions , metav1.Condition {
54
+ if result , err = kubernetes .CreateOrUpdate (ctx , i .Client ,
55
+ & apps.Deployment {
56
+ ObjectMeta : metav1.ObjectMeta {
57
+ Name : actions .LogserverDeploymentName ,
58
+ Namespace : instance .Namespace ,
59
+ },
60
+ },
61
+ trillianUtils .EnsureServerDeployment (insCopy , constants .TrillianServerImage , actions .LogserverDeploymentName , actions .RBACName , labels ),
62
+ ensure .ControllerReference [* apps.Deployment ](insCopy , i .Client ),
63
+ ensure .Labels [* apps.Deployment ](maps .Keys (labels ), labels ),
64
+ ensure .Proxy (),
65
+ ensure .TrustedCA (insCopy .Spec .TrustedCA ),
66
+ ensure .Optional (trillianUtils .UseTLS (insCopy ), i .withTlsDB (ctx , insCopy )),
67
+ ); err != nil {
68
+ return i .Error (ctx , fmt .Errorf ("could not create Trillian server: %w" , err ), instance , metav1.Condition {
78
69
Type : actions .ServerCondition ,
79
70
Status : metav1 .ConditionFalse ,
80
71
Reason : constants .Failure ,
81
72
Message : err .Error (),
82
73
})
83
- meta .SetStatusCondition (& instance .Status .Conditions , metav1.Condition {
84
- Type : constants .Ready ,
85
- Status : metav1 .ConditionFalse ,
86
- Reason : constants .Failure ,
87
- Message : err .Error (),
88
- })
89
- return i .FailedWithStatusUpdate (ctx , fmt .Errorf ("could not create Trillian server: %w" , err ), instance )
90
74
}
91
75
92
- if updated {
76
+ if result != controllerutil . OperationResultNone {
93
77
meta .SetStatusCondition (& instance .Status .Conditions , metav1.Condition {
94
78
Type : actions .ServerCondition ,
95
79
Status : metav1 .ConditionFalse ,
@@ -101,3 +85,23 @@ func (i deployAction) Handle(ctx context.Context, instance *rhtasv1alpha1.Trilli
101
85
return i .Continue ()
102
86
}
103
87
}
88
+
89
+ func (i deployAction ) withTlsDB (ctx context.Context , instance * rhtasv1alpha1.Trillian ) func (deployment * apps.Deployment ) error {
90
+ return func (dp * apps.Deployment ) error {
91
+ caPath , err := trillianUtils .CAPath (ctx , i .Client , instance )
92
+ if err != nil {
93
+ return fmt .Errorf ("failed to get CA path: %w" , err )
94
+ }
95
+
96
+ c := kubernetes .FindContainerByNameOrCreate (& dp .Spec .Template .Spec , actions .LogserverDeploymentName )
97
+ c .Args = append (c .Args , "--mysql_tls_ca" , caPath )
98
+
99
+ mysqlServerName := "$(MYSQL_HOSTNAME)." + instance .Namespace + ".svc"
100
+ if ! * instance .Spec .Db .Create {
101
+ mysqlServerName = "$(MYSQL_HOSTNAME)"
102
+ }
103
+ c .Args = append (c .Args , "--mysql_server_name" , mysqlServerName )
104
+ return nil
105
+ }
106
+
107
+ }
0 commit comments