-
Notifications
You must be signed in to change notification settings - Fork 91
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
Fix support for label selectors #971
Merged
openshift-merge-robot
merged 1 commit into
redhat-developer:master
from
baijum:label-selector
Jun 10, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
e "errors" | ||
"fmt" | ||
|
||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
|
@@ -21,6 +22,7 @@ import ( | |
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/dynamic/fake" | ||
"k8s.io/client-go/testing" | ||
) | ||
|
||
var _ = Describe("Context", func() { | ||
|
@@ -91,7 +93,146 @@ var _ = Describe("Context", func() { | |
Entry("no binding path specified", nil, defaultContainerPath), | ||
Entry("binding path specified", &v1alpha1.BindingPath{ContainersPath: "foo.bar"}, "foo.bar"), | ||
) | ||
DescribeTable("should return slice of size 2 if 2 applications are specified through label seclector", func(bindingPath *v1alpha1.BindingPath, expectedContainerPath string) { | ||
ls := &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{"env": "prod"}, | ||
} | ||
|
||
ref := v1alpha1.Application{ | ||
Ref: v1alpha1.Ref{ | ||
Group: "app", | ||
Version: "v1", | ||
Kind: "Foo", | ||
}, | ||
LabelSelector: ls, | ||
BindingPath: bindingPath, | ||
} | ||
|
||
sb := v1alpha1.ServiceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sb1", | ||
Namespace: "ns1", | ||
}, | ||
Spec: v1alpha1.ServiceBindingSpec{ | ||
Application: &ref, | ||
}, | ||
} | ||
gvr := &schema.GroupVersionResource{Group: "app", Version: "v1", Resource: "foos"} | ||
typeLookup.EXPECT().ResourceForReferable(&ref).Return(gvr, nil) | ||
|
||
u1 := &unstructured.Unstructured{} | ||
u1.SetName("app1") | ||
u1.SetNamespace(sb.Namespace) | ||
u1.SetGroupVersionKind(schema.GroupVersionKind{Group: "app", Version: "v1", Kind: "Foo"}) | ||
u1.SetLabels(map[string]string{"env": "prod"}) | ||
|
||
u2 := &unstructured.Unstructured{} | ||
u2.SetName("app2") | ||
u2.SetNamespace(sb.Namespace) | ||
u2.SetGroupVersionKind(schema.GroupVersionKind{Group: "app", Version: "v1", Kind: "Foo"}) | ||
u2.SetLabels(map[string]string{"env": "prod"}) | ||
|
||
client := fake.NewSimpleDynamicClient(runtime.NewScheme(), u1, u2) | ||
|
||
ctx := &impl{client: client, serviceBinding: &sb, typeLookup: typeLookup} | ||
|
||
applications, err := ctx.Applications() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(applications).To(HaveLen(2)) | ||
pedjak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Expect(applications[0].Resource().GetName()).NotTo(Equal(applications[1].Resource().GetName())) | ||
Expect(applications[0].Resource()).Should(BeElementOf(u1, u2)) | ||
Expect(applications[1].Resource()).Should(BeElementOf(u1, u2)) | ||
Expect(applications[0].ContainersPath()).To(Equal(expectedContainerPath)) | ||
Expect(applications[1].ContainersPath()).To(Equal(expectedContainerPath)) | ||
}, | ||
Entry("no binding path specified", nil, defaultContainerPath), | ||
Entry("binding path specified", &v1alpha1.BindingPath{ContainersPath: "foo.bar"}, "foo.bar"), | ||
) | ||
DescribeTable("should return slice of size 0 if no application is matching through label seclector", func(bindingPath *v1alpha1.BindingPath, expectedContainerPath string) { | ||
ls := &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{"env": "prod"}, | ||
} | ||
|
||
ref := v1alpha1.Application{ | ||
Ref: v1alpha1.Ref{ | ||
Group: "app", | ||
Version: "v1", | ||
Kind: "Foo", | ||
}, | ||
LabelSelector: ls, | ||
BindingPath: bindingPath, | ||
} | ||
|
||
sb := v1alpha1.ServiceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sb1", | ||
Namespace: "ns1", | ||
}, | ||
Spec: v1alpha1.ServiceBindingSpec{ | ||
Application: &ref, | ||
}, | ||
} | ||
gvr := &schema.GroupVersionResource{Group: "app", Version: "v1", Resource: "foos"} | ||
typeLookup.EXPECT().ResourceForReferable(&ref).Return(gvr, nil) | ||
|
||
u := &unstructured.Unstructured{} | ||
u.SetName("app") | ||
u.SetNamespace(sb.Namespace) | ||
u.SetGroupVersionKind(schema.GroupVersionKind{Group: "app", Version: "v1", Kind: "Foo"}) | ||
u.SetLabels(map[string]string{"env": "stage"}) | ||
client := fake.NewSimpleDynamicClient(runtime.NewScheme(), u) | ||
|
||
ctx := &impl{client: client, serviceBinding: &sb, typeLookup: typeLookup} | ||
|
||
applications, err := ctx.Applications() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(applications).To(HaveLen(0)) | ||
}, | ||
Entry("no binding path specified", nil, defaultContainerPath), | ||
Entry("binding path specified", &v1alpha1.BindingPath{ContainersPath: "foo.bar"}, "foo.bar"), | ||
) | ||
|
||
It("should return error if application list returns error", func() { | ||
ls := &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{"env": "prod"}, | ||
} | ||
|
||
ref := v1alpha1.Application{ | ||
Ref: v1alpha1.Ref{ | ||
Group: "app", | ||
Version: "v1", | ||
Kind: "Foo", | ||
}, | ||
LabelSelector: ls, | ||
} | ||
|
||
sb := v1alpha1.ServiceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sb1", | ||
Namespace: "ns1", | ||
}, | ||
Spec: v1alpha1.ServiceBindingSpec{ | ||
Application: &ref, | ||
}, | ||
} | ||
pedjak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
gvr := &schema.GroupVersionResource{Group: "app", Version: "v1", Resource: "foos"} | ||
typeLookup.EXPECT().ResourceForReferable(&ref).Return(gvr, nil) | ||
|
||
client := fake.NewSimpleDynamicClient(runtime.NewScheme()) | ||
expectedError := "Error listing foo" | ||
client.PrependReactor("list", "foos", | ||
func(action testing.Action) (handled bool, ret runtime.Object, err error) { | ||
return true, nil, e.New(expectedError) | ||
}) | ||
|
||
ctx := &impl{client: client, serviceBinding: &sb, typeLookup: typeLookup} | ||
|
||
_, err := ctx.Applications() | ||
Expect(err).To(HaveOccurred()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, but which error has occurred? we need to assert that as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. af6e990 |
||
Expect(err.Error()).To(Equal(expectedError)) | ||
}) | ||
It("should return error if application is not found", func() { | ||
ref := v1alpha1.Application{ | ||
Ref: v1alpha1.Ref{ | ||
|
59 changes: 59 additions & 0 deletions
59
test/acceptance/features/bindMultipleAppsToSingleService.feature
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,59 @@ | ||
Feature: Bind multiple applications to a single service | ||
|
||
As a user of Service Binding Operator | ||
I want to bind multiple applications to a single service that depends on | ||
|
||
Background: | ||
Given Namespace [TEST_NAMESPACE] is used | ||
* Service Binding Operator is running | ||
|
||
Scenario: Successfully bind two applications to a single service | ||
Given Test applications "gen-app-a-s-f-1" and "gen-app-a-s-f-2" is running | ||
* The common label "app-custom=test" is set for both apps | ||
* CustomResourceDefinition backends.stable.example.com is available | ||
* The Secret is present | ||
""" | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: backend-secret | ||
stringData: | ||
username: AzureDiamond | ||
password: hunter2 | ||
""" | ||
And The Custom Resource is present | ||
""" | ||
apiVersion: stable.example.com/v1 | ||
kind: Backend | ||
metadata: | ||
name: service-a-s-f | ||
annotations: | ||
service.binding: path={.status.data.dbCredentials},objectType=Secret,elementType=map | ||
status: | ||
data: | ||
dbCredentials: backend-secret | ||
""" | ||
When Service Binding is applied | ||
""" | ||
apiVersion: binding.operators.coreos.com/v1alpha1 | ||
kind: ServiceBinding | ||
metadata: | ||
name: service-binding-a-s-f | ||
spec: | ||
bindAsFiles: false | ||
services: | ||
- group: stable.example.com | ||
version: v1 | ||
kind: Backend | ||
name: service-a-s-f | ||
application: | ||
labelSelector: | ||
matchLabels: | ||
app-custom: test | ||
group: apps | ||
version: v1 | ||
resource: deployments | ||
""" | ||
Then Service Binding "service-binding-a-s-f" is ready | ||
And The application env var "BACKEND_USERNAME" has value "AzureDiamond" in both apps | ||
And The application env var "BACKEND_PASSWORD" has value "hunter2" in both apps |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
persistedResource
value (address of the item) is taken using the index.Ref. https://play.golang.org/p/ePs7st6f91g