Skip to content
This repository was archived by the owner on May 24, 2025. It is now read-only.

Commit 8030ab2

Browse files
committed
#5 added another watcher for secrets to check for events regarding them. Works, but ugly and no README update yet
1 parent b3a1b4c commit 8030ab2

File tree

5 files changed

+125
-42
lines changed

5 files changed

+125
-42
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
go.sum
33
main
44
vendor
5+
.env

cmd/main.go

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2324
var dovecotLabels string
2425
var dovecotDirectorLabels string
2526
var dovecotDirectorContainerName string
26-
27+
var syncFrequencyDuration int
2728

2829
var namespace string
2930
var 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

5465
func 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

182230
func InClusterAuth() (*kubernetes.Clientset, error) {
183231
var err error

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.15.3-alpine3.12 as builder
1+
FROM golang:1.19.2-alpine as builder
22

33
WORKDIR /workdir/go
44

docker/docker-compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ services:
1212
- DOVECOT_LABELS=app.kubernetes.io/instance=test,app.kubernetes.io/name=dovecot
1313
- DOVECOT_DIRECTOR_LABELS=app.kubernetes.io/instance=test,app.kubernetes.io/name=dovecot-director
1414
- DOVECOT_DIRECTOR_CONTAINER_NAME=dovecot-director
15+
- SYNC_FREQUENCY_DURATION=80

go.mod

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
11
module cmd/main.go
22

3-
go 1.15
3+
go 1.19
44

55
require (
6-
github.com/golang/protobuf v1.4.3 // indirect
7-
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
8-
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 // indirect
9-
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 // indirect
10-
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
11-
k8s.io/api v0.19.2
12-
k8s.io/apimachinery v0.19.2
13-
k8s.io/client-go v0.19.2
14-
k8s.io/utils v0.0.0-20201015054608-420da100c033 // indirect
6+
github.com/PuerkitoBio/purell v1.1.1 // indirect
7+
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
10+
github.com/go-logr/logr v1.2.3 // indirect
11+
github.com/go-openapi/jsonpointer v0.19.5 // indirect
12+
github.com/go-openapi/jsonreference v0.19.5 // indirect
13+
github.com/go-openapi/swag v0.19.14 // indirect
14+
github.com/gogo/protobuf v1.3.2 // indirect
15+
github.com/golang/protobuf v1.5.2 // indirect
16+
github.com/google/gnostic v0.5.7-v3refs // indirect
17+
github.com/google/go-cmp v0.5.8 // indirect
18+
github.com/google/gofuzz v1.1.0 // indirect
19+
github.com/imdario/mergo v0.3.6 // indirect
20+
github.com/josharian/intern v1.0.0 // indirect
21+
github.com/json-iterator/go v1.1.12 // indirect
22+
github.com/mailru/easyjson v0.7.6 // indirect
23+
github.com/moby/spdystream v0.2.0 // indirect
24+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
25+
github.com/modern-go/reflect2 v1.0.2 // indirect
26+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
27+
github.com/spf13/pflag v1.0.5 // indirect
28+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
29+
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
30+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
31+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
32+
golang.org/x/text v0.3.7 // indirect
33+
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
34+
google.golang.org/appengine v1.6.7 // indirect
35+
google.golang.org/protobuf v1.28.0 // indirect
36+
gopkg.in/inf.v0 v0.9.1 // indirect
37+
gopkg.in/yaml.v2 v2.4.0 // indirect
38+
gopkg.in/yaml.v3 v3.0.1 // indirect
39+
k8s.io/api v0.25.3 // indirect
40+
k8s.io/apimachinery v0.25.3 // indirect
41+
k8s.io/client-go v0.25.3 // indirect
42+
k8s.io/klog/v2 v2.70.1 // indirect
43+
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
44+
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
45+
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
46+
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
47+
sigs.k8s.io/yaml v1.2.0 // indirect
1548
)

0 commit comments

Comments
 (0)