Skip to content

cleanup is only called on restart #1599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
qinzhichao01 opened this issue Nov 15, 2022 · 8 comments · Fixed by #1600
Closed

cleanup is only called on restart #1599

qinzhichao01 opened this issue Nov 15, 2022 · 8 comments · Fixed by #1600
Assignees
Milestone

Comments

@qinzhichao01
Copy link

qinzhichao01 commented Nov 15, 2022

I am going to use java-operator-sdk to implement my own operator, but I found that when I implemented the Cleaner method, I found that when deleting a pod, the cleanup method was not called, and the k8s pod would not be deleted, only in The cleanup method will be called after restarting the application, and the pod will be deleted. My implementation is as follows. Is my implementation wrong?

@ControllerConfiguration
public class CloudIdeOperator implements Reconciler<Pod>, Cleaner<Pod> {
    private volatile int count = 0;
    private volatile int count2 = 0;
    @Override
    public DeleteControl cleanup(Pod pod, Context<Pod> context) {
        count2++;
        System.out.println("cleanUp times:" + count2+"   "+ pod.getMetadata().getName() + ": " + pod.getStatus().getPodIP());
        return DeleteControl.defaultDelete();
    }

    @Override
    public UpdateControl<Pod> reconcile(Pod pod, Context<Pod> context) throws Exception {
        System.out.println(pod.getMetadata().getName() + ": " + pod.getStatus().getPodIP());
        count++;
        System.out.println("crete pod times:" + count);

        return UpdateControl.noUpdate();

    }
}

calll method

    public static void main(String[] args) {
        KubernetesClient client = new DefaultKubernetesClient();
        Operator operator = new Operator(client);
        operator.register(new CloudIdeOperator());

        operator.installShutdownHook();
        operator.start();
//        operator.stop();

    }
@qinzhichao01
Copy link
Author

@metacosm Do you have a good idea, hope to get your help

@csviri csviri added this to the 4.2 milestone Nov 15, 2022
@csviri csviri self-assigned this Nov 15, 2022
@csviri
Copy link
Collaborator

csviri commented Nov 15, 2022

@qinzhichao01 are you using the newest version?

@qinzhichao01
Copy link
Author

yes,my pom version is 4.1.1

@qinzhichao01
Copy link
Author

do you have any idea to save this problem?

@csviri
Copy link
Collaborator

csviri commented Nov 15, 2022

will take a look into it

@csviri
Copy link
Collaborator

csviri commented Nov 15, 2022

@qinzhichao01 added a PR, this will fix the issue. The problem was with something else, the generation filter, the pod does not seems to have generation in metadata. This was unexpected, see how it is fixed in the pr.

Until released you can turn off generation aware processing:
https://github.com/java-operator-sdk/java-operator-sdk/blob/2e7fac2a25ea470696de719b52baa880ec252bc4/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/ControllerConfiguration.java#L43-L43

by setting this flag to false. That should fix you issue.

@qinzhichao01
Copy link
Author

@csviri good job, thanks for your solution

@csviri
Copy link
Collaborator

csviri commented Nov 16, 2022

see also on k8s slack: https://kubernetes.slack.com/archives/C0EG7JC6T/p1668518118307749

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants