Hello, quick question about the pulumi state drift...
# general
c
Hello, quick question about the pulumi state drift and sync - i created kubernetes cronjob with pulumi pytohn kuberentes, after that i changed cronjob manually with kubectl edit, but now pulumi up and pulumi preview did not detected the drift between the code and the actual state , seems like --refresh syncs the cronjob changes with the state file, which is not what i want, i want to run pulumi on a way that it will sync the applied resources with the code (to remove the drift), as terraform apply do, how can i make that? Thanks!
b
if I understand correctly you want the refresh to happen automatically as part of the pulumi up?
Oh wait no you want pulumi to automatically update the pulumi program based on changes outside of pulumi? is that it?
c
i want to do pulumi up, and to synch the manually modified resources with the code (to remove the drift), i was thinking that if i run pulumi refresh, and then on "Do you want to perform this refresh?" NO, if after that i run pulumi up again, it will detect that resources are edited manually with kubectl edit, but it still says no changes. For instance if someone did kubectl edit and modify image of the deployment, i want to revert changes with pulumi command. BTW thank you for reaching me !
b
I don't think this is a feature that's available in pulumi today, but I absolutely could be wrong so let's wait for others to participate in the thread.
b
Just to be clear in the behaviour. pulumi refresh will sync the the cronjobs current status (or any other resource) to Pulumi state pulumi up -r will first read the resources current status, and try and then use your code to correct it There is no mechanism, in Pulumi or terraform that will modify the code for you. Terraform apply doesn’t do that either, it has the same behaviour as pulumi up -r
c
exactly, i try to correct the manually modified resource so it matches the state, defined in code, but it did not work, here are the steps
# initial setup (dummy kubernetes cronjob resource)
pulumi up
# kubectl get cronjob my-cron-job -o yaml | grep image
image: alpine
# modifying the resource manually, changin the image from alipne to nxing
kubectl edit cronjob my-cron-job  > from alpine to nginx
pulumi up -r
Resources:
2 unchanged
~ image                   : "alpine" => "nginx"
Do you want to perform this update?
no matter if i choose yes or no, image is still nginx, which did not match what is inside the code, this is edited version
b
ah I misunderstood you, so you just want a refresh but it's not working for you, glad Lee clarified
What about pulumi refresh what does that do? Still no changes?
c
yes, as far as i am aware, pulumi refresh syncs the state file with the existing setup, so in my case it adds manually added image nginx to the pulumi state file, output of pulumi refresh:
Copy code
~ image                   : "alpine" => "nginx"
but i want quite an opposite - to restore the state from the code, so it should return to alpine again
o
@cuddly-king-39663 do you receive more than one prompt on
up -r
? If I recall correctly,
up -r
is like doing
pulumi refresh && pulumi up
. The second phase of the up would run your program, and I'd expect it to detect the diff between state (which should now contain
nginx
) and then correct it, by updating the cron back to
alpine
.
The first prompt you see:
Copy code
pulumi up -r
Resources:
    2 unchanged
~ image                   : "alpine" => "nginx"
Do you want to perform this update?
Is the refresh updating the state. There are some resource options that could modify this behavior, could you copy the code you're using to manage the cronjob into this thread?
c
yes, i guess this is the behavior that i need, restoring everything to the state of the code, removing manual modifications, but it did not work in my case, here is the code:
Copy code
from pulumi_kubernetes.batch.v1beta1 import CronJob

# Create a CronJob.
cron_job = CronJob(
    "my-cron-job",
    metadata={
        "name": "my-cron-job"
    },
    spec={
        "schedule": "*/1 * * * *",
        "job_template": {
            "spec": {
                "template": {
                    "spec": {
                        "containers": [{
                            "name": "my-container",
                            "image": "alpine",
                            "command": ["/bin/sh", "-c", 'echo "Hello World"']
                        }],
                        "restart_policy": "OnFailure"
                    }
                }
            }
        }
    }
)
o
So if I understand correctly, if you run
pulumi refresh --yes
then
pulumi up
, it doesn't report any changes to deploy on the up?
c
after manual change, pulumi refresh gives me
Copy code
~ image                   : "alpine" => "nginx"
so it notices the differences, but when i click yes, it saved manual changes to the state file, so pulumi up did not revert the manual changes
i want to return the cronjob to the state, described in code, in my case - to restore it to alpine
o
Sorry, I have only seen the output of
refresh
in this thread. What does
pulumi up
show?
Pulumi Up works by comparing the resources declared in your program to the state. There may be a bug here in computing the diff during up, but I would expect
up
to detect that the state does not equal what your program declares.
c
when i click on details, it did not shows code with changes, only this:
Copy code
Do you want to perform this update? details
  pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:cronjob::kubernetes-cronjob::pulumi:pulumi:Stack::kubernetes-cronjob-cronjob]
Do you want to perform this update? yes and noting happens, image is still nginx
Copy code
Resources:
    2 unchanged

kubectl get cronjob my-cron-job -o yaml | grep image
            image: nginx
o
Thanks, that helps narrow down where the issue might be. Could you file an issue on the Pulumi-Kubernetes repository with this code sample?
c
sure, thanks for the help! i tried other modules as well, for instance deployment, namespace, etc, none of them worked, i will create an issue and see