References:

CPS-1005 - Getting issue details... STATUS

Overview:

This POC is for finding ways to create a resource (NCMP for this instance).

Few possibilities were discussed during the Grooming session which are listed as below.

  1. init-container
  2. k8s hooks
  3. Events in Spring.
  4. Sidecar container

Issues and decisions

#

Questions/Open Issues

Notes

Decision/Answer

1Events in Spring
  1. we keep the underlying class in cps-ncmp-service so it will work only when ncmp is installed or cps-and-ncmp are installed and doesn't affect anything when just cps-core is installed.
  2. Use this approach for small amount of data as there can be initial delay and there can be race-condition with other request trying to do some related task.
Use Spring Events when we need to do some one-off task(which doesn't takes too much time)

Analysis

  1. init-container

    Init containers are exactly like regular containers, except:
        -  Init containers always run to completion.
        -  Each init container must complete successfully before the next one starts.

    Since our application container would not be running the before the init-container so we cant invoke any endpoint to create a resource and we dont want to go with DB related script(SQL) to create a resource (insert a row in the database) so I am not exploring this further for this use case.

  2. k8s hooks

    - postStart hook :
    It runs only once and if it fails the container is unable to start. No provision to restart / retry. (So if it runs before the application is not ready then it will eventually fail)

  3. Events in Spring

    We can use the ApplicationReadyEvent which is published when the application is ready to service requests.
    We can listen to this event , which is thrown by the SpringApplication container itself and then execute some code (which is creating a dataspace in our scenario)

    Example

    ApplicationReadyEvent
    @EventListener(ApplicationReadyEvent.class)
        public void initializeNcmp() {
            log.info("Initializing NCMP when the ApplicationReadyEvent is received");
            try {
                cpsAdminService.createDataspace("NCMP-CmHandle-Operational-Data");
            } catch (final AlreadyDefinedException ex) {
                log.info("Already defined : {} ", ex.getMessage());
            } catch (final Exception ex) {
                log.warn("Failed to create dataspace, cause : {}", ex.getMessage());
            }
        }
  4. Sidecar container

     - Wait for the readiness check of the main container to pass and then execute the script to create a dataspace.
     - Looks like we are unable to wait for the other container readiness check and then execute the command. (Connection Refused ) - need more analysis on this.
     - If we are unable to wait for the main container to start then dont go ahead with this approach (as all the containers run in parallel)

    Also general use case of sidecar container is for doing continuous help of main container (eg. log shipping , monitoring etc) , but our use case is kind off a one-off activity.
    We can execute some script , may be wait for some time and then try to create a dataspace.


Summary


Sr No.ApproachAnalysisComments
1init-containerWould require dependency on SQL (as app container doesn't come up until this point)
2k8s-hooksPostStart hook : executed once, if failed we cant retry.
3Events in SpringApplication specific solution. We can listen the event and trigger the action.
4Sidecar ContainerBoth container runs in parallel (if we cant establish relationship i.e sidecar container wait for main container to start then difficult to execute command on a non ready container)


References

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/event/ApplicationReadyEvent.html

https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

https://medium.com/bb-tutorials-and-thoughts/kubernetes-learn-sidecar-container-pattern-6d8c21f873d

https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/




  • No labels