Skip to content

CRD watch stream starts processing very old deleted resources #693

Closed
@max-rocket-internet

Description

@max-rocket-internet

I have a basic controller running to watch CustomObjectsApi but about 2 minutes after starting it starts processing old resources that were already deleted days ago.

Here's my code:

crds = client.CustomObjectsApi()
resource_version = ''

while True:
    logger.info('controller initializing')
    stream = watch.Watch().stream(crds.list_cluster_custom_object, 'postgresql.org', 'v1', 'pgdatabases', resource_version=resource_version)
    try:
        for event in stream:
            event_type = event["type"]
            obj = event["object"]
            metadata = obj.get('metadata')
            spec = obj.get('spec')
            code = obj.get('code')

            if code == 410:
                new_version = parse_too_old_failure(obj.get('message'))
                if new_version == None:
                    resource_version = ''
                    break
                else:
                    resource_version = new_version
                    logger.error('Updating resource version to {0} due to "too old" error'.format(new_version))

            if not metadata or not spec:
                logger.error('No metadata or spec in object, skipping: {0}'.format(json.dumps(obj, indent=1)))
                continue

            if metadata['resourceVersion'] is not None:
                resource_version = metadata['resourceVersion']
                logger.debug('resourceVersion now: {0}'.format(resource_version))

            logger.debug('{0}: {1} {2}'.format(event_type, resource_version, metadata.get('name')))

And the output is this:

{"time":"2018-11-27 11:50:15,570", "level":"INFO", "message":"controller initializing"}
{"time":"2018-11-27 11:50:16,163", "level":"DEBUG", "message":"resourceVersion now: 1147502"}
# This CRD resource (app-real) exists in the cluster
{"time":"2018-11-27 11:50:16,163", "level":"DEBUG", "message":"ADDED: 1147502 app-real"}
# Wait 2 mins...
{"time":"2018-11-27 11:51:37,431", "level":"DEBUG", "message":"resourceVersion now: 3070373"}
{"time":"2018-11-27 11:51:37,431", "level":"DEBUG", "message":"ADDED: 3070373 app1"}
{"time":"2018-11-27 11:51:37,432", "level":"DEBUG", "message":"resourceVersion now: 3070374"}
{"time":"2018-11-27 11:51:37,432", "level":"DEBUG", "message":"ADDED: 3070374 app2"}

The problem is that the resources 3070373 (app1) & 3070374 (app2) were deleted from the cluster a long time ago. The don't show in kubectl get mycrd.

I have 2 controllers running watching the exact same CRD. One is running in the cluster and the one above is running on my workstation. Is the cause of this error the fact I'm running 2 controllers watching the same CRD? Or is there a mistake in my code?

Kubernetes 1.10.3
kubernetes-client/python: 8.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    lifecycle/staleDenotes an issue or PR has remained open with no activity and has become stale.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions