Skip to content

Commit

Permalink
Implement code review comments to NameReferenceTransformer changes.
Browse files Browse the repository at this point in the history
- Add comments where code with potentially misleading.
- Rename functions according to comments
  • Loading branch information
jbrette committed Jul 17, 2019
1 parent 579995d commit 9b40f8a
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions pkg/transformers/namereference.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ func (o *nameReferenceTransformer) Transform(m resmap.ResMap) error {
return nil
}

// utility function to replace a simple string by the new name
// selectReferral picks the referral among a subset of candidates.
// It returns the current name and namespace of the selected candidate.
// Note that the content of the referricalCandidateSubset slice is most of the time
// identical to the referralCandidates resmap. Still in some cases, such
// as ClusterRoleBinding, the subset only contains the resources of a specific
// namespace.
func (o *nameReferenceTransformer) selectReferral(
oldName string,
referrer *resource.Resource,
Expand Down Expand Up @@ -138,7 +143,7 @@ func (o *nameReferenceTransformer) selectReferral(
}

// utility function to replace a simple string by the new name
func (o *nameReferenceTransformer) mapSimpleNameField(
func (o *nameReferenceTransformer) getSimpleNameField(
oldName string,
referrer *resource.Resource,
target gvk.Gvk,
Expand All @@ -153,7 +158,7 @@ func (o *nameReferenceTransformer) mapSimpleNameField(

// utility function to replace name field within a map[string]interface{}
// and leverage the namespace field.
func (o *nameReferenceTransformer) mapNameAndNsStruct(
func (o *nameReferenceTransformer) getNameAndNsStruct(
inMap map[string]interface{},
referrer *resource.Resource,
target gvk.Gvk,
Expand Down Expand Up @@ -205,7 +210,7 @@ func (o *nameReferenceTransformer) getNewNameFunc(
switch in.(type) {
case string:
oldName, _ := in.(string)
return o.mapSimpleNameField(oldName, referrer, target,
return o.getSimpleNameField(oldName, referrer, target,
referralCandidates, referralCandidates.Resources())
case []interface{}:
l, _ := in.([]interface{})
Expand All @@ -215,7 +220,7 @@ func (o *nameReferenceTransformer) getNewNameFunc(
// Kind: Role/ClusterRole
// FieldSpec is rules.resourceNames
oldName, _ := item.(string)
newName, err := o.mapSimpleNameField(oldName, referrer, target,
newName, err := o.getSimpleNameField(oldName, referrer, target,
referralCandidates, referralCandidates.Resources())
if err != nil {
return nil, err
Expand All @@ -224,8 +229,13 @@ func (o *nameReferenceTransformer) getNewNameFunc(
case map[string]interface{}:
// Kind: RoleBinding/ClusterRoleBinding
// FieldSpec is subjects
// Note: The corresponding fieldSpec had been changed from
// from path: subjects/name to just path: subjects. This is
// what get mutatefield to request the mapping of the whole
// map containing namespace and name instead of just a simple
// string field containing the name
oldMap, _ := item.(map[string]interface{})
newMap, err := o.mapNameAndNsStruct(oldMap, referrer, target,
newMap, err := o.getNameAndNsStruct(oldMap, referrer, target,
referralCandidates)
if err != nil {
return nil, err
Expand Down

0 comments on commit 9b40f8a

Please sign in to comment.