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 9b13525e2f182e20e1ed38366cde1bca9dae465a Author: Tadayoshi Sato <[email protected]> AuthorDate: Tue Jun 21 16:02:01 2022 +0900 feat(api): strongly-typed traits configuration API for addons --- addons/keda/keda.go | 9 +++++++-- addons/master/master.go | 11 ++++++++--- addons/strimzi/strimzi_test.go | 4 ++-- addons/threescale/3scale.go | 10 ++++++++-- addons/tracing/tracing.go | 9 +++++++-- pkg/apis/camel/v1/common_types.go | 2 +- pkg/apis/camel/v1/trait_container.go | 1 + pkg/cmd/debug.go | 1 - pkg/cmd/run.go | 11 +++++------ pkg/cmd/run_test.go | 27 --------------------------- pkg/trait/trait_configure.go | 3 ++- pkg/trait/util.go | 2 +- 12 files changed, 42 insertions(+), 48 deletions(-) diff --git a/addons/keda/keda.go b/addons/keda/keda.go index e6e1d5e17..a556cc575 100644 --- a/addons/keda/keda.go +++ b/addons/keda/keda.go @@ -72,8 +72,8 @@ const ( // The KEDA trait is disabled by default. // // +camel-k:trait=keda. -type kedaTrait struct { - trait.BaseTrait `property:",squash"` +type Trait struct { + camelv1.Trait `property:",squash" json:",inline"` // Enables automatic configuration of the trait. Allows the trait to infer KEDA triggers from the Kamelets. Auto *bool `property:"auto" json:"auto,omitempty"` // Set the spec->replicas field on the top level controller to an explicit value if missing, to allow KEDA to recognize it as a scalable resource. @@ -95,6 +95,11 @@ type kedaTrait struct { Triggers []kedaTrigger `property:"triggers" json:"triggers,omitempty"` } +type kedaTrait struct { + trait.BaseTrait + Trait `property:",squash"` +} + type kedaTrigger struct { Type string `property:"type" json:"type,omitempty"` Metadata map[string]string `property:"metadata" json:"metadata,omitempty"` diff --git a/addons/master/master.go b/addons/master/master.go index 6acf221f0..ddb2e06ec 100644 --- a/addons/master/master.go +++ b/addons/master/master.go @@ -41,8 +41,8 @@ import ( // It's recommended to use a different service account than "default" when running the integration. // // +camel-k:trait=master. -type masterTrait struct { - trait.BaseTrait `property:",squash"` +type Trait struct { + v1.Trait `property:",squash" json:",inline"` // Enables automatic configuration of the trait. Auto *bool `property:"auto" json:"auto,omitempty"` // When this flag is active, the operator analyzes the source code to add dependencies required by delegate endpoints. @@ -57,7 +57,12 @@ type masterTrait struct { // Label that will be used to identify all pods contending the lock. Defaults to "camel.apache.org/integration". LabelKey *string `property:"label-key" json:"labelKey,omitempty"` // Label value that will be used to identify all pods contending the lock. Defaults to the integration name. - LabelValue *string `property:"label-value" json:"labelValue,omitempty"` + LabelValue *string `property:"label-value" json:"labelValue,omitempty"` +} + +type masterTrait struct { + trait.BaseTrait + Trait `property:",squash"` delegateDependencies []string `json:"-"` } diff --git a/addons/strimzi/strimzi_test.go b/addons/strimzi/strimzi_test.go index 1ef679d04..b1df8ebdc 100644 --- a/addons/strimzi/strimzi_test.go +++ b/addons/strimzi/strimzi_test.go @@ -64,7 +64,7 @@ func TestStrimziDirect(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, binding) assert.Equal(t, "kafka:mytopic?brokers=my-cluster-kafka-bootstrap%3A9092", binding.URI) - assert.Nil(t, binding.Traits) + assert.Equal(t, camelv1.Traits{}, binding.Traits) } func TestStrimziLookup(t *testing.T) { @@ -124,7 +124,7 @@ func TestStrimziLookup(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, binding) assert.Equal(t, "kafka:mytopicy?brokers=my-clusterx-kafka-bootstrap%3A9092", binding.URI) - assert.Nil(t, binding.Traits) + assert.Equal(t, camelv1.Traits{}, binding.Traits) } func asEndpointProperties(props map[string]string) *v1alpha1.EndpointProperties { diff --git a/addons/threescale/3scale.go b/addons/threescale/3scale.go index a1ee7dfbd..7f11cd585 100644 --- a/addons/threescale/3scale.go +++ b/addons/threescale/3scale.go @@ -22,6 +22,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/trait" ) @@ -31,8 +32,8 @@ import ( // The 3scale trait is disabled by default. // // +camel-k:trait=3scale. -type threeScaleTrait struct { - trait.BaseTrait `property:",squash"` +type Trait struct { + v1.Trait `property:",squash" json:",inline"` // Enables automatic configuration of the trait. Auto *bool `property:"auto" json:"auto,omitempty"` // The scheme to use to contact the service (default `http`) @@ -45,6 +46,11 @@ type threeScaleTrait struct { DescriptionPath *string `property:"description-path" json:"descriptionPath,omitempty"` } +type threeScaleTrait struct { + trait.BaseTrait + Trait `property:",squash"` +} + const ( // ThreeScaleSchemeAnnotation --. ThreeScaleSchemeAnnotation = "discovery.3scale.net/scheme" diff --git a/addons/tracing/tracing.go b/addons/tracing/tracing.go index dc3843c3c..f43f5e88b 100644 --- a/addons/tracing/tracing.go +++ b/addons/tracing/tracing.go @@ -34,8 +34,8 @@ import ( // The Tracing trait is disabled by default. // // +camel-k:trait=tracing. -type tracingTrait struct { - trait.BaseTrait `property:",squash"` +type Trait struct { + v1.Trait `property:",squash" json:",inline"` // Enables automatic configuration of the trait, including automatic discovery of the tracing endpoint. Auto *bool `property:"auto" json:"auto,omitempty"` // The name of the service that publishes tracing data (defaults to the integration name) @@ -48,6 +48,11 @@ type tracingTrait struct { SamplerParam *string `property:"sampler-param" json:"samplerParam,omitempty"` } +type tracingTrait struct { + trait.BaseTrait + Trait `property:",squash"` +} + const ( propEnabled = "propEnabled" propEndpoint = "propEndpoint" diff --git a/pkg/apis/camel/v1/common_types.go b/pkg/apis/camel/v1/common_types.go index 34553daa7..04e822224 100644 --- a/pkg/apis/camel/v1/common_types.go +++ b/pkg/apis/camel/v1/common_types.go @@ -113,7 +113,7 @@ type Traits struct { Container *ContainerTrait `property:"container" json:"container,omitempty"` // The configuration of Cron trait Cron *CronTrait `property:"cron" json:"cron,omitempty"` - // DependenciesTrait is the configuration of Dependencies trait + // The configuration of Dependencies trait Dependencies *DependenciesTrait `property:"dependencies" json:"dependencies,omitempty"` // The configuration of Deployer trait Deployer *DeployerTrait `property:"deployer" json:"deployer,omitempty"` diff --git a/pkg/apis/camel/v1/trait_container.go b/pkg/apis/camel/v1/trait_container.go index c88b2be34..9d4ed991d 100644 --- a/pkg/apis/camel/v1/trait_container.go +++ b/pkg/apis/camel/v1/trait_container.go @@ -28,6 +28,7 @@ import corev1 "k8s.io/api/core/v1" type ContainerTrait struct { Trait `property:",squash" json:",inline"` + // To automatically enable the trait Auto *bool `property:"auto" json:"auto,omitempty"` // The minimum amount of CPU required. diff --git a/pkg/cmd/debug.go b/pkg/cmd/debug.go index 35f6c16f3..c1d67c34d 100644 --- a/pkg/cmd/debug.go +++ b/pkg/cmd/debug.go @@ -131,7 +131,6 @@ func (o *debugCmdOptions) run(cmd *cobra.Command, args []string) error { return kubernetes.PortForward(o.Context, cmdClient, o.Namespace, selector, o.Port, o.RemotePort, cmd.OutOrStdout(), cmd.ErrOrStderr()) } -// nolint: unparam func (o *debugCmdOptions) toggleDebug(c camelv1.IntegrationsGetter, it *v1.Integration, active bool) (*v1.Integration, error) { if it.Spec.Traits.JVM == nil { it.Spec.Traits.JVM = &v1.JVMTrait{} diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index e78f484ab..e9e603976 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -307,8 +307,7 @@ func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error { return err } - catalog := trait.NewCatalog(c) - integration, err := o.createOrUpdateIntegration(cmd, c, args, catalog) + integration, err := o.createOrUpdateIntegration(cmd, c, args) if err != nil { return err } @@ -333,7 +332,7 @@ func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error { } if o.Sync || o.Dev { - err = o.syncIntegration(cmd, c, args, catalog) + err = o.syncIntegration(cmd, c, args) if err != nil { return err } @@ -423,7 +422,7 @@ func (o *runCmdOptions) waitForIntegrationReady(cmd *cobra.Command, c client.Cli return watch.HandleIntegrationStateChanges(o.Context, c, integration, handler) } -func (o *runCmdOptions) syncIntegration(cmd *cobra.Command, c client.Client, sources []string, catalog trait.Finder) error { +func (o *runCmdOptions) syncIntegration(cmd *cobra.Command, c client.Client, sources []string) error { // Let's watch all relevant files when in dev mode var files []string files = append(files, sources...) @@ -461,7 +460,7 @@ func (o *runCmdOptions) syncIntegration(cmd *cobra.Command, c client.Client, sou newCmd.Args = o.validateArgs newCmd.PreRunE = o.decode newCmd.RunE = func(cmd *cobra.Command, args []string) error { - _, err := o.createOrUpdateIntegration(cmd, c, sources, catalog) + _, err := o.createOrUpdateIntegration(cmd, c, sources) return err } newCmd.PostRunE = nil @@ -485,7 +484,7 @@ func (o *runCmdOptions) syncIntegration(cmd *cobra.Command, c client.Client, sou } // nolint: gocyclo -func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.Client, sources []string, catalog trait.Finder) (*v1.Integration, error) { +func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.Client, sources []string) (*v1.Integration, error) { namespace := o.Namespace name := o.GetIntegrationName(sources) diff --git a/pkg/cmd/run_test.go b/pkg/cmd/run_test.go index f5c1dd350..a1543b64c 100644 --- a/pkg/cmd/run_test.go +++ b/pkg/cmd/run_test.go @@ -382,33 +382,6 @@ func assertTraitConfiguration(t *testing.T, trait interface{}, expected interfac assert.Equal(t, expected, trait) } -type customTrait struct { - trait.BaseTrait `property:",squash"` - // SimpleMap - SimpleMap map[string]string `property:"simple-map" json:"simpleMap,omitempty"` - DoubleMap map[string]map[string]string `property:"double-map" json:"doubleMap,omitempty"` - SliceOfMap []map[string]string `property:"slice-of-map" json:"sliceOfMap,omitempty"` -} - -func (c customTrait) Configure(environment *trait.Environment) (bool, error) { - panic("implement me") -} -func (c customTrait) Apply(environment *trait.Environment) error { - panic("implement me") -} - -var _ trait.Trait = &customTrait{} - -type customTraitFinder struct { -} - -func (finder customTraitFinder) GetTrait(id string) trait.Trait { - if id == "custom" { - return &customTrait{} - } - return nil -} - func TestRunUseFlowsFlag(t *testing.T) { runCmdOptions, rootCmd, _ := initializeRunCmdOptions(t) _, err := test.ExecuteCommand(rootCmd, cmdRun, "--use-flows=false", integrationSource) diff --git a/pkg/trait/trait_configure.go b/pkg/trait/trait_configure.go index 223d6926f..fb442b28e 100644 --- a/pkg/trait/trait_configure.go +++ b/pkg/trait/trait_configure.go @@ -66,8 +66,9 @@ func (c *Catalog) configureTraits(traits interface{}) error { } for id, trait := range traitsMap { + t := trait // Avoid G601: Implicit memory aliasing in for loop if catTrait := c.GetTrait(id); catTrait != nil { - if err := decodeTraitSpec(&trait, catTrait); err != nil { + if err := decodeTraitSpec(&t, catTrait); err != nil { return err } } diff --git a/pkg/trait/util.go b/pkg/trait/util.go index 97ae4748d..8b56e6f78 100644 --- a/pkg/trait/util.go +++ b/pkg/trait/util.go @@ -244,7 +244,7 @@ func ToMap(traits interface{}) (map[string]map[string]interface{}, error) { return traitsMap, nil } -// ToTrait unmarshals a map configuration to a target trait +// ToTrait unmarshals a map configuration to a target trait. func ToTrait(trait map[string]interface{}, target interface{}) error { data, err := json.Marshal(trait) if err != nil {
