Skip to content

Commit

Permalink
fix(trait): more robust, case-insensitive trait profile comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
tadayosi committed Jul 20, 2022
1 parent 9b8588b commit 16caeaf
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 45 deletions.
17 changes: 17 additions & 0 deletions pkg/apis/camel/v1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 16 additions & 9 deletions pkg/apis/camel/v1/common_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -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)
}
Expand All @@ -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 {
Expand Down
19 changes: 1 addition & 18 deletions pkg/apis/camel/v1/integrationplatform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down
11 changes: 0 additions & 11 deletions pkg/apis/camel/v1/integrationplatform_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package v1

import (
"strconv"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions pkg/apis/camel/v1/maven_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/knative_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/trait/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 16caeaf

Please sign in to comment.