Skip to content

Commit 1f71c8d

Browse files
Avoid reading the namespace file every time the NamespacedName value is decoded. (zalando#243)
1 parent b0549c3 commit 1f71c8d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

pkg/spec/types.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ type ControllerConfig struct {
154154
Namespace string
155155
}
156156

157+
// cached value for the GetOperatorNamespace
158+
var operatorNamespace string
159+
157160
func (n NamespacedName) String() string {
158161
return types.NamespacedName(n).String()
159162
}
@@ -192,11 +195,12 @@ func (n *NamespacedName) DecodeWorker(value, operatorNamespace string) error {
192195
// GetOperatorNamespace assumes serviceaccount secret is mounted by kubernetes
193196
// Placing this func here instead of pgk/util avoids circular import
194197
func GetOperatorNamespace() string {
195-
196-
operatorNamespaceBytes, err := ioutil.ReadFile(fileWithNamespace)
197-
if err != nil {
198-
log.Fatalf("Unable to detect operator namespace from within its pod due to: %v", err)
198+
if operatorNamespace == "" {
199+
operatorNamespaceBytes, err := ioutil.ReadFile(fileWithNamespace)
200+
if err != nil {
201+
log.Fatalf("Unable to detect operator namespace from within its pod due to: %v", err)
202+
}
203+
operatorNamespace = string(operatorNamespaceBytes)
199204
}
200-
201-
return string(operatorNamespaceBytes)
205+
return operatorNamespace
202206
}

0 commit comments

Comments
 (0)