"container started" event of a pod from kubernetes using pythons kubernetes library

4/18/2019

I've a deployment with one container having postStart hook as shown below

      containers:
      - name: openvas
        image: my-image:test
        lifecycle:
            postStart:
              exec:
                 command:
                 - /usr/local/tools/is_service_ready.sh

I'm watching for the events for pods using python's kubernetes library. when the pod gets deployed, container comes up and postStart script will be executed until postStart script exits successfully. I want to get the event from kubernetes using pythons kubernetes library when CONTAINER comes up. I tried watching the event, I get the event with status as 'containersReady' only when postStart completes and the POD comes up,it can be seen below.

 'status': {'conditions': [{'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'Initialized'},
                           {'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 26, 51, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'Ready'},
                           {'last_probe_time': None,
                            'last_transition_time': None,
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'ContainersReady'},
                           {'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'PodScheduled'}],
                       'container_statuses': [{'container_id': 'docker://1c39e13dc777a34c38d4194edc23c3668697223746b60276acffe3d62f9f0c44',
                                    'image': 'my-image:test',
                                    'image_id': 'docker://sha256:9903437699d871c1f3af7958a7294fe419ed7b1076cdb8e839687e67501b301b',
                                    'last_state': {'running': None,
                                                   'terminated': None,
                                                   'waiting': None},
                                    'name': 'samplename',
                                    'ready': True,
                                    'restart_count': 0,
                                    'state': {'running': {'started_at': datetime.datetime(2019, 4, 18, 16, 25, 14, tzinfo=tzlocal())},
                                              'terminated': None,
                                              'waiting': None}}],

and before this I get status 'podScheduled' as 'True'

 'status': {'conditions': [{'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'Initialized'},
                           {'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': 'containers with unready status: [openvas]',
                            'reason': 'ContainersNotReady',
                            'status': 'False',
                            'type': 'Ready'},
                           {'last_probe_time': None,
                            'last_transition_time': None,
                            'message': 'containers with unready status: [openvas]',
                            'reason': 'ContainersNotReady',
                            'status': 'False',
                            'type': 'ContainersReady'},
                           {'last_probe_time': None,
                            'last_transition_time': datetime.datetime(2019, 4, 18, 16, 25, 3, tzinfo=tzlocal()),
                            'message': None,
                            'reason': None,
                            'status': 'True',
                            'type': 'PodScheduled'}],
            'container_statuses': [{'container_id': None,
                                    'image': 'ns-openvas:test',
                                    'image_id': '',
                                    'last_state': {'running': None,
                                                   'terminated': None,
                                                   'waiting': None},
                                    'name': 'openvas',
                                    'ready': False,
                                    'restart_count': 0,
                                    'state': {'running': None,
                                              'terminated': None,
                                              'waiting': {'message': None,
                                                          'reason': 'ContainerCreating'}}}],

Anything I can try to get the event when the CONTAINER comes up.

-- ahmed meraj
kubernetes
python-2.7

1 Answer

4/23/2019

Obviously, with current approach you will never get it working, because, as describe here :

The postStart handler runs asynchronously relative to the Container’s code, but Kubernetes’ management of the container blocks until the postStart handler completes. The Container’s status is not set to RUNNING until the postStart handler completes.

Maybe you should create another pod with is_service_ready.sh script, which will be watching events of the main pod.

-- A_Suh
Source: StackOverflow