|
| 1 | +// |
| 2 | +// Copyright (c) 2019-2023 Red Hat, Inc. |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +package handler |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/devfile/devworkspace-operator/pkg/constants" |
| 23 | + |
| 24 | + dwv2 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" |
| 25 | +) |
| 26 | + |
| 27 | +// Checks whether the given DevWorkspace Template Spec has multiple container components with the |
| 28 | +// controller.devfile.io/merge-contribution attribute set to true. |
| 29 | +// If only a single container component has the controller.devfile.io/merge-contribution attribute set to true, nil is returned. |
| 30 | +// If multiple container component have the controller.devfile.io/merge-contribution attribute set to true, or an error occurs |
| 31 | +// while parsing the attribute, an error is returned. |
| 32 | +func checkMultipleContainerContributionTargets(devWorkspaceSpec dwv2.DevWorkspaceTemplateSpec) error { |
| 33 | + var componentNames []string |
| 34 | + for _, component := range devWorkspaceSpec.Components { |
| 35 | + if component.Container == nil { |
| 36 | + // Ignore attribute on non-container components as it's not clear what this would mean |
| 37 | + continue |
| 38 | + } |
| 39 | + if component.Attributes.Exists(constants.MergeContributionAttribute) { |
| 40 | + var errHolder error |
| 41 | + if component.Attributes.GetBoolean(constants.MergeContributionAttribute, &errHolder) { |
| 42 | + componentNames = append(componentNames, component.Name) |
| 43 | + } |
| 44 | + |
| 45 | + if errHolder != nil { |
| 46 | + return fmt.Errorf("failed to parse %s attribute on component %s as true or false", constants.MergeContributionAttribute, component.Name) |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if len(componentNames) > 1 { |
| 52 | + return fmt.Errorf("only a single component may have the %s attribute set to true. The following %d components have the %s attribute set to true: %s", constants.MergeContributionAttribute, len(componentNames), constants.MergeContributionAttribute, strings.Join(componentNames, ", ")) |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
0 commit comments