Skip to content

Commit 66bc553

Browse files
committed
Update openshift api and service options
Signed-off-by: Maysun J Faisal <[email protected]>
1 parent 1f0a90e commit 66bc553

File tree

7 files changed

+231
-16
lines changed

7 files changed

+231
-16
lines changed

go.mod

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ require (
1111
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348
1212
github.com/mattn/go-colorable v0.1.2 // indirect
1313
github.com/mattn/go-isatty v0.0.12 // indirect
14-
github.com/openshift/api v3.9.0+incompatible
15-
github.com/pkg/errors v0.8.1
14+
github.com/openshift/api v0.0.0-20200930075302-db52bc4ef99f
15+
github.com/pkg/errors v0.9.1
1616
github.com/spf13/afero v1.2.2
1717
github.com/stretchr/testify v1.6.1 // indirect
1818
github.com/xeipuuv/gojsonschema v1.2.0
19-
golang.org/x/sys v0.0.0-20200509044756-6aff5f38e54f // indirect
20-
k8s.io/api v0.18.6
21-
k8s.io/apimachinery v0.18.6
19+
k8s.io/api v0.19.0
20+
k8s.io/apimachinery v0.19.0
2221
k8s.io/klog v1.0.0
2322
sigs.k8s.io/yaml v1.2.0
2423
)

go.sum

Lines changed: 46 additions & 0 deletions
Large diffs are not rendered by default.

pkg/devfile/generator/generators.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ type ServiceParams struct {
152152
}
153153

