Repository: aquasecurity/starboard
Pull request #969 (2022-02-17 15:50:43)

refactor: rename ObjectToObjectMetadata to ObjectToObjectMeta
Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>

codecov[bot]: # [Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/969?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) Report
> Merging [#969](https://codecov.io/gh/aquasecurity/starboard/pull/969?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) (5e35917) into [main](https://codecov.io/gh/aquasecurity/starboard/commit/b9849c0808017db7539a94f0aa5769407a4251a1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) (b9849c0) will **decrease** coverage by `18.34%`.
> The diff coverage is `66.66%`.

[![Impacted file tree graph](https://codecov.io/gh/aquasecurity/starboard/pull/969/graphs/tree.svg?width=650&height=150&src=pr&token=qqTGKAJ0Wb&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity)](https://codecov.io/gh/aquasecurity/starboard/pull/969?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity)

```diff
@@             Coverage Diff             @@
##             main     #969       +/-   ##
===========================================
- Coverage   64.38%   46.04%   -18.35%     
===========================================
  Files          59       42       -17     
  Lines        7039     5849     -1190     
===========================================
- Hits         4532     2693     -1839     
- Misses       2051     2992      +941     
+ Partials      456      164      -292     
```


| [Impacted Files](https://codecov.io/gh/aquasecurity/starboard/pull/969?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) | Coverage Δ | |
|---|---|---|
| [pkg/kube/object.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL2t1YmUvb2JqZWN0Lmdv) | `42.47% <42.85%> (-8.26%)` | :arrow_down: |
| [pkg/configauditreport/builder.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL2NvbmZpZ2F1ZGl0cmVwb3J0L2J1aWxkZXIuZ28=) | `67.39% <80.00%> (-11.42%)` | :arrow_down: |
| [pkg/vulnerabilityreport/builder.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL3Z1bG5lcmFiaWxpdHlyZXBvcnQvYnVpbGRlci5nbw==) | `70.39% <100.00%> (-8.56%)` | :arrow_down: |
| [pkg/plugin/factory.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL3BsdWdpbi9mYWN0b3J5Lmdv) | `0.00% <0.00%> (-81.14%)` | :arrow_down: |
| [pkg/kubehunter/scanner.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL2t1YmVodW50ZXIvc2Nhbm5lci5nbw==) | `0.00% <0.00%> (-74.49%)` | :arrow_down: |
| [pkg/kube/runnable\_job.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL2t1YmUvcnVubmFibGVfam9iLmdv) | `0.00% <0.00%> (-63.73%)` | :arrow_down: |
| [pkg/kubehunter/model.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL2t1YmVodW50ZXIvbW9kZWwuZ28=) | `0.00% <0.00%> (-62.86%)` | :arrow_down: |
| [pkg/operator/controller/configauditreport.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL29wZXJhdG9yL2NvbnRyb2xsZXIvY29uZmlnYXVkaXRyZXBvcnQuZ28=) | `0.00% <0.00%> (-61.56%)` | :arrow_down: |
| [pkg/vulnerabilityreport/scanner.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL3Z1bG5lcmFiaWxpdHlyZXBvcnQvc2Nhbm5lci5nbw==) | `0.00% <0.00%> (-59.44%)` | :arrow_down: |
| [pkg/operator/controller/vulnerabilityreport.go](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL29wZXJhdG9yL2NvbnRyb2xsZXIvdnVsbmVyYWJpbGl0eXJlcG9ydC5nbw==) | `0.00% <0.00%> (-58.67%)` | :arrow_down: |
| ... and [33 more](https://codecov.io/gh/aquasecurity/starboard/pull/969/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/969?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/969?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity). Last update [b9849c0...5e35917](https://codecov.io/gh/aquasecurity/starboard/pull/969?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity).


Patch:
diff --git a/pkg/configauditreport/builder.go b/pkg/configauditreport/builder.go
index 0517a9e..a4b4dac 100644
--- a/pkg/configauditreport/builder.go
+++ b/pkg/configauditreport/builder.go
@@ -123,12 +123,12 @@ func (s *ScanJobBuilder) Get() (*batchv1.Job, []*corev1.Secret, error) {
 		},
 	}
 
-	err = kube.ObjectToObjectMetadata(s.object, &job.ObjectMeta)
+	err = kube.ObjectToObjectMeta(s.object, &job.ObjectMeta)
 	if err != nil {
 		return nil, nil, err
 	}
 
-	err = kube.ObjectToObjectMetadata(s.object, &job.Spec.Template.ObjectMeta)
+	err = kube.ObjectToObjectMeta(s.object, &job.Spec.Template.ObjectMeta)
 	if err != nil {
 		return nil, nil, err
 	}
@@ -140,7 +140,7 @@ func (s *ScanJobBuilder) Get() (*batchv1.Job, []*corev1.Secret, error) {
 		for k, v := range labelsSet {
 			secret.Labels[k] = v
 		}
-		err = kube.ObjectToObjectMetadata(s.object, &secret.ObjectMeta)
+		err = kube.ObjectToObjectMeta(s.object, &secret.ObjectMeta)
 	}
 
 	return job, secrets, nil
@@ -214,7 +214,7 @@ func (b *ReportBuilder) GetClusterReport() (v1alpha1.ClusterConfigAuditReport, e
 		},
 		Report: b.data,
 	}
-	err := kube.ObjectToObjectMetadata(b.controller, &report.ObjectMeta)
+	err := kube.ObjectToObjectMeta(b.controller, &report.ObjectMeta)
 	if err != nil {
 		return v1alpha1.ClusterConfigAuditReport{}, err
 	}
@@ -251,7 +251,7 @@ func (b *ReportBuilder) GetReport() (v1alpha1.ConfigAuditReport, error) {
 		},
 		Report: b.data,
 	}
-	err := kube.ObjectToObjectMetadata(b.controller, &report.ObjectMeta)
+	err := kube.ObjectToObjectMeta(b.controller, &report.ObjectMeta)
 	if err != nil {
 		return v1alpha1.ConfigAuditReport{}, err
 	}
diff --git a/pkg/kube/object.go b/pkg/kube/object.go
index 7358987..6ae9c9d 100644
--- a/pkg/kube/object.go
+++ b/pkg/kube/object.go
@@ -72,8 +72,12 @@ func IsBuiltInWorkload(controller *metav1.OwnerReference) bool {
 			controller.Kind == string(KindJob))
 }
 
-func IsClusterScopedKind(k string) bool {
-	switch k {
+// IsClusterScopedKind returns true if the specified kind is ClusterRole,
+// ClusterRoleBinding, and CustomResourceDefinition.
+//
+// TODO Use discovery client to have a generic implementation.
+func IsClusterScopedKind(kind string) bool {
+	switch kind {
 	case string(KindClusterRole), string(KindClusterRoleBindings), string(KindCustomResourceDefinition):
 		return true
 	default:
@@ -100,9 +104,9 @@ func ObjectRefToLabels(obj ObjectRef) map[string]string {
 	return labels
 }
 
-// ObjectToObjectMetadata encodes the specified client.Object as a set of labels
+// ObjectToObjectMeta encodes the specified client.Object as a set of labels
 // and annotations added to the given ObjectMeta.
-func ObjectToObjectMetadata(obj client.Object, meta *metav1.ObjectMeta) error {
+func ObjectToObjectMeta(obj client.Object, meta *metav1.ObjectMeta) error {
 	if meta.Labels == nil {
 		meta.Labels = make(map[string]string)
 	}
@@ -159,7 +163,8 @@ func GVRForResource(mapper meta.RESTMapper, resource string) (gvr schema.GroupVe
 	return
 }
 
-// ContainerImages is a simple structure to hold the mapping between container names and container image references.
+// ContainerImages is a simple structure to hold the mapping between container
+// names and container image references.
 type ContainerImages map[string]string
 
 func (ci ContainerImages) AsJSON() (string, error) {
@@ -194,10 +199,10 @@ func ObjectRefFromKindAndNamespacedName(kind Kind, name types.NamespacedName) Ob
 	}
 }
 
-// ComputeSpecHash computes hash of the specified K8s client.Object.
-// The hash is used to indicate whether the client.Object should be
-// rescanned or not by adding it as the starboard.LabelResourceSpecHash
-// label to an instance of a security report.
+// ComputeSpecHash computes hash of the specified K8s client.Object. The hash is
+// used to indicate whether the client.Object should be rescanned or not by
+// adding it as the starboard.LabelResourceSpecHash label to an instance of a
+// security report.
 func ComputeSpecHash(obj client.Object) (string, error) {
 	switch t := obj.(type) {
 	case *corev1.Pod, *appsv1.Deployment, *appsv1.ReplicaSet, *corev1.ReplicationController, *appsv1.StatefulSet, *appsv1.DaemonSet, *batchv1.CronJob, *batchv1.Job:
@@ -225,9 +230,8 @@ func ComputeSpecHash(obj client.Object) (string, error) {
 	}
 }
 
-// GetPodSpec returns v1.PodSpec from the specified Kubernetes
-// client.Object. Returns error if the given client.Object
-// is not a Kubernetes workload.
+// GetPodSpec returns v1.PodSpec from the specified Kubernetes client.Object.
+// Returns error if the given client.Object is not a Kubernetes workload.
 func GetPodSpec(obj client.Object) (corev1.PodSpec, error) {
 	switch t := obj.(type) {
 	case *corev1.Pod:
@@ -251,13 +255,17 @@ func GetPodSpec(obj client.Object) (corev1.PodSpec, error) {
 	}
 }
 
+var ErrReplicaSetNotFound = errors.New("replicaset not found")
+var ErrNoRunningPods = errors.New("no active pods for controller")
+var ErrUnSupportedKind = errors.New("unsupported workload kind")
+
 type ObjectResolver struct {
 	client.Client
 }
 
-func (o *ObjectResolver) ObjectFromObjectRef(ctx context.Context, workload ObjectRef) (client.Object, error) {
+func (o *ObjectResolver) ObjectFromObjectRef(ctx context.Context, ref ObjectRef) (client.Object, error) {
 	var obj client.Object
-	switch workload.Kind {
+	switch ref.Kind {
 	case KindPod:
 		obj = &corev1.Pod{}
 	case KindReplicaSet:
@@ -289,19 +297,15 @@ func (o *ObjectResolver) ObjectFromObjectRef(ctx context.Context, workload Objec
 	case KindCustomResourceDefinition:
 		obj = &apiextensionsv1.CustomResourceDefinition{}
 	default:
-		return nil, fmt.Errorf("unknown kind: %s", workload.Kind)
+		return nil, fmt.Errorf("unknown kind: %s", ref.Kind)
 	}
-	err := o.Client.Get(ctx, types.NamespacedName{Name: workload.Name, Namespace: workload.Namespace}, obj)
+	err := o.Client.Get(ctx, types.NamespacedName{Name: ref.Name, Namespace: ref.Namespace}, obj)
 	if err != nil {
 		return nil, err
 	}
 	return o.ensureGVK(obj)
 }
 
-var ErrReplicaSetNotFound = errors.New("replicaset not found")
-var ErrNoRunningPods = errors.New("no active pods for controller")
-var ErrUnSupportedKind = errors.New("unsupported workload kind")
-
 // ReportOwner resolves the owner of a security report for the specified object.
 func (o *ObjectResolver) ReportOwner(ctx context.Context, obj client.Object) (client.Object, error) {
 	switch obj.(type) {
@@ -344,9 +348,9 @@ func (o *ObjectResolver) ReportOwner(ctx context.Context, obj client.Object) (cl
 	}
 }
 
-// ReplicaSetByDeployment returns the current revision of the specified Deployment.
-// If the current revision cannot be found the ErrReplicaSetNotFound error
-// is returned.
+// ReplicaSetByDeployment returns the current revision of the specified
+// Deployment. If the current revision cannot be found the ErrReplicaSetNotFound
+// error is returned.
 func (o *ObjectResolver) ReplicaSetByDeployment(ctx context.Context, deploy *appsv1.Deployment) (*appsv1.ReplicaSet, error) {
 	var rsList appsv1.ReplicaSetList
 	err := o.Client.List(ctx, &rsList,
@@ -446,9 +450,11 @@ func (o *ObjectResolver) GetRelatedReplicasetName(ctx context.Context, object Ob
 	return "", fmt.Errorf("can only get related ReplicaSet for Deployment or Pod, not %q", string(object.Kind))
 }
 
-// GetNodeName will return the nodeName from one of the running pod of any kubernetes kind
-// if there are no running pods then ErrNoRunningPods will be returned.
-// if there are not active replicaset for deployment then ErrReplicaSetNotFound will be returned.
+// GetNodeName returns the name of the node on which the given workload is
+// scheduled. If there are no running pods then the ErrNoRunningPods error is
+// returned. If there are no active ReplicaSets for the Deployment the
+// ErrReplicaSetNotFound error is returned. If the specified workload is a
+// CronJob the ErrUnSupportedKind error is returned.
 func (o *ObjectResolver) GetNodeName(ctx context.Context, obj client.Object) (string, error) {
 	switch obj.(type) {
 	case *corev1.Pod:
diff --git a/pkg/vulnerabilityreport/builder.go b/pkg/vulnerabilityreport/builder.go
index b326b3e..3f9f526 100644
--- a/pkg/vulnerabilityreport/builder.go
+++ b/pkg/vulnerabilityreport/builder.go
@@ -130,12 +130,12 @@ func (s *ScanJobBuilder) Get() (*batchv1.Job, []*corev1.Secret, error) {
 		},
 	}
 
-	err = kube.ObjectToObjectMetadata(s.object, &job.ObjectMeta)
+	err = kube.ObjectToObjectMeta(s.object, &job.ObjectMeta)
 	if err != nil {
 		return nil, nil, err
 	}
 
-	err = kube.ObjectToObjectMetadata(s.object, &job.Spec.Template.ObjectMeta)
+	err = kube.ObjectToObjectMeta(s.object, &job.Spec.Template.ObjectMeta)
 	if err != nil {
 		return nil, nil, err
 	}
@@ -225,7 +225,7 @@ func (b *ReportBuilder) Get() (v1alpha1.VulnerabilityReport, error) {
 			v1alpha1.TTLReportAnnotation: b.reportTTL.String(),
 		}
 	}
-	err := kube.ObjectToObjectMetadata(b.controller, &report.ObjectMeta)
+	err := kube.ObjectToObjectMeta(b.controller, &report.ObjectMeta)
 	if err != nil {
 		return v1alpha1.VulnerabilityReport{}, err
 	}


Test patch:
diff --git a/pkg/kube/object_test.go b/pkg/kube/object_test.go
index 6810faf..d64c77b 100644
--- a/pkg/kube/object_test.go
+++ b/pkg/kube/object_test.go
@@ -132,7 +132,7 @@ func TestObjectRefToLabels(t *testing.T) {
 	}
 }
 
-func TestObjectToObjectMetadata(t *testing.T) {
+func TestObjectToObjectMeta(t *testing.T) {
 	testCases := []struct {
 		name     string
 		meta     metav1.ObjectMeta
@@ -220,7 +220,7 @@ func TestObjectToObjectMetadata(t *testing.T) {
 	}
 	for _, tc := range testCases {
 		t.Run(tc.name, func(t *testing.T) {
-			err := kube.ObjectToObjectMetadata(tc.object, &tc.meta)
+			err := kube.ObjectToObjectMeta(tc.object, &tc.meta)
 			require.NoError(t, err)
 			assert.Equal(t, tc.expected, tc.meta)
 		})