Skip to content

Commit d09025f

Browse files
committed
update
1 parent 658fe07 commit d09025f

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# configmap-restart
1+
# configmap-restart main logical
22

3-
Watch configmap data update and restart releated deployments which mounted the configmap
3+
- opsappv1.Configrestart{} define the configMap to watch and deployemnts to restart
4+
- watch configMap data update, then get the same namespace opsappv1.ConfigrestartList{}
5+
and restart deployment
46

57
```yaml
68
# example cr

internal/controller/configmap_controller.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,49 +76,57 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
7676

7777
for _, configRestart := range configRestartList.Items {
7878

79-
suspend := configRestart.Spec.Suspend
80-
configName := configRestart.Spec.ConfigName
81-
deployments := configRestart.Spec.Deployments
82-
ns := configRestart.Namespace
83-
84-
go func(deployments []string, configName, namespace string, suspend bool) {
85-
86-
if configName != configMap.Name {
79+
go func(configRestart *opsappv1.Configrestart) {
80+
// go func(deployments []string, configName, namespace string, suspend bool) {
81+
suspend := configRestart.Spec.Suspend
82+
configMapName := configRestart.Spec.ConfigName
83+
deploymentNames := configRestart.Spec.Deployments
84+
ns := configRestart.Namespace
85+
86+
if configMapName != configMap.Name {
8787
log.Info("configName not match, skip restart", "configMapName", configMap.Name)
8888
return
8989
}
9090

9191
if suspend {
92-
log.Info("configrestart is suspended, skip restart", "configName", configName)
92+
log.Info("configrestart is suspended, skip restart", "configName", configMapName)
9393
return
9494
}
9595
// if deploymentName is empty, restart all deployments
96-
if len(deployments) == 0 {
96+
if len(deploymentNames) == 0 {
9797

98-
err := restartDeploymentWithConfigMap(ctx, r.Client, configName, namespace)
98+
err := restartDeploymentWithConfigMap(ctx, r.Client, configMapName, ns)
9999
if err != nil {
100100
log.Error(err, "restart deployment with configmap failed")
101101
return
102102
}
103-
log.Info("restart deployment with configmap success", "configName", configName)
103+
log.Info("restart deployment with configmap success", "configName", configMapName)
104104
return
105105
}
106106
// if deployments is not empty, restart deployments
107-
for _, deployment := range deployments {
108-
err := restartDeployment(ctx, r.Client, deployment, namespace)
107+
for _, deploymentName := range deploymentNames {
108+
err := restartDeployment(ctx, r.Client, deploymentName, ns)
109109
if err != nil {
110-
log.Error(err, "restart deployment failed", "deployment", deployment, "ns", namespace)
110+
log.Error(err, "restart deployment failed", "deployment", deploymentName, "ns", ns)
111111
return
112112
}
113-
log.Info("restart deployment success", "deployment", deployment, "ns", namespace)
113+
log.Info("restart deployment success", "deployment", deploymentName, "ns", ns)
114114
}
115-
}(deployments, configName, ns, suspend)
115+
}(&configRestart)
116116
}
117117

118118
log.Info("Reconciling configmap finished")
119119
return ctrl.Result{}, nil
120120
}
121121

122+
// SetupWithManager sets up the controller with the Manager.
123+
func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error {
124+
125+
return ctrl.NewControllerManagedBy(mgr).
126+
For(&corev1.ConfigMap{}, builder.WithPredicates(configMapUpdatePredicate())).
127+
Complete(r)
128+
}
129+
122130
func restartDeployment(ctx context.Context, c client.Client, deploymentName, ns string) error {
123131

124132
var deploy appsv1.Deployment
@@ -174,14 +182,6 @@ func restartDeploymentWithConfigMap(ctx context.Context, c client.Client, config
174182
return nil
175183
}
176184

177-
// SetupWithManager sets up the controller with the Manager.
178-
func (r *ConfigMapReconciler) SetupWithManager(mgr ctrl.Manager) error {
179-
180-
return ctrl.NewControllerManagedBy(mgr).
181-
For(&corev1.ConfigMap{}, builder.WithPredicates(configMapUpdatePredicate())).
182-
Complete(r)
183-
}
184-
185185
func configMapUpdatePredicate() predicate.Funcs {
186186
controllerLeader := "control-plane.alpha.kubernetes.io/leader"
187187
return predicate.Funcs{

0 commit comments

Comments
 (0)