-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreloader.go
More file actions
183 lines (152 loc) · 5.6 KB
/
reloader.go
File metadata and controls
183 lines (152 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright © 2023 Cisco
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package reloader
import (
"context"
"fmt"
"log/slog"
"strconv"
"sync"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (c *Controller) runReloader(ctx context.Context) { //nolint:revive
reloaderLogger := c.logger.With(slog.String("worker", "reloader"))
reloaderLogger.Info("Reloader started")
if len(c.workloadSecrets.GetWorkloadSecretsMap()) == 0 {
reloaderLogger.Info("No workloads to reload")
return
}
err := c.initVaultClient()
if err != nil {
reloaderLogger.Error(fmt.Errorf("failed to initialize Vault client: %w", err).Error())
return
}
// Create a secretWorkloads map and compare the currently used secrets' version
// with the one stored in the secretVersions map, while creating a new secretVersions map
workloadsToReload := make(map[workload]bool)
newSecretVersions := make(map[string]int)
var wg sync.WaitGroup
var mu sync.Mutex
for secretPath, workloads := range c.workloadSecrets.GetSecretWorkloadsMap() {
wg.Add(1)
go func(secretPath string, workloads []workload) {
defer wg.Done()
reloaderLogger.Debug(fmt.Sprintf("Checking secret: %s", secretPath))
// Get current secret version
currentVersion, err := getSecretVersionFromVault(c.vaultClient.Logical(), secretPath)
if err != nil {
c.handleSecretError(err, secretPath, reloaderLogger)
return
}
mu.Lock()
defer mu.Unlock()
// Compare secret versions
switch c.secretVersions[secretPath] {
case 0:
reloaderLogger.Debug(fmt.Sprintf("Secret %s not found in secretVersions map, creating it", secretPath))
case currentVersion:
reloaderLogger.Debug(fmt.Sprintf("Secret %s did not change", secretPath))
default:
reloaderLogger.Debug(fmt.Sprintf("Secret version stored: %d current: %d", c.secretVersions[secretPath], currentVersion))
for _, workload := range workloads {
workloadsToReload[workload] = true
}
}
newSecretVersions[secretPath] = currentVersion
}(secretPath, workloads)
}
wg.Wait()
// Reloading workloads
for workloadToReload := range workloadsToReload {
go func(workloadToReload workload) {
defer wg.Done()
reloaderLogger.Info(fmt.Sprintf("Reloading workload: %s", workloadToReload))
err := c.reloadWorkload(workloadToReload)
if err != nil {
reloaderLogger.Error(fmt.Errorf("failed reloading workload: %s: %w", workloadToReload, err).Error())
}
}(workloadToReload)
}
// Replace secretVersions map with the new one so we don't keep deleted secrets in the map
c.secretVersions = newSecretVersions
reloaderLogger.Debug(fmt.Sprintf("Updated secretVersions map: %#v", newSecretVersions))
if len(workloadsToReload) == 0 {
reloaderLogger.Info("No workloads to reload")
}
}
func (c *Controller) reloadWorkload(workload workload) error {
// Reload object based on its type
switch workload.kind {
case DeploymentKind:
deployment, err := c.kubeClient.AppsV1().Deployments(workload.namespace).Get(context.Background(), workload.name, metav1.GetOptions{})
if err != nil {
return err
}
incrementReloadCountAnnotation(&deployment.Spec.Template)
_, err = c.kubeClient.AppsV1().Deployments(workload.namespace).Update(context.Background(), deployment, metav1.UpdateOptions{})
if err != nil {
return err
}
case DaemonSetKind:
daemonSet, err := c.kubeClient.AppsV1().DaemonSets(workload.namespace).Get(context.Background(), workload.name, metav1.GetOptions{})
if err != nil {
return err
}
incrementReloadCountAnnotation(&daemonSet.Spec.Template)
_, err = c.kubeClient.AppsV1().DaemonSets(workload.namespace).Update(context.Background(), daemonSet, metav1.UpdateOptions{})
if err != nil {
return err
}
case StatefulSetKind:
statefulSet, err := c.kubeClient.AppsV1().StatefulSets(workload.namespace).Get(context.Background(), workload.name, metav1.GetOptions{})
if err != nil {
return err
}
incrementReloadCountAnnotation(&statefulSet.Spec.Template)
_, err = c.kubeClient.AppsV1().StatefulSets(workload.namespace).Update(context.Background(), statefulSet, metav1.UpdateOptions{})
if err != nil {
return err
}
default:
return fmt.Errorf("unknown object type: %s", workload.kind)
}
return nil
}
func (c *Controller) handleSecretError(err error, secretPath string, logger *slog.Logger) {
switch err.(type) {
case ErrSecretNotFound:
if !c.vaultConfig.IgnoreMissingSecrets {
logger.Error(err.Error())
} else {
logger.Warn(fmt.Sprintf(
"Path not found: %s - We couldn't find a secret path. This is not an error since missing secrets can be ignored according to the configuration you've set (env: VAULT_IGNORE_MISSING_SECRETS).",
secretPath,
))
}
default:
logger.Error(fmt.Errorf("failed to get secret version: %w", err).Error())
}
}
func incrementReloadCountAnnotation(podTemplate *corev1.PodTemplateSpec) {
version := "1"
if reloadCount := podTemplate.GetAnnotations()[ReloadCountAnnotationName]; reloadCount != "" {
count, err := strconv.Atoi(reloadCount)
if err == nil {
count++
version = strconv.Itoa(count)
}
}
podTemplate.GetAnnotations()[ReloadCountAnnotationName] = version
}