utils_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package predicates
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/labels"
  19. )
  20. // ExampleUtils is a https://blog.golang.org/examples styled unit test.
  21. func ExampleFindLabelsInSet() {
  22. labelSubset := labels.Set{}
  23. labelSubset["label1"] = "value1"
  24. labelSubset["label2"] = "value2"
  25. // Lets make believe that these pods are on the cluster.
  26. // Utility functions will inspect their labels, filter them, and so on.
  27. nsPods := []*v1.Pod{
  28. {
  29. ObjectMeta: metav1.ObjectMeta{
  30. Name: "pod1",
  31. Namespace: "ns1",
  32. Labels: map[string]string{
  33. "label1": "wontSeeThis",
  34. "label2": "wontSeeThis",
  35. "label3": "will_see_this",
  36. },
  37. },
  38. }, // first pod which will be used via the utilities
  39. {
  40. ObjectMeta: metav1.ObjectMeta{
  41. Name: "pod2",
  42. Namespace: "ns1",
  43. },
  44. },
  45. {
  46. ObjectMeta: metav1.ObjectMeta{
  47. Name: "pod3ThatWeWontSee",
  48. },
  49. },
  50. }
  51. fmt.Println(FindLabelsInSet([]string{"label1", "label2", "label3"}, nsPods[0].ObjectMeta.Labels)["label3"])
  52. AddUnsetLabelsToMap(labelSubset, []string{"label1", "label2", "label3"}, nsPods[0].ObjectMeta.Labels)
  53. fmt.Println(labelSubset)
  54. for _, pod := range FilterPodsByNamespace(nsPods, "ns1") {
  55. fmt.Print(pod.Name, ",")
  56. }
  57. // Output:
  58. // will_see_this
  59. // label1=value1,label2=value2,label3=will_see_this
  60. // pod1,pod2,
  61. }