Skip to content

Commit 464ccb4

Browse files
committed
Add check for watch_namespace before mutating Pod
Signed-off-by: Janario Oliveira <[email protected]>
1 parent 18e50b0 commit 464ccb4

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

internal/config/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type Config struct {
5454
autoInstrumentationJavaImage string
5555
openshiftRoutesAvailability openshift.RoutesAvailability
5656
labelsFilter []string
57+
namespaces []string
5758
}
5859

5960
// New constructs a new configuration based on the given options.
@@ -91,6 +92,7 @@ func New(opts ...Option) Config {
9192
autoInstrumentationApacheHttpdImage: o.autoInstrumentationApacheHttpdImage,
9293
autoInstrumentationNginxImage: o.autoInstrumentationNginxImage,
9394
labelsFilter: o.labelsFilter,
95+
namespaces: o.namespaces,
9496
}
9597
}
9698

@@ -190,3 +192,8 @@ func (c *Config) AutoInstrumentationNginxImage() string {
190192
func (c *Config) LabelsFilter() []string {
191193
return c.labelsFilter
192194
}
195+
196+
// Namespaces Returns the namespaces to be watched.
197+
func (c *Config) Namespaces() []string {
198+
return c.namespaces
199+
}

internal/config/options.go

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type options struct {
4949
operatorOpAMPBridgeImage string
5050
openshiftRoutesAvailability openshift.RoutesAvailability
5151
labelsFilter []string
52+
namespaces []string
5253
}
5354

5455
func WithAutoDetect(a autodetect.AutoDetect) Option {
@@ -179,3 +180,9 @@ func WithLabelFilters(labelFilters []string) Option {
179180
o.labelsFilter = filters
180181
}
181182
}
183+
184+
func WithNamespaces(namespaces []string) Option {
185+
return func(o *options) {
186+
o.namespaces = namespaces
187+
}
188+
}

internal/webhook/podmutation/webhookhandler.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ package podmutation
1818
import (
1919
"context"
2020
"encoding/json"
21-
"net/http"
22-
2321
"github.com/go-logr/logr"
2422
corev1 "k8s.io/api/core/v1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2524
"k8s.io/apimachinery/pkg/types"
25+
"net/http"
2626
"sigs.k8s.io/controller-runtime/pkg/client"
2727
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
28+
"slices"
2829

2930
"github.com/open-telemetry/opentelemetry-operator/internal/config"
3031
)
@@ -88,7 +89,12 @@ func (p *podMutationWebhook) Handle(ctx context.Context, req admission.Request)
8889
return res
8990
}
9091

91-
for _, m := range p.podMutators {
92+
var mutators []PodMutator
93+
// mutate only in case the namespace is marked to be watched
94+
if slices.Contains(p.config.Namespaces(), ns.Name) || slices.Contains(p.config.Namespaces(), metav1.NamespaceAll) {
95+
mutators = p.podMutators
96+
}
97+
for _, m := range mutators {
9298
pod, err = m.Mutate(ctx, ns, pod)
9399
if err != nil {
94100
res := admission.Errored(http.StatusInternalServerError, err)

main.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"crypto/tls"
2020
"flag"
2121
"fmt"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2223
"os"
2324
"runtime"
2425
"strings"
@@ -177,6 +178,21 @@ func main() {
177178
os.Exit(1)
178179
}
179180

181+
var watchNamespaces []string
182+
var namespaces map[string]cache.Config
183+
watchNamespace, found := os.LookupEnv("WATCH_NAMESPACE")
184+
if found {
185+
setupLog.Info("watching namespace(s)", "namespaces", watchNamespace)
186+
namespaces = map[string]cache.Config{}
187+
watchNamespaces = strings.Split(watchNamespace, ",")
188+
for _, ns := range watchNamespaces {
189+
namespaces[ns] = cache.Config{}
190+
}
191+
} else {
192+
setupLog.Info("the env var WATCH_NAMESPACE isn't set, watching all namespaces")
193+
watchNamespaces = []string{metav1.NamespaceAll}
194+
}
195+
180196
cfg := config.New(
181197
config.WithLogger(ctrl.Log.WithName("config")),
182198
config.WithVersion(v),
@@ -194,24 +210,13 @@ func main() {
194210
config.WithAutoInstrumentationNginxImage(autoInstrumentationNginx),
195211
config.WithAutoDetect(ad),
196212
config.WithLabelFilters(labelsFilter),
213+
config.WithNamespaces(watchNamespaces),
197214
)
198215
err = cfg.AutoDetect()
199216
if err != nil {
200217
setupLog.Error(err, "failed to autodetect config variables")
201218
}
202219

203-
var namespaces map[string]cache.Config
204-
watchNamespace, found := os.LookupEnv("WATCH_NAMESPACE")
205-
if found {
206-
setupLog.Info("watching namespace(s)", "namespaces", watchNamespace)
207-
namespaces = map[string]cache.Config{}
208-
for _, ns := range strings.Split(watchNamespace, ",") {
209-
namespaces[ns] = cache.Config{}
210-
}
211-
} else {
212-
setupLog.Info("the env var WATCH_NAMESPACE isn't set, watching all namespaces")
213-
}
214-
215220
// see https://github.com/openshift/library-go/blob/4362aa519714a4b62b00ab8318197ba2bba51cb7/pkg/config/leaderelection/leaderelection.go#L104
216221
leaseDuration := time.Second * 137
217222
renewDeadline := time.Second * 107

0 commit comments

Comments
 (0)