Skip to content

Commit 87dba94

Browse files
committed
Add check for watch_namespace before mutating Pod
Signed-off-by: Janario Oliveira <[email protected]>
1 parent fb23fde commit 87dba94

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
@@ -59,6 +59,7 @@ type Config struct {
5959
openshiftRoutesAvailability openshift.RoutesAvailability
6060
labelsFilter []string
6161
annotationsFilter []string
62+
namespaces []string
6263
}
6364

6465
// New constructs a new configuration based on the given options.
@@ -101,6 +102,7 @@ func New(opts ...Option) Config {
101102
autoInstrumentationNginxImage: o.autoInstrumentationNginxImage,
102103
labelsFilter: o.labelsFilter,
103104
annotationsFilter: o.annotationsFilter,
105+
namespaces: o.namespaces,
104106
}
105107
}
106108

@@ -225,3 +227,8 @@ func (c *Config) LabelsFilter() []string {
225227
func (c *Config) AnnotationsFilter() []string {
226228
return c.annotationsFilter
227229
}
230+
231+
// Namespaces Returns the namespaces to be watched.
232+
func (c *Config) Namespaces() []string {
233+
return c.namespaces
234+
}

internal/config/options.go

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

5960
func WithAutoDetect(a autodetect.AutoDetect) Option {
@@ -225,3 +226,9 @@ func WithAnnotationFilters(annotationFilters []string) Option {
225226
o.annotationsFilter = filters
226227
}
227228
}
229+
230+
func WithNamespaces(namespaces []string) Option {
231+
return func(o *options) {
232+
o.namespaces = namespaces
233+
}
234+
}

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
)
@@ -89,7 +90,12 @@ func (p *podMutationWebhook) Handle(ctx context.Context, req admission.Request)
8990
return res
9091
}
9192

92-
for _, m := range p.podMutators {
93+
var mutators []PodMutator
94+
// mutate only in case the namespace is marked to be watched
95+
if slices.Contains(p.config.Namespaces(), ns.Name) || slices.Contains(p.config.Namespaces(), metav1.NamespaceAll) {
96+
mutators = p.podMutators
97+
}
98+
for _, m := range mutators {
9399
pod, err = m.Mutate(ctx, ns, pod)
94100
if err != nil {
95101
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"
@@ -195,6 +196,21 @@ func main() {
195196
os.Exit(1)
196197
}
197198

199+
var watchNamespaces []string
200+
var namespaces map[string]cache.Config
201+
watchNamespace, found := os.LookupEnv("WATCH_NAMESPACE")
202+
if found {
203+
setupLog.Info("watching namespace(s)", "namespaces", watchNamespace)
204+
namespaces = map[string]cache.Config{}
205+
watchNamespaces = strings.Split(watchNamespace, ",")
206+
for _, ns := range watchNamespaces {
207+
namespaces[ns] = cache.Config{}
208+
}
209+
} else {
210+
setupLog.Info("the env var WATCH_NAMESPACE isn't set, watching all namespaces")
211+
watchNamespaces = []string{metav1.NamespaceAll}
212+
}
213+
198214
cfg := config.New(
199215
config.WithLogger(ctrl.Log.WithName("config")),
200216
config.WithVersion(v),
@@ -217,24 +233,13 @@ func main() {
217233
config.WithAutoDetect(ad),
218234
config.WithLabelFilters(labelsFilter),
219235
config.WithAnnotationFilters(annotationsFilter),
236+
config.WithNamespaces(watchNamespaces),
220237
)
221238
err = cfg.AutoDetect()
222239
if err != nil {
223240
setupLog.Error(err, "failed to autodetect config variables")
224241
}
225242

226-
var namespaces map[string]cache.Config
227-
watchNamespace, found := os.LookupEnv("WATCH_NAMESPACE")
228-
if found {
229-
setupLog.Info("watching namespace(s)", "namespaces", watchNamespace)
230-
namespaces = map[string]cache.Config{}
231-
for _, ns := range strings.Split(watchNamespace, ",") {
232-
namespaces[ns] = cache.Config{}
233-
}
234-
} else {
235-
setupLog.Info("the env var WATCH_NAMESPACE isn't set, watching all namespaces")
236-
}
237-
238243
// see https://github.com/openshift/library-go/blob/4362aa519714a4b62b00ab8318197ba2bba51cb7/pkg/config/leaderelection/leaderelection.go#L104
239244
leaseDuration := time.Second * 137
240245
renewDeadline := time.Second * 107

0 commit comments

Comments
 (0)