Skip to content

Commit 8da0764

Browse files
committed
MINOR: expect any podname format in GetPodPrefix
1 parent 114abe4 commit 8da0764

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

controller/kubernetes.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ func (k *K8s) EventsSecrets(channel chan SyncDataEvent, stop chan struct{}, info
740740
}
741741

742742
func (k *K8s) EventPods(namespace, podPrefix string, resyncPeriod time.Duration, eventChan chan SyncDataEvent) {
743+
var prefix string
744+
if podPrefix == "" {
745+
return
746+
}
743747
watchlist := cache.NewListWatchFromClient(k.API.CoreV1().RESTClient(), "pods", namespace, fields.Nothing())
744748
_, eController := cache.NewInformer(
745749
watchlist,
@@ -748,15 +752,16 @@ func (k *K8s) EventPods(namespace, podPrefix string, resyncPeriod time.Duration,
748752
cache.ResourceEventHandlerFuncs{
749753
AddFunc: func(obj interface{}) {
750754
meta := obj.(*corev1.Pod).ObjectMeta
751-
if utils.GetPodPrefix(meta.Name) != podPrefix {
755+
prefix, _ = utils.GetPodPrefix(meta.Name)
756+
if prefix != podPrefix {
752757
return
753758
}
754759
eventChan <- SyncDataEvent{SyncType: POD, Namespace: meta.Namespace, Data: store.PodEvent{Created: true}}
755760
},
756761
DeleteFunc: func(obj interface{}) {
757762
meta := obj.(*corev1.Pod).ObjectMeta
758-
759-
if utils.GetPodPrefix(meta.Name) != podPrefix {
763+
prefix, _ = utils.GetPodPrefix(meta.Name)
764+
if prefix != podPrefix {
760765
return
761766
}
762767
eventChan <- SyncDataEvent{SyncType: POD, Namespace: meta.Namespace, Data: store.PodEvent{}}

controller/utils/utils.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package utils
1616

1717
import (
1818
"encoding/hex"
19+
"fmt"
1920
"hash/fnv"
2021
"os"
2122
"strconv"
@@ -116,11 +117,17 @@ func GetBoolValue(dataValue, dataName string) (result bool, err error) {
116117
return result, nil
117118
}
118119

119-
func GetPodPrefix(podName string) string {
120+
func GetPodPrefix(podName string) (prefix string, err error) {
120121
i := strings.LastIndex(podName, "-")
121122
if i == -1 {
122-
return ""
123+
err = fmt.Errorf("incorrect podName format: '%s'", podName)
124+
return
123125
}
124126
i = strings.LastIndex(string([]rune(podName)[:i]), "-")
125-
return string([]rune(podName)[:i])
127+
if i == -1 {
128+
err = fmt.Errorf("incorrect podName format: '%s'", podName)
129+
return
130+
}
131+
prefix = string([]rune(podName)[:i])
132+
return
126133
}

main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,14 @@ func main() {
150150
}
151151
logger.Error(os.Chdir(cfg.Env.CfgDir))
152152

153+
prefix, errPrefix := utils.GetPodPrefix(podName)
154+
logger.Error(errPrefix)
155+
153156
controller := c.HAProxyController{
154157
Cfg: cfg,
155158
OSArgs: osArgs,
156159
PodNamespace: os.Getenv("POD_NAMESPACE"),
157-
PodPrefix: utils.GetPodPrefix(podName),
158-
}
160+
PodPrefix: prefix}
159161
logger.FileName = true
160162
// K8s Store
161163
s := store.NewK8sStore(osArgs)

0 commit comments

Comments
 (0)