This is an automated email from the ASF dual-hosted git repository. tsato pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 16caeaf2d44f53f2e43c3356d318254e32f743c2 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 a0906a52d..660ab9a60 100644 --- a/pkg/apis/camel/v1/common_types.go +++ b/pkg/apis/camel/v1/common_types.go @@ -103,6 +103,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} + // Traits represents the collection of trait configurations type Traits struct { // The configuration of Affinity trait diff --git a/pkg/apis/camel/v1/common_types_support.go b/pkg/apis/camel/v1/common_types_support.go index cf66c8b7f..7f071a834 100644 --- a/pkg/apis/camel/v1/common_types_support.go +++ b/pkg/apis/camel/v1/common_types_support.go @@ -21,6 +21,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -29,15 +30,6 @@ 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) } @@ -52,6 +44,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 0bb94e061..8f4551841 100644 --- a/pkg/apis/camel/v1/integrationplatform_types.go +++ b/pkg/apis/camel/v1/integrationplatform_types.go @@ -96,7 +96,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" @@ -105,23 +105,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 769aea622..fdd53b664 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 93b0bd591..5ee3aaf1f 100644 --- a/pkg/trait/ingress.go +++ b/pkg/trait/ingress.go @@ -46,7 +46,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 7117a31e0..bdf03b3d7 100644 --- a/pkg/trait/knative.go +++ b/pkg/trait/knative.go @@ -65,7 +65,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 3ea30c67e..55c813d00 100644 --- a/pkg/trait/knative_service.go +++ b/pkg/trait/knative_service.go @@ -62,7 +62,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 7a23a0128..5b6da6616 100644 --- a/pkg/trait/route.go +++ b/pkg/trait/route.go @@ -48,7 +48,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 79199041a..a83a7c545 100644 --- a/pkg/trait/service.go +++ b/pkg/trait/service.go @@ -43,8 +43,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 17504e242..5106102de 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
