Skip to content

Commit

Permalink
refactor(kube-api-rewriter): rewrite owner refs (#108)
Browse files Browse the repository at this point in the history

Signed-off-by: Dmitry Lopatin <[email protected]>
  • Loading branch information
LopatinDmitr authored May 31, 2024
1 parent 2dd0301 commit 9426767
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 41 deletions.
16 changes: 12 additions & 4 deletions images/kube-api-proxy/pkg/rewriter/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ import (
)

const (
ClusterRoleKind = "ClusterRole"
ClusterRoleListKind = "ClusterRoleList"
RoleKind = "Role"
RoleListKind = "RoleList"
ClusterRoleKind = "ClusterRole"
ClusterRoleListKind = "ClusterRoleList"
RoleKind = "Role"
RoleListKind = "RoleList"
RoleBindingKind = "RoleBinding"
RoleBindingListKind = "RoleBindingList"
PodDisruptionBudgetKind = "PodDisruptionBudget"
PodDisruptionBudgetListKind = "PodDisruptionBudgetList"
ControllerRevisionKind = "ControllerRevision"
ControllerRevisionListKind = "ControllerRevisionList"
DeploymentKind = "Deployment"
DeploymentListKind = "DeploymentList"
)

func RewriteClusterRoleOrList(rules *RewriteRules, obj []byte, action Action) ([]byte, error) {
Expand Down
23 changes: 23 additions & 0 deletions images/kube-api-proxy/pkg/rewriter/rule_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ func (rw *RuleBasedRewriter) RewriteJSONPayload(targetReq *TargetRequest, obj []
return obj, err
}

if shouldRewriteOwnerReferences(kind) {
rwrBytes, err = RewriteOwnerReferences(rw.Rules, rwrBytes, action)
}

// Return obj bytes as-is in case of the error.
if err != nil {
return obj, err
}

return rwrBytes, nil
}

Expand All @@ -209,3 +218,17 @@ func (rw *RuleBasedRewriter) RewritePatch(targetReq *TargetRequest, obj []byte)

return obj, nil
}

func shouldRewriteOwnerReferences(resourceType string) bool {
switch resourceType {
case CRDKind, CRDListKind,
RoleKind, RoleListKind,
RoleBindingKind, RoleBindingListKind,
PodDisruptionBudgetKind, PodDisruptionBudgetListKind,
ControllerRevisionKind, ControllerRevisionListKind,
DeploymentKind, DeploymentListKind:
return true
}

return false
}
75 changes: 38 additions & 37 deletions images/kube-api-proxy/pkg/rewriter/target_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (tr *TargetRequest) RawQuery() string {
// ShouldRewriteRequest returns true if incoming payload should
// be rewritten.
func (tr *TargetRequest) ShouldRewriteRequest() bool {

// Consider known webhook should be rewritten. Unknown paths will be passed as-is.
if tr.webhookRule != nil {
return true
Expand All @@ -126,28 +127,12 @@ func (tr *TargetRequest) ShouldRewriteRequest() bool {
if tr.targetEndpoint == nil {
// Pass resources without rules as is, except some special types.

if tr.originEndpoint.IsCore {
switch tr.originEndpoint.ResourceType {
case "pods":
return true
}
}

switch tr.originEndpoint.ResourceType {
case "mutatingwebhookconfigurations",
"validatingwebhookconfigurations",
"clusterroles",
"roles":
return true
}

// Rewrite request body when creating CRD.
if tr.originEndpoint.ResourceType == "customresourcedefinitions" && tr.originEndpoint.Name == "" {
return true
}

// Should not rewrite request if path is not rewritten.
return false
return shouldRewriteResource(tr.originEndpoint.ResourceType, tr.originEndpoint.IsCore)
}
}

Expand All @@ -171,16 +156,6 @@ func (tr *TargetRequest) ShouldRewriteResponse() bool {
return false
}

// Some core resources should be rewritten.
if tr.originEndpoint.IsCore {
switch tr.originEndpoint.ResourceType {
case "pods":
return true
// pods should be rewritten
}
return false
}

if tr.originEndpoint.IsCRD {
// Rewrite CRD List.
if tr.originEndpoint.Name == "" {
Expand All @@ -203,16 +178,7 @@ func (tr *TargetRequest) ShouldRewriteResponse() bool {
return true
}

// Rewrite special resources.
switch tr.originEndpoint.ResourceType {
// Webhook configurations should be rewritten.
case "mutatingwebhookconfigurations",
"validatingwebhookconfigurations",
"clusterroles":
return true
}

return false
return shouldRewriteResource(tr.originEndpoint.ResourceType, tr.originEndpoint.IsCore)
}

func (tr *TargetRequest) ResourceForLog() string {
Expand Down Expand Up @@ -276,3 +242,38 @@ func (tr *TargetRequest) ResourceForLog() string {

return "UNKNOWN"
}

func shouldRewriteResource(kind string, isCore bool) bool {
// Some core resources should be rewritten.
if isCore {
switch kind {
case "pods",
"configmaps",
"secrets",
"services",
"serviceaccounts":

return true
}
return false
}

// Rewrite special resources.
switch kind {
case "mutatingwebhookconfigurations",
"validatingwebhookconfigurations",
"clusterroles",
"roles",
"rolebindings",
"clusterrolebindings",
"deployments",
"statefulsets",
"daemonsets",
"poddisruptionbudgets",
"controllerrevisions":

return true
}

return false
}

0 comments on commit 9426767

Please sign in to comment.