Skip to content

Commit

Permalink
Merge pull request #153 from maysunfaisal/update-library-1
Browse files Browse the repository at this point in the history
Add in util funcs to help get deploy components
  • Loading branch information
maysunfaisal authored Nov 9, 2022
2 parents a1fa6ca + 5bcd18c commit 63790be
Show file tree
Hide file tree
Showing 5 changed files with 515 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3988,7 +3988,7 @@ func Test_parseFromRegistry(t *testing.T) {
ImportReferenceUnion: v1.ImportReferenceUnion{
Id: registryId,
},
Version: "2.0.1",
Version: "2.1.0",
RegistryUrl: stagingRegistry,
},
},
Expand Down Expand Up @@ -4381,7 +4381,7 @@ func getUnsetBooleanDevfileTestData(apiVersion string) (devfileData data.Devfile

}

//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
// 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
func getBooleanDevfileTestData(apiVersion string, setDefault bool) (devfileData data.DevfileData, err error) {

type boolValues struct {
Expand Down
7 changes: 7 additions & 0 deletions pkg/devfile/parser/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type KubernetesResources struct {
Services []corev1.Service
Routes []routev1.Route
Ingresses []extensionsv1.Ingress
Others []interface{}
}

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

for _, value := range values {
var deployment appsv1.Deployment
var service corev1.Service
var route routev1.Route
var ingress extensionsv1.Ingress
var otherResource interface{}

byteData, err := k8yaml.Marshal(value)
if err != nil {
Expand All @@ -133,6 +136,9 @@ func ParseKubernetesYaml(values []interface{}) (KubernetesResources, error) {
case "Ingress":
err = k8yaml.Unmarshal(byteData, &ingress)
ingresses = append(ingresses, ingress)
default:
err = k8yaml.Unmarshal(byteData, &otherResource)
otherResources = append(otherResources, otherResource)
}

if err != nil {
Expand All @@ -145,5 +151,6 @@ func ParseKubernetesYaml(values []interface{}) (KubernetesResources, error) {
Services: services,
Routes: routes,
Ingresses: ingresses,
Others: otherResources,
}, nil
}
8 changes: 8 additions & 0 deletions pkg/devfile/parser/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
services := resources.Services
routes := resources.Routes
ingresses := resources.Ingresses
otherResources := resources.Others

for _, deploy := range deployments {
assert.Contains(t, tt.wantDeploymentNames, deploy.Name)
Expand All @@ -205,6 +206,13 @@ func TestReadAndParseKubernetesYaml(t *testing.T) {
for _, ingress := range ingresses {
assert.Contains(t, tt.wantIngressNames, ingress.Name)
}
for _, resource := range otherResources {
kubernetesMap := resource.(map[string]interface{})
metadata := kubernetesMap["metadata"]
metadataMap := metadata.(map[string]interface{})
name := metadataMap["name"]
assert.Contains(t, tt.wantOtherNames, name)
}
}
}
})
Expand Down
111 changes: 111 additions & 0 deletions pkg/devfile/parser/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// Copyright 2022 Red Hat, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package parser

import (
"fmt"
"reflect"

devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
)

// GetDeployComponents gets the default deploy command associated components
func GetDeployComponents(devfileObj DevfileObj) (map[string]string, error) {
deployCommandFilter := common.DevfileOptions{
CommandOptions: common.CommandOptions{
CommandGroupKind: devfilev1.DeployCommandGroupKind,
},
}
deployCommands, err := devfileObj.Data.GetCommands(deployCommandFilter)
if err != nil {
return nil, err
}

deployAssociatedComponents := make(map[string]string)
var deployAssociatedSubCommands []string

for _, command := range deployCommands {
if command.Apply != nil {
if len(deployCommands) > 1 && command.Apply.Group.IsDefault != nil && !*command.Apply.Group.IsDefault {
continue
}
deployAssociatedComponents[command.Apply.Component] = command.Apply.Component
} else if command.Composite != nil {
if len(deployCommands) > 1 && command.Composite.Group.IsDefault != nil && !*command.Composite.Group.IsDefault {
continue
}
deployAssociatedSubCommands = append(deployAssociatedSubCommands, command.Composite.Commands...)
}
}

applyCommandFilter := common.DevfileOptions{
CommandOptions: common.CommandOptions{
CommandType: devfilev1.ApplyCommandType,
},
}
applyCommands, err := devfileObj.Data.GetCommands(applyCommandFilter)
if err != nil {
return nil, err
}

for _, command := range applyCommands {
if command.Apply != nil {
for _, deployCommand := range deployAssociatedSubCommands {
if deployCommand == command.Id {
deployAssociatedComponents[command.Apply.Component] = command.Apply.Component
}
}

}
}

return deployAssociatedComponents, nil
}

// GetImageBuildComponent gets the image build component from the deploy associated components
func GetImageBuildComponent(devfileObj DevfileObj, deployAssociatedComponents map[string]string) (devfilev1.Component, error) {
imageComponentFilter := common.DevfileOptions{
ComponentOptions: common.ComponentOptions{
ComponentType: devfilev1.ImageComponentType,
},
}

imageComponents, err := devfileObj.Data.GetComponents(imageComponentFilter)
if err != nil {
return devfilev1.Component{}, err
}

var imageBuildComponent devfilev1.Component
for _, component := range imageComponents {
if _, ok := deployAssociatedComponents[component.Name]; ok && component.Image != nil {
if reflect.DeepEqual(imageBuildComponent, devfilev1.Component{}) {
imageBuildComponent = component
} else {
errMsg := "expected to find one devfile image component with a deploy command for build. Currently there is more than one image component"
return devfilev1.Component{}, fmt.Errorf(errMsg)
}
}
}

// If there is not one image component defined in the deploy command, err out
if reflect.DeepEqual(imageBuildComponent, devfilev1.Component{}) {
errMsg := "expected to find one devfile image component with a deploy command for build. Currently there is no image component"
return devfilev1.Component{}, fmt.Errorf(errMsg)
}

return imageBuildComponent, nil
}
Loading

0 comments on commit 63790be

Please sign in to comment.