Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

detect in-cluster namespace by default, --system-namespace takes precedence if set #650

Merged
merged 1 commit into from
Jul 6, 2023
Merged
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
17 changes: 10 additions & 7 deletions cmd/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func main() {
flag.StringVar(&httpExternalAddr, "http-external-address", "http://localhost:8080", "The external address at which the http server is reachable.")
flag.StringVar(&bundleCAFile, "bundle-ca-file", "", "The file containing the certificate authority for connecting to bundle content servers.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&systemNamespace, "system-namespace", util.DefaultSystemNamespace, "Configures the namespace that gets used to deploy system resources.")
flag.StringVar(&systemNamespace, "system-namespace", "", "Configures the namespace that gets used to deploy system resources.")
flag.StringVar(&unpackImage, "unpack-image", util.DefaultUnpackImage, "Configures the container image that gets used to unpack Bundle contents.")
flag.StringVar(&baseUploadManagerURL, "base-upload-manager-url", "", "The base URL from which to fetch uploaded bundles.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
Expand Down Expand Up @@ -117,10 +117,13 @@ func main() {
dependentSelector := labels.NewSelector().Add(*dependentRequirement)

cfg := ctrl.GetConfigOrDie()
systemNs := util.PodNamespace(systemNamespace)
if systemNamespace == "" {
systemNamespace = util.PodNamespace()
}

systemNsCluster, err := cluster.New(cfg, func(opts *cluster.Options) {
opts.Scheme = scheme
opts.Namespace = systemNs
opts.Namespace = systemNamespace
})
if err != nil {
setupLog.Error(err, "unable to create system namespace cluster")
Expand Down Expand Up @@ -215,7 +218,7 @@ func main() {
os.Exit(1)
}

unpacker, err := source.NewDefaultUnpacker(systemNsCluster, systemNs, unpackImage, baseUploadManagerURL, rootCAs)
unpacker, err := source.NewDefaultUnpacker(systemNsCluster, systemNamespace, unpackImage, baseUploadManagerURL, rootCAs)
if err != nil {
setupLog.Error(err, "unable to setup bundle unpacker")
os.Exit(1)
Expand All @@ -230,12 +233,12 @@ func main() {
cfgGetter := helmclient.NewActionConfigGetter(mgr.GetConfig(), mgr.GetRESTMapper(), mgr.GetLogger())
acg := helmclient.NewActionClientGetter(cfgGetter)
commonBDProvisionerOptions := []bundledeployment.Option{
bundledeployment.WithReleaseNamespace(systemNs),
bundledeployment.WithReleaseNamespace(systemNamespace),
bundledeployment.WithActionClientGetter(acg),
bundledeployment.WithStorage(bundleStorage),
}

if err := bundle.SetupWithManager(mgr, systemNsCluster.GetCache(), systemNs, append(
if err := bundle.SetupWithManager(mgr, systemNsCluster.GetCache(), systemNamespace, append(
commonBundleProvisionerOptions,
bundle.WithProvisionerID(plain.ProvisionerID),
bundle.WithHandler(bundle.HandlerFunc(plain.HandleBundle)),
Expand All @@ -244,7 +247,7 @@ func main() {
os.Exit(1)
}

if err := bundle.SetupWithManager(mgr, systemNsCluster.GetCache(), systemNs, append(
if err := bundle.SetupWithManager(mgr, systemNsCluster.GetCache(), systemNamespace, append(
commonBundleProvisionerOptions,
bundle.WithProvisionerID(registry.ProvisionerID),
bundle.WithHandler(bundle.HandlerFunc(registry.HandleBundle)),
Expand Down
14 changes: 8 additions & 6 deletions cmd/helm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func main() {
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&unpackImage, "unpack-image", util.DefaultUnpackImage, "Configures the container image that gets used to unpack Bundle contents.")
flag.StringVar(&baseUploadManagerURL, "base-upload-manager-url", "", "The base URL from which to fetch uploaded bundles.")
flag.StringVar(&systemNamespace, "system-namespace", util.DefaultSystemNamespace, "Configures the namespace that gets used to deploy system resources.")
flag.StringVar(&systemNamespace, "system-namespace", "", "Configures the namespace that gets used to deploy system resources.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
Expand Down Expand Up @@ -107,10 +107,12 @@ func main() {
dependentSelector := labels.NewSelector().Add(*dependentRequirement)

cfg := ctrl.GetConfigOrDie()
systemNs := util.PodNamespace(systemNamespace)
if systemNamespace == "" {
systemNamespace = util.PodNamespace()
}
systemNsCluster, err := cluster.New(cfg, func(opts *cluster.Options) {
opts.Scheme = scheme
opts.Namespace = systemNs
opts.Namespace = systemNamespace
})
if err != nil {
setupLog.Error(err, "unable to create system namespace cluster")
Expand Down Expand Up @@ -197,7 +199,7 @@ func main() {
os.Exit(1)
}

unpacker, err := source.NewDefaultUnpacker(systemNsCluster, systemNs, unpackImage, baseUploadManagerURL, rootCAs)
unpacker, err := source.NewDefaultUnpacker(systemNsCluster, systemNamespace, unpackImage, baseUploadManagerURL, rootCAs)
if err != nil {
setupLog.Error(err, "unable to setup bundle unpacker")
os.Exit(1)
Expand All @@ -212,12 +214,12 @@ func main() {
cfgGetter := helmclient.NewActionConfigGetter(mgr.GetConfig(), mgr.GetRESTMapper(), mgr.GetLogger())
acg := helmclient.NewActionClientGetter(cfgGetter)
commonBDProvisionerOptions := []bundledeployment.Option{
bundledeployment.WithReleaseNamespace(systemNs),
bundledeployment.WithReleaseNamespace(systemNamespace),
bundledeployment.WithActionClientGetter(acg),
bundledeployment.WithStorage(bundleStorage),
}

if err := bundle.SetupWithManager(mgr, systemNsCluster.GetCache(), systemNs, append(
if err := bundle.SetupWithManager(mgr, systemNsCluster.GetCache(), systemNamespace, append(
commonBundleProvisionerOptions,
bundle.WithProvisionerID(helm.ProvisionerID),
bundle.WithHandler(bundle.HandlerFunc(helm.HandleBundle)),
Expand Down
10 changes: 6 additions & 4 deletions cmd/webhooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
var rukpakVersion bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&systemNamespace, "system-namespace", util.DefaultSystemNamespace, "Configures the namespace that gets used to deploy system resources.")
flag.StringVar(&systemNamespace, "system-namespace", "", "Configures the namespace that gets used to deploy system resources.")
flag.BoolVar(&rukpakVersion, "version", false, "Displays rukpak version information")
opts := zap.Options{
Development: true,
Expand All @@ -69,10 +69,12 @@ func main() {
setupLog.Info("starting up the rukpak webhooks", "git commit", version.String())

cfg := ctrl.GetConfigOrDie()
systemNs := util.PodNamespace(systemNamespace)
if systemNamespace == "" {
systemNamespace = util.PodNamespace()
}
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme,
Namespace: systemNs,
Namespace: systemNamespace,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
Expand All @@ -84,7 +86,7 @@ func main() {

if err = (&webhook.Bundle{
Client: mgr.GetClient(),
SystemNamespace: systemNs,
SystemNamespace: systemNamespace,
}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", rukpakv1alpha1.BundleKind)
os.Exit(1)
Expand Down
8 changes: 4 additions & 4 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,14 @@
})
}

// GetPodNamespace checks whether the controller is running in a Pod vs.
// PodNamespace checks whether the controller is running in a Pod vs.
// being run locally by inspecting the namespace file that gets mounted
// automatically for Pods at runtime. If that file doesn't exist, then
// return the @defaultNamespace namespace parameter.
func PodNamespace(defaultNamespace string) string {
// return DefaultSystemNamespace.
func PodNamespace() string {
namespace, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
return defaultNamespace
return DefaultSystemNamespace

Check warning on line 329 in internal/util/util.go

View check run for this annotation

Codecov / codecov/patch

internal/util/util.go#L329

Added line #L329 was not covered by tests
}
return string(namespace)
}
Expand Down