Skip to content
This repository was archived by the owner on Jun 26, 2024. It is now read-only.

Commit 2311f33

Browse files
committed
wip
Signed-off-by: Andy Sadler <[email protected]>
1 parent f5a7d4f commit 2311f33

File tree

6 files changed

+336
-171
lines changed

6 files changed

+336
-171
lines changed

config/manager/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
1212
kind: Kustomization
1313
images:
1414
- name: controller
15-
newName: quay.io/redhat-developer/servicebinding-operator
16-
newTag: e4860168
15+
newName: quay.io/ansadler/sbo-wrm
16+
newTag: latest

pkg/reconcile/pipeline/context/impl.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,16 +534,18 @@ func (i *impl) WorkloadResourceTemplate(gvr *schema.GroupVersionResource) (*pipe
534534
}
535535

536536
for _, template := range mapping.Spec.Versions {
537-
switch template.Version {
538-
case "*":
539-
wildcardTemplate = template
540-
case gvr.Version:
537+
fmt.Printf("Version: %v\ngvr version: %v\n", template.Version, gvr.Version)
538+
if template.Version == gvr.Version {
541539
mappingTemplate = &template
540+
break
541+
} else if template.Version == "*" {
542+
wildcardTemplate = template
542543
}
543544
}
544545
if mappingTemplate == nil {
545546
mappingTemplate = &wildcardTemplate
546547
}
548+
fmt.Printf("Mapping template: %v\n", mappingTemplate)
547549
} else {
548550
return nil, err
549551
}

