Skip to content

Commit 29b00ac

Browse files
authored
Get pods, services and PVCs within a specific namespace. (#697)
1 parent e6c3ed5 commit 29b00ac

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

cmd/orc-handler/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func setPrimaryLabel(ctx context.Context, primary string) error {
107107

108108
primaryName := strings.TrimSuffix(strings.TrimSuffix(primary, "."+ns), "."+mysql.ServiceName(cr))
109109

110-
pods, err := k8s.PodsByLabels(ctx, cl, mysql.MatchLabels(cr))
110+
pods, err := k8s.PodsByLabels(ctx, cl, mysql.MatchLabels(cr), cr.Namespace)
111111
if err != nil {
112112
return errors.Wrap(err, "get MySQL pods")
113113
}

pkg/controller/ps/controller.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (r *PerconaServerMySQLReconciler) applyFinalizers(ctx context.Context, cr *
169169
func (r *PerconaServerMySQLReconciler) deleteMySQLPods(ctx context.Context, cr *apiv1alpha1.PerconaServerMySQL) error {
170170
log := logf.FromContext(ctx).WithName("deleteMySQLPods")
171171

172-
pods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr))
172+
pods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr), cr.Namespace)
173173
if err != nil {
174174
return errors.Wrap(err, "get pods")
175175
}
@@ -838,7 +838,7 @@ func (r *PerconaServerMySQLReconciler) reconcileReplication(ctx context.Context,
838838

839839
// In the case of a cluster downscale, we need to forget replicas that are not part of the cluster
840840
if len(clusterInstances) > int(cr.MySQLSpec().Size) {
841-
mysqlPods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr))
841+
mysqlPods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr), cr.Namespace)
842842
if err != nil {
843843
return errors.Wrap(err, "get mysql pods")
844844
}
@@ -911,7 +911,7 @@ func (r *PerconaServerMySQLReconciler) reconcileGroupReplication(ctx context.Con
911911
return nil
912912
}
913913

