Skip to content

Commit

Permalink
fix: properly set delegates to specific instances to call their methods
Browse files Browse the repository at this point in the history
We need to do so so that we have a semblance of polymorphism: the
generic functions work on the delegate and generic role/rolebinding
need to set their delegate to specific instances when these are created
so that the generic functions call the most specific versions first,
which can then default to non-overridden versions if not implemented…
Writing OOP code in go be like 🤷‍ or 🤦‍
  • Loading branch information
metacosm committed Oct 23, 2019
1 parent aa68032 commit cfabecd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
6 changes: 4 additions & 2 deletions pkg/controller/capability/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type role struct {
}

func newRole(owner framework.Resource) role {
r := controller.NewOwnedRole(owner, func() string { return "scc-privileged-role" })
return role{Role: r}
generic := controller.NewOwnedRole(owner, func() string { return "scc-privileged-role" })
r := role{Role: generic}
generic.SetDelegate(r)
return r
}
6 changes: 4 additions & 2 deletions pkg/controller/capability/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ type roleBinding struct {
}

func newRoleBinding(owner framework.Resource) roleBinding {
rb := controller.NewOwnedRoleBinding(owner,
generic := controller.NewOwnedRoleBinding(owner,
func() string { return "use-scc-privileged" },
func() string { return newRole(owner).Name() },
func() string { return newPostgres(owner).Name() })
return roleBinding{RoleBinding: rb}
rb := roleBinding{RoleBinding: generic}
generic.SetDelegate(rb)
return rb
}

func (res roleBinding) NewInstanceWith(owner framework.Resource) framework.DependentResource {
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/component/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ type role struct {
}

func newRole(owner framework.Resource) role {
r := controller.NewOwnedRole(owner, func() string { return "image-scc-privileged-role" })
return role{Role: r}
generic := controller.NewOwnedRole(owner, func() string { return "image-scc-privileged-role" })
r := role{Role: generic}
generic.SetDelegate(r) // we need to set the parent's delegate to the object we're creating so that its specific methods are called
return r
}

func (res role) NewInstanceWith(owner framework.Resource) framework.DependentResource {
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/component/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ type roleBinding struct {
}

func newRoleBinding(owner framework.Resource) roleBinding {
rb := controller.NewOwnedRoleBinding(owner,
generic := controller.NewOwnedRoleBinding(owner,
func() string { return "use-image-scc-privileged" },
func() string { return newRole(owner).Name() },
func() string { return ServiceAccountName(owner) })
return roleBinding{RoleBinding: rb}
rb := roleBinding{RoleBinding: generic}
generic.SetDelegate(rb)
return rb
}

func (res roleBinding) NewInstanceWith(owner framework.Resource) framework.DependentResource {
Expand Down

0 comments on commit cfabecd

Please sign in to comment.