Skip to content

Commit a32789f

Browse files
authored
Merge pull request #409 from stakater/add-sync-after-restart
Implement sync after restart
2 parents e0a1c25 + 77b725c commit a32789f

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,12 @@ You can enable to scrape Reloader's Prometheus metrics by setting `serviceMonito
265265

266266
**Note:** Reloading of OpenShift (DeploymentConfig) and/or Argo Rollouts has to be enabled explicitly because it might not be always possible to use it on a cluster with restricted permissions. This can be done by changing the following parameters:
267267

268-
| Parameter | Description | Type |
269-
| ---------------- |------------------------------------------------------------------------------| ------- |
270-
| isOpenshift | Enable OpenShift DeploymentConfigs. Valid value are either `true` or `false` | boolean |
271-
| isArgoRollouts | Enable Argo Rollouts. Valid value are either `true` or `false` | boolean |
272-
| reloadOnCreate | Enable reload on create events. Valid value are either `true` or `false` | boolean |
268+
| Parameter | Description | Type |
269+
|------------------|------------------------------------------------------------------------------------------------------------------------------------------| ------- |
270+
| isOpenshift | Enable OpenShift DeploymentConfigs. Valid value are either `true` or `false` | boolean |
271+
| isArgoRollouts | Enable Argo Rollouts. Valid value are either `true` or `false` | boolean |
272+
| reloadOnCreate | Enable reload on create events. Valid value are either `true` or `false` | boolean |
273+
| syncAfterRestart | Enable sync after reloader restarts for **Add** events, works only when reloadOnCreate is `true`. Valid value are either `true` or `false` | boolean |
273274

274275
**ReloadOnCreate** reloadOnCreate controls how Reloader handles secrets being added to the cache for the first time. If reloadOnCreate is set to true:
275276
* Configmaps/secrets being added to the cache will cause Reloader to perform a rolling update of the associated workload.

deployments/kubernetes/chart/reloader/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ spec:
204204
{{- if eq .Values.reloader.reloadOnCreate true }}
205205
- "--reload-on-create={{ .Values.reloader.reloadOnCreate }}"
206206
{{- end }}
207+
{{- if eq .Values.reloader.syncAfterRestart true }}
208+
- "--sync-after-restart={{ .Values.reloader.syncAfterRestart }}"
209+
{{- end }}
207210
{{- if ne .Values.reloader.reloadStrategy "default" }}
208211
- "--reload-strategy={{ .Values.reloader.reloadStrategy }}"
209212
{{- end }}

deployments/kubernetes/chart/reloader/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ reloader:
1717
ignoreSecrets: false
1818
ignoreConfigMaps: false
1919
reloadOnCreate: false
20+
syncAfterRestart: false
2021
reloadStrategy: default # Set to default, env-vars or annotations
2122
ignoreNamespaces: "" # Comma separated list of namespaces to ignore
2223
namespaceSelector: "" # Comma separated list of 'key:value' labels for namespaces selection

internal/pkg/cmd/reloader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func NewReloaderCommand() *cobra.Command {
4444
cmd.PersistentFlags().StringVar(&options.ReloadStrategy, constants.ReloadStrategyFlag, constants.EnvVarsReloadStrategy, "Specifies the desired reload strategy")
4545
cmd.PersistentFlags().StringVar(&options.ReloadOnCreate, "reload-on-create", "false", "Add support to watch create events")
4646
cmd.PersistentFlags().BoolVar(&options.EnableHA, "enable-ha", false, "Adds support for running multiple replicas via leadership election")
47+
cmd.PersistentFlags().BoolVar(&options.SyncAfterRestart, "sync-after-restart", false, "Sync add events after reloader restarts")
4748

4849
return cmd
4950
}

internal/pkg/controller/controller.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,32 @@ type Controller struct {
3131
queue workqueue.RateLimitingInterface
3232
informer cache.Controller
3333
namespace string
34+
resource string
3435
ignoredNamespaces util.List
3536
collectors metrics.Collectors
3637
recorder record.EventRecorder
3738
namespaceSelector map[string]string
3839
}
3940

4041
// controllerInitialized flag determines whether controlled is being initialized
41-
var controllerInitialized bool = false
42+
var secretControllerInitialized bool = false
43+
var configmapControllerInitialized bool = false
4244

4345
// NewController for initializing a Controller
4446
func NewController(
4547
client kubernetes.Interface, resource string, namespace string, ignoredNamespaces []string, namespaceLabelSelector map[string]string, collectors metrics.Collectors) (*Controller, error) {
4648

49+
if options.SyncAfterRestart {
50+
secretControllerInitialized = true
51+
configmapControllerInitialized = true
52+
}
53+
4754
c := Controller{
4855
client: client,
4956
namespace: namespace,
5057
ignoredNamespaces: ignoredNamespaces,
5158
namespaceSelector: namespaceLabelSelector,
59+
resource: resource,
5260
}
5361
eventBroadcaster := record.NewBroadcaster()
5462
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{
@@ -77,7 +85,7 @@ func NewController(
7785
// Add function to add a new object to the queue in case of creating a resource
7886
func (c *Controller) Add(obj interface{}) {
7987
if options.ReloadOnCreate == "true" {
80-
if !c.resourceInIgnoredNamespace(obj) && c.resourceInNamespaceSelector(obj) && controllerInitialized {
88+
if !c.resourceInIgnoredNamespace(obj) && c.resourceInNamespaceSelector(obj) && secretControllerInitialized && configmapControllerInitialized {
8189
c.queue.Add(handler.ResourceCreatedHandler{
8290
Resource: obj,
8391
Collectors: c.collectors,
@@ -175,7 +183,11 @@ func (c *Controller) Run(threadiness int, stopCh chan struct{}) {
175183

176184
func (c *Controller) runWorker() {
177185
// At this point the controller is fully initialized and we can start processing the resources
178-
controllerInitialized = true
186+
if c.resource == "secrets" {
187+
secretControllerInitialized = true
188+
} else if c.resource == "configMaps" {
189+
configmapControllerInitialized = true
190+
}
179191

180192
for c.processNextItem() {
181193
}

internal/pkg/options/flags.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ var (
2424
// ReloadStrategy Specify the update strategy
2525
ReloadStrategy = constants.EnvVarsReloadStrategy
2626
// ReloadOnCreate Adds support to watch create events
27-
ReloadOnCreate = "false"
27+
ReloadOnCreate = "false"
28+
SyncAfterRestart = false
2829
// EnableHA adds support for running multiple replicas via leadership election
2930
EnableHA = false
3031
)

0 commit comments

Comments
 (0)