Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit ad5cec6

Browse files
authored
Merge pull request #232 from xgp01/customize-namespace-install
Support customize namespace install
2 parents 08eb05f + 9af654e commit ad5cec6

File tree

5 files changed

+35
-40
lines changed

5 files changed

+35
-40
lines changed

cmd/manager/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ var (
7474
includedNamespacesRegex string
7575
webhooksOnly bool
7676
enableHRQ bool
77+
hncNamespace string
7778
)
7879

7980
// init preloads some global vars before main() starts. Since this is the top-level module, I'm not
@@ -151,6 +152,7 @@ func parseFlags() {
151152
flag.Var(&managedNamespaceAnnots, "managed-namespace-annotation", "A regex indicating the annotations on namespaces that are managed by HNC. These annotations may only be set via the HierarchyConfiguration object. All regexes are implictly wrapped by \"^...$\". This argument can be specified multiple times. See the user guide for more information.")
152153
flag.BoolVar(&webhooksOnly, "webhooks-only", false, "Disables the controllers so HNC can be run in HA webhook mode")
153154
flag.BoolVar(&enableHRQ, "enable-hrq", false, "Enables hierarchical resource quotas")
155+
flag.StringVar(&hncNamespace, "namespace", "hnc-system", "Namespace where hnc-manager and hnc resources deployed")
154156
flag.Parse()
155157

156158
// Assign the array args to the configuration variables after the args are parsed.
@@ -166,6 +168,9 @@ func parseFlags() {
166168
setupLog.Info("Cannot set both --webhooks-only and --no-webhooks")
167169
os.Exit(1)
168170
}
171+
172+
// Set hnc namespace
173+
config.SetHNCNamespace(hncNamespace)
169174
}
170175

171176
// enableMetrics returns a function to call from main() to export any remaining metrics when main()

internal/config/namespace.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ var (
3131
// this list is removed from all managed namespaces unless specifically specified by the HC of the
3232
// namespace or one of its ancestors.
3333
managedAnnotations []*regexp.Regexp
34+
35+
// hncNamespace is the namespace where hnc-manager and hnc resources deployed. It set by commandline argument,
36+
// default to hnc-system.
37+
hncNamespace string
3438
)
3539

40+
func SetHNCNamespace(ns string) {
41+
hncNamespace = ns
42+
}
43+
3644
func SetNamespaces(regex string, excluded ...string) {
3745
if regex == "" {
3846
regex = ".*"
@@ -125,3 +133,8 @@ func IsManagedAnnotation(k string) bool {
125133
}
126134
return false
127135
}
136+
137+
// GetHNCNamespace return the namespace where hnc-manager and hnc resources deployed
138+
func GetHNCNamespace() string {
139+
return hncNamespace
140+
}

internal/hierarchyconfig/validator.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"os"
87
"strings"
98

109
"github.com/go-logr/logr"
@@ -117,7 +116,7 @@ func (v *Validator) handle(ctx context.Context, log logr.Logger, req *request) a
117116
// exists on the K8s server, we need to be able to update its status even though the rest of the
118117
// object wouldn't pass legality. We should probably only give the HNC SA the ability to modify
119118
// the _status_, though. TODO: https://github.com/kubernetes-sigs/hierarchical-namespaces/issues/80.
120-
if isHNCServiceAccount(req.ui) {
119+
if webhooks.IsHNCServiceAccount(req.ui) {
121120
return allow("HNC SA")
122121
}
123122

@@ -447,26 +446,6 @@ func (v *Validator) decodeRequest(in admission.Request) (*request, error) {
447446
}, nil
448447
}
449448

450-
// isHNCServiceAccount is inspired by isGKServiceAccount from open-policy-agent/gatekeeper.
451-
func isHNCServiceAccount(user *authnv1.UserInfo) bool {
452-
if user == nil {
453-
// useful for unit tests
454-
return false
455-
}
456-
457-
ns, found := os.LookupEnv("POD_NAMESPACE")
458-
if !found {
459-
ns = "hnc-system"
460-
}
461-
saGroup := fmt.Sprintf("system:serviceaccounts:%s", ns)
462-
for _, g := range user.Groups {
463-
if g == saGroup {
464-
return true
465-
}
466-
}
467-
return false
468-
}
469-
470449
func (v *Validator) InjectClient(c client.Client) error {
471450
v.server = &realClient{client: c}
472451
return nil

internal/setup/webhooks.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sigs.k8s.io/controller-runtime/pkg/webhook"
1010

1111
"sigs.k8s.io/hierarchical-namespaces/internal/anchor"
12+
"sigs.k8s.io/hierarchical-namespaces/internal/config"
1213
"sigs.k8s.io/hierarchical-namespaces/internal/forest"
1314
"sigs.k8s.io/hierarchical-namespaces/internal/hierarchyconfig"
1415
"sigs.k8s.io/hierarchical-namespaces/internal/hncconfig"
@@ -18,24 +19,24 @@ import (
1819
)
1920

2021
const (
21-
serviceName = "hnc-webhook-service"
22-
vwhName = "hnc-validating-webhook-configuration"
23-
mwhName = "hnc-mutating-webhook-configuration"
24-
caName = "hnc-ca"
25-
caOrganization = "hnc"
26-
secretNamespace = "hnc-system"
27-
secretName = "hnc-webhook-server-cert"
28-
certDir = "/tmp/k8s-webhook-server/serving-certs"
22+
serviceName = "hnc-webhook-service"
23+
vwhName = "hnc-validating-webhook-configuration"
24+
mwhName = "hnc-mutating-webhook-configuration"
25+
caName = "hnc-ca"
26+
caOrganization = "hnc"
27+
secretName = "hnc-webhook-server-cert"
28+
certDir = "/tmp/k8s-webhook-server/serving-certs"
2929
)
3030

31-
// DNSName is <service name>.<namespace>.svc
32-
var dnsName = fmt.Sprintf("%s.%s.svc", serviceName, secretNamespace)
33-
3431
// ManageCerts creates all certs for webhooks. This function is called from main.go.
3532
func ManageCerts(mgr ctrl.Manager, setupFinished chan struct{}, restartOnSecretRefresh bool) error {
33+
hncNamespace := config.GetHNCNamespace()
34+
// DNSName is <service name>.<hncNamespace>.svc
35+
dnsName := fmt.Sprintf("%s.%s.svc", serviceName, hncNamespace)
36+
3637
return cert.AddRotator(mgr, &cert.CertRotator{
3738
SecretKey: types.NamespacedName{
38-
Namespace: secretNamespace,
39+
Namespace: hncNamespace,
3940
Name: secretName,
4041
},
4142
CertDir: certDir,

internal/webhooks/webhooks.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package webhooks
22

33
import (
44
"fmt"
5-
"os"
65

76
k8sadm "k8s.io/api/admission/v1"
87
authnv1 "k8s.io/api/authentication/v1"
@@ -11,6 +10,8 @@ import (
1110
"k8s.io/apimachinery/pkg/runtime/schema"
1211
"k8s.io/apimachinery/pkg/util/validation/field"
1312
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
13+
14+
"sigs.k8s.io/hierarchical-namespaces/internal/config"
1415
)
1516

1617
// IsHNCServiceAccount is inspired by isGKServiceAccount from open-policy-agent/gatekeeper.
@@ -20,11 +21,7 @@ func IsHNCServiceAccount(user *authnv1.UserInfo) bool {
2021
return false
2122
}
2223

23-
ns, found := os.LookupEnv("POD_NAMESPACE")
24-
if !found {
25-
ns = "hnc-system"
26-
}
27-
saGroup := fmt.Sprintf("system:serviceaccounts:%s", ns)
24+
saGroup := fmt.Sprintf("system:serviceaccounts:%s", config.GetHNCNamespace())
2825
for _, g := range user.Groups {
2926
if g == saGroup {
3027
return true

0 commit comments

Comments
 (0)