8
8
import org .slf4j .Logger ;
9
9
import org .slf4j .LoggerFactory ;
10
10
11
- import java .time .Instant ;
12
11
import java .util .Arrays ;
13
12
import java .util .List ;
14
13
import java .util .stream .Collectors ;
15
14
15
+ import static io .apicurio .registry .operator .utils .Mapper .toYAML ;
16
+ import static java .time .Instant .now ;
17
+ import static java .util .Objects .requireNonNull ;
18
+
16
19
public class StatusUpdater {
17
20
18
21
private static final Logger log = LoggerFactory .getLogger (StatusUpdater .class );
@@ -22,79 +25,82 @@ public class StatusUpdater {
22
25
public static final String STARTED_TYPE = "STARTED" ;
23
26
public static final String UNKNOWN_TYPE = "UNKNOWN" ;
24
27
25
- private ApicurioRegistry3 registry ;
28
+ private final ApicurioRegistry3 registry ;
26
29
27
30
public StatusUpdater (ApicurioRegistry3 registry ) {
28
31
this .registry = registry ;
29
32
}
30
33
31
34
private Condition defaultCondition () {
32
35
var condition = new Condition ();
33
- condition .setStatus (ConditionStatus .TRUE );
34
36
condition .setObservedGeneration (
35
37
registry .getMetadata () == null ? null : registry .getMetadata ().getGeneration ());
36
- condition .setLastTransitionTime (Instant . now ());
38
+ condition .setLastTransitionTime (now ());
37
39
return condition ;
38
40
}
39
41
40
- public ApicurioRegistry3Status errorStatus (Exception e ) {
42
+ public void updateWithException (Exception e ) {
43
+ // TODO: Ignore some KubernetesClientException-s that are caused by update conflicts.
41
44
var errorCondition = defaultCondition ();
42
45
errorCondition .setType (ERROR_TYPE );
43
- errorCondition .setMessage (
44
- Arrays .stream (e .getStackTrace ()).map (st -> st .toString ()).collect (Collectors .joining ("\n " )));
46
+ errorCondition .setStatus (ConditionStatus .TRUE );
45
47
errorCondition .setReason ("ERROR_STATUS" );
48
+ errorCondition .setMessage (Arrays .stream (e .getStackTrace ()).map (StackTraceElement ::toString )
49
+ .collect (Collectors .joining ("\n " ))); // TODO: Make the message more useful.
46
50
47
- var status = new ApicurioRegistry3Status ( );
48
- status . setConditions ( List . of ( errorCondition ));
51
+ registry . withStatus (). setConditions ( List . of ( errorCondition ) );
52
+ }
49
53
50
- return status ;
54
+ private Condition getOrCreateCondition (ApicurioRegistry3Status status , String type ) {
55
+ var conditions = status .getConditions ().stream ().filter (c -> type .equals (c .getType ())).toList ();
56
+ if (conditions .size () > 1 ) {
57
+ throw new OperatorException ("Duplicate conditions: " + conditions );
58
+ } else if (conditions .size () == 1 ) {
59
+ return conditions .get (0 );
60
+ } else {
61
+ var newCondition = defaultCondition ();
62
+ newCondition .setType (type );
63
+ status .getConditions ().add (newCondition );
64
+ return newCondition ;
65
+ }
51
66
}
52
67
53
- public ApicurioRegistry3Status next (Deployment deployment ) {
54
- log .debug ("Setting status based on Deployment: " + deployment );
55
- if (deployment != null && deployment .getStatus () != null ) {
56
- if (deployment .getStatus ().getConditions ().stream ()
57
- .anyMatch (condition -> condition .getStatus ().equalsIgnoreCase (
58
- ConditionStatus .TRUE .getValue ()) && condition .getType ().equals ("Available" ))
59
- && !registry .withStatus ().getConditions ().stream ()
60
- .anyMatch (condition -> condition .getType ().equals (READY_TYPE ))) {
61
- var readyCondition = defaultCondition ();
62
- readyCondition .setType (READY_TYPE );
63
- readyCondition .setMessage ("Deployment is available" );
64
- readyCondition .setReason ("READY_STATUS" );
65
- var conditions = registry .getStatus ().getConditions ();
66
- conditions .add (readyCondition );
67
- return registry .getStatus ();
68
- } else if (deployment .getStatus ().getConditions ().size () > 0
69
- && !registry .withStatus ().getConditions ().stream ()
70
- .anyMatch (condition -> condition .getStatus ().getValue ().equals (STARTED_TYPE ))) {
71
- var generation = registry .getMetadata () == null ? null
72
- : registry .getMetadata ().getGeneration ();
73
- var nextCondition = defaultCondition ();
74
- nextCondition .setType (STARTED_TYPE );
75
- nextCondition .setMessage ("Deployment conditions:\n " + deployment .getStatus ().getConditions ()
76
- .stream ().map (dc -> dc .getType ()).collect (Collectors .joining ("\n " )));
77
- nextCondition .setReason ("DEPLOYMENT_STARTED" );
78
-
79
- var status = new ApicurioRegistry3Status ();
80
- status .setConditions (List .of (nextCondition ));
81
-
82
- return status ;
83
- } else {
84
- var nextCondition = defaultCondition ();
85
- nextCondition .setType (UNKNOWN_TYPE );
86
- nextCondition .setMessage ("Deployment conditions:\n " + deployment .getStatus ().getConditions ()
87
- .stream ().map (dc -> dc .getType ()).collect (Collectors .joining ("\n " )));
88
- nextCondition .setReason ("UNKNOWN_STATUS" );
89
-
90
- var status = new ApicurioRegistry3Status ();
91
- status .setConditions (List .of (nextCondition ));
92
-
93
- return status ;
94
- }
68
+ public void update (Deployment deployment ) {
69
+ requireNonNull (deployment );
70
+ log .debug ("Setting status based on Deployment:\n {}" , toYAML (deployment .getStatus ()));
71
+
72
+ // Remove error condition if present
73
+ registry .withStatus ().getConditions ().removeIf (c -> ERROR_TYPE .equals (c .getType ()));
74
+
75
+ // Ready
76
+ var readyCondition = getOrCreateCondition (registry .getStatus (), READY_TYPE );
77
+ if (deployment .getStatus () != null && deployment .getStatus ().getConditions ().stream ()
78
+ .anyMatch (condition -> condition .getType ().equals ("Available" )
79
+ && condition .getStatus ().equalsIgnoreCase (ConditionStatus .TRUE .getValue ()))) {
80
+ readyCondition .setStatus (ConditionStatus .TRUE );
81
+ readyCondition .setReason ("DEPLOYMENT_AVAILABLE" ); // TODO: Constants
82
+ readyCondition .setMessage ("App Deployment is available." ); // TODO: What about other components?
83
+ } else {
84
+ readyCondition .setStatus (ConditionStatus .FALSE );
85
+ readyCondition .setReason ("DEPLOYMENT_NOT_AVAILABLE" );
86
+ readyCondition .setMessage ("App Deployment is not available." );
87
+ }
95
88
89
+ // Started
90
+ var startedCondition = getOrCreateCondition (registry .getStatus (), STARTED_TYPE ); // TODO: Do we
91
+ // actually need
92
+ // this?
93
+ if (deployment .getStatus () != null && deployment .getStatus ().getConditions ().size () > 0 ) {
94
+ startedCondition .setStatus (ConditionStatus .TRUE );
95
+ startedCondition .setReason ("DEPLOYMENT_STARTED" );
96
+ startedCondition .setMessage ("App Deployment conditions: " + deployment .getStatus ().getConditions ()
97
+ .stream ().map (dc -> dc .getType () + " = " + dc .getStatus ())
98
+ .collect (Collectors .joining (", " )) + "." );
96
99
} else {
97
- return errorStatus (new OperatorException ("Expected deployment not found" ));
100
+ startedCondition .setStatus (ConditionStatus .UNKNOWN );
101
+ startedCondition .setReason ("DEPLOYMENT_STATUS_UNKNOWN" );
102
+ startedCondition .setMessage ("No App Deployment conditions available." ); // TODO: This does not
103
+ // make much sense.
98
104
}
99
105
}
100
106
}
0 commit comments