154154
// GetService gets the service
155-
func GetService(devfileObj parser.DevfileObj, serviceParams ServiceParams) (*corev1.Service, error) {
155+
func GetService(devfileObj parser.DevfileObj, serviceParams ServiceParams, options common.DevfileOptions) (*corev1.Service, error) {
156156

157-
serviceSpec, err := getServiceSpec(devfileObj, serviceParams.SelectorLabels)
157+
serviceSpec, err := getServiceSpec(devfileObj, serviceParams.SelectorLabels, options)
158158
if err != nil {
159159
return nil, err
160160
}

pkg/devfile/generator/generators_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
v1 "github.com/devfile/api/pkg/apis/workspaces/v1alpha2"
8+
"github.com/devfile/api/pkg/attributes"
89
"github.com/devfile/library/pkg/devfile/parser"
910
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1011
"github.com/devfile/library/pkg/testingutil"
@@ -27,6 +28,7 @@ func TestGetContainers(t *testing.T) {
2728
tests := []struct {
2829
name string
2930
containerComponents []v1.Component
31+
filterOptions common.DevfileOptions
3032
wantContainerName string
3133
wantContainerImage string
3234
wantContainerEnv []corev1.EnvVar
@@ -122,6 +124,44 @@ func TestGetContainers(t *testing.T) {
122124
wantContainerName: containerNames[0],
123125
wantContainerImage: containerImages[0],
124126
},
127+
{
128+
name: "Case 4: Filter containers",
129+
containerComponents: []v1.Component{
130+
{
131+
Name: containerNames[0],
132+
ComponentUnion: v1.ComponentUnion{
133+
Container: &v1.ContainerComponent{
134+
Container: v1.Container{
135+
Image: containerImages[0],
136+
MountSources: &falseMountSources,
137+
},
138+
},
139+
},
140+
},
141+
{
142+
Name: containerNames[1],
143+
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
144+
"firstString": "firstStringValue",
145+
"thirdString": "thirdStringValue",
146+
}),
147+
ComponentUnion: v1.ComponentUnion{
148+
Container: &v1.ContainerComponent{
149+
Container: v1.Container{
150+
Image: containerImages[0],
151+
MountSources: &falseMountSources,
152+
},
153+
},
154+
},
155+
},
156+
},
157+
wantContainerName: containerNames[1],
158+
wantContainerImage: containerImages[0],
159+
filterOptions: common.DevfileOptions{
160+
Filter: map[string]interface{}{
161+
"firstString": "firstStringValue",
162+
},
163+
},
164+
},
125165
}
126166
for _, tt := range tests {
127167
t.Run(tt.name, func(t *testing.T) {
@@ -132,7 +172,7 @@ func TestGetContainers(t *testing.T) {
132172
},
133173
}
134174

135-
containers, err := GetContainers(devObj, common.DevfileOptions{})
175+
containers, err := GetContainers(devObj, tt.filterOptions)
136176
// Unexpected error
137177
if (err != nil) != tt.wantErr {
138178
t.Errorf("TestGetContainers() error = %v, wantErr %v", err, tt.wantErr)

pkg/devfile/generator/utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,14 @@ func getDeploymentSpec(deploySpecParams deploymentSpecParams) *appsv1.Deployment
194194
}
195195

196196
// getServiceSpec iterates through the devfile components and returns a ServiceSpec
197-
func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]string) (*corev1.ServiceSpec, error) {
197+
func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]string, options common.DevfileOptions) (*corev1.ServiceSpec, error) {
198198

199199
var containerPorts []corev1.ContainerPort
200-
portExposureMap, err := getPortExposure(devfileObj)
200+
portExposureMap, err := getPortExposure(devfileObj, options)
201201
if err != nil {
202202
return nil, err
203203
}
204-
containers, err := GetContainers(devfileObj, common.DevfileOptions{})
204+
containers, err := GetContainers(devfileObj, options)
205205
if err != nil {
206206
return nil, err
207207
}
@@ -242,9 +242,9 @@ func getServiceSpec(devfileObj parser.DevfileObj, selectorLabels map[string]stri
242242

243243
// getPortExposure iterates through all endpoints and returns the highest exposure level of all TargetPort.
244244
// exposure level: public > internal > none
245-
func getPortExposure(devfileObj parser.DevfileObj) (map[int]v1.EndpointExposure, error) {
245+
func getPortExposure(devfileObj parser.DevfileObj, options common.DevfileOptions) (map[int]v1.EndpointExposure, error) {
246246
portExposureMap := make(map[int]v1.EndpointExposure)
247-
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(common.DevfileOptions{})
247+
containerComponents, err := devfileObj.Data.GetDevfileContainerComponents(options)
248248
if err != nil {
249249
return portExposureMap, err
250250
}

pkg/devfile/generator/utils_test.go

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/devfile/api/pkg/attributes"
910
"github.com/devfile/library/pkg/devfile/parser"
11+
"github.com/devfile/library/pkg/devfile/parser/data/v2/common"
1012
"github.com/devfile/library/pkg/testingutil"
1113
buildv1 "github.com/openshift/api/build/v1"
1214

@@ -613,6 +615,7 @@ func TestGetServiceSpec(t *testing.T) {
613615
name string
614616
containerComponents []v1.Component
615617
labels map[string]string
618+
filterOptions common.DevfileOptions
616619
wantPorts []corev1.ServicePort
617620
wantErr bool
618621
}{
@@ -685,6 +688,57 @@ func TestGetServiceSpec(t *testing.T) {
685688
},
686689
wantErr: false,
687690
},
691+
{
692+
name: "Case 3: filter components",
693+
containerComponents: []v1.Component{
694+
{
695+
Name: "testcontainer1",
696+
ComponentUnion: v1.ComponentUnion{
697+
Container: &v1.ContainerComponent{
698+
Endpoints: []v1.Endpoint{
699+
{
700+
Name: endpointNames[0],
701+
TargetPort: 8080,
702+
},
703+
},
704+
},
705+
},
706+
},
707+
{
708+
Name: "testcontainer2",
709+
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
710+
"firstString": "firstStringValue",
711+
"thirdString": "thirdStringValue",
712+
}),
713+
ComponentUnion: v1.ComponentUnion{
714+
Container: &v1.ContainerComponent{
715+
Endpoints: []v1.Endpoint{
716+
{
717+
Name: endpointNames[2],
718+
TargetPort: 9090,
719+
},
720+
},
721+
},
722+
},
723+
},
724+
},
725+
labels: map[string]string{
726+
"component": "testcomponent",
727+
},
728+
wantPorts: []corev1.ServicePort{
729+
{
730+
Name: "port-9090",
731+
Port: 9090,
732+
TargetPort: intstr.FromInt(9090),
733+
},
734+
},
735+
wantErr: false,
736+
filterOptions: common.DevfileOptions{
737+
Filter: map[string]interface{}{
738+
"firstString": "firstStringValue",
739+
},
740+
},
741+
},
688742
}
689743
for _, tt := range tests {
690744
t.Run(tt.name, func(t *testing.T) {
@@ -695,7 +749,7 @@ func TestGetServiceSpec(t *testing.T) {
695749
},
696750
}
697751

698-
serviceSpec, err := getServiceSpec(devObj, tt.labels)
752+
serviceSpec, err := getServiceSpec(devObj, tt.labels, tt.filterOptions)
699753

700754
// Unexpected error
701755
if (err != nil) != tt.wantErr {
@@ -733,6 +787,7 @@ func TestGetPortExposure(t *testing.T) {
733787
tests := []struct {
734788
name string
735789
containerComponents []v1.Component
790+
filterOptions common.DevfileOptions
736791
wantMap map[int]v1.EndpointExposure
737792
wantErr bool
738793
}{
@@ -918,6 +973,65 @@ func TestGetPortExposure(t *testing.T) {
918973
},
919974
},
920975
},
976+
{
977+
name: "Case 7: Filter components",
978+
wantMap: map[int]v1.EndpointExposure{
979+
8080: v1.PublicEndpointExposure,
980+
3000: v1.NoneEndpointExposure,
981+
},
982+
containerComponents: []v1.Component{
983+
{
984+
Name: "testcontainer1",
985+
Attributes: attributes.Attributes{}.FromStringMap(map[string]string{
986+
"firstString": "firstStringValue",
987+
"thirdString": "thirdStringValue",
988+
}),
989+
ComponentUnion: v1.ComponentUnion{
990+
Container: &v1.ContainerComponent{
991+
Container: v1.Container{
992+
Image: "image",
993+
},
994+
Endpoints: []v1.Endpoint{
995+
{
996+
Name: urlName,
997+
TargetPort: 8080,
998+
},
999+
{
1000+
Name: urlName,
1001+
TargetPort: 3000,
1002+
Exposure: v1.NoneEndpointExposure,
1003+
},
1004+
},
1005+
},
1006+
},
1007+
},
1008+
{
1009+
Name: "testcontainer2",
1010+
ComponentUnion: v1.ComponentUnion{
1011+
Container: &v1.ContainerComponent{
1012+
Container: v1.Container{
1013+
Image: "image",
1014+
},
1015+
Endpoints: []v1.Endpoint{
1016+
{
1017+
Name: urlName2,
1018+
TargetPort: 9090,
1019+
Secure: true,
1020+
Path: "/testpath",
1021+
Exposure: v1.InternalEndpointExposure,
1022+
Protocol: v1.HTTPSEndpointProtocol,
1023+
},
1024+
},
1025+
},
1026+
},
1027+
},
1028+
},
1029+
filterOptions: common.DevfileOptions{
1030+
Filter: map[string]interface{}{
1031+
"firstString": "firstStringValue",
1032+
},
1033+
},
1034+
},
9211035
}
9221036
for _, tt := range tests {
9231037
t.Run(tt.name, func(t *testing.T) {
@@ -926,7 +1040,7 @@ func TestGetPortExposure(t *testing.T) {
9261040
Components: tt.containerComponents,
9271041
},
9281042
}
929-
mapCreated, _ := getPortExposure(devObj)
1043+
mapCreated, _ := getPortExposure(devObj, tt.filterOptions)
9301044
if !reflect.DeepEqual(mapCreated, tt.wantMap) {
9311045
t.Errorf("Expected: %v, got %v", tt.wantMap, mapCreated)
9321046
}

pkg/testingutil/devfile.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,23 @@ func (d TestDevfileData) UpdateEvents(postStart, postStop, preStart, preStop []s
5353

5454
// GetComponents is a mock function to get the components from a devfile
5555
func (d TestDevfileData) GetComponents(options common.DevfileOptions) ([]v1.Component, error) {
56-
return d.Components, nil
56+
if len(options.Filter) == 0 {
57+
return d.Components, nil
58+
}
59+
60+
var components []v1.Component
61+
for _, comp := range d.Components {
62+
filterIn, err := common.FilterDevfileObject(comp.Attributes, options)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
if filterIn {
68+
components = append(components, comp)
69+
}
70+
}
71+
72+
return components, nil
5773
}
5874

5975
// AddComponents is a mock function to add components to the test devfile

0 commit comments

Comments
 (0)