Skip to content

Commit 440ca38

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

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
@@ -58,6 +58,7 @@ type Config struct {
5858
openshiftRoutesAvailability openshift.RoutesAvailability
5959
labelsFilter []string
6060
annotationsFilter []string
61+
namespaces []string
6162
}
6263

6364
// New constructs a new configuration based on the given options.
@@ -99,6 +100,7 @@ func New(opts ...Option) Config {
99100
autoInstrumentationNginxImage: o.autoInstrumentationNginxImage,
100101
labelsFilter: o.labelsFilter,
101102
annotationsFilter: o.annotationsFilter,
103+
namespaces: o.namespaces,
102104
}
103105
}
104106

@@ -218,3 +220,8 @@ func (c *Config) LabelsFilter() []string {
218220
func (c *Config) AnnotationsFilter() []string {
219221
return c.annotationsFilter
220222
}
223+
224+
// Namespaces Returns the namespaces to be watched.
225+
func (c *Config) Namespaces() []string {
226+
return c.namespaces
227+
}

internal/config/options.go

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type options struct {
5353
openshiftRoutesAvailability openshift.RoutesAvailability
5454
labelsFilter []string
5555
annotationsFilter []string
56+
namespaces []string
5657
}
5758

5859
func WithAutoDetect(a autodetect.AutoDetect) Option {
@@ -219,3 +220,9 @@ func WithAnnotationFilters(annotationFilters []string) Option {
219220
o.annotationsFilter = filters
220221
}
221222
}
223+
224+
func WithNamespaces(namespaces []string) Option {
225+
return func(o *options) {
226+
o.namespaces = namespaces
227+
}
228+
}

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"
@@ -194,6 +195,21 @@ func main() {
194195
os.Exit(1)
195196
}
196197

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

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

0 commit comments

Comments
 (0)