Skip to content

Delete and Modify events are not getting trigged  #3652

Closed as not planned
Closed as not planned
@MohammadNC

Description

@MohammadNC

Describe the bug
I am watching kubernate secret in infine loop, in any change happened to secret, watch should trigger appropriate events like ADDED or MODIFIED or DELETED.
but in my case DELETE event is not triggered and most important point is that Thread is getting struck.

Client Version
19.0.0

Kubernetes Version
1.23.10

Java Version
. Java 17

below is code snippet to reproduce the issue.

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Secret;
import io.kubernetes.client.openapi.models.V1SecretList;
import io.kubernetes.client.util.Config;
import io.kubernetes.client.util.Watch;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

public class SecretWatcher {

public static void watchSecret() throws IOException, ApiException {
    ApiClient client = Config.defaultClient();
    CoreV1Api api = new CoreV1Api(client);

    List<String> secretNames = List.of("secret1", "secret2", "secret3");

    while (true) {
try (Watch<V1Secret> watch = watchForNamespace(this.nameSpace)) {
		try {
			for (Watch.Response<V1Secret> item : watch) {
				if (secretNames.contains(item.object.getMetadata().getName())) {
					logger.debug("Item type received is: " + item.type);
					switch (item.type) {
						case "ADDED":
							handleEventAdded(item.object);
							break;
						case "MODIFIED":
							handleEventModified(item.object);
							break;
						case "DELETED":
							handleEventDeleted(item.object);
							break;
						default:
							continue;
					}
					break;
				}
			}
		} catch (Exception e) {

			logger.error("Exception occurred during handling item.type event. Reason: " + e.getMessage());
		}
	} catch (Exception excep) {
		logger.error("Exception occurred while monitoring K8 Resource  " + excep.getMessage());
	}
    }
}


	public Watch<V1Secret> watchForNamespace(String nameSpace) {
	Watch<V1Secret> watch = null;
	try {
		watch = Watch.createWatch(getClient(), getApi().listNamespacedSecretCall(nameSpace, null, null, null, null,
				null, null, null, null, Boolean.TRUE, null, Boolean.TRUE, null), new TypeToken<Watch.Response<V1Secret>>() {
		}.getType());
		logger.debug("Watch created for secrets in namespace: " + nameSpace);
	} catch (ApiException e) {
		logger.error("Failed to create watch for secrets in namespace:" + nameSpace + " Reason: " + e.getMessage());
	}
	return watch;
}

}

Kindly suggest how we can achieve all event triggered correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    lifecycle/rottenDenotes an issue or PR that has aged beyond stale and will be auto-closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions