Skip to content

Commit

Permalink
Allow webhook to run when mcm is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Jun 16, 2021
1 parent ba25850 commit 918498f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
2 changes: 2 additions & 0 deletions charts/rancher-webhook/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ spec:
value: "{{.Values.stamp}}"
- name: ENABLE_CAPI
value: "{{.Values.capi.enabled}}"
- name: ENABLE_MCM
value: "{{.Values.mcm.enabled}}"
- name: NAMESPACE
valueFrom:
fieldRef:
Expand Down
3 changes: 3 additions & 0 deletions charts/rancher-webhook/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ global:

capi:
enabled: false

mcm:
enabled: true
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func run() error {
cfg.RateLimiter = ratelimit.None

ctx := signals.SetupSignalHandler(context.Background())
if err := server.ListenAndServe(ctx, cfg, os.Getenv("ENABLE_CAPI") == "true"); err != nil {
if err := server.ListenAndServe(ctx, cfg, os.Getenv("ENABLE_CAPI") == "true", os.Getenv("ENABLE_MCM") != "false"); err != nil {
return err
}

Expand Down
28 changes: 17 additions & 11 deletions pkg/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (
type Clients struct {
clients.Clients

Management managementv3.Interface
EscalationChecker *auth.EscalationChecker
MultiClusterManagement bool
Management managementv3.Interface
EscalationChecker *auth.EscalationChecker
}

func New(ctx context.Context, rest *rest.Config) (*Clients, error) {
func New(ctx context.Context, rest *rest.Config, mcmEnabled bool) (*Clients, error) {
clients, err := clients.NewFromConfig(rest, nil)
if err != nil {
return nil, err
Expand All @@ -47,12 +48,17 @@ func New(ctx context.Context, rest *rest.Config) (*Clients, error) {
}

ruleResolver := rbacregistryvalidation.NewDefaultRuleResolver(rbacRestGetter, rbacRestGetter, rbacRestGetter, rbacRestGetter)
escalationChecker := auth.NewEscalationChecker(ruleResolver,
mgmt.Management().V3().RoleTemplate().Cache(), clients.RBAC.ClusterRole().Cache())

return &Clients{
Clients: *clients,
Management: mgmt.Management().V3(),
EscalationChecker: escalationChecker,
}, nil

result := &Clients{
Clients: *clients,
Management: mgmt.Management().V3(),
MultiClusterManagement: mcmEnabled,
}

if result.MultiClusterManagement {
result.EscalationChecker = auth.NewEscalationChecker(ruleResolver,
mgmt.Management().V3().RoleTemplate().Cache(), clients.RBAC.ClusterRole().Cache())
}

return result, nil
}
4 changes: 2 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var (
sideEffectClassNoneOnDryRun = v1.SideEffectClassNoneOnDryRun
)

func ListenAndServe(ctx context.Context, cfg *rest.Config, capiEnabled bool) error {
clients, err := clients.New(ctx, cfg)
func ListenAndServe(ctx context.Context, cfg *rest.Config, capiEnabled, mcmEnabled bool) error {
clients, err := clients.New(ctx, cfg, mcmEnabled)
if err != nil {
return err
}
Expand Down
19 changes: 11 additions & 8 deletions pkg/server/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ import (
)

func Validation(clients *clients.Clients) (http.Handler, error) {
globalRoleBindings := globalrolebinding.NewValidator(clients.Management.GlobalRole().Cache(), clients.EscalationChecker)
prtbs := projectroletemplatebinding.NewValidator(clients.Management.RoleTemplate().Cache(), clients.EscalationChecker)
crtbs := clusterroletemplatebinding.NewValidator(clients.Management.RoleTemplate().Cache(), clients.EscalationChecker)
roleTemplates := roletemplate.NewValidator(clients.EscalationChecker)
clusters := cluster.NewValidator(clients.K8s.AuthorizationV1().SubjectAccessReviews())

router := webhook.NewRouter()
router.Kind("Cluster").Group(management.GroupName).Type(&v3.Cluster{}).Handle(clusters)
router.Kind("RoleTemplate").Group(management.GroupName).Type(&v3.RoleTemplate{}).Handle(roleTemplates)
router.Kind("GlobalRoleBinding").Group(management.GroupName).Type(&v3.GlobalRoleBinding{}).Handle(globalRoleBindings)
router.Kind("ClusterRoleTemplateBinding").Group(management.GroupName).Type(&v3.ClusterRoleTemplateBinding{}).Handle(crtbs)
router.Kind("ProjectRoleTemplateBinding").Group(management.GroupName).Type(&v3.ProjectRoleTemplateBinding{}).Handle(prtbs)

if clients.MultiClusterManagement {
globalRoleBindings := globalrolebinding.NewValidator(clients.Management.GlobalRole().Cache(), clients.EscalationChecker)
prtbs := projectroletemplatebinding.NewValidator(clients.Management.RoleTemplate().Cache(), clients.EscalationChecker)
crtbs := clusterroletemplatebinding.NewValidator(clients.Management.RoleTemplate().Cache(), clients.EscalationChecker)
roleTemplates := roletemplate.NewValidator(clients.EscalationChecker)
router.Kind("RoleTemplate").Group(management.GroupName).Type(&v3.RoleTemplate{}).Handle(roleTemplates)
router.Kind("GlobalRoleBinding").Group(management.GroupName).Type(&v3.GlobalRoleBinding{}).Handle(globalRoleBindings)
router.Kind("ClusterRoleTemplateBinding").Group(management.GroupName).Type(&v3.ClusterRoleTemplateBinding{}).Handle(crtbs)
router.Kind("ProjectRoleTemplateBinding").Group(management.GroupName).Type(&v3.ProjectRoleTemplateBinding{}).Handle(prtbs)
}

return router, nil
}

0 comments on commit 918498f

Please sign in to comment.