2025-01-29 23:44:09 +02:00
|
|
|
/*
|
|
|
|
|
Copyright 2025.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/tls"
|
2026-03-18 12:00:31 +01:00
|
|
|
"errors"
|
2025-01-29 23:44:09 +02:00
|
|
|
"flag"
|
2025-03-06 10:57:45 +02:00
|
|
|
"fmt"
|
2025-01-29 23:44:09 +02:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2025-10-07 13:59:50 +03:00
|
|
|
"strings"
|
2025-01-29 23:44:09 +02:00
|
|
|
|
|
|
|
|
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
|
|
|
|
|
// to ensure that exec-entrypoint and run can make use of them.
|
|
|
|
|
_ "k8s.io/client-go/plugin/pkg/client/auth"
|
|
|
|
|
|
2026-03-16 11:23:26 +01:00
|
|
|
netbirdrest "github.com/netbirdio/netbird/shared/management/client/rest"
|
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
2025-01-29 23:44:09 +02:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
|
|
|
|
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
|
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
|
2026-04-13 12:20:35 +02:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2025-01-29 23:44:09 +02:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/healthz"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
|
|
|
|
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
|
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/webhook"
|
2026-03-19 13:01:58 +01:00
|
|
|
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
|
2026-03-23 09:59:48 +01:00
|
|
|
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
|
2025-01-29 23:44:09 +02:00
|
|
|
|
|
|
|
|
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
2026-04-13 12:20:35 +02:00
|
|
|
netbirdiov1alpha1 "github.com/netbirdio/kubernetes-operator/api/v1alpha1"
|
2025-01-29 23:44:09 +02:00
|
|
|
"github.com/netbirdio/kubernetes-operator/internal/controller"
|
|
|
|
|
webhooknetbirdiov1 "github.com/netbirdio/kubernetes-operator/internal/webhook/v1"
|
|
|
|
|
// +kubebuilder:scaffold:imports
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
scheme = runtime.NewScheme()
|
|
|
|
|
setupLog = ctrl.Log.WithName("setup")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
|
|
|
|
|
|
|
|
|
|
utilruntime.Must(netbirdiov1.AddToScheme(scheme))
|
|
|
|
|
utilruntime.Must(corev1.AddToScheme(scheme))
|
2026-03-19 13:01:58 +01:00
|
|
|
utilruntime.Must(gatewayv1.Install(scheme))
|
2026-03-23 09:59:48 +01:00
|
|
|
utilruntime.Must(gatewayv1alpha2.Install(scheme))
|
2026-04-13 12:20:35 +02:00
|
|
|
utilruntime.Must(netbirdiov1alpha1.AddToScheme(scheme))
|
2025-01-29 23:44:09 +02:00
|
|
|
// +kubebuilder:scaffold:scheme
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// nolint:gocyclo
|
|
|
|
|
func main() {
|
|
|
|
|
// NB Specific flags
|
|
|
|
|
var (
|
2026-03-18 12:00:31 +01:00
|
|
|
runtimeNamespace string
|
2025-04-13 23:00:23 +02:00
|
|
|
managementURL string
|
|
|
|
|
clientImage string
|
|
|
|
|
clusterName string
|
|
|
|
|
namespacedNetworks bool
|
|
|
|
|
clusterDNS string
|
|
|
|
|
netbirdAPIKey string
|
|
|
|
|
allowAutomaticPolicyCreation bool
|
2025-10-07 13:59:50 +03:00
|
|
|
defaultLabels string
|
2026-03-19 18:28:35 +01:00
|
|
|
gatewayAPIEnabled bool
|
2025-01-29 23:44:09 +02:00
|
|
|
)
|
2026-03-18 12:00:31 +01:00
|
|
|
flag.StringVar(&runtimeNamespace, "runtime-namespace", "", "Namespace the controller is running in")
|
2025-01-29 23:44:09 +02:00
|
|
|
flag.StringVar(&managementURL, "netbird-management-url", "https://api.netbird.io", "Management service URL")
|
|
|
|
|
flag.StringVar(&clientImage, "netbird-client-image", "netbirdio/netbird:latest", "Image for netbird client container")
|
2025-03-06 10:57:45 +02:00
|
|
|
flag.StringVar(
|
|
|
|
|
&clusterName,
|
|
|
|
|
"cluster-name",
|
|
|
|
|
"kubernetes",
|
|
|
|
|
"User-friendly name for kubernetes cluster for NetBird resource creation",
|
|
|
|
|
)
|
|
|
|
|
flag.BoolVar(
|
|
|
|
|
&namespacedNetworks,
|
|
|
|
|
"namespaced-networks",
|
|
|
|
|
false,
|
|
|
|
|
"Create NetBird Network per namespace, set to true if a NetworkPolicy exists that would require this",
|
|
|
|
|
)
|
|
|
|
|
flag.StringVar(&clusterDNS, "cluster-dns", "svc.cluster.local", "Cluster DNS name")
|
|
|
|
|
flag.StringVar(&netbirdAPIKey, "netbird-api-key", "", "API key for NetBird API operations")
|
2025-04-13 23:00:23 +02:00
|
|
|
flag.BoolVar(
|
|
|
|
|
&allowAutomaticPolicyCreation,
|
|
|
|
|
"allow-automatic-policy-creation",
|
|
|
|
|
false,
|
|
|
|
|
"Allow creating NBPolicy resources from annotations on Services",
|
|
|
|
|
)
|
2025-10-07 13:59:50 +03:00
|
|
|
flag.StringVar(
|
|
|
|
|
&defaultLabels,
|
|
|
|
|
"default-labels",
|
|
|
|
|
"",
|
|
|
|
|
"Default labels used for all resources, in format key=value,key=value",
|
|
|
|
|
)
|
2026-03-19 18:28:35 +01:00
|
|
|
flag.BoolVar(&gatewayAPIEnabled, "gateway-api-enabled", false, "When true Gateway API resources will be reconciled.")
|
|
|
|
|
|
2025-01-29 23:44:09 +02:00
|
|
|
// Controller generic flags
|
|
|
|
|
var (
|
|
|
|
|
metricsAddr string
|
|
|
|
|
webhookCertPath string
|
|
|
|
|
webhookCertName string
|
|
|
|
|
webhookCertKey string
|
|
|
|
|
enableLeaderElection bool
|
|
|
|
|
probeAddr string
|
|
|
|
|
enableWebhooks bool
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
|
|
|
|
|
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
|
|
|
|
|
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
|
|
|
|
|
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
|
|
|
|
|
"Enable leader election for controller manager. "+
|
|
|
|
|
"Enabling this will ensure there is only one active controller manager.")
|
|
|
|
|
flag.StringVar(&webhookCertPath, "webhook-cert-path", "", "The directory that contains the webhook certificate.")
|
|
|
|
|
flag.StringVar(&webhookCertName, "webhook-cert-name", "tls.crt", "The name of the webhook certificate file.")
|
|
|
|
|
flag.StringVar(&webhookCertKey, "webhook-cert-key", "tls.key", "The name of the webhook key file.")
|
|
|
|
|
flag.BoolVar(&enableWebhooks, "enable-webhooks", true, "If set, enable Mutating and Validating webhooks.")
|
|
|
|
|
opts := zap.Options{
|
|
|
|
|
Development: true,
|
|
|
|
|
}
|
|
|
|
|
opts.BindFlags(flag.CommandLine)
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
2026-03-18 09:56:43 +01:00
|
|
|
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
|
|
|
|
|
|
2026-03-18 12:00:31 +01:00
|
|
|
runtimeNamespace, err := getRuntimeNamespace(runtimeNamespace)
|
|
|
|
|
if err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to get runtime namespace")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-07 13:59:50 +03:00
|
|
|
defaultLabelsMap := make(map[string]string)
|
|
|
|
|
if defaultLabels != "" {
|
2026-03-12 21:40:36 +01:00
|
|
|
for s := range strings.SplitSeq(defaultLabels, ",") {
|
2025-10-07 13:59:50 +03:00
|
|
|
kv := strings.Split(s, "=")
|
|
|
|
|
if len(kv) != 2 {
|
|
|
|
|
panic(fmt.Errorf("invalid label format: %s", s))
|
|
|
|
|
}
|
|
|
|
|
defaultLabelsMap[kv[0]] = kv[1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 09:56:43 +01:00
|
|
|
// Setup webhook server.
|
|
|
|
|
type TLSOption = func(*tls.Config)
|
|
|
|
|
certWatcher, tlsOpt, err := func() (*certwatcher.CertWatcher, TLSOption, error) {
|
|
|
|
|
if webhookCertPath == "" {
|
|
|
|
|
return nil, nil, nil
|
|
|
|
|
}
|
2025-01-29 23:44:09 +02:00
|
|
|
|
2026-03-18 09:56:43 +01:00
|
|
|
certWatcher, err := certwatcher.New(
|
2025-01-29 23:44:09 +02:00
|
|
|
filepath.Join(webhookCertPath, webhookCertName),
|
|
|
|
|
filepath.Join(webhookCertPath, webhookCertKey),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
2026-03-18 09:56:43 +01:00
|
|
|
return nil, nil, err
|
2025-01-29 23:44:09 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-18 09:56:43 +01:00
|
|
|
tlsOpt := func(config *tls.Config) {
|
|
|
|
|
config.GetCertificate = certWatcher.GetCertificate
|
|
|
|
|
}
|
2025-01-29 23:44:09 +02:00
|
|
|
|
2026-03-18 09:56:43 +01:00
|
|
|
return certWatcher, tlsOpt, nil
|
|
|
|
|
}()
|
|
|
|
|
if err != nil {
|
|
|
|
|
setupLog.Error(err, "Failed to initialize webhook certificate watcher")
|
|
|
|
|
os.Exit(1)
|
2025-01-29 23:44:09 +02:00
|
|
|
}
|
2026-03-18 09:56:43 +01:00
|
|
|
webhookServer := webhook.NewServer(webhook.Options{TLSOpts: []TLSOption{tlsOpt}})
|
2025-01-29 23:44:09 +02:00
|
|
|
|
2026-03-18 09:56:43 +01:00
|
|
|
// Setup controller manager.
|
2025-01-29 23:44:09 +02:00
|
|
|
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
|
2026-03-18 09:56:43 +01:00
|
|
|
Scheme: scheme,
|
|
|
|
|
Metrics: metricsserver.Options{
|
|
|
|
|
BindAddress: metricsAddr,
|
|
|
|
|
},
|
2026-04-13 12:20:35 +02:00
|
|
|
Client: client.Options{
|
|
|
|
|
FieldOwner: "netbird-operator",
|
|
|
|
|
},
|
2026-03-18 12:00:31 +01:00
|
|
|
WebhookServer: webhookServer,
|
|
|
|
|
HealthProbeBindAddress: probeAddr,
|
|
|
|
|
LeaderElectionNamespace: runtimeNamespace,
|
|
|
|
|
LeaderElection: enableLeaderElection,
|
|
|
|
|
LeaderElectionID: "operator.netbird.io",
|
2025-01-29 23:44:09 +02:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to start manager")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nbSetupKeyController := &controller.NBSetupKeyReconciler{
|
|
|
|
|
Client: mgr.GetClient(),
|
|
|
|
|
}
|
|
|
|
|
if err = nbSetupKeyController.SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "NBSetupKey")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if enableWebhooks {
|
2026-02-27 19:07:58 +01:00
|
|
|
if err = webhooknetbirdiov1.SetupPodWebhookWithManager(mgr, managementURL, clientImage); err != nil {
|
2025-01-29 23:44:09 +02:00
|
|
|
setupLog.Error(err, "unable to create webhook", "webhook", "Pod")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-10 21:31:54 +01:00
|
|
|
|
2025-03-06 10:57:45 +02:00
|
|
|
if len(netbirdAPIKey) > 0 {
|
2026-03-16 11:23:26 +01:00
|
|
|
netbird := netbirdrest.New(managementURL, netbirdAPIKey)
|
|
|
|
|
|
2025-03-06 10:57:45 +02:00
|
|
|
if err = (&controller.NBRoutingPeerReconciler{
|
|
|
|
|
Client: mgr.GetClient(),
|
2026-03-16 11:23:26 +01:00
|
|
|
Netbird: netbird,
|
2025-03-06 10:57:45 +02:00
|
|
|
ClientImage: clientImage,
|
|
|
|
|
ClusterName: clusterName,
|
|
|
|
|
ManagementURL: managementURL,
|
|
|
|
|
NamespacedNetworks: namespacedNetworks,
|
2025-10-07 13:59:50 +03:00
|
|
|
DefaultLabels: defaultLabelsMap,
|
2025-03-06 10:57:45 +02:00
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "NBRoutingPeer")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = (&controller.ServiceReconciler{
|
|
|
|
|
Client: mgr.GetClient(),
|
|
|
|
|
ClusterName: clusterName,
|
|
|
|
|
ClusterDNS: clusterDNS,
|
|
|
|
|
NamespacedNetworks: namespacedNetworks,
|
2026-03-18 12:00:31 +01:00
|
|
|
ControllerNamespace: runtimeNamespace,
|
2025-10-07 13:59:50 +03:00
|
|
|
DefaultLabels: defaultLabelsMap,
|
2025-03-06 10:57:45 +02:00
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "Service")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = (&controller.NBResourceReconciler{
|
2025-04-13 23:00:23 +02:00
|
|
|
Client: mgr.GetClient(),
|
2026-03-16 11:23:26 +01:00
|
|
|
Netbird: netbird,
|
2025-04-13 23:00:23 +02:00
|
|
|
AllowAutomaticPolicyCreation: allowAutomaticPolicyCreation,
|
|
|
|
|
ClusterName: clusterName,
|
2025-10-07 13:59:50 +03:00
|
|
|
DefaultLabels: defaultLabelsMap,
|
2025-03-06 10:57:45 +02:00
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "NBResource")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = (&controller.NBGroupReconciler{
|
2026-03-16 11:23:26 +01:00
|
|
|
Client: mgr.GetClient(),
|
|
|
|
|
Netbird: netbird,
|
2025-03-06 10:57:45 +02:00
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "NBGroup")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = (&controller.NBPolicyReconciler{
|
2026-03-16 11:23:26 +01:00
|
|
|
Client: mgr.GetClient(),
|
|
|
|
|
Netbird: netbird,
|
2025-03-06 10:57:45 +02:00
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "NBPolicy")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if enableWebhooks {
|
2025-03-28 09:55:41 +02:00
|
|
|
if err = webhooknetbirdiov1.SetupNBGroupWebhookWithManager(mgr); err != nil {
|
2025-03-06 10:57:45 +02:00
|
|
|
setupLog.Error(err, "unable to create webhook", "webhook", "NBGroup")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-19 13:01:58 +01:00
|
|
|
|
2026-04-13 12:20:35 +02:00
|
|
|
if err := (&controller.SetupKeyReconciler{
|
|
|
|
|
Client: mgr.GetClient(),
|
|
|
|
|
Netbird: netbird,
|
|
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "Failed to create controller", "controller", "SetupKey")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2026-03-19 18:28:35 +01:00
|
|
|
if gatewayAPIEnabled {
|
|
|
|
|
if err = (&controller.GatewayClassReconciler{
|
|
|
|
|
Client: mgr.GetClient(),
|
|
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "GatewayClass")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
if err = (&controller.GatewayReconciler{
|
|
|
|
|
Client: mgr.GetClient(),
|
|
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "Gateway")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
if err = (&controller.HTTPRouteReconciler{
|
|
|
|
|
Client: mgr.GetClient(),
|
|
|
|
|
Netbird: netbird,
|
|
|
|
|
ClusterDNS: clusterDNS,
|
|
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "HTTPRoute")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2026-03-23 09:59:48 +01:00
|
|
|
if err = (&controller.TCPRouteReconciler{
|
|
|
|
|
Client: mgr.GetClient(),
|
|
|
|
|
ClusterDNS: clusterDNS,
|
|
|
|
|
}).SetupWithManager(mgr); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to create controller", "controller", "TCPRoute")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2026-03-19 13:01:58 +01:00
|
|
|
}
|
2025-03-06 10:57:45 +02:00
|
|
|
} else {
|
|
|
|
|
setupLog.Info("netbird API key not provided, ingress capabilities disabled")
|
|
|
|
|
}
|
2025-01-29 23:44:09 +02:00
|
|
|
// +kubebuilder:scaffold:builder
|
|
|
|
|
|
2026-03-18 09:56:43 +01:00
|
|
|
if certWatcher != nil {
|
2025-01-29 23:44:09 +02:00
|
|
|
setupLog.Info("Adding webhook certificate watcher to manager")
|
2026-03-18 09:56:43 +01:00
|
|
|
if err := mgr.Add(certWatcher); err != nil {
|
2025-01-29 23:44:09 +02:00
|
|
|
setupLog.Error(err, "unable to add webhook certificate watcher to manager")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
|
|
|
|
|
setupLog.Error(err, "unable to set up health check")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2026-03-18 09:56:43 +01:00
|
|
|
readyChecker := healthz.Ping
|
|
|
|
|
if certWatcher != nil {
|
|
|
|
|
readyChecker = mgr.GetWebhookServer().StartedChecker()
|
|
|
|
|
}
|
|
|
|
|
if err := mgr.AddReadyzCheck("readyz", readyChecker); err != nil {
|
2025-01-29 23:44:09 +02:00
|
|
|
setupLog.Error(err, "unable to set up ready check")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setupLog.Info("starting manager")
|
|
|
|
|
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
|
|
|
|
|
setupLog.Error(err, "problem running manager")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-06 10:57:45 +02:00
|
|
|
|
2026-03-18 12:00:31 +01:00
|
|
|
func getRuntimeNamespace(runtimeNamespace string) (string, error) {
|
|
|
|
|
if runtimeNamespace != "" {
|
|
|
|
|
return runtimeNamespace, nil
|
|
|
|
|
}
|
|
|
|
|
inClusterNamespacePath := "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
|
|
|
|
|
b, err := os.ReadFile(inClusterNamespacePath)
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
return "", fmt.Errorf("not running in-cluster, runtime namespace needs to be set")
|
2025-03-06 10:57:45 +02:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("error reading namespace file: %w", err)
|
|
|
|
|
}
|
2026-03-18 12:00:31 +01:00
|
|
|
return string(b), nil
|
2025-03-06 10:57:45 +02:00
|
|
|
}
|