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: remove ObjectFromLabelsSet method #783

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 0 additions & 15 deletions pkg/kube/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,6 @@ func PartialObjectFromObjectMetadata(objectMeta metav1.ObjectMeta) (Object, erro
}, nil
}

// Deprecated use PartialObjectFromObjectMetadata instead.
func ObjectFromLabelsSet(set labels.Set) (Object, error) {
if !set.Has(starboard.LabelResourceKind) {
return Object{}, fmt.Errorf("required label does not exist: %s", starboard.LabelResourceKind)
}
if !set.Has(starboard.LabelResourceName) {
return Object{}, fmt.Errorf("required label does not exist: %s", starboard.LabelResourceName)
}
return Object{
Kind: Kind(set.Get(starboard.LabelResourceKind)),
Name: set.Get(starboard.LabelResourceName),
Namespace: set.Get(starboard.LabelResourceNamespace),
}, nil
}

func GVRForResource(mapper meta.RESTMapper, resource string) (gvr schema.GroupVersionResource, gvk schema.GroupVersionKind, err error) {
fullySpecifiedGVR, groupResource := schema.ParseResourceArg(strings.ToLower(resource))
if fullySpecifiedGVR != nil {
Expand Down
65 changes: 0 additions & 65 deletions pkg/kube/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kube_test

import (
"context"
"errors"
"fmt"
"testing"

Expand All @@ -15,7 +14,6 @@ import (
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -230,69 +228,6 @@ func TestObjectToObjectMetadata(t *testing.T) {
}
}

func TestObjectFromLabelsSet(t *testing.T) {
testCases := []struct {
name string
labelsSet labels.Set
expectedObject kube.Object
expectedError error
}{
{
name: "Should return object for namespaced object",
labelsSet: labels.Set{
starboard.LabelResourceKind: "Deployment",
starboard.LabelResourceName: "my-deployment",
starboard.LabelResourceNamespace: "my-namespace",
},
expectedObject: kube.Object{
Kind: kube.KindDeployment,
Name: "my-deployment",
Namespace: "my-namespace",
},
},
{
name: "Should return object for cluster-scoped object",
labelsSet: labels.Set{
starboard.LabelResourceKind: "Node",
starboard.LabelResourceName: "my-node",
},
expectedObject: kube.Object{
Kind: kube.KindNode,
Name: "my-node",
Namespace: "",
},
},
{
name: "Should return error when object kind is not specified as label",
labelsSet: labels.Set{
starboard.LabelResourceName: "my-deployment",
starboard.LabelResourceNamespace: "my-namespace",
},
expectedError: errors.New("required label does not exist: starboard.resource.kind"),
},
{
name: "Should return error when object name is not specified as label",
labelsSet: labels.Set{
starboard.LabelResourceKind: "Deployment",
},
expectedError: errors.New("required label does not exist: starboard.resource.name"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
obj, err := kube.ObjectFromLabelsSet(tc.labelsSet)
switch {
case tc.expectedError == nil:
require.NoError(t, err)
assert.Equal(t, tc.expectedObject, obj)
default:
assert.EqualError(t, err, tc.expectedError.Error())
}
})
}
}

func TestContainerImages_AsJSON_And_FromJSON(t *testing.T) {
containerImages := kube.ContainerImages{
"nginx": "nginx:1.16",
Expand Down
11 changes: 3 additions & 8 deletions pkg/operator/controller/ciskubebenchreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand Down Expand Up @@ -240,18 +239,14 @@ func (r *CISKubeBenchReportReconciler) reconcileJobs() reconcile.Func {

func (r *CISKubeBenchReportReconciler) processCompleteScanJob(ctx context.Context, job *batchv1.Job) error {
log := r.Logger.WithValues("job", fmt.Sprintf("%s/%s", job.Namespace, job.Name))
log.V(1).Info("Processing complete scan job")

log.V(1).Info("Resolving node reference from labels")
nodeRef, err := kube.ObjectFromLabelsSet(job.Labels)
nodeRef, err := kube.PartialObjectFromObjectMetadata(job.ObjectMeta)
if err != nil {
return fmt.Errorf("getting node reference from job labels: %w", err)
return fmt.Errorf("getting owner ref from scan job metadata: %w", err)
}
log = log.WithValues("node", nodeRef.Name)

log.V(1).Info("Getting node from cache")
node := &corev1.Node{}
err = r.Client.Get(ctx, types.NamespacedName{Name: nodeRef.Name}, node)
err = r.Client.Get(ctx, client.ObjectKey{Name: nodeRef.Name}, node)
if err != nil {
if errors.IsNotFound(err) {
log.V(1).Info("Ignore processing scan job for node that must have been deleted")
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/controller/configauditreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func (r *ConfigAuditReportReconciler) processCompleteScanJob(ctx context.Context

ownerRef, err := kube.PartialObjectFromObjectMetadata(job.ObjectMeta)
if err != nil {
return fmt.Errorf("getting owner from scan job metadata: %w", err)
return fmt.Errorf("getting owner ref from scan job metadata: %w", err)
}

owner, err := r.GetObjectFromPartialObject(ctx, ownerRef)
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/controller/vulnerabilityreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (r *VulnerabilityReportReconciler) processCompleteScanJob(ctx context.Conte

ownerRef, err := kube.PartialObjectFromObjectMetadata(job.ObjectMeta)
if err != nil {
return fmt.Errorf("getting owner from scan job metadata: %w", err)
return fmt.Errorf("getting owner ref from scan job metadata: %w", err)
}

owner, err := r.GetObjectFromPartialObject(ctx, ownerRef)
Expand Down