This is an automated email from the ASF dual-hosted git repository. tsato pushed a commit to branch release-1.9.x in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 603fa2c9c4e19a172d1b45473f238d26d7ac0529 Author: Tadayoshi Sato <[email protected]> AuthorDate: Tue Jul 19 19:27:37 2022 +0900 fix(trait): more robust, case-insensitive trait profile comparison --- pkg/apis/camel/v1/common_types.go | 17 +++++++++++++++ pkg/apis/camel/v1/common_types_support.go | 25 ++++++++++++++-------- pkg/apis/camel/v1/integrationplatform_types.go | 19 +--------------- .../camel/v1/integrationplatform_types_support.go | 11 ---------- pkg/apis/camel/v1/maven_types_support.go | 9 ++++++++ pkg/trait/ingress.go | 2 +- pkg/trait/knative.go | 2 +- pkg/trait/knative_service.go | 2 +- pkg/trait/route.go | 2 +- pkg/trait/service.go | 4 ++-- pkg/trait/trait_types.go | 2 +- 11 files changed, 50 insertions(+), 45 deletions(-) diff --git a/pkg/apis/camel/v1/common_types.go b/pkg/apis/camel/v1/common_types.go index 1d9be8ceb..43e5c97fb 100644 --- a/pkg/apis/camel/v1/common_types.go +++ b/pkg/apis/camel/v1/common_types.go @@ -101,6 +101,23 @@ type FailureRecovery struct { AttemptTime metav1.Time `json:"attemptTime"` } +// TraitProfile represents lists of traits that are enabled for the specific installation/integration +type TraitProfile string + +const ( + // TraitProfileOpenShift is used by default on OpenShift clusters + TraitProfileOpenShift TraitProfile = "OpenShift" + // TraitProfileKubernetes is used by default on Kubernetes clusters + TraitProfileKubernetes TraitProfile = "Kubernetes" + // TraitProfileKnative is used by default on OpenShift/Kubernetes clusters powered by Knative + TraitProfileKnative TraitProfile = "Knative" + // DefaultTraitProfile is the trait profile used as default when no other profile is set + DefaultTraitProfile = TraitProfileKubernetes +) + +// AllTraitProfiles contains all allowed profiles +var AllTraitProfiles = []TraitProfile{TraitProfileKubernetes, TraitProfileKnative, TraitProfileOpenShift} + // A TraitSpec contains the configuration of a trait type TraitSpec struct { // TraitConfiguration parameters configuration diff --git a/pkg/apis/camel/v1/common_types_support.go b/pkg/apis/camel/v1/common_types_support.go index aec43a183..c4ce7f0f4 100644 --- a/pkg/apis/camel/v1/common_types_support.go +++ b/pkg/apis/camel/v1/common_types_support.go @@ -21,21 +21,13 @@ import ( "encoding/json" "errors" "fmt" + "strings" ) func (in *Artifact) String() string { return in.ID } -func (in *MavenArtifact) GetDependencyID() string { - switch { - case in.Version == "": - return "mvn:" + in.GroupID + ":" + in.ArtifactID - default: - return "mvn:" + in.GroupID + ":" + in.ArtifactID + ":" + in.Version - } -} - func (in *ConfigurationSpec) String() string { return fmt.Sprintf("%s=%s", in.Type, in.Value) } @@ -50,6 +42,21 @@ func (in *RuntimeSpec) CapabilityDependencies(capability string) []MavenArtifact return deps } +// TraitProfileByName returns the trait profile corresponding to the given name (case insensitive). +func TraitProfileByName(name string) TraitProfile { + for _, p := range AllTraitProfiles { + if strings.EqualFold(name, string(p)) { + return p + } + } + return "" +} + +// Equal checks if the profile is equal to the given profile (case insensitive). +func (p TraitProfile) Equal(other TraitProfile) bool { + return strings.EqualFold(string(p), string(other)) +} + // MarshalJSON returns m as the JSON encoding of m. func (m RawMessage) MarshalJSON() ([]byte, error) { if m == nil { diff --git a/pkg/apis/camel/v1/integrationplatform_types.go b/pkg/apis/camel/v1/integrationplatform_types.go index 2bf9f8f4b..03ae42ba2 100644 --- a/pkg/apis/camel/v1/integrationplatform_types.go +++ b/pkg/apis/camel/v1/integrationplatform_types.go @@ -95,7 +95,7 @@ type IntegrationPlatformList struct { type IntegrationPlatformCluster string const ( - // IntegrationPlatformClusterOpenShift is used when targeting a OpenShift cluster + // IntegrationPlatformClusterOpenShift is used when targeting an OpenShift cluster IntegrationPlatformClusterOpenShift IntegrationPlatformCluster = "OpenShift" // IntegrationPlatformClusterKubernetes is used when targeting a Kubernetes cluster IntegrationPlatformClusterKubernetes IntegrationPlatformCluster = "Kubernetes" @@ -104,23 +104,6 @@ const ( // AllIntegrationPlatformClusters -- var AllIntegrationPlatformClusters = []IntegrationPlatformCluster{IntegrationPlatformClusterOpenShift, IntegrationPlatformClusterKubernetes} -// TraitProfile represents lists of traits that are enabled for the specific installation/integration -type TraitProfile string - -const ( - // TraitProfileOpenShift is used by default on OpenShift clusters - TraitProfileOpenShift TraitProfile = "OpenShift" - // TraitProfileKubernetes is used by default on Kubernetes clusters - TraitProfileKubernetes TraitProfile = "Kubernetes" - // TraitProfileKnative is used by default on OpenShift/Kubernetes clusters powered by Knative - TraitProfileKnative TraitProfile = "Knative" - // DefaultTraitProfile is the trait profile used as default when no other profile is set - DefaultTraitProfile = TraitProfileKubernetes -) - -// AllTraitProfiles contains all allowed profiles -var AllTraitProfiles = []TraitProfile{TraitProfileKubernetes, TraitProfileKnative, TraitProfileOpenShift} - // IntegrationPlatformBuildSpec contains platform related build information. // This configuration can be used to tune the behavior of the Integration/IntegrationKit image builds. // You can define the build strategy, the image registry to use and the Maven configuration to adopt. diff --git a/pkg/apis/camel/v1/integrationplatform_types_support.go b/pkg/apis/camel/v1/integrationplatform_types_support.go index b644f462e..3ff4bb6b5 100644 --- a/pkg/apis/camel/v1/integrationplatform_types_support.go +++ b/pkg/apis/camel/v1/integrationplatform_types_support.go @@ -19,7 +19,6 @@ package v1 import ( "strconv" - "strings" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -49,16 +48,6 @@ func NewIntegrationPlatform(namespace string, name string) IntegrationPlatform { } } -// TraitProfileByName returns the trait profile corresponding to the given name (case insensitive) -func TraitProfileByName(name string) TraitProfile { - for _, p := range AllTraitProfiles { - if strings.EqualFold(name, string(p)) { - return p - } - } - return "" -} - // Configurations -- func (in *IntegrationPlatformSpec) Configurations() []ConfigurationSpec { if in == nil { diff --git a/pkg/apis/camel/v1/maven_types_support.go b/pkg/apis/camel/v1/maven_types_support.go index 0cf9dadc4..d4777bffc 100644 --- a/pkg/apis/camel/v1/maven_types_support.go +++ b/pkg/apis/camel/v1/maven_types_support.go @@ -19,6 +19,15 @@ package v1 import "encoding/xml" +func (in *MavenArtifact) GetDependencyID() string { + switch { + case in.Version == "": + return "mvn:" + in.GroupID + ":" + in.ArtifactID + default: + return "mvn:" + in.GroupID + ":" + in.ArtifactID + ":" + in.Version + } +} + type propertiesEntry struct { XMLName xml.Name Value string `xml:",chardata"` diff --git a/pkg/trait/ingress.go b/pkg/trait/ingress.go index a5e2b7e89..3302fde74 100644 --- a/pkg/trait/ingress.go +++ b/pkg/trait/ingress.go @@ -51,7 +51,7 @@ func newIngressTrait() Trait { // IsAllowedInProfile overrides default. func (t *ingressTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { - return profile == v1.TraitProfileKubernetes + return profile.Equal(v1.TraitProfileKubernetes) } func (t *ingressTrait) Configure(e *Environment) (bool, error) { diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go index f3b611bb0..1ca0adb3e 100644 --- a/pkg/trait/knative.go +++ b/pkg/trait/knative.go @@ -100,7 +100,7 @@ func newKnativeTrait() Trait { // IsAllowedInProfile overrides default. func (t *knativeTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { - return profile == v1.TraitProfileKnative + return profile.Equal(v1.TraitProfileKnative) } func (t *knativeTrait) Configure(e *Environment) (bool, error) { diff --git a/pkg/trait/knative_service.go b/pkg/trait/knative_service.go index 82576f95b..b0fb03523 100644 --- a/pkg/trait/knative_service.go +++ b/pkg/trait/knative_service.go @@ -97,7 +97,7 @@ func newKnativeServiceTrait() Trait { // IsAllowedInProfile overrides default. func (t *knativeServiceTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { - return profile == v1.TraitProfileKnative + return profile.Equal(v1.TraitProfileKnative) } func (t *knativeServiceTrait) Configure(e *Environment) (bool, error) { diff --git a/pkg/trait/route.go b/pkg/trait/route.go index 3f5b494c6..82a9d2937 100644 --- a/pkg/trait/route.go +++ b/pkg/trait/route.go @@ -104,7 +104,7 @@ func newRouteTrait() Trait { // IsAllowedInProfile overrides default. func (t *routeTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { - return profile == v1.TraitProfileOpenShift + return profile.Equal(v1.TraitProfileOpenShift) } func (t *routeTrait) Configure(e *Environment) (bool, error) { diff --git a/pkg/trait/service.go b/pkg/trait/service.go index a070c8160..9f362bb94 100644 --- a/pkg/trait/service.go +++ b/pkg/trait/service.go @@ -50,8 +50,8 @@ func newServiceTrait() Trait { // IsAllowedInProfile overrides default. func (t *serviceTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { - return profile == v1.TraitProfileKubernetes || - profile == v1.TraitProfileOpenShift + return profile.Equal(v1.TraitProfileKubernetes) || + profile.Equal(v1.TraitProfileOpenShift) } func (t *serviceTrait) Configure(e *Environment) (bool, error) { diff --git a/pkg/trait/trait_types.go b/pkg/trait/trait_types.go index a53ec9841..56bf93dc4 100644 --- a/pkg/trait/trait_types.go +++ b/pkg/trait/trait_types.go @@ -76,7 +76,7 @@ type Trait interface { // RequiresIntegrationPlatform indicates that the trait cannot work without an integration platform set RequiresIntegrationPlatform() bool - // IsAllowedInProfile tels if the trait supports the given profile + // IsAllowedInProfile tells if the trait supports the given profile IsAllowedInProfile(v1.TraitProfile) bool // Order is the order in which the trait should be executed in the normal flow
