Skip to content

Commit

Permalink
chore(trait): remove fancy '&[]bool{true}[0]' initialisers for bool p…
Browse files Browse the repository at this point in the history
…ointers
  • Loading branch information
tadayosi authored and astefanutti committed Dec 10, 2020
1 parent abc5860 commit f554750
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 51 deletions.
17 changes: 9 additions & 8 deletions pkg/trait/affinity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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,
Expand Down Expand Up @@ -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
}

Expand All @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions pkg/trait/affinity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions pkg/trait/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func newContainerTrait() Trait {
ServicePort: defaultServicePort,
ServicePortName: httpPortName,
Name: defaultContainerName,
ProbesEnabled: &[]bool{false}[0],
ProbesEnabled: util.BoolP(false),
ProbePath: defaultProbePath,
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/trait/container_probes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/trait/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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"`
}

Expand All @@ -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),
}
}

Expand All @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/trait/jvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func newJvmTrait() Trait {
return &jvmTrait{
BaseTrait: NewBaseTrait("jvm", 2000),
DebugAddress: "*:5005",
PrintCommand: &[]bool{true}[0],
PrintCommand: util.BoolP(true),
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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, " ")
Expand Down
10 changes: 5 additions & 5 deletions pkg/trait/jvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions pkg/trait/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const (
func newPrometheusTrait() Trait {
return &prometheusTrait{
BaseTrait: NewBaseTrait("prometheus", 1900),
ServiceMonitor: &[]bool{true}[0],
ServiceMonitor: util.BoolP(true),
}
}

Expand Down Expand Up @@ -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
Expand Down
16 changes: 0 additions & 16 deletions pkg/trait/trait_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit f554750

Please sign in to comment.