|
| 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