@@ -18,6 +18,7 @@ package readiness
18
18
19
19
import (
20
20
"context"
21
+ "encoding/json"
21
22
"errors"
22
23
"fmt"
23
24
"log/slog"
@@ -26,6 +27,7 @@ import (
26
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
28
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28
29
"k8s.io/apimachinery/pkg/runtime/schema"
30
+ "k8s.io/apimachinery/pkg/types"
29
31
30
32
"github.com/deckhouse/module-sdk/pkg"
31
33
)
@@ -72,6 +74,7 @@ const (
72
74
conditionStatusIsReady = "IsReady"
73
75
modulePhaseReconciling = "Reconciling"
74
76
modulePhaseReady = "Ready"
77
+ modulePhaseHookError = "Error"
75
78
)
76
79
77
80
func CheckModuleReadiness (cfg * ReadinessHookConfig ) func (ctx context.Context , input * pkg.HookInput ) error {
@@ -128,7 +131,7 @@ func CheckModuleReadiness(cfg *ReadinessHookConfig) func(ctx context.Context, in
128
131
return errors .New ("can't find status.phase" )
129
132
}
130
133
131
- if phase != modulePhaseReconciling && phase != modulePhaseReady {
134
+ if phase != modulePhaseReconciling && phase != modulePhaseReady && phase != modulePhaseHookError {
132
135
logger .Debug ("waiting for sustainable phase" , slog .String ("phase" , phase ))
133
136
134
137
return nil
@@ -164,44 +167,45 @@ func CheckModuleReadiness(cfg *ReadinessHookConfig) func(ctx context.Context, in
164
167
condIdx = len (uConditions ) - 1
165
168
}
166
169
167
- if cond ["message" ] == probeMessage && probePhase == phase {
168
- logger .Debug ("condition is unchanged" )
169
- return nil
170
- }
170
+ cond ["lastProbeTime" ] = input .DC .GetClock ().Now ().Format ("2006-01-02T15:04:05Z" )
171
171
172
- if probeStatus != cond ["status" ] {
173
- cond ["lastTransitionTime" ] = input .DC .GetClock ().Now ().Format ("2006-01-02T15:04:05Z" )
174
- }
172
+ if cond ["message" ] != probeMessage || probePhase != phase {
173
+ // if probe status changed - update time
174
+ if probeStatus != cond ["status" ] {
175
+ cond ["lastTransitionTime" ] = input .DC .GetClock ().Now ().Format ("2006-01-02T15:04:05Z" )
176
+ }
175
177
176
- // Update condition
177
- cond ["status" ] = probeStatus
178
+ cond ["status" ] = probeStatus
178
179
179
- cond ["message" ] = probeMessage
180
- if probeMessage == "" {
181
- delete (cond , "message" )
182
- }
180
+ cond ["message" ] = probeMessage
181
+ if probeMessage == "" {
182
+ delete (cond , "message" )
183
+ }
183
184
184
- cond ["reason" ] = probeReason
185
- if probeReason == "" {
186
- delete (cond , "reason" )
185
+ cond ["reason" ] = probeReason
186
+ if probeReason == "" {
187
+ delete (cond , "reason" )
188
+ }
189
+
190
+ // Update module status phase
191
+ phase = probePhase
187
192
}
188
193
189
194
uConditions [condIdx ] = cond
190
- // Update module status phase
191
- phase = probePhase
192
-
193
- // Update module status phase
194
- if err := unstructured .SetNestedField (uModule .Object , phase , "status" , "phase" ); err != nil {
195
- return fmt .Errorf ("failed to change status.phase: %w" , err )
196
- }
197
195
198
- // Update module status conditions
199
- if err := unstructured .SetNestedSlice (uModule .Object , uConditions , "status" , "conditions" ); err != nil {
200
- return fmt .Errorf ("failed to change status.conditions: %w" , err )
196
+ // creating patch
197
+ patch , err := json .Marshal (map [string ]any {
198
+ "status" : map [string ]any {
199
+ "conditions" : uConditions ,
200
+ "phase" : phase ,
201
+ },
202
+ })
203
+ if err != nil {
204
+ return fmt .Errorf ("patch marshal error: %w" , err )
201
205
}
202
206
203
- if _ , err = k8sClient .Dynamic ().Resource (* GetModuleGVK ()).UpdateStatus (ctx , uModule , metav1.UpdateOptions {} ); err != nil {
204
- return fmt .Errorf ("update module resource: %w" , err )
207
+ if _ , err = k8sClient .Dynamic ().Resource (* GetModuleGVK ()).Patch (ctx , cfg . ModuleName , types . MergePatchType , patch , metav1.PatchOptions {}, "status" ); err != nil {
208
+ return fmt .Errorf ("patch module resource: %w" , err )
205
209
}
206
210
207
211
return nil
0 commit comments