Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/opensearch-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type: application
# This is the opensearch-operator chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 2.8.0
version: 2.8.1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not increment versions in a PR please


# This is the version number of the application being deployed (the operator). This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ spec:
- --metrics-bind-address=127.0.0.1:8080
- --leader-elect
{{- if .Values.manager.watchNamespace }}
{{- if kindIs "slice" .Values.manager.watchNamespace }}
- --watch-namespace={{ .Values.manager.watchNamespace | join "," }}
{{- else }}
- --watch-namespace={{ .Values.manager.watchNamespace }}
{{- end }}
{{- end }}
- --loglevel={{ .Values.manager.loglevel }}
command:
- /manager
Expand Down
4 changes: 4 additions & 0 deletions charts/opensearch-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ manager:

# If a watchNamespace is specified, the manager's cache will be restricted to
# watch objects in the desired namespace. Defaults is to watch all namespaces.
# To watch multiple namespaces, separate them by commas, or define it as a list.
# Examples:
# watchNamespace: ns1,ns2
# watchNamespace: [ns1, ns2]
watchNamespace:

# Install the Custom Resource Definitions with Helm
Expand Down
4 changes: 4 additions & 0 deletions docs/userguide/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ manager:
loglevel: info

# If specified, the operator will be restricted to watch objects only in the desired namespace. Defaults is to watch all namespaces.
# To watch multiple namespaces, either separate their name via commas or define it as a list.
# Examples:
# watchNamespaces: 'ns1,ns2'
# watchNamespace: [ns1, ns2]
watchNamespace:

# Configure extra environment variables for the operator. You can also pull them from secrets or configmaps
Expand Down
23 changes: 14 additions & 9 deletions opensearch-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"flag"
"fmt"
"os"
"strconv"
"strings"

"sigs.k8s.io/controller-runtime/pkg/cache"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"strconv"

"github.com/Opster/opensearch-k8s-operator/opensearch-operator/controllers"
"go.uber.org/zap/zapcore"
Expand All @@ -32,13 +34,14 @@ import (
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

"net/http"
_ "net/http/pprof"

opsterv1 "github.com/Opster/opensearch-k8s-operator/opensearch-operator/api/v1"
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"net/http"
_ "net/http/pprof"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
Expand Down Expand Up @@ -68,22 +71,24 @@ func main() {
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&watchNamespace, "watch-namespace", "",
"The namespace that controller manager is restricted to watch. If not set, default is to watch all namespaces.")
"The comma-separated list of namespaces that the controller manager is restricted to watch. If not set, default is to watch all namespaces.")
flag.StringVar(&logLevel, "loglevel", "info", "The log level to use for the operator logs. Possible values: debug,info,warn,error")

var cacheOpts cache.Options
if watchNamespace != "" {
cacheOpts.DefaultNamespaces = map[string]cache.Config{
watchNamespace: {},
}
}

opts := zap.Options{
Development: false,
TimeEncoder: zapcore.ISO8601TimeEncoder,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()

watchNamespaceList := strings.Split(watchNamespace, ",")
cacheOpts.DefaultNamespaces = make(map[string]cache.Config)
for _, watchNs := range watchNamespaceList {
cacheOpts.DefaultNamespaces[watchNs] = cache.Config{}
}

Comment on lines +86 to +91
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When watchNamespace is not set, it will be an empty string "". You end up with cacheOpts.DefaultNamespaces containing one entry with an empty string as the key: {"": {}}.

"error":"unable to list: default because of unknown namespace for the cache"

You need to check if watchNamespace is empty before processing it:

Suggested change
watchNamespaceList := strings.Split(watchNamespace, ",")
cacheOpts.DefaultNamespaces = make(map[string]cache.Config)
for _, watchNs := range watchNamespaceList {
cacheOpts.DefaultNamespaces[watchNs] = cache.Config{}
}
if watchNamespaces != "" {
watchNamespaceList := strings.Split(watchNamespace, ",")
cacheOpts.DefaultNamespaces = make(map[string]cache.Config)
for _, watchNs := range watchNamespaceList {
cacheOpts.DefaultNamespaces[watchNs] = cache.Config{}
}
}

level, err := zapcore.ParseLevel(logLevel)
if err != nil {
fmt.Printf("Invalid log level '%s'. Leaving on info", logLevel)
Expand Down