-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
poddisruptionbudget pkg: get list option was added
- Loading branch information
1 parent
1fb7f3c
commit 4803cdf
Showing
2 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package poddisruptionbudget | ||
|
||
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 podDisruptionBudget inventory in the given namespace. | ||
func List(apiClient *clients.Settings, nsname string, options ...metav1.ListOptions) ([]*Builder, error) { | ||
if apiClient == nil { | ||
glog.V(100).Infof("The apiClient is empty") | ||
|
||
return nil, fmt.Errorf("podDisruptionBudget 'apiClient' cannot be empty") | ||
} | ||
|
||
if nsname == "" { | ||
glog.V(100).Infof("podDisruptionBudget 'nsname' parameter can not be empty") | ||
|
||
return nil, fmt.Errorf("failed to list podDisruptionBudgets, 'nsname' parameter is empty") | ||
} | ||
|
||
logMessage := fmt.Sprintf("Listing podDisruptionBudget in the nsname %s", nsname) | ||
passedOptions := metav1.ListOptions{} | ||
|
||
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) | ||
|
||
pdbList, err := apiClient.PodDisruptionBudgets(nsname).List(context.TODO(), passedOptions) | ||
|
||
if err != nil { | ||
glog.V(100).Infof("Failed to list podDisruptionBudget in the nsname %s due to %s", | ||
nsname, err.Error()) | ||
|
||
return nil, err | ||
} | ||
|
||
var pdbObjects []*Builder | ||
|
||
for _, foundPDB := range pdbList.Items { | ||
copiedPDB := foundPDB | ||
pdbBuilder := &Builder{ | ||
apiClient: apiClient.PolicyV1Interface, | ||
Object: &copiedPDB, | ||
Definition: &copiedPDB, | ||
} | ||
|
||
pdbObjects = append(pdbObjects, pdbBuilder) | ||
} | ||
|
||
return pdbObjects, nil | ||
} | ||
|
||
// ListInAllNamespaces returns a cluster-wide podDisruptionBudget inventory. | ||
func ListInAllNamespaces(apiClient *clients.Settings, options ...metav1.ListOptions) ([]*Builder, error) { | ||
logMessage := "Listing all podDisruptionBudget in all namespaces" | ||
passedOptions := metav1.ListOptions{} | ||
|
||
if apiClient == nil { | ||
glog.V(100).Infof("The apiClient is empty") | ||
|
||
return nil, fmt.Errorf("podDisruptionBudget 'apiClient' cannot be empty") | ||
} | ||
|
||
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) | ||
|
||
pdbList, err := apiClient.PodDisruptionBudgets("").List(context.TODO(), passedOptions) | ||
|
||
if err != nil { | ||
glog.V(100).Infof("Failed to list all podDisruptionBudgets due to %s", err.Error()) | ||
|
||
return nil, err | ||
} | ||
|
||
var pdbObjects []*Builder | ||
|
||
for _, foundPDB := range pdbList.Items { | ||
copiedPDB := foundPDB | ||
pdbBuilder := &Builder{ | ||
apiClient: apiClient.PolicyV1Interface, | ||
Object: &copiedPDB, | ||
Definition: &copiedPDB, | ||
} | ||
|
||
pdbObjects = append(pdbObjects, pdbBuilder) | ||
} | ||
|
||
return pdbObjects, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package poddisruptionbudget | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/openshift-kni/eco-goinfra/pkg/clients" | ||
"github.com/stretchr/testify/assert" | ||
policyv1 "k8s.io/api/policy/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
var ( | ||
testSchemes = []clients.SchemeAttacher{ | ||
policyv1.AddToScheme, | ||
} | ||
defaultPDBName = "pdbtest" | ||
defaultPDBNsName = "pdbnamespace" | ||
) | ||
|
||
func TestPDBList(t *testing.T) { | ||
testCases := []struct { | ||
pdb []*Builder | ||
nsName string | ||
listOptions []metav1.ListOptions | ||
expectedError error | ||
client bool | ||
}{ | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
nsName: defaultPDBNsName, | ||
expectedError: nil, | ||
client: true, | ||
}, | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
nsName: defaultPDBNsName, | ||
listOptions: []metav1.ListOptions{{LabelSelector: "test1"}}, | ||
expectedError: nil, | ||
client: true, | ||
}, | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
nsName: defaultPDBNsName, | ||
listOptions: []metav1.ListOptions{{LabelSelector: ""}}, | ||
expectedError: nil, | ||
client: true, | ||
}, | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
nsName: defaultPDBNsName, | ||
listOptions: []metav1.ListOptions{{LabelSelector: "test1"}, {LabelSelector: "test2"}}, | ||
expectedError: fmt.Errorf("error: more than one ListOptions was passed"), | ||
client: true, | ||
}, | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
nsName: "", | ||
expectedError: fmt.Errorf("failed to list podDisruptionBudgets, 'nsname' parameter is empty"), | ||
client: true, | ||
}, | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
nsName: defaultPDBNsName, | ||
expectedError: fmt.Errorf("podDisruptionBudget 'apiClient' cannot be empty"), | ||
client: false, | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
var testSettings *clients.Settings | ||
|
||
if testCase.client { | ||
testSettings = clients.GetTestClients(clients.TestClientParams{ | ||
K8sMockObjects: buildDummyPDBObject(), | ||
SchemeAttachers: testSchemes, | ||
}) | ||
} | ||
|
||
pdbBuilders, err := List(testSettings, testCase.nsName, testCase.listOptions...) | ||
assert.Equal(t, testCase.expectedError, err) | ||
|
||
if testCase.expectedError == nil && len(testCase.listOptions) == 0 { | ||
assert.Equal(t, len(pdbBuilders), len(testCase.pdb)) | ||
} | ||
} | ||
} | ||
|
||
func TestPDBListInAllNamespaces(t *testing.T) { | ||
testCases := []struct { | ||
pdb []*Builder | ||
listOptions []metav1.ListOptions | ||
expectedError error | ||
client bool | ||
}{ | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
expectedError: nil, | ||
client: true, | ||
}, | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
listOptions: []metav1.ListOptions{{LabelSelector: "test1"}}, | ||
expectedError: nil, | ||
client: true, | ||
}, | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
listOptions: []metav1.ListOptions{{LabelSelector: ""}}, | ||
expectedError: nil, | ||
client: true, | ||
}, | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
listOptions: []metav1.ListOptions{{LabelSelector: "test1"}, {LabelSelector: "test2"}}, | ||
expectedError: fmt.Errorf("error: more than one ListOptions was passed"), | ||
client: true, | ||
}, | ||
{ | ||
pdb: []*Builder{ | ||
buildValidPDBTestBuilder(buildTestClientWithDummyObject())}, | ||
expectedError: fmt.Errorf("podDisruptionBudget 'apiClient' cannot be empty"), | ||
client: false, | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
var testSettings *clients.Settings | ||
|
||
if testCase.client { | ||
testSettings = clients.GetTestClients(clients.TestClientParams{ | ||
K8sMockObjects: buildDummyPDBObject(), | ||
SchemeAttachers: testSchemes, | ||
}) | ||
} | ||
|
||
pdbBuilders, err := ListInAllNamespaces(testSettings, testCase.listOptions...) | ||
assert.Equal(t, testCase.expectedError, err) | ||
|
||
if testCase.expectedError == nil && len(testCase.listOptions) == 0 { | ||
assert.Equal(t, len(pdbBuilders), len(testCase.pdb)) | ||
} | ||
} | ||
} | ||
|
||
// buildValidTestBuilder returns a valid Builder for testing purposes. | ||
func buildValidPDBTestBuilder(apiClient *clients.Settings) *Builder { | ||
return NewBuilder(apiClient, defaultPDBName, defaultPDBNsName) | ||
} | ||
|
||
func buildTestClientWithDummyObject() *clients.Settings { | ||
return clients.GetTestClients(clients.TestClientParams{ | ||
K8sMockObjects: buildDummyPDBObject(), | ||
SchemeAttachers: testSchemes, | ||
}) | ||
} | ||
|
||
func buildDummyPDBObject() []runtime.Object { | ||
return append([]runtime.Object{}, &policyv1.PodDisruptionBudget{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: defaultPDBName, | ||
Namespace: defaultPDBNsName, | ||
}, | ||
Spec: policyv1.PodDisruptionBudgetSpec{}, | ||
}) | ||
} |