Skip to content

Conversation

hasbro17
Copy link
Contributor

Added action for update.

@hasbro17
Copy link
Contributor Author

Test cases for Update:

func main() {
	samplePod := getSamplePod("Pod", "v1", "test-pod", "default")
	fmt.Printf("Pod template: \n%s\n\n", getFormattedJSON(samplePod))

	// Update non-existent pod
	logrus.Infof("Test case 1: Update a non-existent pod")
	pod := samplePod
	// Create pod
	err := action.Update(pod)
	if err != nil {
		logrus.Error(err)
	} else {
		logrus.Errorf("Updated non-existent pod")
		fmt.Printf("Updated Pod: \n%s\n\n", getFormattedJSON(pod))
	}

	// Update pod with correct resource version
	logrus.Infof("Test case 2: Update a pod with correct resource version")
	pod = samplePod
	err = action.Create(pod)
	if err != nil {
		logrus.Errorf("Failed to create pod")
		logrus.Fatal(err)
	} else {
		fmt.Printf("Created Pod: \n%s\n\n", getFormattedJSON(pod))
	}
	// Wait until pod is created and stops changing resource version
	time.Sleep(time.Second * 10)
	// Get the pod
	err = query.Get(pod)
	if err != nil {
		logrus.Errorf("Failed to get pod")
		logrus.Fatal(err)
	} else {
		fmt.Printf("Got Pod: \n%s\n\n", getFormattedJSON(pod))
	}
	// Update the pod's label
	ls := map[string]string{"app": "v1"}
	pod.ObjectMeta.Labels = ls
	err = action.Update(pod)
	if err != nil {
		logrus.Errorf("Failed to update pod's labels with correct resource version(%v)", pod.ResourceVersion)
		logrus.Fatal(err)
	} else {
		fmt.Printf("Updated Pod: \n%s\n\n", getFormattedJSON(pod))
	}

	// Update again with incorrect resource version
        logrus.Infof("Test case 3: Update with incorrect resource version")
	ls = map[string]string{"app": "v2"}
	pod.ObjectMeta.Labels = ls
	pod.ObjectMeta.ResourceVersion = "1"
	err = action.Update(pod)
	if err != nil {
		logrus.Errorf("Failed to update pod's labels with incorrect resource version(%v)", pod.ResourceVersion)
		logrus.Error(err)
	} else {
		logrus.Errorf("Updated pod with incorrect resource version")
		fmt.Printf("Updated Pod: \n%s\n\n", getFormattedJSON(pod))
	}
}

Test logs:

Pod template:
{
    "kind": "Pod",
    "apiVersion": "v1",
    "metadata": {
        "name": "test-pod",
        "namespace": "default",
        "creationTimestamp": null
    },
    "spec": {
        "containers": [
            {
                "name": "mypod",
                "image": "alpine",
                "command": [
                    "/bin/sh",
                    "-c",
                    "while true; do echo running; sleep 100; done"
                ],
                "resources": {}
            }
        ],
        "restartPolicy": "Never"
    },
    "status": {}
}

time="2018-03-26T19:56:37Z" level=info msg="Test case 1: Update a non-existent pod"
time="2018-03-26T19:56:37Z" level=error msg="pods \"test-pod\" not found"
time="2018-03-26T19:56:37Z" level=info msg="Test case 2: Update a pod with correct resource version"
Created Pod:
{
    "kind": "Pod",
    "apiVersion": "v1",
    "metadata": {
        "name": "test-pod",
        "namespace": "default",
        "selfLink": "/api/v1/namespaces/default/pods/test-pod",
        "uid": "cb41b361-312f-11e8-ba9f-0800274106a1",
        "resourceVersion": "185886",
        "creationTimestamp": "2018-03-26T19:56:37Z"
    },
...
}

Got Pod:
{
    "kind": "Pod",
    "apiVersion": "v1",
    "metadata": {
        "name": "test-pod",
        "namespace": "default",
        "selfLink": "/api/v1/namespaces/default/pods/test-pod",
        "uid": "cb41b361-312f-11e8-ba9f-0800274106a1",
        "resourceVersion": "185897",
        "creationTimestamp": "2018-03-26T19:56:37Z"
    },
...
}

Updated Pod:
{
    "kind": "Pod",
    "apiVersion": "v1",
    "metadata": {
        "name": "test-pod",
        "namespace": "default",
        "selfLink": "/api/v1/namespaces/default/pods/test-pod",
        "uid": "cb41b361-312f-11e8-ba9f-0800274106a1",
        "resourceVersion": "185903",
        "creationTimestamp": "2018-03-26T19:56:37Z",
        "labels": {
            "app": "v1"
        }
    },
...
}

time="2018-03-26T19:56:47Z" level=info msg="Test case 3: Update with incorrect resource version"
time="2018-03-26T19:56:47Z" level=error msg="Failed to update pod's labels with incorrect resource version(1)"
time="2018-03-26T19:56:47Z" level=error msg="Operation cannot be fulfilled on pods \"test-pod\": the object has been modified; please apply your changes to the latest version and try again"

@hasbro17 hasbro17 changed the title [WIP] action: add update action: add update Mar 26, 2018
@hasbro17
Copy link
Contributor Author

@fanminshi PTAL.

@fanminshi
Copy link
Contributor

do you know why the created pod's "resourceVersion": "185886" and Got pod's "resourceVersion": "185897" differ for Test case 2?

@hasbro17
Copy link
Contributor Author

do you know why the created pod's "resourceVersion": "185886" and Got pod's "resourceVersion": "185897" differ for Test case 2?

Yes so depending on the object it can be updated by other components like the scheduler. So when the pod was first created it's status.phase=Pending. When the scheduler schedules it's state will change to status.phase=ContainerCreating and then finally to running.
So it's normal for an object that is created to get updated a lot of times and have it's resource version change.

@fanminshi
Copy link
Contributor

fanminshi commented Mar 26, 2018

@hasbro17 thanks for the clarification.
LGTM

@hasbro17 hasbro17 merged commit 1008648 into operator-framework:master Mar 26, 2018
@hasbro17 hasbro17 deleted the haseeb/add-action-update branch March 26, 2018 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants