Skip to content

Conversation

fanminshi
Copy link
Contributor

@fanminshi fanminshi commented Mar 26, 2018

add NewListOp() factory function to create a listOp
add RuntimeObjectIntoRuntimeObject() which takes in runtime.Object and unmarshal its content into another runtime.Object of the same type.
Implement List that allows user to to query a list of objects.

@fanminshi fanminshi changed the title [WIP] *: implement List and add necessary pieces for it [WIP] *: implement List and add necessary pieces Mar 26, 2018
@fanminshi
Copy link
Contributor Author

fanminshi commented Mar 26, 2018

Manual Testing:

	namespace := "default"

	// Test cases for Memcached List
	logrus.Println("TestCase: Get Memcached List")
	l := getMemcachedList("Memcached", "cache.example.com/v1alpha1")
	err := query.List(namespace, l)
	if err != nil {
		logrus.Infof("Failed to get list : %v", err)
	}
	logrus.Println("TestCase: Get NonExistant CR")
	l = getMemcachedList("bad", "cache.example.com/v1alpha1")
	err = query.List(namespace, l)
	if err != nil {
		logrus.Infof("Failed to get list: %v", err)
	}
	logrus.Println("TestCase: Get Memcached List with APIVersion: bad")
	l = getMemcachedList("Memcached", "bad")
	err = query.List(namespace, l)
	if err != nil {
		logrus.Infof("Failed to get list %v : %v", err)
	}

	// Test cases for pod List
	logrus.Println("TestCase: Get Pod List")
	pl := getPodList("Pod", "v1")
	err = query.List(namespace, pl)
	if err != nil {
		logrus.Infof("Failed to get list : %v", err)
	}
	logrus.Println("TestCase: Get Pod List with Option")
	pl = getPodList("Pod", "v1")
	lset := map[string]string{
		"name": "memcached-operator2",
	}
	ls := labels.SelectorFromSet(lset).String()
	lop := &metav1.ListOptions{LabelSelector: ls}
	err = query.List(namespace, pl, query.WithListOptions(lop))
	if err != nil {
		logrus.Infof("Failed to get list : %v", err)
	}
	if len(pl.Items) != 1 {
		logrus.Infof("expect size of the list to be %v, got %v", 1, pl.Size())
	}
	logrus.Println("TestCase: Get Pod List with wrong label")
	pl = getPodList("Pod", "v1")
	lset = map[string]string{
		"name": "bad",
	}
	ls = labels.SelectorFromSet(lset).String()
	lop = &metav1.ListOptions{LabelSelector: ls}
	err = query.List(namespace, pl, query.WithListOptions(lop))
	if err != nil {
		logrus.Infof("Failed to get list : %v", err)
	}
	if len(pl.Items) != 0 {
		logrus.Infof("expect size of the list %b, got %v", 0, pl.Size())
	}

func getMemcachedList(kind, APIVersion string) *v1alpha1.MemcachedList {
	return &v1alpha1.MemcachedList{
		TypeMeta: metav1.TypeMeta{
			Kind:       kind,
			APIVersion: APIVersion,
		},
	}
}

func getPodList(kind, APIVersion string) *v1.PodList {
	return &v1.PodList{
		TypeMeta: metav1.TypeMeta{
			Kind:       kind,
			APIVersion: APIVersion,
		},
	}
}

Output:

time="2018-03-26T19:46:35Z" level=info msg="TestCase: Get Memcached List"
time="2018-03-26T19:46:35Z" level=info msg="TestCase: Get NonExistant CR"
time="2018-03-26T19:46:35Z" level=info msg="Failed to get list: failed to get resource client for (apiVersion:cache.example.com/v1alpha1, kind:bad, ns:default): failed to get resource type: failed to get the resource REST mapping for GroupVersionKind(cache.example.com/v1alpha1, Kind=bad): no matches for cache.example.com/, Kind=bad"
time="2018-03-26T19:46:35Z" level=info msg="TestCase: Get Memcached List with APIVersion: bad"
time="2018-03-26T19:46:35Z" level=info msg="Failed to get list failed to get resource client for (apiVersion:bad, kind:Memcached, ns:default): failed to get resource type: failed to get the resource REST mapping for GroupVersionKind(/bad, Kind=Memcached): no matches for /, Kind=Memcached : %!v(MISSING)"
time="2018-03-26T19:46:35Z" level=info msg="TestCase: Get Pod List"
time="2018-03-26T19:46:35Z" level=info msg="TestCase: Get Pod List with Option"
time="2018-03-26T19:46:35Z" level=info msg="TestCase: Get Pod List with wrong label"

@fanminshi fanminshi changed the title [WIP] *: implement List and add necessary pieces *: implement List and add necessary pieces Mar 26, 2018
@fanminshi
Copy link
Contributor Author

PTAL cc/ @hasbro17

return nil
}

// List lists a list of kubernetes object and then unmarshals the retrieved data into the "into" object.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List lists a list of kubernetes object and then unmarshals the retrieved data into the "into" object.

List retrieves the specified object list and unmarshals the retrieved data into the "into" object.

@hasbro17
Copy link
Contributor

LGTM after nit.

@fanminshi fanminshi merged commit 8a2ae59 into operator-framework:master Mar 26, 2018
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