generated from cybozu-go/neco-template
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from cybozu-go/watch-pod
Watch Pod deletion and notify the cluster manager
- Loading branch information
Showing
10 changed files
with
308 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/cybozu-go/moco/clustering" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
type mockManager struct { | ||
mu sync.Mutex | ||
clusters map[string]struct{} | ||
updated []types.NamespacedName | ||
} | ||
|
||
var _ clustering.ClusterManager = &mockManager{} | ||
|
||
func (m *mockManager) Update(ctx context.Context, key types.NamespacedName) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
m.clusters[key.String()] = struct{}{} | ||
} | ||
|
||
func (m *mockManager) UpdateNoStart(ctx context.Context, key types.NamespacedName) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
m.updated = append(m.updated, key) | ||
} | ||
|
||
func (m *mockManager) Stop(key types.NamespacedName) { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
delete(m.clusters, key.String()) | ||
} | ||
|
||
func (m *mockManager) StopAll() {} | ||
|
||
func (m *mockManager) getKeys() map[string]bool { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
keys := make(map[string]bool) | ||
for k := range m.clusters { | ||
keys[k] = true | ||
} | ||
return keys | ||
} | ||
|
||
func (m *mockManager) isUpdated(key types.NamespacedName) bool { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
|
||
for _, k := range m.updated { | ||
if k.Namespace == key.Namespace && k.Name == key.Name { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
|
||
mocov1beta1 "github.com/cybozu-go/moco/api/v1beta1" | ||
"github.com/cybozu-go/moco/clustering" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
crlog "sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
// PodWatcher Watched MySQL pods and informs the cluster manager of the event. | ||
type PodWatcher struct { | ||
client.Client | ||
ClusterManager clustering.ClusterManager | ||
} | ||
|
||
//+kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch | ||
//+kubebuilder:rbac:groups=apps,resources=statefulsets,verbs=get;list;watch | ||
|
||
// Reconcile implements Reconciler interface. | ||
// See https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile#Reconciler | ||
func (r *PodWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
log := crlog.FromContext(ctx) | ||
|
||
pod := &corev1.Pod{} | ||
if err := r.Get(ctx, req.NamespacedName, pod); err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
if pod.DeletionTimestamp == nil { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
ref := metav1.GetControllerOfNoCopy(pod) | ||
if ref == nil { | ||
return ctrl.Result{}, nil | ||
} | ||
refGV, err := schema.ParseGroupVersion(ref.APIVersion) | ||
if err != nil { | ||
//lint:ignore nilerr intentional | ||
return ctrl.Result{}, nil | ||
} | ||
if ref.Kind != "StatefulSet" || refGV.Group != appsv1.GroupName { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
sts := &appsv1.StatefulSet{} | ||
if err := r.Get(ctx, client.ObjectKey{Namespace: pod.Namespace, Name: ref.Name}, sts); err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
ref = metav1.GetControllerOfNoCopy(sts) | ||
if ref == nil { | ||
return ctrl.Result{}, nil | ||
} | ||
refGV, err = schema.ParseGroupVersion(ref.APIVersion) | ||
if err != nil { | ||
//lint:ignore nilerr intentional | ||
return ctrl.Result{}, nil | ||
} | ||
if ref.Kind != "MySQLCluster" || refGV.Group != mocov1beta1.GroupVersion.Group { | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
log.Info("detected mysql pod deletion", "name", pod.Name) | ||
r.ClusterManager.UpdateNoStart(ctx, types.NamespacedName{Namespace: pod.Namespace, Name: ref.Name}) | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *PodWatcher) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&corev1.Pod{}). | ||
WithOptions( | ||
controller.Options{MaxConcurrentReconciles: 8}, | ||
). | ||
Complete(r) | ||
} |
Oops, something went wrong.