@@ -17,6 +17,7 @@ import (
17
17
"k8s.io/utils/ptr"
18
18
19
19
mocov1beta2 "github.com/cybozu-go/moco/api/v1beta2"
20
+ "github.com/cybozu-go/moco/pkg/constants"
20
21
ctrl "sigs.k8s.io/controller-runtime"
21
22
"sigs.k8s.io/controller-runtime/pkg/client"
22
23
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -27,6 +28,11 @@ func testNewStatefulSet(cluster *mocov1beta2.MySQLCluster) *appsv1.StatefulSet {
27
28
ObjectMeta : metav1.ObjectMeta {
28
29
Name : cluster .PrefixedName (),
29
30
Namespace : cluster .Namespace ,
31
+ Labels : map [string ]string {
32
+ constants .LabelAppName : constants .AppNameMySQL ,
33
+ constants .LabelAppInstance : cluster .Name ,
34
+ constants .LabelAppCreatedBy : constants .AppCreator ,
35
+ },
30
36
OwnerReferences : []metav1.OwnerReference {
31
37
* metav1 .NewControllerRef (cluster , mocov1beta2 .GroupVersion .WithKind ("MySQLCluster" )),
32
38
},
@@ -41,11 +47,19 @@ func testNewStatefulSet(cluster *mocov1beta2.MySQLCluster) *appsv1.StatefulSet {
41
47
},
42
48
},
43
49
Selector : & metav1.LabelSelector {
44
- MatchLabels : map [string ]string {"foo" : "bar" },
50
+ MatchLabels : map [string ]string {
51
+ constants .LabelAppName : constants .AppNameMySQL ,
52
+ constants .LabelAppInstance : cluster .Name ,
53
+ constants .LabelAppCreatedBy : constants .AppCreator ,
54
+ },
45
55
},
46
56
Template : corev1.PodTemplateSpec {
47
57
ObjectMeta : metav1.ObjectMeta {
48
- Labels : map [string ]string {"foo" : "bar" },
58
+ Labels : map [string ]string {
59
+ constants .LabelAppName : constants .AppNameMySQL ,
60
+ constants .LabelAppInstance : cluster .Name ,
61
+ constants .LabelAppCreatedBy : constants .AppCreator ,
62
+ },
49
63
},
50
64
Spec : corev1.PodSpec {
51
65
Containers : []corev1.Container {
@@ -64,14 +78,13 @@ func testNewPods(sts *appsv1.StatefulSet) []*corev1.Pod {
64
78
pods := make ([]* corev1.Pod , 0 , * sts .Spec .Replicas )
65
79
66
80
for i := 0 ; i < int (* sts .Spec .Replicas ); i ++ {
81
+ podLabels := sts .Spec .Template .DeepCopy ().Labels
82
+ podLabels [appsv1 .ControllerRevisionHashLabelKey ] = "rev1"
67
83
pods = append (pods , & corev1.Pod {
68
84
ObjectMeta : metav1.ObjectMeta {
69
- Name : fmt .Sprintf ("%s-%d" , sts .Name , i ),
70
- Namespace : sts .Namespace ,
71
- Labels : map [string ]string {
72
- appsv1 .ControllerRevisionHashLabelKey : "rev1" ,
73
- "foo" : "bar" ,
74
- },
85
+ Name : fmt .Sprintf ("%s-%d" , sts .Name , i ),
86
+ Namespace : sts .Namespace ,
87
+ Labels : podLabels ,
75
88
OwnerReferences : []metav1.OwnerReference {* metav1 .NewControllerRef (sts , appsv1 .SchemeGroupVersion .WithKind ("StatefulSet" ))},
76
89
},
77
90
Spec : corev1.PodSpec {
@@ -86,7 +99,7 @@ func testNewPods(sts *appsv1.StatefulSet) []*corev1.Pod {
86
99
87
100
func rolloutPods (ctx context.Context , sts * appsv1.StatefulSet , rev1 int , rev2 int ) {
88
101
pods := & corev1.PodList {}
89
- err := k8sClient .List (ctx , pods , client .InNamespace ("partition" ), client .MatchingLabels (map [ string ] string { "foo" : "bar" } ))
102
+ err := k8sClient .List (ctx , pods , client .InNamespace ("partition" ), client .MatchingLabels (sts . Spec . Template . Labels ))
90
103
Expect (err ).NotTo (HaveOccurred ())
91
104
Expect (len (pods .Items )).To (Equal (rev1 + rev2 ))
92
105
0 commit comments