From 2a82d2862f619900f8e6e8f8d2c5fdd2dce0071d Mon Sep 17 00:00:00 2001 From: Tadayoshi Sato Date: Thu, 10 Dec 2020 17:23:56 +0900 Subject: [PATCH] chore(trait): remove fancy '&[]bool{true}[0]' initialisers for bool pointers --- pkg/trait/affinity.go | 17 +++++++++-------- pkg/trait/affinity_test.go | 9 +++++---- pkg/trait/container.go | 12 ++++++------ pkg/trait/container_probes_test.go | 3 ++- pkg/trait/environment.go | 11 ++++++----- pkg/trait/jvm.go | 8 ++++---- pkg/trait/jvm_test.go | 10 +++++----- pkg/trait/prometheus.go | 4 ++-- pkg/trait/trait_types.go | 16 ---------------- pkg/util/util.go | 29 +++++++++++++++++++++++++++++ 10 files changed, 68 insertions(+), 51 deletions(-) diff --git a/pkg/trait/affinity.go b/pkg/trait/affinity.go index 23d4dec3cf..5c77cf62e1 100644 --- a/pkg/trait/affinity.go +++ b/pkg/trait/affinity.go @@ -28,6 +28,7 @@ import ( "k8s.io/apimachinery/pkg/selection" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" + "github.com/apache/camel-k/pkg/util" ) // Allows constraining which nodes the integration pod(s) are eligible to be scheduled on, based on labels on the node, @@ -55,17 +56,17 @@ type affinityTrait struct { func newAffinityTrait() Trait { return &affinityTrait{ BaseTrait: NewBaseTrait("affinity", 1300), - PodAffinity: &[]bool{false}[0], - PodAntiAffinity: &[]bool{false}[0], + PodAffinity: util.BoolP(false), + PodAntiAffinity: util.BoolP(false), } } func (t *affinityTrait) Configure(e *Environment) (bool, error) { - if isNilOrFalse(t.Enabled) { + if util.IsNilOrFalse(t.Enabled) { return false, nil } - if isTrue(t.PodAffinity) && isTrue(t.PodAntiAffinity) { + if util.IsTrue(t.PodAffinity) && util.IsTrue(t.PodAntiAffinity) { return false, fmt.Errorf("both pod affinity and pod anti-affinity can't be set simultaneously") } @@ -138,7 +139,7 @@ func (t *affinityTrait) addNodeAffinity(_ *Environment, deployment *appsv1.Deplo } func (t *affinityTrait) addPodAffinity(e *Environment, deployment *appsv1.Deployment) error { - if isNilOrFalse(t.PodAffinity) && len(t.PodAffinityLabels) == 0 { + if util.IsNilOrFalse(t.PodAffinity) && len(t.PodAffinityLabels) == 0 { return nil } @@ -163,7 +164,7 @@ func (t *affinityTrait) addPodAffinity(e *Environment, deployment *appsv1.Deploy } } - if isTrue(t.PodAffinity) { + if util.IsTrue(t.PodAffinity) { labelSelectorRequirements = append(labelSelectorRequirements, metav1.LabelSelectorRequirement{ Key: v1.IntegrationLabel, Operator: metav1.LabelSelectorOpIn, @@ -194,7 +195,7 @@ func (t *affinityTrait) addPodAffinity(e *Environment, deployment *appsv1.Deploy } func (t *affinityTrait) addPodAntiAffinity(e *Environment, deployment *appsv1.Deployment) error { - if isNilOrFalse(t.PodAntiAffinity) && len(t.PodAntiAffinityLabels) == 0 { + if util.IsNilOrFalse(t.PodAntiAffinity) && len(t.PodAntiAffinityLabels) == 0 { return nil } @@ -219,7 +220,7 @@ func (t *affinityTrait) addPodAntiAffinity(e *Environment, deployment *appsv1.De } } - if isTrue(t.PodAntiAffinity) { + if util.IsTrue(t.PodAntiAffinity) { labelSelectorRequirements = append(labelSelectorRequirements, metav1.LabelSelectorRequirement{ Key: v1.IntegrationLabel, Operator: metav1.LabelSelectorOpIn, diff --git a/pkg/trait/affinity_test.go b/pkg/trait/affinity_test.go index 165c5b3f49..f74bc6005d 100644 --- a/pkg/trait/affinity_test.go +++ b/pkg/trait/affinity_test.go @@ -27,6 +27,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/util" "github.com/apache/camel-k/pkg/util/kubernetes" ) @@ -40,8 +41,8 @@ func TestConfigureAffinityTraitDoesSucceed(t *testing.T) { func TestConfigureAffinityTraitWithConflictingAffinitiesFails(t *testing.T) { affinityTrait, environment, _ := createNominalAffinityTest() - affinityTrait.PodAffinity = &[]bool{true}[0] - affinityTrait.PodAntiAffinity = &[]bool{true}[0] + affinityTrait.PodAffinity = util.BoolP(true) + affinityTrait.PodAntiAffinity = util.BoolP(true) configured, err := affinityTrait.Configure(environment) assert.False(t, configured) @@ -83,7 +84,7 @@ func TestApplyNodeAffinityLabelsDoesSucceed(t *testing.T) { func TestApplyPodAntiAffinityLabelsDoesSucceed(t *testing.T) { affinityTrait, environment, deployment := createNominalAffinityTest() - affinityTrait.PodAntiAffinity = &[]bool{true}[0] + affinityTrait.PodAntiAffinity = util.BoolP(true) affinityTrait.PodAntiAffinityLabels = []string{"criteria != value"} err := affinityTrait.Apply(environment) @@ -105,7 +106,7 @@ func TestApplyPodAntiAffinityLabelsDoesSucceed(t *testing.T) { func TestApplyPodAffinityLabelsDoesSucceed(t *testing.T) { affinityTrait, environment, deployment := createNominalAffinityTest() - affinityTrait.PodAffinity = &[]bool{true}[0] + affinityTrait.PodAffinity = util.BoolP(true) affinityTrait.PodAffinityLabels = []string{"!criteria"} err := affinityTrait.Apply(environment) diff --git a/pkg/trait/container.go b/pkg/trait/container.go index 33f51fbbb6..0be5f156f2 100644 --- a/pkg/trait/container.go +++ b/pkg/trait/container.go @@ -113,7 +113,7 @@ func newContainerTrait() Trait { ServicePort: defaultServicePort, ServicePortName: httpPortName, Name: defaultContainerName, - ProbesEnabled: &[]bool{false}[0], + ProbesEnabled: util.BoolP(false), ProbePath: defaultProbePath, } } @@ -127,7 +127,7 @@ func (t *containerTrait) Configure(e *Environment) (bool, error) { return false, nil } - if isNilOrTrue(t.Auto) { + if util.IsNilOrTrue(t.Auto) { if t.Expose == nil { e := e.Resources.GetServiceForIntegration(e.Integration) != nil t.Expose = &e @@ -155,7 +155,7 @@ func (t *containerTrait) IsPlatformTrait() bool { } func (t *containerTrait) configureDependencies(e *Environment) { - if isNilOrFalse(t.ProbesEnabled) { + if util.IsNilOrFalse(t.ProbesEnabled) { return } @@ -207,7 +207,7 @@ func (t *containerTrait) configureContainer(e *Environment) error { // Deployment // if err := e.Resources.VisitDeploymentE(func(deployment *appsv1.Deployment) error { - if isTrue(t.ProbesEnabled) && t.PortName == httpPortName { + if util.IsTrue(t.ProbesEnabled) && t.PortName == httpPortName { if err := t.configureProbes(e, &container, t.Port, t.ProbePath); err != nil { return err } @@ -236,7 +236,7 @@ func (t *containerTrait) configureContainer(e *Environment) error { // Knative Service // if err := e.Resources.VisitKnativeServiceE(func(service *serving.Service) error { - if isTrue(t.ProbesEnabled) && t.PortName == httpPortName { + if util.IsTrue(t.ProbesEnabled) && t.PortName == httpPortName { // don't set the port on Knative service as it is not allowed. if err := t.configureProbes(e, &container, 0, t.ProbePath); err != nil { return err @@ -277,7 +277,7 @@ func (t *containerTrait) configureContainer(e *Environment) error { // CronJob // if err := e.Resources.VisitCronJobE(func(cron *v1beta1.CronJob) error { - if isTrue(t.ProbesEnabled) && t.PortName == httpPortName { + if util.IsTrue(t.ProbesEnabled) && t.PortName == httpPortName { if err := t.configureProbes(e, &container, t.Port, t.ProbePath); err != nil { return err } diff --git a/pkg/trait/container_probes_test.go b/pkg/trait/container_probes_test.go index c2ef9fdd85..026d683d62 100644 --- a/pkg/trait/container_probes_test.go +++ b/pkg/trait/container_probes_test.go @@ -27,6 +27,7 @@ import ( appsv1 "k8s.io/api/apps/v1" v1 "github.com/apache/camel-k/pkg/apis/camel/v1" + "github.com/apache/camel-k/pkg/util" "github.com/apache/camel-k/pkg/util/camel" "github.com/apache/camel-k/pkg/util/kubernetes" ) @@ -57,7 +58,7 @@ func newTestProbesEnv(t *testing.T, provider v1.RuntimeProvider) Environment { func newTestContainerTrait() *containerTrait { tr := newContainerTrait().(*containerTrait) - tr.ProbesEnabled = &[]bool{true}[0] + tr.ProbesEnabled = util.BoolP(true) return tr } diff --git a/pkg/trait/environment.go b/pkg/trait/environment.go index e30107db51..f81fcd1fa9 100644 --- a/pkg/trait/environment.go +++ b/pkg/trait/environment.go @@ -19,6 +19,7 @@ package trait import ( v1 "github.com/apache/camel-k/pkg/apis/camel/v1" + "github.com/apache/camel-k/pkg/util" "github.com/apache/camel-k/pkg/util/defaults" "github.com/apache/camel-k/pkg/util/envvar" ) @@ -28,7 +29,8 @@ import ( // // +camel-k:trait=environment type environmentTrait struct { - BaseTrait `property:",squash"` + BaseTrait `property:",squash"` + // Enables injection of NAMESPACE and POD_NAME environment variables (default `true`) ContainerMeta *bool `property:"container-meta" json:"containerMeta,omitempty"` } @@ -51,9 +53,8 @@ const ( func newEnvironmentTrait() Trait { return &environmentTrait{ - BaseTrait: NewBaseTrait("environment", 800), - // Enable injection of NAMESPACE and POD_NAME environment variables. - ContainerMeta: &[]bool{true}[0], + BaseTrait: NewBaseTrait("environment", 800), + ContainerMeta: util.BoolP(true), } } @@ -74,7 +75,7 @@ func (t *environmentTrait) Apply(e *Environment) error { envvar.SetVal(&e.EnvVars, envVarMountPathConfigMaps, ConfigMapsMountPath) envvar.SetVal(&e.EnvVars, envVarMountPathSecrets, SecretsMountPath) - if isNilOrTrue(t.ContainerMeta) { + if util.IsNilOrTrue(t.ContainerMeta) { envvar.SetValFrom(&e.EnvVars, envVarNamespace, "metadata.namespace") envvar.SetValFrom(&e.EnvVars, envVarPodName, "metadata.name") } diff --git a/pkg/trait/jvm.go b/pkg/trait/jvm.go index 2c6567fbf1..59b1f80b02 100644 --- a/pkg/trait/jvm.go +++ b/pkg/trait/jvm.go @@ -54,7 +54,7 @@ func newJvmTrait() Trait { return &jvmTrait{ BaseTrait: NewBaseTrait("jvm", 2000), DebugAddress: "*:5005", - PrintCommand: &[]bool{true}[0], + PrintCommand: util.BoolP(true), } } @@ -115,9 +115,9 @@ func (t *jvmTrait) Apply(e *Environment) error { args := container.Args // Remote debugging - if isTrue(t.Debug) { + if util.IsTrue(t.Debug) { suspend := "n" - if isTrue(t.DebugSuspend) { + if util.IsTrue(t.DebugSuspend) { suspend = "y" } args = append(args, @@ -167,7 +167,7 @@ func (t *jvmTrait) Apply(e *Environment) error { args = append(args, "-cp", strings.Join(items, ":")) args = append(args, e.CamelCatalog.Runtime.ApplicationClass) - if isNilOrTrue(t.PrintCommand) { + if util.IsNilOrTrue(t.PrintCommand) { args = append([]string{"exec", "java"}, args...) container.Command = []string{"/bin/sh", "-c"} cmd := strings.Join(args, " ") diff --git a/pkg/trait/jvm_test.go b/pkg/trait/jvm_test.go index 78a0f5bd48..c8a88adf8f 100644 --- a/pkg/trait/jvm_test.go +++ b/pkg/trait/jvm_test.go @@ -22,6 +22,7 @@ import ( "sort" "testing" + "github.com/apache/camel-k/pkg/util" "github.com/apache/camel-k/pkg/util/camel" "github.com/scylladb/go-set/strset" @@ -145,8 +146,8 @@ func TestApplyJvmTraitWithKNativeResource(t *testing.T) { func TestApplyJvmTraitWithDebugEnabled(t *testing.T) { trait, environment := createNominalJvmTest() - trait.Debug = &[]bool{true}[0] - trait.DebugSuspend = &[]bool{true}[0] + trait.Debug = util.BoolP(true) + trait.DebugSuspend = util.BoolP(true) d := appsv1.Deployment{ Spec: appsv1.DeploymentSpec{ @@ -202,9 +203,8 @@ func createJvmTestWithKitType(kitType string) (*jvmTrait, *Environment) { ) trait := newJvmTrait().(*jvmTrait) - enabled := true - trait.Enabled = &enabled - trait.PrintCommand = &[]bool{false}[0] + trait.Enabled = util.BoolP(true) + trait.PrintCommand = util.BoolP(false) trait.Ctx = context.TODO() trait.Client = client diff --git a/pkg/trait/prometheus.go b/pkg/trait/prometheus.go index 449f02bd59..8d4efb29f4 100644 --- a/pkg/trait/prometheus.go +++ b/pkg/trait/prometheus.go @@ -72,7 +72,7 @@ const ( func newPrometheusTrait() Trait { return &prometheusTrait{ BaseTrait: NewBaseTrait("prometheus", 1900), - ServiceMonitor: &[]bool{true}[0], + ServiceMonitor: util.BoolP(true), } } @@ -182,7 +182,7 @@ func (t *prometheusTrait) Apply(e *Environment) (err error) { condition.Message = fmt.Sprintf("%s(%s/%d) -> ", service.Name, servicePort.Name, servicePort.Port) + condition.Message // Add the ServiceMonitor resource - if isNilOrTrue(t.ServiceMonitor) { + if util.IsNilOrTrue(t.ServiceMonitor) { smt, err := t.getServiceMonitorFor(e) if err != nil { return err diff --git a/pkg/trait/trait_types.go b/pkg/trait/trait_types.go index 45dc7a2226..8343fb9734 100644 --- a/pkg/trait/trait_types.go +++ b/pkg/trait/trait_types.go @@ -188,22 +188,6 @@ type ControllerStrategySelector interface { ControllerStrategySelectorOrder() int } -func isTrue(b *bool) bool { - return b != nil && *b -} - -func isNilOrTrue(b *bool) bool { - return b == nil || *b -} - -func isFalse(b *bool) bool { - return b != nil && !*b -} - -func isNilOrFalse(b *bool) bool { - return b == nil || !*b -} - /* Environment */ // A Environment provides the context where the trait is executed diff --git a/pkg/util/util.go b/pkg/util/util.go index 335b064091..587083e7a3 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -461,3 +461,32 @@ func GetEnvironmentVariable(variable string) (string, error) { return value, nil } + +/// Bool operations: + +// BoolP returns a pointer to a bool value +func BoolP(b bool) *bool { + return &b +} + +// IsTrue checks if the bool pointer is defined and true +func IsTrue(b *bool) bool { + return b != nil && *b +} + +// IsNilOrTrue checks if the bool pointer is nil or true. +// You can use it if the bool pointer is meant to be true by default. +func IsNilOrTrue(b *bool) bool { + return b == nil || *b +} + +// IsFalse checks if the bool pointer is defined and false +func IsFalse(b *bool) bool { + return b != nil && !*b +} + +// IsNilOrFalse checks if the bool pointer is nil or false. +// You can use it if the bool pointer is meant to be false by default. +func IsNilOrFalse(b *bool) bool { + return b == nil || !*b +}