pkg/reconcile/pipeline/context/impl_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package context
33
import (
44
"context"
55
"encoding/base64"
6+
"encoding/json"
67
e "errors"
78
"fmt"
9+
810
"github.com/golang/mock/gomock"
911
. "github.com/onsi/ginkgo"
1012
. "github.com/onsi/ginkgo/extensions/table"
1113
. "github.com/onsi/gomega"
1214
"github.com/redhat-developer/service-binding-operator/apis"
1315
bindingapi "github.com/redhat-developer/service-binding-operator/apis/binding/v1alpha1"
16+
"github.com/redhat-developer/service-binding-operator/apis/spec/v1alpha3"
1417
"github.com/redhat-developer/service-binding-operator/pkg/client/kubernetes/mocks"
1518
"github.com/redhat-developer/service-binding-operator/pkg/converter"
1619
"github.com/redhat-developer/service-binding-operator/pkg/reconcile/pipeline"
@@ -905,4 +908,72 @@ var _ = Describe("Context", func() {
905908
Expect(intermediateSecret.StringData).Should(HaveKeyWithValue("foo3", "val3"))
906909
})
907910
})
911+
912+
Describe("Mapping template", func() {
913+
It("should not use a wildcard when a matching version is specified", func() {
914+
ref := bindingapi.Application{
915+
Ref: bindingapi.Ref{
916+
Group: "app",
917+
Version: "v1",
918+
Kind: "Foo",
919+
Name: "app1",
920+
},
921+
}
922+
923+
sb := bindingapi.ServiceBinding{
924+
ObjectMeta: metav1.ObjectMeta{
925+
Name: "sb1",
926+
Namespace: "ns1",
927+
},
928+
Spec: bindingapi.ServiceBindingSpec{
929+
Application: ref,
930+
},
931+
}
932+
gvr := schema.GroupVersionResource{Group: "app", Version: "v1", Resource: "foos"}
933+
934+
mappingSpec := v1alpha3.ClusterWorkloadResourceMapping{
935+
Spec: v1alpha3.ClusterWorkloadResourceMappingSpec{
936+
Versions: []v1alpha3.ClusterWorkloadResourceMappingTemplate{
937+
{
938+
Version: "v1",
939+
Volumes: ".spec.volumeSpec",
940+
},
941+
{
942+
Version: "*",
943+
Volumes: ".spec.volumes",
944+
},
945+
},
946+
},
947+
ObjectMeta: metav1.ObjectMeta{
948+
Name: "foos.app",
949+
},
950+
TypeMeta: metav1.TypeMeta{
951+
Kind: v1alpha3.WorkloadResourceMappingGroupVersionKind.Kind,
952+
APIVersion: "servicebinding.io/v1alpha3",
953+
},
954+
}
955+
956+
bytes, err := json.Marshal(mappingSpec)
957+
Expect(err).NotTo(HaveOccurred())
958+
959+
var data map[string]interface{}
960+
err = json.Unmarshal(bytes, &data)
961+
Expect(err).NotTo(HaveOccurred())
962+
963+
u := &unstructured.Unstructured{}
964+
u.SetGroupVersionKind(v1alpha3.WorkloadResourceMappingGroupVersionKind)
965+
u.SetName("foos.app")
966+
u.SetUnstructuredContent(data)
967+
fmt.Printf("data: %s\n", u)
968+
client := fake.NewSimpleDynamicClient(runtime.NewScheme(), u)
969+
authClient := &fakeauth.FakeAuthorizationV1{}
970+
971+
ctx, err := Provider(client, authClient.SubjectAccessReviews(), typeLookup).Get(&sb)
972+
Expect(err).NotTo(HaveOccurred())
973+
974+
mapping, err := ctx.WorkloadResourceTemplate(&gvr)
975+
Expect(err).NotTo(HaveOccurred())
976+
Expect(mapping.Volume).To(Equal([]string{"spec", "volumeSpec"}))
977+
})
978+
})
908979
})
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package pipeline_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/golang/mock/gomock"
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
"github.com/redhat-developer/service-binding-operator/apis/spec/v1alpha3"
10+
"github.com/redhat-developer/service-binding-operator/pkg/reconcile/pipeline"
11+
"k8s.io/client-go/util/jsonpath"
12+
)
13+
14+
func jsonPath(path string) *jsonpath.JSONPath {
15+
jpath := jsonpath.New("")
16+
jpath.Parse(fmt.Sprintf("{%s}", path))
17+
return jpath
18+
}
19+
20+
var _ = Describe("Parsing", func() {
21+
var (
22+
mockCtrl *gomock.Controller
23+
)
24+
25+
BeforeEach(func() {
26+
mockCtrl = gomock.NewController(GinkgoT())
27+
})
28+
29+
AfterEach(func() {
30+
mockCtrl.Finish()
31+
})
32+
33+
Context("Successful parsing", func() {
34+
It("should parse valid mappings", func() {
35+
template := v1alpha3.ClusterWorkloadResourceMappingTemplate{
36+
Version: "*",
37+
Annotations: ".spec.annotations",
38+
Containers: []v1alpha3.ClusterWorkloadResourceMappingContainer{
39+
{
40+
Name: ".name",
41+
Env: ".env",
42+
Path: ".spec.spec.containers[*]",
43+
VolumeMounts: ".volumeMounts",
44+
},
45+
},
46+
Volumes: ".spec.spec.volumes",
47+
}
48+
mapping := pipeline.WorkloadMapping{
49+
Volume: []string{"spec", "spec", "volumes"},
50+
Containers: []pipeline.WorkloadContainer{
51+
{
52+
Name: []string{"name"},
53+
Env: []string{"env"},
54+
EnvFrom: []string{"envFrom"},
55+
VolumeMounts: []string{"volumeMounts"},
56+
Path: jsonPath(".spec.spec.containers[*]"),
57+
},
58+
},
59+
}
60+
61+
result, err := pipeline.FromWorkloadResourceMappingTemplate(template)
62+
Expect(err).NotTo(HaveOccurred())
63+
Expect(result.Volume).To(Equal(mapping.Volume))
64+
Expect(result.Containers[0].Name).To(Equal(mapping.Containers[0].Name))
65+
Expect(result.Containers[0].Env).To(Equal(mapping.Containers[0].Env))
66+
Expect(result.Containers[0].EnvFrom).To(Equal(mapping.Containers[0].EnvFrom))
67+
Expect(result.Containers[0].VolumeMounts).To(Equal(mapping.Containers[0].VolumeMounts))
68+
})
69+
It("should use defaults when not specified", func() {
70+
template := v1alpha3.ClusterWorkloadResourceMappingTemplate{
71+
Version: "*",
72+
Annotations: ".spec.annotations",
73+
Containers: []v1alpha3.ClusterWorkloadResourceMappingContainer{
74+
{
75+
Path: ".spec.spec.containers[*]",
76+
},
77+
},
78+
Volumes: ".spec.spec.volumes",
79+
}
80+
mapping := pipeline.WorkloadMapping{
81+
Volume: []string{"spec", "spec", "volumes"},
82+
Containers: []pipeline.WorkloadContainer{
83+
{
84+
Name: []string{"name"},
85+
Env: []string{"env"},
86+
EnvFrom: []string{"envFrom"},
87+
VolumeMounts: []string{"volumeMounts"},
88+
Path: jsonPath(".spec.spec.containers[*]"),
89+
},
90+
},
91+
}
92+
93+
result, err := pipeline.FromWorkloadResourceMappingTemplate(template)
94+
Expect(err).NotTo(HaveOccurred())
95+
Expect(result.Volume).To(Equal(mapping.Volume))
96+
Expect(result.Containers[0].Name).To(Equal(mapping.Containers[0].Name))
97+
Expect(result.Containers[0].Env).To(Equal(mapping.Containers[0].Env))
98+
Expect(result.Containers[0].EnvFrom).To(Equal(mapping.Containers[0].EnvFrom))
99+
Expect(result.Containers[0].VolumeMounts).To(Equal(mapping.Containers[0].VolumeMounts))
100+
})
101+
It("should return an error on invalid mappings", func() {
102+
template := v1alpha3.ClusterWorkloadResourceMappingTemplate{
103+
Version: "*",
104+
Annotations: ".spec.annotations",
105+
Containers: []v1alpha3.ClusterWorkloadResourceMappingContainer{
106+
{
107+
Path: ".spec.spec.containers[",
108+
},
109+
},
110+
Volumes: ".spec.spec.volumes",
111+
}
112+
113+
_, err := pipeline.FromWorkloadResourceMappingTemplate(template)
114+
Expect(err).To(HaveOccurred())
115+
})
116+
})
117+
})

0 commit comments

Comments
 (0)