Skip to content

Commit 63790be

Browse files
authored
Merge pull request #153 from maysunfaisal/update-library-1
Add in util funcs to help get deploy components
2 parents a1fa6ca + 5bcd18c commit 63790be

5 files changed

Lines changed: 515 additions & 2 deletions

File tree

pkg/devfile/parser/parse_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3988,7 +3988,7 @@ func Test_parseFromRegistry(t *testing.T) {
39883988
ImportReferenceUnion: v1.ImportReferenceUnion{
39893989
Id: registryId,
39903990
},
3991-
Version: "2.0.1",
3991+
Version: "2.1.0",
39923992
RegistryUrl: stagingRegistry,
39933993
},
39943994
},
@@ -4381,7 +4381,7 @@ func getUnsetBooleanDevfileTestData(apiVersion string) (devfileData data.Devfile
43814381

43824382
}
43834383

4384-
//getBooleanDevfileTestData returns a DevfileData object that contains set values for the boolean properties. If setDefault is true, an object with the default boolean values will be returned
4384+
// getBooleanDevfileTestData returns a DevfileData object that contains set values for the boolean properties. If setDefault is true, an object with the default boolean values will be returned
43854385
func getBooleanDevfileTestData(apiVersion string, setDefault bool) (devfileData data.DevfileData, err error) {
43864386

43874387
type boolValues struct {

pkg/devfile/parser/reader.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type KubernetesResources struct {
4949
Services []corev1.Service
5050
Routes []routev1.Route
5151
Ingresses []extensionsv1.Ingress
52+
Others []interface{}
5253
}
5354

5455
// ReadKubernetesYaml reads a yaml Kubernetes file from either the Path, URL or Data provided.
@@ -105,12 +106,14 @@ func ParseKubernetesYaml(values []interface{}) (KubernetesResources, error) {
105106
var services []corev1.Service
106107
var routes []routev1.Route
107108
var ingresses []extensionsv1.Ingress
109+
var otherResources []interface{}
108110

109111
for _, value := range values {
110112
var deployment appsv1.Deployment
111113
var service corev1.Service
112114
var route routev1.Route
113115
var ingress extensionsv1.Ingress
116+
var otherResource interface{}
114117

115118
byteData, err := k8yaml.Marshal(value)
116119
if err != nil {
@@ -133,6 +136,9 @@ func ParseKubernetesYaml(values []interface{}) (KubernetesResources, error) {
133136
case "Ingress":
134137
err = k8yaml.Unmarshal(byteData, &ingress)
135138
ingresses = append(ingresses, ingress)
139+
default:
140+
err = k8yaml.Unmarshal(byteData, &otherResource)
141+
otherResources = append(otherResources, otherResource)
136142
}
137143

138144
if err != nil {
@@ -145,5 +151,6 @@ func ParseKubernetesYaml(values []interface{}) (KubernetesResources, error) {
145151
Services: services,
146152
Routes: routes,
147153
Ingresses: ingresses,
154+
Others: otherResources,
148155
}, nil
149156
}

pkg/devfile/parser/reader_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
192192
services := resources.Services
193193
routes := resources.Routes
194194
ingresses := resources.Ingresses
195+
otherResources := resources.Others
195196

196197
for _, deploy := range deployments {
197198
assert.Contains(t, tt.wantDeploymentNames, deploy.Name)
@@ -205,6 +206,13 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
205206
for _, ingress := range ingresses {
206207
assert.Contains(t, tt.wantIngressNames, ingress.Name)
207208
}
209+
for _, resource := range otherResources {
210+
kubernetesMap := resource.(map[string]interface{})
211+
metadata := kubernetesMap["metadata"]
212+
metadataMap := metadata.(map[string]interface{})
213+
name := metadataMap["name"]
214+
assert.Contains(t, tt.wantOtherNames, name)
215+
}
208216
}
209217
}
210218
})

pkg/devfile/parser/utils.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//
2+
// Copyright 2022 Red Hat, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
package parser
17+
18+
import (
19+
"fmt"
20+
"reflect"
21+
22+
devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
23+
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
24+
)
25+
26+
// GetDeployComponents gets the default deploy command associated components
27+
func GetDeployComponents(devfileObj DevfileObj) (map[string]string, error) {
28+
deployCommandFilter := common.DevfileOptions{
29+
CommandOptions: common.CommandOptions{
30+
CommandGroupKind: devfilev1.DeployCommandGroupKind,
31+
},
32+
}
33+
deployCommands, err := devfileObj.Data.GetCommands(deployCommandFilter)
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
deployAssociatedComponents := make(map[string]string)
39+
var deployAssociatedSubCommands []string
40+
41+
for _, command := range deployCommands {
42+
if command.Apply != nil {
43+
if len(deployCommands) > 1 && command.Apply.Group.IsDefault != nil && !*command.Apply.Group.IsDefault {
44+
continue
45+
}
46+
deployAssociatedComponents[command.Apply.Component] = command.Apply.Component
47+
} else if command.Composite != nil {
48+
if len(deployCommands) > 1 && command.Composite.Group.IsDefault != nil && !*command.Composite.Group.IsDefault {
49+
continue
50+
}
51+
deployAssociatedSubCommands = append(deployAssociatedSubCommands, command.Composite.Commands...)
52+
}
53+
}
54+
55+
applyCommandFilter := common.DevfileOptions{
56+
CommandOptions: common.CommandOptions{
57+
CommandType: devfilev1.ApplyCommandType,
58+
},
59+
}
60+
applyCommands, err := devfileObj.Data.GetCommands(applyCommandFilter)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
for _, command := range applyCommands {
66+
if command.Apply != nil {
67+
for _, deployCommand := range deployAssociatedSubCommands {
68+
if deployCommand == command.Id {
69+
deployAssociatedComponents[command.Apply.Component] = command.Apply.Component
70+
}
71+
}
72+
73+
}
74+
}
75+
76+
return deployAssociatedComponents, nil
77+
}
78+
79+
// GetImageBuildComponent gets the image build component from the deploy associated components
80+
func GetImageBuildComponent(devfileObj DevfileObj, deployAssociatedComponents map[string]string) (devfilev1.Component, error) {
81+
imageComponentFilter := common.DevfileOptions{
82+
ComponentOptions: common.ComponentOptions{
83+
ComponentType: devfilev1.ImageComponentType,
84+
},
85+
}
86+
87+
imageComponents, err := devfileObj.Data.GetComponents(imageComponentFilter)
88+
if err != nil {
89+
return devfilev1.Component{}, err
90+
}
91+
92+
var imageBuildComponent devfilev1.Component
93+
for _, component := range imageComponents {
94+
if _, ok := deployAssociatedComponents[component.Name]; ok && component.Image != nil {
95+
if reflect.DeepEqual(imageBuildComponent, devfilev1.Component{}) {
96+
imageBuildComponent = component
97+
} else {
98+
errMsg := "expected to find one devfile image component with a deploy command for build. Currently there is more than one image component"
99+
return devfilev1.Component{}, fmt.Errorf(errMsg)
100+
}
101+
}
102+
}
103+
104+
// If there is not one image component defined in the deploy command, err out
105+
if reflect.DeepEqual(imageBuildComponent, devfilev1.Component{}) {
106+
errMsg := "expected to find one devfile image component with a deploy command for build. Currently there is no image component"
107+
return devfilev1.Component{}, fmt.Errorf(errMsg)
108+
}
109+
110+
return imageBuildComponent, nil
111+
}

0 commit comments

Comments
 (0)