22
22
import org .apache .flink .kubernetes .operator .api .FlinkBlueGreenDeployment ;
23
23
import org .apache .flink .kubernetes .operator .api .FlinkDeployment ;
24
24
import org .apache .flink .kubernetes .operator .api .bluegreen .DeploymentType ;
25
- import org .apache .flink .kubernetes .operator .api .bluegreen .TransitionMode ;
26
25
import org .apache .flink .kubernetes .operator .api .lifecycle .ResourceLifecycleState ;
27
26
import org .apache .flink .kubernetes .operator .api .spec .FlinkBlueGreenDeploymentSpec ;
28
27
import org .apache .flink .kubernetes .operator .api .spec .JobState ;
33
32
import org .apache .flink .kubernetes .operator .service .FlinkResourceContextFactory ;
34
33
import org .apache .flink .util .Preconditions ;
35
34
36
- import org .apache .flink .shaded .guava31 .com .google .common .collect .ImmutableSet ;
37
-
38
35
import com .fasterxml .jackson .core .JsonProcessingException ;
39
- import io .fabric8 .kubernetes .api .model .Event ;
40
36
import io .fabric8 .kubernetes .api .model .ObjectMeta ;
41
- import io .fabric8 .kubernetes .api .model .OwnerReference ;
42
37
import io .fabric8 .kubernetes .api .model .StatusDetails ;
43
- import io .fabric8 .kubernetes .client .dsl .PodResource ;
44
- import io .fabric8 .kubernetes .client .dsl .Resource ;
45
38
import io .javaoperatorsdk .operator .api .config .informer .InformerConfiguration ;
46
39
import io .javaoperatorsdk .operator .api .reconciler .Context ;
47
40
import io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ;
55
48
import org .slf4j .Logger ;
56
49
import org .slf4j .LoggerFactory ;
57
50
58
- import javax .naming .OperationNotSupportedException ;
59
-
60
- import java .time .Instant ;
61
51
import java .util .List ;
62
52
import java .util .Map ;
63
53
import java .util .Optional ;
64
- import java .util .Set ;
65
- import java .util .stream .Collectors ;
66
- import java .util .stream .Stream ;
67
54
68
55
/** Controller that runs the main reconcile loop for Flink Blue/Green deployments. */
69
56
@ ControllerConfiguration
@@ -101,12 +88,6 @@ public UpdateControl<FlinkBlueGreenDeployment> reconcile(
101
88
FlinkBlueGreenDeployment bgDeployment , Context <FlinkBlueGreenDeployment > josdkContext )
102
89
throws Exception {
103
90
104
- // TODO: this verification is only for FLIP-503, remove later.
105
- if (bgDeployment .getSpec ().getTemplate ().getTransitionMode () != TransitionMode .BASIC ) {
106
- throw new OperationNotSupportedException (
107
- "Only TransitionMode == BASIC is currently supported" );
108
- }
109
-
110
91
FlinkBlueGreenDeploymentStatus deploymentStatus = bgDeployment .getStatus ();
111
92
112
93
if (deploymentStatus == null ) {
@@ -430,61 +411,6 @@ private static void setLastReconciledSpec(
430
411
deploymentStatus .setLastReconciledTimestamp (System .currentTimeMillis ());
431
412
}
432
413
433
- public void logPotentialWarnings (
434
- FlinkDeployment flinkDeployment ,
435
- Context <FlinkBlueGreenDeployment > josdkContext ,
436
- long lastReconciliationTimestamp ) {
437
- // Event reason constants
438
- // https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/events/event.go
439
- Set <String > badEventPatterns =
440
- ImmutableSet .of (
441
- "FAIL" , "EXCEPTION" , "BACKOFF" , "ERROR" , "EVICTION" , "KILL" , "EXCEED" );
442
- Set <String > goodPodPhases = ImmutableSet .of ("PENDING" , "RUNNING" );
443
-
444
- Set <String > podPhases =
445
- getDeploymentPods (josdkContext , flinkDeployment )
446
- .map (p -> p .get ().getStatus ().getPhase ().toUpperCase ())
447
- .collect (Collectors .toSet ());
448
-
449
- podPhases .removeAll (goodPodPhases );
450
-
451
- if (!podPhases .isEmpty ()) {
452
- LOG .warn ("Deployment not healthy, some Pods have the following status: " + podPhases );
453
- }
454
-
455
- List <Event > abnormalEvents =
456
- josdkContext
457
- .getClient ()
458
- .v1 ()
459
- .events ()
460
- .inNamespace (flinkDeployment .getMetadata ().getNamespace ())
461
- .resources ()
462
- .map (Resource ::item )
463
- .filter (e -> !e .getType ().equalsIgnoreCase ("NORMAL" ))
464
- .filter (
465
- e ->
466
- e .getInvolvedObject ()
467
- .getName ()
468
- .contains (flinkDeployment .getMetadata ().getName ()))
469
- .filter (
470
- e ->
471
- Instant .parse (e .getLastTimestamp ()).toEpochMilli ()
472
- > lastReconciliationTimestamp )
473
- .filter (
474
- e ->
475
- badEventPatterns .stream ()
476
- .anyMatch (
477
- p ->
478
- e .getReason ()
479
- .toUpperCase ()
480
- .contains (p )))
481
- .collect (Collectors .toList ());
482
-
483
- if (!abnormalEvents .isEmpty ()) {
484
- LOG .warn ("Abnormal events detected: " + abnormalEvents );
485
- }
486
- }
487
-
488
414
private static Savepoint configureSavepoint (
489
415
FlinkResourceContext <FlinkDeployment > resourceContext ) throws Exception {
490
416
// TODO: if the user specified an initialSavepointPath, use it and skip this?
@@ -533,43 +459,8 @@ private boolean isDeploymentReady(
533
459
FlinkDeployment deployment ,
534
460
Context <FlinkBlueGreenDeployment > josdkContext ,
535
461
FlinkBlueGreenDeploymentStatus deploymentStatus ) {
536
- if (ResourceLifecycleState .STABLE == deployment .getStatus ().getLifecycleState ()
537
- && JobStatus .RUNNING == deployment .getStatus ().getJobStatus ().getState ()) {
538
- // TODO: checking for running pods seems to be redundant, check if this can be removed
539
- int notRunningPods =
540
- (int )
541
- getDeploymentPods (josdkContext , deployment )
542
- .filter (
543
- p ->
544
- !p .get ()
545
- .getStatus ()
546
- .getPhase ()
547
- .equalsIgnoreCase ("RUNNING" ))
548
- .count ();
549
-
550
- if (notRunningPods > 0 ) {
551
- LOG .warn ("Waiting for " + notRunningPods + " Pods to transition to RUNNING status" );
552
- }
553
-
554
- return notRunningPods == 0 ;
555
- }
556
-
557
- logPotentialWarnings (
558
- deployment , josdkContext , deploymentStatus .getLastReconciledTimestamp ());
559
- return false ;
560
- }
561
-
562
- private static Stream <PodResource > getDeploymentPods (
563
- Context <FlinkBlueGreenDeployment > josdkContext , FlinkDeployment deployment ) {
564
- var namespace = deployment .getMetadata ().getNamespace ();
565
- var deploymentName = deployment .getMetadata ().getName ();
566
-
567
- return josdkContext
568
- .getClient ()
569
- .pods ()
570
- .inNamespace (namespace )
571
- .withLabel ("app" , deploymentName )
572
- .resources ();
462
+ return ResourceLifecycleState .STABLE == deployment .getStatus ().getLifecycleState ()
463
+ && JobStatus .RUNNING == deployment .getStatus ().getJobStatus ().getState ();
573
464
}
574
465
575
466
private boolean hasSpecChanged (
@@ -578,8 +469,6 @@ private boolean hasSpecChanged(
578
469
String lastReconciledSpec = deploymentStatus .getLastReconciledSpec ();
579
470
String newSpecSerialized = SpecUtils .serializeObject (newSpec , "spec" );
580
471
581
- // TODO: in FLIP-504 check here the TransitionMode has not been changed
582
-
583
472
return !lastReconciledSpec .equals (newSpecSerialized );
584
473
}
585
474
@@ -620,7 +509,7 @@ private void deploy(
620
509
bgMeta .getName () + "-" + deploymentType .toString ().toLowerCase ();
621
510
622
511
FlinkBlueGreenDeploymentSpec adjustedSpec =
623
- adjustNameReferences (
512
+ FlinkBlueGreenDeploymentUtils . adjustNameReferences (
624
513
spec ,
625
514
bgMeta .getName (),
626
515
childDeploymentName ,
@@ -636,7 +525,8 @@ private void deploy(
636
525
flinkDeployment .setSpec (adjustedSpec .getTemplate ().getSpec ());
637
526
638
527
// Deployment metadata
639
- ObjectMeta flinkDeploymentMeta = getDependentObjectMeta (bgDeployment );
528
+ ObjectMeta flinkDeploymentMeta =
529
+ FlinkBlueGreenDeploymentUtils .getDependentObjectMeta (bgDeployment );
640
530
flinkDeploymentMeta .setName (childDeploymentName );
641
531
flinkDeploymentMeta .setLabels (
642
532
Map .of (deploymentType .getClass ().getSimpleName (), deploymentType .toString ()));
@@ -668,36 +558,7 @@ private static void deleteDeployment(
668
558
}
669
559
}
670
560
671
- private ObjectMeta getDependentObjectMeta (FlinkBlueGreenDeployment bgDeployment ) {
672
- ObjectMeta bgMeta = bgDeployment .getMetadata ();
673
- ObjectMeta objectMeta = new ObjectMeta ();
674
- objectMeta .setNamespace (bgMeta .getNamespace ());
675
- objectMeta .setOwnerReferences (
676
- List .of (
677
- new OwnerReference (
678
- bgDeployment .getApiVersion (),
679
- true ,
680
- false ,
681
- bgDeployment .getKind (),
682
- bgMeta .getName (),
683
- bgMeta .getUid ())));
684
- return objectMeta ;
685
- }
686
-
687
- private static <T > T adjustNameReferences (
688
- T spec ,
689
- String deploymentName ,
690
- String childDeploymentName ,
691
- String wrapperKey ,
692
- Class <T > valueType )
693
- throws JsonProcessingException {
694
- String serializedSpec = SpecUtils .serializeObject (spec , wrapperKey );
695
- String replacedSerializedSpec = serializedSpec .replace (deploymentName , childDeploymentName );
696
- return SpecUtils .deserializeObject (replacedSerializedSpec , wrapperKey , valueType );
697
- }
698
-
699
561
public static void logAndThrow (String message ) {
700
- LOG .error (message );
701
562
throw new RuntimeException (message );
702
563
}
703
564
}
0 commit comments