914-
func (r *PerconaServerMySQLReconciler) cleanupOutdatedServices(ctx context.Context, exposer Exposer) error {
914+
func (r *PerconaServerMySQLReconciler) cleanupOutdatedServices(ctx context.Context, exposer Exposer, ns string) error {
915915
log := logf.FromContext(ctx).WithName("cleanupOutdatedServices")
916916
size := int(exposer.Size())
917917
svcNames := make(map[string]struct{}, size)
@@ -929,7 +929,7 @@ func (r *PerconaServerMySQLReconciler) cleanupOutdatedServices(ctx context.Conte
929929

930930
svcLabels := exposer.Labels()
931931
svcLabels[naming.LabelExposed] = "true"
932-
services, err := k8s.ServicesByLabels(ctx, r.Client, svcLabels)
932+
services, err := k8s.ServicesByLabels(ctx, r.Client, svcLabels, ns)
933933
if err != nil {
934934
return errors.Wrap(err, "get exposed services")
935935
}
@@ -951,7 +951,7 @@ func (r *PerconaServerMySQLReconciler) cleanupOutdatedServices(ctx context.Conte
951951
func (r *PerconaServerMySQLReconciler) cleanupMysql(ctx context.Context, cr *apiv1alpha1.PerconaServerMySQL) error {
952952
if !cr.Spec.Pause {
953953
mysqlExposer := mysql.Exposer(*cr)
954-
if err := r.cleanupOutdatedServices(ctx, &mysqlExposer); err != nil {
954+
if err := r.cleanupOutdatedServices(ctx, &mysqlExposer, cr.Namespace); err != nil {
955955
return errors.Wrap(err, "cleanup MySQL services")
956956
}
957957
}
@@ -965,7 +965,7 @@ func (r *PerconaServerMySQLReconciler) cleanupOrchestrator(ctx context.Context,
965965
}
966966

967967
orcExposer := orchestrator.Exposer(*cr)
968-
if err := r.cleanupOutdatedServices(ctx, &orcExposer); err != nil {
968+
if err := r.cleanupOutdatedServices(ctx, &orcExposer, cr.Namespace); err != nil {
969969
return errors.Wrap(err, "cleanup Orchestrator services")
970970
}
971971
}
@@ -1216,7 +1216,7 @@ func (r *PerconaServerMySQLReconciler) startAsyncReplication(ctx context.Context
12161216
}
12171217

12181218
func getReadyMySQLPod(ctx context.Context, cl client.Reader, cr *apiv1alpha1.PerconaServerMySQL) (*corev1.Pod, error) {
1219-
pods, err := k8s.PodsByLabels(ctx, cl, mysql.MatchLabels(cr))
1219+
pods, err := k8s.PodsByLabels(ctx, cl, mysql.MatchLabels(cr), cr.Namespace)
12201220
if err != nil {
12211221
return nil, errors.Wrap(err, "get pods")
12221222
}
@@ -1241,7 +1241,7 @@ func getMySQLPod(ctx context.Context, cl client.Reader, cr *apiv1alpha1.PerconaS
12411241
}
12421242

12431243
func getReadyOrcPod(ctx context.Context, cl client.Reader, cr *apiv1alpha1.PerconaServerMySQL) (*corev1.Pod, error) {
1244-
pods, err := k8s.PodsByLabels(ctx, cl, orchestrator.MatchLabels(cr))
1244+
pods, err := k8s.PodsByLabels(ctx, cl, orchestrator.MatchLabels(cr), cr.Namespace)
12451245
if err != nil {
12461246
return nil, errors.Wrap(err, "get pods")
12471247
}

pkg/controller/ps/crash_recovery.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
func (r *PerconaServerMySQLReconciler) reconcileFullClusterCrash(ctx context.Context, cr *apiv1alpha1.PerconaServerMySQL) error {
2020
log := logf.FromContext(ctx).WithName("Crash recovery")
2121

22-
pods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr))
22+
pods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr), cr.Namespace)
2323
if err != nil {
2424
return errors.Wrap(err, "get pods")
2525
}
@@ -35,11 +35,6 @@ func (r *PerconaServerMySQLReconciler) reconcileFullClusterCrash(ctx context.Con
3535
}
3636
}
3737

38-
operatorPass, err := k8s.UserPassword(ctx, r.Client, cr, apiv1alpha1.UserOperator)
39-
if err != nil {
40-
return errors.Wrap(err, "get operator password")
41-
}
42-
4338
var outb, errb bytes.Buffer
4439
cmd := []string{"cat", "/var/lib/mysql/full-cluster-crash"}
4540

@@ -62,6 +57,11 @@ func (r *PerconaServerMySQLReconciler) reconcileFullClusterCrash(ctx context.Con
6257
continue
6358
}
6459

60+
operatorPass, err := k8s.UserPassword(ctx, r.Client, cr, apiv1alpha1.UserOperator)
61+
if err != nil {
62+
return errors.Wrap(err, "get operator password")
63+
}
64+
6565
podFQDN := fmt.Sprintf("%s.%s.%s", pod.Name, mysql.ServiceName(cr), cr.Namespace)
6666
podUri := fmt.Sprintf("%s:%s@%s", apiv1alpha1.UserOperator, operatorPass, podFQDN)
6767

@@ -97,7 +97,7 @@ func (r *PerconaServerMySQLReconciler) reconcileFullClusterCrash(ctx context.Con
9797
func (r *PerconaServerMySQLReconciler) cleanupFullClusterCrashFile(ctx context.Context, cr *apiv1alpha1.PerconaServerMySQL) error {
9898
log := logf.FromContext(ctx)
9999

100-
pods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr))
100+
pods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr), cr.Namespace)
101101
if err != nil {
102102
return errors.Wrap(err, "get pods")
103103
}

pkg/controller/ps/status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (r *PerconaServerMySQLReconciler) reconcileCRStatus(ctx context.Context, cr
142142
}
143143

144144
if cr.Spec.MySQL.IsGR() {
145-
pods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr))
145+
pods, err := k8s.PodsByLabels(ctx, r.Client, mysql.MatchLabels(cr), cr.Namespace)
146146
if err != nil {
147147
return errors.Wrap(err, "get pods")
148148
}
@@ -377,7 +377,7 @@ func (r *PerconaServerMySQLReconciler) appStatus(ctx context.Context, cr *apiv1a
377377
return status, err
378378
}
379379

380-
pods, err := k8s.PodsByLabels(ctx, r.Client, labels)
380+
pods, err := k8s.PodsByLabels(ctx, r.Client, labels, cr.Namespace)
381381
if err != nil {
382382
return status, errors.Wrap(err, "get pod list")
383383
}

pkg/controller/psrestore/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (r *PerconaServerMySQLRestoreReconciler) Reconcile(ctx context.Context, req
257257
func (r *PerconaServerMySQLRestoreReconciler) deletePVCs(ctx context.Context, cluster *apiv1alpha1.PerconaServerMySQL) error {
258258
log := logf.FromContext(ctx)
259259

260-
pvcs, err := k8s.PVCsByLabels(ctx, r.Client, mysql.MatchLabels(cluster))
260+
pvcs, err := k8s.PVCsByLabels(ctx, r.Client, mysql.MatchLabels(cluster), cluster.Namespace)
261261
if err != nil {
262262
return errors.Wrap(err, "get PVC list")
263263
}

pkg/k8s/utils.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,32 +338,41 @@ func ObjectHash(obj runtime.Object) (string, error) {
338338
return hex.EncodeToString(hash[:]), nil
339339
}
340340

341-
func PodsByLabels(ctx context.Context, cl client.Reader, l map[string]string) ([]corev1.Pod, error) {
341+
func PodsByLabels(ctx context.Context, cl client.Reader, l map[string]string, namespace string) ([]corev1.Pod, error) {
342342
podList := &corev1.PodList{}
343343

344-
opts := &client.ListOptions{LabelSelector: labels.SelectorFromSet(l)}
344+
opts := &client.ListOptions{
345+
LabelSelector: labels.SelectorFromSet(l),
346+
Namespace: namespace,
347+
}
345348
if err := cl.List(ctx, podList, opts); err != nil {
346349
return nil, err
347350
}
348351

349352
return podList.Items, nil
350353
}
351354

352-
func ServicesByLabels(ctx context.Context, cl client.Reader, l map[string]string) ([]corev1.Service, error) {
355+
func ServicesByLabels(ctx context.Context, cl client.Reader, l map[string]string, namespace string) ([]corev1.Service, error) {
353356
svcList := &corev1.ServiceList{}
354357

355-
opts := &client.ListOptions{LabelSelector: labels.SelectorFromSet(l)}
358+
opts := &client.ListOptions{
359+
LabelSelector: labels.SelectorFromSet(l),
360+
Namespace: namespace,
361+
}
356362
if err := cl.List(ctx, svcList, opts); err != nil {
357363
return nil, err
358364
}
359365

360366
return svcList.Items, nil
361367
}
362368

363-
func PVCsByLabels(ctx context.Context, cl client.Reader, l map[string]string) ([]corev1.PersistentVolumeClaim, error) {
369+
func PVCsByLabels(ctx context.Context, cl client.Reader, l map[string]string, namespace string) ([]corev1.PersistentVolumeClaim, error) {
364370
pvcList := &corev1.PersistentVolumeClaimList{}
365371

366-
opts := &client.ListOptions{LabelSelector: labels.SelectorFromSet(l)}
372+
opts := &client.ListOptions{
373+
LabelSelector: labels.SelectorFromSet(l),
374+
Namespace: namespace,
375+
}
367376
if err := cl.List(ctx, pvcList, opts); err != nil {
368377
return nil, err
369378
}

0 commit comments

Comments
 (0)