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 1722623a175000c4542dc0d3929e3610287e58c5 Author: Tadayoshi Sato <[email protected]> AuthorDate: Tue Aug 3 15:19:13 2021 +0900 refactor(trait): use bool pointer functions throughout traits --- addons/tracing/tracing.go | 4 ++-- addons/tracing/tracing_test.go | 3 +-- pkg/trait/affinity_test.go | 5 ++--- pkg/trait/builder.go | 2 +- pkg/trait/camel.go | 2 +- pkg/trait/camel_test.go | 5 ++--- pkg/trait/container.go | 4 ++-- pkg/trait/container_probes_test.go | 12 +++--------- pkg/trait/cron.go | 19 +++++++++---------- pkg/trait/dependencies.go | 2 +- pkg/trait/deployer.go | 2 +- pkg/trait/deployment.go | 4 ++-- pkg/trait/deployment_test.go | 5 ++--- pkg/trait/environment.go | 2 +- pkg/trait/error_handler.go | 2 +- pkg/trait/gc.go | 2 +- pkg/trait/gc_test.go | 5 ++--- pkg/trait/ingress.go | 4 ++-- pkg/trait/ingress_test.go | 10 ++++------ pkg/trait/init.go | 2 +- pkg/trait/istio.go | 2 +- pkg/trait/jolokia.go | 2 +- pkg/trait/jolokia_test.go | 7 +++---- pkg/trait/jvm.go | 2 +- pkg/trait/jvm_test.go | 2 +- pkg/trait/kamelets.go | 7 ++++--- pkg/trait/knative.go | 3 +-- pkg/trait/knative_service.go | 4 ++-- pkg/trait/openapi.go | 2 +- pkg/trait/owner.go | 2 +- pkg/trait/pdb.go | 4 ++-- pkg/trait/pdb_test.go | 3 +-- pkg/trait/platform.go | 8 ++++---- pkg/trait/platform_test.go | 9 +++------ pkg/trait/pod.go | 2 +- pkg/trait/prometheus.go | 2 +- pkg/trait/pull_secret_test.go | 16 +++------------- pkg/trait/quarkus.go | 6 +----- pkg/trait/quarkus_test.go | 5 ++--- pkg/trait/route.go | 2 +- pkg/trait/service.go | 15 +++------------ pkg/trait/service_binding.go | 2 +- 42 files changed, 80 insertions(+), 123 deletions(-) diff --git a/addons/tracing/tracing.go b/addons/tracing/tracing.go index 685f35d..8c01fba 100644 --- a/addons/tracing/tracing.go +++ b/addons/tracing/tracing.go @@ -76,11 +76,11 @@ func NewTracingTrait() trait.Trait { } func (t *tracingTrait) Configure(e *trait.Environment) (bool, error) { - if t.Enabled == nil || !*t.Enabled { + if trait.IsNilOrFalse(t.Enabled) { return false, nil } - if t.Auto == nil || *t.Auto { + if trait.IsNilOrTrue(t.Auto) { if t.Endpoint == "" { for _, locator := range discovery.TracingLocators { endpoint, err := locator.FindEndpoint(e.C, t.Client, t.L, e) diff --git a/addons/tracing/tracing_test.go b/addons/tracing/tracing_test.go index 7a3c3e0..04c47ab 100644 --- a/addons/tracing/tracing_test.go +++ b/addons/tracing/tracing_test.go @@ -30,8 +30,7 @@ import ( func TestTracingTraitOnQuarkus(t *testing.T) { e := createEnvironment(t, camel.QuarkusCatalog) tracing := NewTracingTrait() - enabled := true - tracing.(*tracingTrait).Enabled = &enabled + tracing.(*tracingTrait).Enabled = trait.BoolP(true) tracing.(*tracingTrait).Endpoint = "http://endpoint3" ok, err := tracing.Configure(e) assert.Nil(t, err) diff --git a/pkg/trait/affinity_test.go b/pkg/trait/affinity_test.go index bce3441..9dd21cb 100644 --- a/pkg/trait/affinity_test.go +++ b/pkg/trait/affinity_test.go @@ -50,7 +50,7 @@ func TestConfigureAffinityTraitWithConflictingAffinitiesFails(t *testing.T) { func TestConfigureDisabledAffinityTraitFails(t *testing.T) { affinityTrait := createNominalAffinityTest() - affinityTrait.Enabled = new(bool) + affinityTrait.Enabled = BoolP(false) environment, _ := createNominalDeploymentTraitTest() configured, err := affinityTrait.Configure(environment) @@ -181,8 +181,7 @@ func testApplyPodAffinityLabelsDoesSucceed(t *testing.T, trait *affinityTrait, e func createNominalAffinityTest() *affinityTrait { trait := newAffinityTrait().(*affinityTrait) - enabled := true - trait.Enabled = &enabled + trait.Enabled = BoolP(true) return trait } diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go index ec82807..c4da97b 100644 --- a/pkg/trait/builder.go +++ b/pkg/trait/builder.go @@ -58,7 +58,7 @@ func (t *builderTrait) InfluencesKit() bool { } func (t *builderTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } diff --git a/pkg/trait/camel.go b/pkg/trait/camel.go index d64e54c..e02c78b 100644 --- a/pkg/trait/camel.go +++ b/pkg/trait/camel.go @@ -45,7 +45,7 @@ func newCamelTrait() Trait { } func (t *camelTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, errors.New("trait camel cannot be disabled") } diff --git a/pkg/trait/camel_test.go b/pkg/trait/camel_test.go index 3d6b235..7b017fa 100644 --- a/pkg/trait/camel_test.go +++ b/pkg/trait/camel_test.go @@ -40,7 +40,7 @@ func TestConfigureEnabledCamelTraitSucceeds(t *testing.T) { func TestConfigureDisabledCamelTraitFails(t *testing.T) { trait, environment := createNominalCamelTest() - trait.Enabled = new(bool) + trait.Enabled = BoolP(false) configured, err := trait.Configure(environment) assert.NotNil(t, err) @@ -72,8 +72,7 @@ func createNominalCamelTest() (*camelTrait, *Environment) { client, _ := test.NewFakeClient() trait := newCamelTrait().(*camelTrait) - enabled := true - trait.Enabled = &enabled + trait.Enabled = BoolP(true) environment := &Environment{ CamelCatalog: &camel.RuntimeCatalog{ diff --git a/pkg/trait/container.go b/pkg/trait/container.go index 94c155b..c06cb2e 100644 --- a/pkg/trait/container.go +++ b/pkg/trait/container.go @@ -125,7 +125,7 @@ func newContainerTrait() Trait { } func (t *containerTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } @@ -232,7 +232,7 @@ func (t *containerTrait) configureContainer(e *Environment) error { t.configureResources(e, &container) - if t.Expose != nil && *t.Expose { + if IsTrue(t.Expose) { t.configureService(e, &container) } t.configureCapabilities(e) diff --git a/pkg/trait/container_probes_test.go b/pkg/trait/container_probes_test.go index c4d496d..9c2cef6 100644 --- a/pkg/trait/container_probes_test.go +++ b/pkg/trait/container_probes_test.go @@ -85,10 +85,8 @@ func TestProbesOnDeployment(t *testing.T) { env.Integration.Status.Phase = v1.IntegrationPhaseDeploying env.Resources.Add(&target) - expose := true - ctr := newTestContainerTrait() - ctr.Expose = &expose + ctr.Expose = BoolP(true) ctr.LivenessTimeout = 1234 err := ctr.Apply(&env) @@ -112,10 +110,8 @@ func TestProbesOnDeploymentWithCustomScheme(t *testing.T) { env.Integration.Status.Phase = v1.IntegrationPhaseDeploying env.Resources.Add(&target) - expose := true - ctr := newTestContainerTrait() - ctr.Expose = &expose + ctr.Expose = BoolP(true) ctr.LivenessTimeout = 1234 ctr.LivenessScheme = "HTTPS" ctr.ReadinessScheme = "HTTPS" @@ -158,10 +154,8 @@ func TestProbesOnKnativeService(t *testing.T) { env.Integration.Status.Phase = v1.IntegrationPhaseDeploying env.Resources.Add(&target) - expose := true - ctr := newTestContainerTrait() - ctr.Expose = &expose + ctr.Expose = BoolP(true) ctr.LivenessTimeout = 1234 err := ctr.Apply(&env) diff --git a/pkg/trait/cron.go b/pkg/trait/cron.go index daf9249..45a325e 100644 --- a/pkg/trait/cron.go +++ b/pkg/trait/cron.go @@ -114,7 +114,7 @@ func newCronTrait() Trait { } func (t *cronTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { e.Integration.Status.SetCondition( v1.IntegrationConditionCronJobAvailable, corev1.ConditionFalse, @@ -140,7 +140,7 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) { return false, nil } - if t.Auto == nil || *t.Auto { + if IsNilOrTrue(t.Auto) { globalCron, err := t.getGlobalCron(e) if err != nil { e.Integration.Status.SetErrorCondition( @@ -175,8 +175,7 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) { } for _, fromURI := range fromURIs { if uri.GetComponent(fromURI) == genericCronComponent { - _true := true - t.Fallback = &_true + t.Fallback = BoolP(true) break } } @@ -184,7 +183,7 @@ func (t *cronTrait) Configure(e *Environment) (bool, error) { } // Fallback strategy can be implemented in any other controller - if t.Fallback != nil && *t.Fallback { + if IsTrue(t.Fallback) { if e.IntegrationInPhase(v1.IntegrationPhaseDeploying) { e.Integration.Status.SetCondition( v1.IntegrationConditionCronJobAvailable, @@ -225,7 +224,7 @@ func (t *cronTrait) Apply(e *Environment) error { if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) { util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityCron) - if t.Fallback != nil && *t.Fallback { + if IsTrue(t.Fallback) { fallbackArtifact := e.CamelCatalog.GetArtifactByScheme(genericCronComponentFallbackScheme) if fallbackArtifact == nil { return fmt.Errorf("no fallback artifact for scheme %q has been found in camel catalog", genericCronComponentFallbackScheme) @@ -235,7 +234,7 @@ func (t *cronTrait) Apply(e *Environment) error { } } - if (t.Fallback == nil || !*t.Fallback) && e.IntegrationInPhase(v1.IntegrationPhaseDeploying) { + if IsNilOrFalse(t.Fallback) && e.IntegrationInPhase(v1.IntegrationPhaseDeploying) { if e.ApplicationProperties == nil { e.ApplicationProperties = make(map[string]string) } @@ -312,16 +311,16 @@ func (t *cronTrait) getCronJobFor(e *Environment) *v1beta1.CronJob { // SelectControllerStrategy can be used to check if a CronJob can be generated given the integration and trait settings func (t *cronTrait) SelectControllerStrategy(e *Environment) (*ControllerStrategy, error) { cronStrategy := ControllerStrategyCronJob - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return nil, nil } - if t.Fallback != nil && *t.Fallback { + if IsTrue(t.Fallback) { return nil, nil } if t.Schedule != "" { return &cronStrategy, nil } - if t.Auto == nil || *t.Auto { + if IsNilOrTrue(t.Auto) { globalCron, err := t.getGlobalCron(e) if err == nil && globalCron != nil { return &cronStrategy, nil diff --git a/pkg/trait/dependencies.go b/pkg/trait/dependencies.go index 994d046..b5fd7a9 100644 --- a/pkg/trait/dependencies.go +++ b/pkg/trait/dependencies.go @@ -41,7 +41,7 @@ func newDependenciesTrait() Trait { } func (t *dependenciesTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } diff --git a/pkg/trait/deployer.go b/pkg/trait/deployer.go index ba4f378..301d7d0 100644 --- a/pkg/trait/deployer.go +++ b/pkg/trait/deployer.go @@ -170,7 +170,7 @@ func isIncompatibleServerError(err error) bool { } func (t *deployerTrait) SelectControllerStrategy(e *Environment) (*ControllerStrategy, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return nil, nil } if t.Kind != "" { diff --git a/pkg/trait/deployment.go b/pkg/trait/deployment.go index 11d24c6..cf788e7 100644 --- a/pkg/trait/deployment.go +++ b/pkg/trait/deployment.go @@ -44,7 +44,7 @@ func newDeploymentTrait() Trait { } func (t *deploymentTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { e.Integration.Status.SetCondition( v1.IntegrationConditionDeploymentAvailable, corev1.ConditionFalse, @@ -86,7 +86,7 @@ func (t *deploymentTrait) Configure(e *Environment) (bool, error) { } func (t *deploymentTrait) SelectControllerStrategy(e *Environment) (*ControllerStrategy, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return nil, nil } deploymentStrategy := ControllerStrategyDeployment diff --git a/pkg/trait/deployment_test.go b/pkg/trait/deployment_test.go index 2d5a838..a84ca82 100644 --- a/pkg/trait/deployment_test.go +++ b/pkg/trait/deployment_test.go @@ -34,7 +34,7 @@ import ( func TestConfigureDisabledDeploymentTraitDoesNotSucceed(t *testing.T) { deploymentTrait, environment := createNominalDeploymentTest() - deploymentTrait.Enabled = new(bool) + deploymentTrait.Enabled = BoolP(false) configured, err := deploymentTrait.Configure(environment) @@ -145,8 +145,7 @@ func TestApplyDeploymentTraitWhileRunningIntegrationDoesSucceed(t *testing.T) { func createNominalDeploymentTest() (*deploymentTrait, *Environment) { trait := newDeploymentTrait().(*deploymentTrait) - enabled := true - trait.Enabled = &enabled + trait.Enabled = BoolP(true) trait.Client, _ = test.NewFakeClient(&appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: "integration-name", diff --git a/pkg/trait/environment.go b/pkg/trait/environment.go index 8fee827..6ea9e9b 100644 --- a/pkg/trait/environment.go +++ b/pkg/trait/environment.go @@ -58,7 +58,7 @@ func newEnvironmentTrait() Trait { } func (t *environmentTrait) Configure(e *Environment) (bool, error) { - if t.Enabled == nil || *t.Enabled { + if IsNilOrTrue(t.Enabled) { return e.IntegrationInPhase(v1.IntegrationPhaseDeploying, v1.IntegrationPhaseRunning), nil } diff --git a/pkg/trait/error_handler.go b/pkg/trait/error_handler.go index aa59b4e..547ac52 100644 --- a/pkg/trait/error_handler.go +++ b/pkg/trait/error_handler.go @@ -44,7 +44,7 @@ func (t *errorHandlerTrait) IsPlatformTrait() bool { } func (t *errorHandlerTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } diff --git a/pkg/trait/gc.go b/pkg/trait/gc.go index 9c8380b..59210ea 100644 --- a/pkg/trait/gc.go +++ b/pkg/trait/gc.go @@ -73,7 +73,7 @@ func newGarbageCollectorTrait() Trait { } func (t *garbageCollectorTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } diff --git a/pkg/trait/gc_test.go b/pkg/trait/gc_test.go index 1300067..ac650c5 100644 --- a/pkg/trait/gc_test.go +++ b/pkg/trait/gc_test.go @@ -37,7 +37,7 @@ func TestConfigureGarbageCollectorTraitDoesSucceed(t *testing.T) { func TestConfigureDisabledGarbageCollectorTraitDoesNotSucceed(t *testing.T) { gcTrait, environment := createNominalGarbageCollectorTest() - gcTrait.Enabled = new(bool) + gcTrait.Enabled = BoolP(false) configured, err := gcTrait.Configure(environment) @@ -68,8 +68,7 @@ func TestApplyGarbageCollectorTraitDuringInitializationPhaseSkipPostActions(t *t func createNominalGarbageCollectorTest() (*garbageCollectorTrait, *Environment) { trait := newGarbageCollectorTrait().(*garbageCollectorTrait) - enabled := true - trait.Enabled = &enabled + trait.Enabled = BoolP(true) environment := &Environment{ Catalog: NewCatalog(context.TODO(), nil), diff --git a/pkg/trait/ingress.go b/pkg/trait/ingress.go index 9ecb4aa..c1f8207 100644 --- a/pkg/trait/ingress.go +++ b/pkg/trait/ingress.go @@ -57,7 +57,7 @@ func (t *ingressTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { } func (t *ingressTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { e.Integration.Status.SetCondition( v1.IntegrationConditionExposureAvailable, corev1.ConditionFalse, @@ -71,7 +71,7 @@ func (t *ingressTrait) Configure(e *Environment) (bool, error) { return false, nil } - if t.Auto == nil || *t.Auto { + if IsNilOrTrue(t.Auto) { hasService := e.Resources.GetUserServiceForIntegration(e.Integration) != nil hasHost := t.Host != "" enabled := hasService && hasHost diff --git a/pkg/trait/ingress_test.go b/pkg/trait/ingress_test.go index 4536d02..b0f08ca 100644 --- a/pkg/trait/ingress_test.go +++ b/pkg/trait/ingress_test.go @@ -43,7 +43,7 @@ func TestConfigureIngressTraitDoesSucceed(t *testing.T) { func TestConfigureDisabledIngressTraitDoesNotSucceed(t *testing.T) { ingressTrait, environment := createNominalIngressTest() - ingressTrait.Enabled = new(bool) + ingressTrait.Enabled = BoolP(false) configured, err := ingressTrait.Configure(environment) @@ -67,8 +67,7 @@ func TestConfigureIngressTraitInWrongPhaseDoesNotSucceed(t *testing.T) { func TestConfigureAutoIngressTraitWithoutUserServiceDoesNotSucceed(t *testing.T) { ingressTrait, environment := createNominalIngressTest() - auto := true - ingressTrait.Auto = &auto + ingressTrait.Auto = BoolP(true) environment.Resources = kubernetes.NewCollection() configured, err := ingressTrait.Configure(environment) @@ -156,9 +155,8 @@ func TestApplyIngressTraitDoesSucceed(t *testing.T) { func createNominalIngressTest() (*ingressTrait, *Environment) { trait := newIngressTrait().(*ingressTrait) - enabled := true - trait.Enabled = &enabled - trait.Auto = new(bool) + trait.Enabled = BoolP(true) + trait.Auto = BoolP(false) trait.Host = "hostname" environment := &Environment{ diff --git a/pkg/trait/init.go b/pkg/trait/init.go index afd9d32..466c24e 100644 --- a/pkg/trait/init.go +++ b/pkg/trait/init.go @@ -41,7 +41,7 @@ func newInitTrait() Trait { } func (t *initTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, errors.New("trait init cannot be disabled") } diff --git a/pkg/trait/istio.go b/pkg/trait/istio.go index 2c26a60..f6fc951 100644 --- a/pkg/trait/istio.go +++ b/pkg/trait/istio.go @@ -52,7 +52,7 @@ func newIstioTrait() Trait { } func (t *istioTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && *t.Enabled { + if IsTrue(t.Enabled) { return e.IntegrationInPhase(v1.IntegrationPhaseDeploying, v1.IntegrationPhaseRunning), nil } diff --git a/pkg/trait/jolokia.go b/pkg/trait/jolokia.go index 6f42700..f06bda8 100644 --- a/pkg/trait/jolokia.go +++ b/pkg/trait/jolokia.go @@ -75,7 +75,7 @@ func newJolokiaTrait() Trait { } func (t *jolokiaTrait) Configure(e *Environment) (bool, error) { - return t.Enabled != nil && *t.Enabled && e.IntegrationInPhase( + return IsTrue(t.Enabled) && e.IntegrationInPhase( v1.IntegrationPhaseInitialization, v1.IntegrationPhaseDeploying, v1.IntegrationPhaseRunning, diff --git a/pkg/trait/jolokia_test.go b/pkg/trait/jolokia_test.go index deabf64..c3b4a4e 100644 --- a/pkg/trait/jolokia_test.go +++ b/pkg/trait/jolokia_test.go @@ -208,7 +208,7 @@ func TestSetDefaultBoolJolokiaOptionShouldSucceed(t *testing.T) { func TestSetDefaultBoolJolokiaOptionShouldNotOverrideExistingValue(t *testing.T) { trait := newJolokiaTrait().(*jolokiaTrait) options := map[string]string{} - option := new(bool) + option := BoolP(false) trait.setDefaultJolokiaOption(options, &option, "key", true) @@ -251,7 +251,7 @@ func TestAddBoolPointerOptionToJolokiaOptions(t *testing.T) { trait := newJolokiaTrait().(*jolokiaTrait) options := map[string]string{} - trait.addToJolokiaOptions(options, "key", new(bool)) + trait.addToJolokiaOptions(options, "key", BoolP(false)) assert.Len(t, options, 1) assert.Equal(t, "false", options["key"]) @@ -268,8 +268,7 @@ func TestAddWrongTypeOptionToJolokiaOptionsDoesNothing(t *testing.T) { func createNominalJolokiaTest() (*jolokiaTrait, *Environment) { trait := newJolokiaTrait().(*jolokiaTrait) - enabled := true - trait.Enabled = &enabled + trait.Enabled = BoolP(true) environment := &Environment{ Catalog: NewCatalog(context.TODO(), nil), diff --git a/pkg/trait/jvm.go b/pkg/trait/jvm.go index f13099d..b31bca6 100644 --- a/pkg/trait/jvm.go +++ b/pkg/trait/jvm.go @@ -67,7 +67,7 @@ func newJvmTrait() Trait { } func (t *jvmTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } diff --git a/pkg/trait/jvm_test.go b/pkg/trait/jvm_test.go index 3337972..343b25d 100644 --- a/pkg/trait/jvm_test.go +++ b/pkg/trait/jvm_test.go @@ -68,7 +68,7 @@ func TestConfigureJvmTraitInWrongIntegrationKitPhaseDoesNotSucceed(t *testing.T) func TestConfigureJvmDisabledTraitDoesNotSucceed(t *testing.T) { trait, environment := createNominalJvmTest(v1.IntegrationKitTypePlatform) - trait.Enabled = new(bool) + trait.Enabled = BoolP(false) configured, err := trait.Configure(environment) assert.Nil(t, err) diff --git a/pkg/trait/kamelets.go b/pkg/trait/kamelets.go index 2a3a2cf..6b0b2a8 100644 --- a/pkg/trait/kamelets.go +++ b/pkg/trait/kamelets.go @@ -20,12 +20,13 @@ package trait import ( "errors" "fmt" - "github.com/apache/camel-k/pkg/util/source" "regexp" "sort" "strconv" "strings" + "github.com/apache/camel-k/pkg/util/source" + v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" kameletutils "github.com/apache/camel-k/pkg/kamelet" @@ -87,7 +88,7 @@ func (t *kameletsTrait) IsPlatformTrait() bool { } func (t *kameletsTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } @@ -95,7 +96,7 @@ func (t *kameletsTrait) Configure(e *Environment) (bool, error) { return false, nil } - if t.Auto == nil || *t.Auto { + if IsNilOrTrue(t.Auto) { var kamelets []string if t.List == "" { sources, err := kubernetes.ResolveIntegrationSources(e.C, e.Client, e.Integration, e.Resources) diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go index fe4b1de..a76ae57 100644 --- a/pkg/trait/knative.go +++ b/pkg/trait/knative.go @@ -193,8 +193,7 @@ func (t *knativeTrait) Configure(e *Environment) (bool, error) { } if t.FilterSourceChannels == nil { // Filtering is no longer used by default - filter := false - t.FilterSourceChannels = &filter + t.FilterSourceChannels = BoolP(false) } if t.SinkBinding == nil { allowed := t.isSinkBindingAllowed(e) diff --git a/pkg/trait/knative_service.go b/pkg/trait/knative_service.go index 9cc77aa..a86e7bd 100644 --- a/pkg/trait/knative_service.go +++ b/pkg/trait/knative_service.go @@ -91,7 +91,7 @@ func (t *knativeServiceTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { } func (t *knativeServiceTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { e.Integration.Status.SetCondition( v1.IntegrationConditionKnativeServiceAvailable, corev1.ConditionFalse, @@ -139,7 +139,7 @@ func (t *knativeServiceTrait) Configure(e *Environment) (bool, error) { return false, nil } - if t.Auto == nil || *t.Auto { + if IsNilOrTrue(t.Auto) { // Check the right value for minScale, as not all services are allowed to scale down to 0 if t.MinScale == nil { sources, err := kubernetes.ResolveIntegrationSources(t.Ctx, t.Client, e.Integration, e.Resources) diff --git a/pkg/trait/openapi.go b/pkg/trait/openapi.go index 8ad421e..11d63d2 100644 --- a/pkg/trait/openapi.go +++ b/pkg/trait/openapi.go @@ -64,7 +64,7 @@ func (t *openAPITrait) IsPlatformTrait() bool { } func (t *openAPITrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } diff --git a/pkg/trait/owner.go b/pkg/trait/owner.go index a95b67a..baf3b45 100644 --- a/pkg/trait/owner.go +++ b/pkg/trait/owner.go @@ -44,7 +44,7 @@ func newOwnerTrait() Trait { } func (t *ownerTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } diff --git a/pkg/trait/pdb.go b/pkg/trait/pdb.go index 6569c93..f862dae 100644 --- a/pkg/trait/pdb.go +++ b/pkg/trait/pdb.go @@ -49,7 +49,7 @@ func newPdbTrait() Trait { } func (t *pdbTrait) Configure(e *Environment) (bool, error) { - if t.Enabled == nil || !*t.Enabled { + if IsNilOrFalse(t.Enabled) { return false, nil } @@ -86,7 +86,7 @@ func (t *pdbTrait) Apply(e *Environment) error { func (t *pdbTrait) podDisruptionBudgetFor(integration *v1.Integration) *v1beta1.PodDisruptionBudget { pdb := &v1beta1.PodDisruptionBudget{ TypeMeta: metav1.TypeMeta{ - Kind: "PodDisruptionBudget", + Kind: "PodDisruptionBudget", APIVersion: v1beta1.SchemeGroupVersion.String(), }, ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/trait/pdb_test.go b/pkg/trait/pdb_test.go index d2e09aa..358dc97 100644 --- a/pkg/trait/pdb_test.go +++ b/pkg/trait/pdb_test.go @@ -94,8 +94,7 @@ func findPdb(resources *kubernetes.Collection) *v1beta1.PodDisruptionBudget { func createPdbTest() (*pdbTrait, *Environment, *appsv1.Deployment) { trait := newPdbTrait().(*pdbTrait) - enabled := true - trait.Enabled = &enabled + trait.Enabled = BoolP(true) deployment := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/trait/platform.go b/pkg/trait/platform.go index 854bd46..525d9cd 100644 --- a/pkg/trait/platform.go +++ b/pkg/trait/platform.go @@ -48,7 +48,7 @@ func newPlatformTrait() Trait { } func (t *platformTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } @@ -56,7 +56,7 @@ func (t *platformTrait) Configure(e *Environment) (bool, error) { return false, nil } - if t.Auto == nil || !*t.Auto { + if IsNilOrFalse(t.Auto) { if e.Platform == nil { if t.CreateDefault == nil { // Calculate if the platform should be automatically created when missing. @@ -102,13 +102,13 @@ func (t *platformTrait) Apply(e *Environment) error { func (t *platformTrait) getOrCreatePlatform(e *Environment) (*v1.IntegrationPlatform, error) { pl, err := platform.GetOrFind(t.Ctx, t.Client, e.Integration.Namespace, e.Integration.Status.Platform, false) if err != nil && k8serrors.IsNotFound(err) { - if t.CreateDefault != nil && *t.CreateDefault { + if IsTrue(t.CreateDefault) { platformName := e.Integration.Status.Platform if platformName == "" { platformName = platform.DefaultPlatformName } namespace := e.Integration.Namespace - if t.Global != nil && *t.Global { + if IsTrue(t.Global) { operatorNamespace := platform.GetOperatorNamespace() if operatorNamespace != "" { namespace = operatorNamespace diff --git a/pkg/trait/platform_test.go b/pkg/trait/platform_test.go index 8932ad2..16bf160 100644 --- a/pkg/trait/platform_test.go +++ b/pkg/trait/platform_test.go @@ -57,8 +57,7 @@ func TestPlatformTraitChangeStatus(t *testing.T) { } trait := newPlatformTrait().(*platformTrait) - createPlatform := false - trait.CreateDefault = &createPlatform + trait.CreateDefault = BoolP(false) var err error trait.Client, err = test.NewFakeClient() @@ -92,8 +91,7 @@ func TestPlatformTraitCreatesDefaultPlatform(t *testing.T) { } trait := newPlatformTrait().(*platformTrait) - createPlatform := true - trait.CreateDefault = &createPlatform + trait.CreateDefault = BoolP(true) var err error trait.Client, err = test.NewFakeClient() @@ -149,8 +147,7 @@ func TestPlatformTraitExisting(t *testing.T) { } trait := newPlatformTrait().(*platformTrait) - createPlatform := true - trait.CreateDefault = &createPlatform + trait.CreateDefault = BoolP(true) var err error existingPlatform := v1.NewIntegrationPlatform("ns1", "existing") diff --git a/pkg/trait/pod.go b/pkg/trait/pod.go index 328fca9..e45edc0 100644 --- a/pkg/trait/pod.go +++ b/pkg/trait/pod.go @@ -50,7 +50,7 @@ func newPodTrait() Trait { } func (t *podTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil } diff --git a/pkg/trait/prometheus.go b/pkg/trait/prometheus.go index 8971e75..bf192f6 100644 --- a/pkg/trait/prometheus.go +++ b/pkg/trait/prometheus.go @@ -57,7 +57,7 @@ func newPrometheusTrait() Trait { } func (t *prometheusTrait) Configure(e *Environment) (bool, error) { - return t.Enabled != nil && *t.Enabled && e.IntegrationInPhase( + return IsTrue(t.Enabled) && e.IntegrationInPhase( v1.IntegrationPhaseInitialization, v1.IntegrationPhaseDeploying, v1.IntegrationPhaseRunning, diff --git a/pkg/trait/pull_secret_test.go b/pkg/trait/pull_secret_test.go index 44ee3ef..12cf6b6 100644 --- a/pkg/trait/pull_secret_test.go +++ b/pkg/trait/pull_secret_test.go @@ -61,7 +61,7 @@ func TestPullSecretAuto(t *testing.T) { e, _ := getEnvironmentAndDeployment(t) trait := newPullSecretTrait().(*pullSecretTrait) - trait.Auto = newFalse() + trait.Auto = BoolP(false) enabled, err := trait.Configure(e) assert.Nil(t, err) assert.False(t, enabled) @@ -71,8 +71,8 @@ func TestPullSecretImagePullerDelegation(t *testing.T) { e, _ := getEnvironmentAndDeployment(t) trait := newPullSecretTrait().(*pullSecretTrait) - trait.Auto = newFalse() - trait.ImagePullerDelegation = newTrue() + trait.Auto = BoolP(false) + trait.ImagePullerDelegation = BoolP(true) enabled, err := trait.Configure(e) assert.Nil(t, err) assert.True(t, enabled) @@ -123,13 +123,3 @@ func getEnvironmentAndDeployment(t *testing.T) (*Environment, *appsv1.Deployment return e, &deployment } - -func newFalse() *bool { - b := false - return &b -} - -func newTrue() *bool { - b := true - return &b -} diff --git a/pkg/trait/quarkus.go b/pkg/trait/quarkus.go index 8bea05c..5c62f04 100644 --- a/pkg/trait/quarkus.go +++ b/pkg/trait/quarkus.go @@ -38,12 +38,8 @@ func newQuarkusTrait() Trait { } } -func (t *quarkusTrait) isEnabled() bool { - return t.Enabled == nil || *t.Enabled -} - func (t *quarkusTrait) Configure(e *Environment) (bool, error) { - return t.isEnabled(), nil + return IsNilOrTrue(t.Enabled), nil } func (t *quarkusTrait) Apply(e *Environment) error { diff --git a/pkg/trait/quarkus_test.go b/pkg/trait/quarkus_test.go index 181ba78..318bbb3 100644 --- a/pkg/trait/quarkus_test.go +++ b/pkg/trait/quarkus_test.go @@ -38,7 +38,7 @@ func TestConfigureQuarkusTraitShouldSucceed(t *testing.T) { func TestConfigureDisabledQuarkusTraitShouldFail(t *testing.T) { quarkusTrait, environment := createNominalQuarkusTest() - quarkusTrait.Enabled = new(bool) + quarkusTrait.Enabled = BoolP(false) configured, err := quarkusTrait.Configure(environment) @@ -67,8 +67,7 @@ func TestQuarkusTraitAddBuildStepsShouldSucceed(t *testing.T) { func createNominalQuarkusTest() (*quarkusTrait, *Environment) { trait := newQuarkusTrait().(*quarkusTrait) - enabled := true - trait.Enabled = &enabled + trait.Enabled = BoolP(true) environment := &Environment{ CamelCatalog: &camel.RuntimeCatalog{}, diff --git a/pkg/trait/route.go b/pkg/trait/route.go index c664ff5..72d20f7 100644 --- a/pkg/trait/route.go +++ b/pkg/trait/route.go @@ -81,7 +81,7 @@ func (t *routeTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { } func (t *routeTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { if e.Integration != nil { e.Integration.Status.SetCondition( v1.IntegrationConditionExposureAvailable, diff --git a/pkg/trait/service.go b/pkg/trait/service.go index 61cf4a9..dc710e7 100644 --- a/pkg/trait/service.go +++ b/pkg/trait/service.go @@ -54,12 +54,8 @@ func (t *serviceTrait) IsAllowedInProfile(profile v1.TraitProfile) bool { profile == v1.TraitProfileOpenShift } -func (t *serviceTrait) isEnabled() bool { - return t.Enabled == nil || *t.Enabled -} - func (t *serviceTrait) Configure(e *Environment) (bool, error) { - if !t.isEnabled() { + if IsFalse(t.Enabled) { e.Integration.Status.SetCondition( v1.IntegrationConditionServiceAvailable, corev1.ConditionFalse, @@ -74,7 +70,7 @@ func (t *serviceTrait) Configure(e *Environment) (bool, error) { return false, nil } - if t.Auto == nil || *t.Auto { + if IsNilOrTrue(t.Auto) { sources, err := kubernetes.ResolveIntegrationSources(t.Ctx, t.Client, e.Integration, e.Resources) if err != nil { e.Integration.Status.SetCondition( @@ -103,18 +99,13 @@ func (t *serviceTrait) Configure(e *Environment) (bool, error) { return true, nil } -func (t *serviceTrait) isNodePort() bool { - return t.NodePort == nil || *t.NodePort - -} - func (t *serviceTrait) Apply(e *Environment) error { svc := e.Resources.GetServiceForIntegration(e.Integration) // add a new service if not already created if svc == nil { svc = getServiceFor(e) - if t.isNodePort() { + if IsNilOrTrue(t.NodePort) { svc.Spec.Type = corev1.ServiceTypeNodePort } } diff --git a/pkg/trait/service_binding.go b/pkg/trait/service_binding.go index 5c2fbe7..5012050 100644 --- a/pkg/trait/service_binding.go +++ b/pkg/trait/service_binding.go @@ -49,7 +49,7 @@ func newServiceBindingTrait() Trait { } func (t *serviceBindingTrait) Configure(e *Environment) (bool, error) { - if t.Enabled != nil && !*t.Enabled { + if IsFalse(t.Enabled) { return false, nil }
