Skip to content

Commit

Permalink
Merge pull request #1400 from keleustes/defaultsa
Browse files Browse the repository at this point in the history
Force the namespace value for the "default" service object.
  • Loading branch information
k8s-ci-robot authored Jul 26, 2019
2 parents e646bba + 2faf4a4 commit 95f3303
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 52 deletions.
14 changes: 14 additions & 0 deletions pkg/target/namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ subjects:
- kind: ServiceAccount
name: sa3
namespace: random
- kind: ServiceAccount
name: default
namespace: irrelevant
---
apiVersion: admissionregistration.k8s.io/v1beta1
kind: ValidatingWebhookConfiguration
Expand Down Expand Up @@ -193,6 +196,10 @@ metadata:
kind: ClusterRoleBinding
metadata:
name: crb1
subjects:
- kind: ServiceAccount
name: default
namespace: irrelevant
---
kind: PersistentVolume
metadata:
Expand Down Expand Up @@ -254,6 +261,9 @@ subjects:
- kind: ServiceAccount
name: sa3
namespace: random
- kind: ServiceAccount
name: default
namespace: newnamespace
---
apiVersion: admissionregistration.k8s.io/v1beta1
kind: ValidatingWebhookConfiguration
Expand Down Expand Up @@ -288,6 +298,10 @@ metadata:
kind: ClusterRoleBinding
metadata:
name: p1-crb1-s1
subjects:
- kind: ServiceAccount
name: default
namespace: newnamespace
---
kind: PersistentVolume
metadata:
Expand Down
4 changes: 4 additions & 0 deletions pkg/transformers/config/defaultconfig/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ const (
namespace:
- path: metadata/namespace
create: true
- path: subjects
kind: RoleBinding
- path: subjects
kind: ClusterRoleBinding
`
)
90 changes: 65 additions & 25 deletions plugin/builtin/NamespaceTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 65 additions & 25 deletions plugin/builtin/namespacetransformer/NamespaceTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ func (p *plugin) Transform(m resmap.ResMap) error {
return nil
}
for _, r := range m.Resources() {
id := r.OrgId()
fs, ok := p.isSelected(id)
if !ok {
continue
}
if len(r.Map()) == 0 {
// Don't mutate empty objects?
continue
}
if doIt(id, fs) {
if err := p.changeNamespace(r, fs); err != nil {

id := r.OrgId()
applicableFs := p.applicableFieldSpecs(id)

for _, fs := range applicableFs {
err := transformers.MutateField(
r.Map(), fs.PathSlice(), fs.CreateIfNotPresent,
p.changeNamespace(r))
if err != nil {
return err
}
}
Expand All @@ -60,26 +62,64 @@ const metaNamespace = "metadata/namespace"
// all objects have it, even "ClusterKind" objects
// that don't exist in a namespace (the Namespace
// object itself doesn't live in a namespace).
func doIt(id resid.ResId, fs *config.FieldSpec) bool {
return fs.Path != metaNamespace ||
(fs.Path == metaNamespace && id.IsNamespaceableKind())
}

func (p *plugin) changeNamespace(
r *resource.Resource, fs *config.FieldSpec) error {
return transformers.MutateField(
r.Map(), fs.PathSlice(), fs.CreateIfNotPresent,
func(_ interface{}) (interface{}, error) {
return p.Namespace, nil
})
func (p *plugin) applicableFieldSpecs(id resid.ResId) []config.FieldSpec {
res := []config.FieldSpec{}
for _, fs := range p.FieldSpecs {
if id.IsSelected(&fs.Gvk) && (fs.Path != metaNamespace || (fs.Path == metaNamespace && id.IsNamespaceableKind())) {
res = append(res, fs)
}
}
return res
}

func (p *plugin) isSelected(
id resid.ResId) (*config.FieldSpec, bool) {
for _, fs := range p.FieldSpecs {
if id.IsSelected(&fs.Gvk) {
return &fs, true
func (o *plugin) changeNamespace(
referrer *resource.Resource) func(in interface{}) (interface{}, error) {
return func(in interface{}) (interface{}, error) {
switch in.(type) {
case string:
// will happen when the metadata/namespace
// value is replaced
return o.Namespace, nil
case []interface{}:
l, _ := in.([]interface{})
for idx, item := range l {
switch item.(type) {
case map[string]interface{}:
// Will happen when mutating the subjects
// field of ClusterRoleBinding and RoleBinding
inMap, _ := item.(map[string]interface{})
if _, ok := inMap["name"]; !ok {
continue
}
name, ok := inMap["name"].(string)
if !ok {
continue
}
// The only case we need to force the namespace
// if for the "service account". "default" is
// kind of hardcoded here for right now.
if name != "default" {
continue
}
inMap["namespace"] = o.Namespace
l[idx] = inMap
default:
// nothing to do for right now
}
}
return in, nil
case map[string]interface{}:
// Will happen if the createField=true
// when the namespace is added to the
// object
inMap := in.(map[string]interface{})
if len(inMap) == 0 {
return o.Namespace, nil
} else {
return in, nil
}
default:
return in, nil
}
}
return nil, false
}
16 changes: 14 additions & 2 deletions plugin/builtin/namespacetransformer/NamespaceTransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ metadata:
fieldSpecs:
- path: metadata/namespace
create: true
- path: subjects
kind: RoleBinding
group: rbac.authorization.k8s.io
- path: subjects
kind: ClusterRoleBinding
group: rbac.authorization.k8s.io
`, `
apiVersion: v1
kind: ConfigMap
Expand All @@ -54,7 +60,7 @@ apiVersion: v1
kind: ServiceAccount
metadata:
name: default
namespace: system
namespace: test
---
apiVersion: v1
kind: ServiceAccount
Expand Down Expand Up @@ -151,7 +157,7 @@ metadata:
subjects:
- kind: ServiceAccount
name: default
namespace: system
namespace: test
- kind: ServiceAccount
name: service-account
namespace: system
Expand Down Expand Up @@ -222,6 +228,12 @@ metadata:
fieldSpecs:
- path: metadata/namespace
create: true
- path: subjects
kind: RoleBinding
group: rbac.authorization.k8s.io
- path: subjects
kind: ClusterRoleBinding
group: rbac.authorization.k8s.io
`, noChangeExpected)

th.AssertActualEqualsExpected(rm, noChangeExpected)
Expand Down

0 comments on commit 95f3303

Please sign in to comment.