@@ -16,14 +16,15 @@ import (
1616 "k8s.io/client-go/util/homedir"
1717 "os"
1818 "path/filepath"
19+ "strconv"
1920 "time"
2021)
2122
2223// variables: namespace, labels
2324var dovecotLabels string
2425var dovecotDirectorLabels string
2526var dovecotDirectorContainerName string
26-
27+ var syncFrequencyDuration int
2728
2829var namespace string
2930var kubeconf * rest.Config
@@ -42,13 +43,23 @@ func main() {
4243 dovecotDirectorLabels = os .Getenv ("DOVECOT_DIRECTOR_LABELS" )
4344 dovecotDirectorContainerName = os .Getenv ("DOVECOT_DIRECTOR_CONTAINER_NAME" )
4445
46+ syncFrequencyDurationEnv := os .Getenv ("SYNC_FREQUENCY_DURATION" )
47+ syncFrequencyDuration = 70
48+ if syncFrequencyDurationEnv != "" {
49+ syncFrequencyDuration , err = strconv .Atoi (syncFrequencyDurationEnv )
50+ if err != nil {
51+ syncFrequencyDuration = 70
52+ }
53+ }
54+
4555 dovecotLabels = os .Getenv ("DOVECOT_LABELS" )
4656 namespace = os .Getenv ("DOVECOT_NAMESPACE" )
4757
4858 dovecotPods := GetPodsByLabel (clientset , namespace , dovecotLabels )
4959 initialDovecotPodCount = len (dovecotPods .Items )
5060
51- StartWatcher (clientset , namespace )
61+ go StartWatcherSecret (clientset , namespace )
62+ StartWatcherPods (clientset , namespace )
5263}
5364
5465func GetPodsByLabel (clientset * kubernetes.Clientset , namespace string , labels string ) * v1.PodList {
@@ -73,11 +84,11 @@ func ExecuteCommand(command string, podname string, namespace string, clientset
7384 // THE FOLLOWING EXPECTS THE POD TO HAVE ONLY ONE CONTAINER IN WHICH THE COMMAND IS GOING TO BE EXECUTED
7485 option := & v1.PodExecOptions {
7586 Container : dovecotDirectorContainerName ,
76- Command : cmd ,
77- Stdin : false ,
78- Stdout : true ,
79- Stderr : true ,
80- TTY : true ,
87+ Command : cmd ,
88+ Stdin : false ,
89+ Stdout : true ,
90+ Stderr : true ,
91+ TTY : true ,
8192 }
8293
8394 req .VersionedParams (
@@ -116,37 +127,42 @@ func handleEvent(pod *v1.Pod, clientset *kubernetes.Clientset) {
116127 containerStatusSlice := pod .Status .ContainerStatuses
117128
118129 for _ , containerStatus := range containerStatusSlice {
119-
120130 if containerStatus .Ready {
121- podlist := GetPodsByLabel (clientset , namespace , dovecotDirectorLabels )
122-
123- for _ , dovecotDirectorPod := range podlist .Items {
124- time := time .Now ()
125- logLevel := "info"
126- logMessage := "success"
127- formattedTime := time .Format ("2006-01-02 15:04:05 MST" )
128-
129- err := ExecuteCommand (
130- "doveadm reload" ,
131- dovecotDirectorPod .ObjectMeta .Name ,
132- namespace ,
133- clientset )
134-
135- if err != nil {
136- logLevel = "error"
137- logMessage = err .Error ()
138- }
139-
140- log := fmt .Sprintf ("{ \" level\" : \" %s\" , \" timestamp\" : \" %s\" , \" pod\" : \" %s\" , \" command\" : \" doveadm reload\" , \" message\" : \" %s\" }" , logLevel , formattedTime , dovecotDirectorPod .ObjectMeta .Name , logMessage )
141- fmt .Println (log )
142- }
131+ ExecuteDoveAdm (clientset , dovecotDirectorLabels , 0 )
143132 }
144133 }
145134 }
135+ }
146136
137+ func ExecuteDoveAdm (clientset * kubernetes.Clientset , dovecotDirectorLabels string , sleeptime int ) {
138+ if sleeptime != 0 {
139+ time .Sleep (time .Second * time .Duration (int64 (sleeptime )))
140+ }
141+ podlist := GetPodsByLabel (clientset , namespace , dovecotDirectorLabels )
142+
143+ for _ , dovecotDirectorPod := range podlist .Items {
144+ curTime := time .Now ()
145+ logLevel := "info"
146+ logMessage := "success"
147+ formattedTime := curTime .Format ("2006-01-02 15:04:05 MST" )
148+
149+ err := ExecuteCommand (
150+ "doveadm reload" ,
151+ dovecotDirectorPod .ObjectMeta .Name ,
152+ namespace ,
153+ clientset )
154+
155+ if err != nil {
156+ logLevel = "error"
157+ logMessage = err .Error ()
158+ }
159+
160+ log := fmt .Sprintf ("{ \" level\" : \" %s\" , \" timestamp\" : \" %s\" , \" pod\" : \" %s\" , \" command\" : \" doveadm reload\" , \" message\" : \" %s\" }" , logLevel , formattedTime , dovecotDirectorPod .ObjectMeta .Name , logMessage )
161+ fmt .Println (log )
162+ }
147163}
148164
149- func StartWatcher (clientset * kubernetes.Clientset , namespace string ) ( ) {
165+ func StartWatcherPods (clientset * kubernetes.Clientset , namespace string ) {
150166 optionsModifierFunc := func (options * metav1.ListOptions ) {
151167 options .LabelSelector = dovecotLabels
152168 }
@@ -178,6 +194,38 @@ func StartWatcher(clientset *kubernetes.Clientset, namespace string) () {
178194 }
179195}
180196
197+ func StartWatcherSecret (clientset * kubernetes.Clientset , namespace string ) {
198+ watchlist := cache .NewFilteredListWatchFromClient (
199+ clientset .CoreV1 ().RESTClient (),
200+ "secrets" ,
201+ namespace ,
202+ func (options * metav1.ListOptions ) {})
203+
204+ _ , controller := cache .NewInformer (
205+ watchlist ,
206+ & v1.Secret {},
207+ time .Second * 0 ,
208+ cache.ResourceEventHandlerFuncs {
209+ AddFunc : func (obj interface {}) {
210+ secret := obj .(* v1.Secret )
211+ if secret .Type == "kubernetes.io/tls" {
212+ go ExecuteDoveAdm (clientset , dovecotDirectorLabels , syncFrequencyDuration )
213+ }
214+ },
215+ UpdateFunc : func (oldObj , newObj interface {}) {
216+ secret := newObj .(* v1.Secret )
217+ if secret .Type == "kubernetes.io/tls" {
218+ go ExecuteDoveAdm (clientset , dovecotDirectorLabels , syncFrequencyDuration )
219+ }
220+ },
221+ },
222+ )
223+
224+ go controller .Run (make (chan struct {}))
225+ for {
226+ time .Sleep (time .Second )
227+ }
228+ }
181229
182230func InClusterAuth () (* kubernetes.Clientset , error ) {
183231 var err error
0 commit comments