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

networkpolicy: add List() func #678

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
58 changes: 58 additions & 0 deletions pkg/networkpolicy/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package networkpolicy

import (
"context"
"fmt"

"github.com/golang/glog"
"github.com/openshift-kni/eco-goinfra/pkg/clients"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// List returns networkpolicy inventory in the given namespace.
func List(apiClient *clients.Settings, nsname string, options ...metav1.ListOptions) ([]*NetworkPolicyBuilder, error) {
if nsname == "" {
glog.V(100).Infof("networkpolicy 'nsname' parameter can not be empty")

return nil, fmt.Errorf("failed to list networkpolicies, 'nsname' parameter is empty")
}

passedOptions := metav1.ListOptions{}
logMessage := fmt.Sprintf("Listing networkpolicies in the namespace %s", nsname)

if len(options) > 1 {
glog.V(100).Infof("'options' parameter must be empty or single-valued")

return nil, fmt.Errorf("error: more than one ListOptions was passed")
}

if len(options) == 1 {
passedOptions = options[0]
logMessage += fmt.Sprintf(" with the options %v", passedOptions)
}

glog.V(100).Infof(logMessage)

networkpolicyList, err := apiClient.NetworkPolicies(nsname).List(context.TODO(), passedOptions)

if err != nil {
glog.V(100).Infof("Failed to list networkpolicies in the namespace %s due to %s", nsname, err.Error())

return nil, err
}

var networkpolicyObjects []*NetworkPolicyBuilder

for _, runningNetworkPolicy := range networkpolicyList.Items {
copiedNetworkPolicy := runningNetworkPolicy
networkpolicyBuilder := &NetworkPolicyBuilder{
apiClient: apiClient,
Object: &copiedNetworkPolicy,
Definition: &copiedNetworkPolicy,
}

networkpolicyObjects = append(networkpolicyObjects, networkpolicyBuilder)
}

return networkpolicyObjects, nil
}
84 changes: 84 additions & 0 deletions pkg/networkpolicy/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package networkpolicy

import (
"errors"
"testing"

"github.com/openshift-kni/eco-goinfra/pkg/clients"
"github.com/stretchr/testify/assert"
netv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func TestList(t *testing.T) {
generateNetworkPolicy := func(name, namespace string) *netv1.NetworkPolicy {
return &netv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{"demo": name},
},
}
}

testCases := []struct {
sebrandon1 marked this conversation as resolved.
Show resolved Hide resolved
networkPolicyExists bool
testNamespace string
listOptions []metav1.ListOptions
expetedError error
expectedNumNetworkPolicies int
}{
{ // NetworkPolicy does exist
networkPolicyExists: true,
testNamespace: "test-namespace",
expetedError: nil,
listOptions: []metav1.ListOptions{},
expectedNumNetworkPolicies: 1,
},
{ // NetworkPolicy does not exist
networkPolicyExists: false,
testNamespace: "test-namespace",
expetedError: nil,
listOptions: []metav1.ListOptions{},
expectedNumNetworkPolicies: 0,
},
{ // Missing namespace parameter
networkPolicyExists: true,
testNamespace: "",
expetedError: errors.New("failed to list networkpolicies, 'nsname' parameter is empty"),
listOptions: []metav1.ListOptions{},
expectedNumNetworkPolicies: 0,
},
{ // More than one ListOptions was passed
networkPolicyExists: true,
testNamespace: "test-namespace",
expetedError: errors.New("error: more than one ListOptions was passed"),
listOptions: []metav1.ListOptions{{}, {}},
expectedNumNetworkPolicies: 0,
},
{ // Valid number of list options
networkPolicyExists: true,
testNamespace: "test-namespace",
expetedError: nil,
listOptions: []metav1.ListOptions{{}},
expectedNumNetworkPolicies: 1,
},
}

for _, testCase := range testCases {
var runtimeObjects []runtime.Object

if testCase.networkPolicyExists {
runtimeObjects = append(runtimeObjects, generateNetworkPolicy("test-networkpolicy", "test-namespace"))
}

testSettings := clients.GetTestClients(clients.TestClientParams{
K8sMockObjects: runtimeObjects,
})

networkPolicyList, err := List(testSettings, testCase.testNamespace, testCase.listOptions...)
assert.Equal(t, testCase.expetedError, err)
assert.Equal(t, testCase.expectedNumNetworkPolicies, len(networkPolicyList))
}
}
Loading