Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(kube-api-rewriter): rewrite owner refs #108

Merged
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
22cf613
refactor(kube-api-rewriter): rewrite owner refs when patching
LopatinDmitr May 23, 2024
9960abe
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 22, 2024
7a60bff
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 22, 2024
6505c1b
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 23, 2024
8ac5e56
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 26, 2024
3765eb9
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 27, 2024
c7552f7
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 27, 2024
8c657f3
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 27, 2024
f25eaf8
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 27, 2024
54045e7
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 28, 2024
3e8efca
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 28, 2024
f2242e7
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 28, 2024
962f5f8
fix(kube-api-rewriter): add rewrite ownerReferences
LopatinDmitr May 29, 2024
802bec0
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 29, 2024
72fed1b
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 29, 2024
06934f8
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 29, 2024
3f6185f
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 29, 2024
cd97e6b
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 29, 2024
eaf709a
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 30, 2024
e299990
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 30, 2024
ffcc63c
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 30, 2024
208df4d
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 30, 2024
a585260
refactor(kube-api-rewriter): add rewrite ownerReferences when patching
LopatinDmitr May 31, 2024
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
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
27 changes: 27 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,19 @@ 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
}

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

return rwrBytes, nil
}

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

return obj, nil
}

func shouldRewriteOwnerReferences(kind string) bool {
switch kind {
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 {
LopatinDmitr marked this conversation as resolved.
Show resolved Hide resolved
// 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
}