diff --git a/.golangci.yml b/.golangci.yml index c17f6affcf..b4e0be7d57 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -88,7 +88,6 @@ linters: - cyclop - depguard - exhaustive - - exhaustivestruct - exhaustruct - forbidigo - funlen diff --git a/addons/vault/aws/aws_secrets_manager.go b/addons/vault/aws/aws_secrets_manager.go index 913316c035..139d8618e7 100644 --- a/addons/vault/aws/aws_secrets_manager.go +++ b/addons/vault/aws/aws_secrets_manager.go @@ -127,7 +127,7 @@ func (t *awsSecretsManagerTrait) Apply(environment *trait.Environment) error { hits := v1.PlainConfigSecretRegexp.FindAllStringSubmatch(t.AccessKey, -1) if len(hits) >= 1 { - var res, _ = v1.DecodeValueSource(t.AccessKey, "aws-access-key", "The access Key provided is not valid") + var res, _ = v1.DecodeValueSource(t.AccessKey, "aws-access-key") if secretValue, err := kubernetes.ResolveValueSource(environment.Ctx, environment.Client, environment.Platform.Namespace, &res); err != nil { return err } else if secretValue != "" { @@ -138,7 +138,7 @@ func (t *awsSecretsManagerTrait) Apply(environment *trait.Environment) error { } hits = v1.PlainConfigSecretRegexp.FindAllStringSubmatch(t.SecretKey, -1) if len(hits) >= 1 { - var res, _ = v1.DecodeValueSource(t.SecretKey, "aws-secret-key", "The secret Key provided is not valid") + var res, _ = v1.DecodeValueSource(t.SecretKey, "aws-secret-key") if secretValue, err := kubernetes.ResolveValueSource(environment.Ctx, environment.Client, environment.Platform.Namespace, &res); err != nil { return err } else if secretValue != "" { diff --git a/addons/vault/azure/azure_key_vault.go b/addons/vault/azure/azure_key_vault.go index 638b8a7387..cf6a491a35 100644 --- a/addons/vault/azure/azure_key_vault.go +++ b/addons/vault/azure/azure_key_vault.go @@ -140,7 +140,7 @@ func (t *azureKeyVaultTrait) Apply(environment *trait.Environment) error { hits := v1.PlainConfigSecretRegexp.FindAllStringSubmatch(t.ClientSecret, -1) if len(hits) >= 1 { - var res, _ = v1.DecodeValueSource(t.ClientSecret, "azure-key-vault-client-secret", "The Azure Key Vault Client Secret provided is not valid") + var res, _ = v1.DecodeValueSource(t.ClientSecret, "azure-key-vault-client-secret") if secretValue, err := kubernetes.ResolveValueSource(environment.Ctx, environment.Client, environment.Platform.Namespace, &res); err != nil { return err } else if secretValue != "" { @@ -151,7 +151,7 @@ func (t *azureKeyVaultTrait) Apply(environment *trait.Environment) error { } hits = v1.PlainConfigSecretRegexp.FindAllStringSubmatch(t.BlobAccessKey, -1) if len(hits) >= 1 { - var res, _ = v1.DecodeValueSource(t.BlobAccessKey, "azure-storage-blob-access-key", "The Azure Storage Blob Access Key provided is not valid") + var res, _ = v1.DecodeValueSource(t.BlobAccessKey, "azure-storage-blob-access-key") if secretValue, err := kubernetes.ResolveValueSource(environment.Ctx, environment.Client, environment.Platform.Namespace, &res); err != nil { return err } else if secretValue != "" { diff --git a/addons/vault/hashicorp/hashicorp_vault.go b/addons/vault/hashicorp/hashicorp_vault.go index 05a3b94373..d305f2025b 100644 --- a/addons/vault/hashicorp/hashicorp_vault.go +++ b/addons/vault/hashicorp/hashicorp_vault.go @@ -102,7 +102,7 @@ func (t *hashicorpVaultTrait) Apply(environment *trait.Environment) error { hits := v1.PlainConfigSecretRegexp.FindAllStringSubmatch(t.Token, -1) if len(hits) >= 1 { - var res, _ = v1.DecodeValueSource(t.Token, "hashicorp-vault-token", "The Hashicorp Vault Token provided is not valid") + var res, _ = v1.DecodeValueSource(t.Token, "hashicorp-vault-token") secretValue, err := kubernetes.ResolveValueSource(environment.Ctx, environment.Client, environment.Platform.Namespace, &res) if err != nil { diff --git a/cmd/util/doc-gen/generators/traitdocgen.go b/cmd/util/doc-gen/generators/traitdocgen.go index 07c761707a..b1d684b8b2 100644 --- a/cmd/util/doc-gen/generators/traitdocgen.go +++ b/cmd/util/doc-gen/generators/traitdocgen.go @@ -330,14 +330,14 @@ func writeFile(file *os.File, content []string) error { if err := file.Truncate(0); err != nil { return err } - max := 0 + mx := 0 for i, line := range content { if line != "" { - max = i + mx = i } } for i, line := range content { - if i <= max { + if i <= mx { if _, err := file.WriteString(line + "\n"); err != nil { return err } diff --git a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc index 63512e31e9..54b27b790d 100644 --- a/docs/modules/ROOT/partials/apis/camel-k-crds.adoc +++ b/docs/modules/ROOT/partials/apis/camel-k-crds.adoc @@ -6608,7 +6608,7 @@ bool Can be used to enable/disable exposure via kubernetes Service. |`port` + -int +int32 | @@ -6622,7 +6622,7 @@ string To configure a different port name for the port exposed by the container. It defaults to `http` only when the `expose` parameter is true. |`servicePort` + -int +int32 | @@ -7556,7 +7556,7 @@ string The password used for authentication, applicable when the `user` option is set. |`port` + -int +int32 | diff --git a/docs/modules/traits/pages/container.adoc b/docs/modules/traits/pages/container.adoc index 6e96d45239..3b5774c0ea 100755 --- a/docs/modules/traits/pages/container.adoc +++ b/docs/modules/traits/pages/container.adoc @@ -56,7 +56,7 @@ The following configuration options are available: | Can be used to enable/disable exposure via kubernetes Service. | container.port -| int +| int32 | To configure a different port exposed by the container (default `8080`). | container.port-name @@ -64,7 +64,7 @@ The following configuration options are available: | To configure a different port name for the port exposed by the container. It defaults to `http` only when the `expose` parameter is true. | container.service-port -| int +| int32 | To configure under which service port the container port is to be exposed (default `80`). | container.service-port-name diff --git a/docs/modules/traits/pages/jolokia.adoc b/docs/modules/traits/pages/jolokia.adoc index 87426045ba..1b055b4a62 100755 --- a/docs/modules/traits/pages/jolokia.adoc +++ b/docs/modules/traits/pages/jolokia.adoc @@ -63,7 +63,7 @@ the servers binds to every network interface (default `"*"`). | The password used for authentication, applicable when the `user` option is set. | jolokia.port -| int +| int32 | The Jolokia endpoint port (default `8778`). | jolokia.protocol diff --git a/go.mod b/go.mod index a79a4b2477..fece1aafc5 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/apache/camel-k/v2 -go 1.22 +go 1.23 require ( github.com/Masterminds/semver v1.5.0 diff --git a/helm/camel-k/crds/camel-k-crds.yaml b/helm/camel-k/crds/camel-k-crds.yaml index 73e14cef95..7fd648e8f9 100644 --- a/helm/camel-k/crds/camel-k-crds.yaml +++ b/helm/camel-k/crds/camel-k-crds.yaml @@ -3930,6 +3930,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -3964,6 +3965,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -4457,6 +4459,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -6081,6 +6084,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -6115,6 +6119,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -6608,6 +6613,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -8135,6 +8141,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -8169,6 +8176,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -8662,6 +8670,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -10165,6 +10174,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -10199,6 +10209,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -10692,6 +10703,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -18236,6 +18248,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -18270,6 +18283,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -18763,6 +18777,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -20228,6 +20243,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -20262,6 +20278,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -20755,6 +20772,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -28373,6 +28391,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the @@ -28407,6 +28426,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name @@ -28901,6 +28921,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -39310,6 +39331,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the @@ -39344,6 +39366,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name @@ -39838,6 +39861,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` diff --git a/pkg/apis/camel/v1/common_types_support.go b/pkg/apis/camel/v1/common_types_support.go index 8f01f71f13..adb6a7c944 100644 --- a/pkg/apis/camel/v1/common_types_support.go +++ b/pkg/apis/camel/v1/common_types_support.go @@ -224,7 +224,7 @@ func (bc *BuildConfiguration) IsEmpty() bool { } // DecodeValueSource returns a ValueSource object from an input that respects the format configmap|secret:resource-name[/path]. -func DecodeValueSource(input string, defaultKey string, errorMessage string) (ValueSource, error) { +func DecodeValueSource(input string, defaultKey string) (ValueSource, error) { sub := make([]string, 0) hits := PlainConfigSecretRegexp.FindAllStringSubmatch(input, -1) @@ -262,7 +262,7 @@ func DecodeValueSource(input string, defaultKey string, errorMessage string) (Va } } - return ValueSource{}, fmt.Errorf(errorMessage) + return ValueSource{}, fmt.Errorf("could not decode %s", input) } // IsGeneratedFromKamelet determines is a source spec is derived from a Kamelet. diff --git a/pkg/apis/camel/v1/common_types_support_test.go b/pkg/apis/camel/v1/common_types_support_test.go index e04a6e8347..abf81f390b 100644 --- a/pkg/apis/camel/v1/common_types_support_test.go +++ b/pkg/apis/camel/v1/common_types_support_test.go @@ -72,7 +72,7 @@ func TestTraitsMerge(t *testing.T) { assert.NotNil(t, t1.Container) assert.False(t, ptr.Deref(t1.Container.Auto, true)) assert.Equal(t, "http-8081", t1.Container.PortName) - assert.Equal(t, 81, t1.Container.ServicePort) + assert.Equal(t, int32(81), t1.Container.ServicePort) // values from merged trait take precedence over the original ones assert.NotNil(t, t1.Logging) @@ -162,7 +162,7 @@ func TestIntegrationKitTraitsMerge(t *testing.T) { } func TestDecodeValueSourceValid(t *testing.T) { - res, err := DecodeValueSource("configmap:my-configmap", "defaultkey", "errorMessage") + res, err := DecodeValueSource("configmap:my-configmap", "defaultkey") require.NoError(t, err) assert.NotNil(t, res) @@ -170,7 +170,7 @@ func TestDecodeValueSourceValid(t *testing.T) { assert.NotNil(t, res.ConfigMapKeyRef) assert.Equal(t, "defaultkey", res.ConfigMapKeyRef.Key) - res, err = DecodeValueSource("configmap:my-configmap/my-key", "defaultkey", "errorMessage") + res, err = DecodeValueSource("configmap:my-configmap/my-key", "defaultkey") require.NoError(t, err) assert.NotNil(t, res) @@ -178,7 +178,7 @@ func TestDecodeValueSourceValid(t *testing.T) { assert.NotNil(t, res.ConfigMapKeyRef) assert.Equal(t, "my-key", res.ConfigMapKeyRef.Key) - res, err = DecodeValueSource("secret:my-secret/mykey", "defaultkey", "errorMessage") + res, err = DecodeValueSource("secret:my-secret/mykey", "defaultkey") require.NoError(t, err) assert.NotNil(t, res) @@ -186,7 +186,7 @@ func TestDecodeValueSourceValid(t *testing.T) { assert.NotNil(t, res.SecretKeyRef) assert.Equal(t, "mykey", res.SecretKeyRef.Key) - res, err = DecodeValueSource("secret:my-secret", "defaultkey", "errorMessage") + res, err = DecodeValueSource("secret:my-secret", "defaultkey") require.NoError(t, err) assert.NotNil(t, res) @@ -235,10 +235,10 @@ func TestDecodeValueSourceInvalid(t *testing.T) { for i, tc := range testcases { t.Run(fmt.Sprintf("test-%d-%s", i, tc.name), func(t *testing.T) { - res, err := DecodeValueSource(tc.input, tc.defaultKey, tc.errorMessage) + res, err := DecodeValueSource(tc.input, tc.defaultKey) require.Error(t, err) assert.Equal(t, ValueSource{}, res) - assert.Equal(t, err.Error(), tc.errorMessage) + assert.Equal(t, err.Error(), "could not decode "+tc.input) }) } diff --git a/pkg/apis/camel/v1/trait/container.go b/pkg/apis/camel/v1/trait/container.go index 2e6ad91215..cffa4f9288 100644 --- a/pkg/apis/camel/v1/trait/container.go +++ b/pkg/apis/camel/v1/trait/container.go @@ -39,11 +39,11 @@ type ContainerTrait struct { // Can be used to enable/disable exposure via kubernetes Service. Expose *bool `property:"expose" json:"expose,omitempty"` // To configure a different port exposed by the container (default `8080`). - Port int `property:"port" json:"port,omitempty"` + Port int32 `property:"port" json:"port,omitempty"` // To configure a different port name for the port exposed by the container. It defaults to `http` only when the `expose` parameter is true. PortName string `property:"port-name" json:"portName,omitempty"` // To configure under which service port the container port is to be exposed (default `80`). - ServicePort int `property:"service-port" json:"servicePort,omitempty"` + ServicePort int32 `property:"service-port" json:"servicePort,omitempty"` // To configure under which service port name the container port is to be exposed (default `http`). ServicePortName string `property:"service-port-name" json:"servicePortName,omitempty"` // The main container name. It's named `integration` by default. diff --git a/pkg/apis/camel/v1/trait/jolokia.go b/pkg/apis/camel/v1/trait/jolokia.go index c59cb15fab..d90487cd1c 100644 --- a/pkg/apis/camel/v1/trait/jolokia.go +++ b/pkg/apis/camel/v1/trait/jolokia.go @@ -46,7 +46,7 @@ type JolokiaTrait struct { // The password used for authentication, applicable when the `user` option is set. Password *string `property:"password" json:"password,omitempty"` // The Jolokia endpoint port (default `8778`). - Port int `property:"port" json:"port,omitempty"` + Port int32 `property:"port" json:"port,omitempty"` // The protocol to use, either `http` or `https` (default `https` for OpenShift) Protocol *string `property:"protocol" json:"protocol,omitempty"` // The user to be used for authentication diff --git a/pkg/builder/spectrum.go b/pkg/builder/spectrum.go index ffc9ca8818..1f78f7e485 100644 --- a/pkg/builder/spectrum.go +++ b/pkg/builder/spectrum.go @@ -146,6 +146,6 @@ func readSpectrumLogs(newStdOut io.Reader) { for scanner.Scan() { line := scanner.Text() - log.Infof(line) + log.Info(line) } } diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go index 5cead99959..d17c055f95 100644 --- a/pkg/cmd/install.go +++ b/pkg/cmd/install.go @@ -571,8 +571,7 @@ func (o *installCmdOptions) setupIntegrationPlatform(c client.Client, namespace } if o.MavenSettings != "" { - mavenSettings, err := v1.DecodeValueSource(o.MavenSettings, "settings.xml", - "illegal maven setting definition, syntax: configmap|secret:resource-name[/settings path]") + mavenSettings, err := v1.DecodeValueSource(o.MavenSettings, "settings.xml") if err != nil { return nil, err } diff --git a/pkg/cmd/install_test.go b/pkg/cmd/install_test.go index 6fed126d13..ba6d4d6045 100644 --- a/pkg/cmd/install_test.go +++ b/pkg/cmd/install_test.go @@ -460,5 +460,5 @@ func TestInstallDebugLogging4(t *testing.T) { } func decodeMavenSettings(mavenSettings string) (v1.ValueSource, error) { - return v1.DecodeValueSource(mavenSettings, "settings.xml", "illegal maven setting definition, syntax: configmap|secret:resource-name[/settings path]") + return v1.DecodeValueSource(mavenSettings, "settings.xml") } diff --git a/pkg/cmd/operator/operator.go b/pkg/cmd/operator/operator.go index d8d6a32d44..53c85f588d 100644 --- a/pkg/cmd/operator/operator.go +++ b/pkg/cmd/operator/operator.go @@ -60,6 +60,7 @@ import ( "github.com/apache/camel-k/v2/pkg/controller/synthetic" "github.com/apache/camel-k/v2/pkg/install" "github.com/apache/camel-k/v2/pkg/platform" + "github.com/apache/camel-k/v2/pkg/util" "github.com/apache/camel-k/v2/pkg/util/defaults" "github.com/apache/camel-k/v2/pkg/util/kubernetes" logutil "github.com/apache/camel-k/v2/pkg/util/log" @@ -103,8 +104,10 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID default: customLevel, err := strconv.Atoi(strings.ToLower(logLevelVal)) exitOnError(err, "Invalid log-level") + int8Lev, err := util.IToInt8(customLevel) + exitOnError(err, "Invalid log-level") // Need to multiply by -1 to turn logr expected level into zap level - logLevel = zapcore.Level(int8(customLevel) * -1) + logLevel = zapcore.Level(*int8Lev * -1) } } else { logLevel = zapcore.InfoLevel diff --git a/pkg/controller/integration/kits.go b/pkg/controller/integration/kits.go index 21add8eeb1..cfa0d04c87 100644 --- a/pkg/controller/integration/kits.go +++ b/pkg/controller/integration/kits.go @@ -141,7 +141,7 @@ func integrationMatches(ctx context.Context, c client.Client, integration *v1.In } // If IntegrationKit has any source, we must verify that it corresponds with the one in the Integration. // This is important in case of Native builds as we need to rebuild when language requires a source during build. - if (kit.Spec.Sources != nil && len(kit.Spec.Sources) > 0) && !hasMatchingSourcesForNative(integration, kit) { + if len(kit.Spec.Sources) > 0 && !hasMatchingSourcesForNative(integration, kit) { ilog.Debug("Integration and integration-kit sources do not match", "integration", integration.Name, "integration-kit", kit.Name, "namespace", integration.Namespace) return false, nil } diff --git a/pkg/controller/integration/monitor.go b/pkg/controller/integration/monitor.go index 8759af19e4..f7f4f72a40 100644 --- a/pkg/controller/integration/monitor.go +++ b/pkg/controller/integration/monitor.go @@ -21,6 +21,7 @@ import ( "context" "errors" "fmt" + "math" "reflect" "strconv" "strings" @@ -233,8 +234,12 @@ func (action *monitorAction) monitorPods(ctx context.Context, environment *trait } nonTerminatingPods++ } - podCount := int32(len(pendingPods.Items) + nonTerminatingPods) - integration.Status.Replicas = &podCount + podCount := len(pendingPods.Items) + nonTerminatingPods + replicas, err := IToInt32(podCount) + if err != nil { + return nil, err + } + integration.Status.Replicas = replicas // Reconcile Integration phase and ready condition if integration.Status.Phase == v1.IntegrationPhaseDeploying { @@ -249,6 +254,15 @@ func (action *monitorAction) monitorPods(ctx context.Context, environment *trait return integration, nil } +func IToInt32(x int) (*int32, error) { + if x < math.MinInt32 || x > math.MaxInt32 { + return nil, fmt.Errorf("integer overflow casting to int32 type") + } + casted := int32(x) + + return &casted, nil +} + func isInInitializationFailed(status v1.IntegrationStatus) bool { if status.Phase != v1.IntegrationPhaseError { return false @@ -368,7 +382,7 @@ func getIntegrationSecretAndConfigmapResourceVersions(ctx context.Context, clien type controller interface { checkReadyCondition(ctx context.Context) (bool, error) getPodSpec() corev1.PodSpec - updateReadyCondition(readyPods int) bool + updateReadyCondition(readyPods int32) bool hasTemplateIntegrationLabel() bool getControllerName() string } @@ -509,7 +523,7 @@ func arePodsFailingStatuses(integration *v1.Integration, pendingPods []corev1.Po // probeReadiness calls the readiness probes of the non-ready Pods directly to retrieve insights from the Camel runtime. // The func return the number of readyPods, the success of the probe and any error may have happened during its execution. -func (action *monitorAction) probeReadiness(ctx context.Context, environment *trait.Environment, integration *v1.Integration, pods []corev1.Pod) (int, bool, error) { +func (action *monitorAction) probeReadiness(ctx context.Context, environment *trait.Environment, integration *v1.Integration, pods []corev1.Pod) (int32, bool, error) { // as a default we assume the Integration is Ready readyCondition := v1.IntegrationCondition{ Type: v1.IntegrationConditionReady, @@ -517,8 +531,8 @@ func (action *monitorAction) probeReadiness(ctx context.Context, environment *tr Pods: make([]v1.PodCondition, len(pods)), } - readyPods := 0 - unreadyPods := 0 + readyPods := int32(0) + unreadyPods := int32(0) runtimeReady := true runtimeFailed := false diff --git a/pkg/controller/integration/monitor_cronjob.go b/pkg/controller/integration/monitor_cronjob.go index 3f798deb76..189c060cc9 100644 --- a/pkg/controller/integration/monitor_cronjob.go +++ b/pkg/controller/integration/monitor_cronjob.go @@ -81,7 +81,7 @@ func (c *cronJobController) getPodSpec() corev1.PodSpec { return c.obj.Spec.JobTemplate.Spec.Template.Spec } -func (c *cronJobController) updateReadyCondition(readyPods int) bool { +func (c *cronJobController) updateReadyCondition(readyPods int32) bool { switch { case c.obj.Status.LastScheduleTime == nil: c.integration.SetReadyCondition(corev1.ConditionTrue, diff --git a/pkg/controller/integration/monitor_deployment.go b/pkg/controller/integration/monitor_deployment.go index 08b0c35e93..45e3038cd5 100644 --- a/pkg/controller/integration/monitor_deployment.go +++ b/pkg/controller/integration/monitor_deployment.go @@ -59,14 +59,14 @@ func (c *deploymentController) getPodSpec() corev1.PodSpec { return c.obj.Spec.Template.Spec } -func (c *deploymentController) updateReadyCondition(readyPods int) bool { +func (c *deploymentController) updateReadyCondition(readyPods int32) bool { replicas := int32(1) if r := c.integration.Spec.Replicas; r != nil { replicas = *r } // The Deployment status reports updated and ready replicas separately, // so that the number of ready replicas also accounts for older versions. - readyReplicas := int32(readyPods) + readyReplicas := readyPods switch { case readyReplicas >= replicas: // The Integration is considered ready when the number of replicas diff --git a/pkg/controller/integration/monitor_knative.go b/pkg/controller/integration/monitor_knative.go index 1c70195987..1dfe04d251 100644 --- a/pkg/controller/integration/monitor_knative.go +++ b/pkg/controller/integration/monitor_knative.go @@ -52,7 +52,7 @@ func (c *knativeServiceController) getPodSpec() corev1.PodSpec { return c.obj.Spec.Template.Spec.PodSpec } -func (c *knativeServiceController) updateReadyCondition(readyPods int) bool { +func (c *knativeServiceController) updateReadyCondition(readyPods int32) bool { ready := kubernetes.GetKnativeServiceCondition(*c.obj, servingv1.ServiceConditionReady) if ready.IsTrue() { c.integration.SetReadyCondition(corev1.ConditionTrue, diff --git a/pkg/resources/config/crd/bases/camel.apache.org_integrationplatforms.yaml b/pkg/resources/config/crd/bases/camel.apache.org_integrationplatforms.yaml index 776f3d80f7..c53ba55e52 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_integrationplatforms.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_integrationplatforms.yaml @@ -772,6 +772,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -806,6 +807,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -1299,6 +1301,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -2923,6 +2926,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -2957,6 +2961,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -3450,6 +3455,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` diff --git a/pkg/resources/config/crd/bases/camel.apache.org_integrationprofiles.yaml b/pkg/resources/config/crd/bases/camel.apache.org_integrationprofiles.yaml index 88892e8eb8..7d2e56ba66 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_integrationprofiles.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_integrationprofiles.yaml @@ -641,6 +641,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -675,6 +676,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -1168,6 +1170,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -2671,6 +2674,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -2705,6 +2709,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -3198,6 +3203,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` diff --git a/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml b/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml index 5799ff07a7..81a0079de9 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml @@ -6661,6 +6661,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -6695,6 +6696,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -7188,6 +7190,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` @@ -8653,6 +8656,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the port @@ -8687,6 +8691,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name the @@ -9180,6 +9185,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` diff --git a/pkg/resources/config/crd/bases/camel.apache.org_kameletbindings.yaml b/pkg/resources/config/crd/bases/camel.apache.org_kameletbindings.yaml index 1eac6998f2..d841ef7d3b 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_kameletbindings.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_kameletbindings.yaml @@ -6728,6 +6728,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the @@ -6762,6 +6763,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name @@ -7256,6 +7258,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` diff --git a/pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml b/pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml index 9f3275bf8f..5fdd9fd19b 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_pipes.yaml @@ -6726,6 +6726,7 @@ spec: port: description: To configure a different port exposed by the container (default `8080`). + format: int32 type: integer portName: description: To configure a different port name for the @@ -6760,6 +6761,7 @@ spec: servicePort: description: To configure under which service port the container port is to be exposed (default `80`). + format: int32 type: integer servicePortName: description: To configure under which service port name @@ -7254,6 +7256,7 @@ spec: type: string port: description: The Jolokia endpoint port (default `8778`). + format: int32 type: integer protocol: description: The protocol to use, either `http` or `https` diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go index ae17e0872e..5291add368 100644 --- a/pkg/trait/builder.go +++ b/pkg/trait/builder.go @@ -435,8 +435,7 @@ func (t *builderTrait) builderTask(e *Environment, taskConf *v1.BuildConfigurati mavenProfiles := make([]v1.ValueSource, 0) for _, v := range t.MavenProfiles { if v != "" { - mavenProfile, err := v1.DecodeValueSource(v, "profile.xml", - "illegal profile definition, syntax: configmap|secret:resource-name[/profile path]") + mavenProfile, err := v1.DecodeValueSource(v, "profile.xml") if err != nil { return nil, fmt.Errorf("invalid maven profile: %s: %w. ", v, err) } diff --git a/pkg/trait/container.go b/pkg/trait/container.go index 927ea05602..d29b577007 100644 --- a/pkg/trait/container.go +++ b/pkg/trait/container.go @@ -211,7 +211,7 @@ func (t *containerTrait) configureService(e *Environment, container *corev1.Cont } containerPort := corev1.ContainerPort{ Name: name, - ContainerPort: int32(t.getPort()), + ContainerPort: t.getPort(), Protocol: corev1.ProtocolTCP, } if !isKnative { @@ -220,7 +220,7 @@ func (t *containerTrait) configureService(e *Environment, container *corev1.Cont if service != nil { servicePort := corev1.ServicePort{ Name: t.getServicePortName(), - Port: int32(t.getServicePort()), + Port: t.getServicePort(), Protocol: corev1.ProtocolTCP, TargetPort: intstr.FromString(name), } @@ -324,7 +324,7 @@ func (t *containerTrait) getUser(e *Environment) (*int64, error) { return runAsUser, nil } -func (t *containerTrait) getPort() int { +func (t *containerTrait) getPort() int32 { if t.Port == 0 { return defaultContainerPort } @@ -332,7 +332,7 @@ func (t *containerTrait) getPort() int { return t.Port } -func (t *containerTrait) getServicePort() int { +func (t *containerTrait) getServicePort() int32 { if t.ServicePort == 0 { return defaultServicePort } diff --git a/pkg/trait/environment.go b/pkg/trait/environment.go index da49b26f76..d50b22fd96 100644 --- a/pkg/trait/environment.go +++ b/pkg/trait/environment.go @@ -102,7 +102,7 @@ func (t *environmentTrait) Apply(e *Environment) error { k, v := property.SplitPropertyFileEntry(env) confs := v1.PlainConfigSecretRegexp.FindAllStringSubmatch(v, -1) if len(confs) > 0 { - var res, err = v1.DecodeValueSource(v, "", "Invalid configuration "+v) + var res, err = v1.DecodeValueSource(v, "") if err != nil { return err } diff --git a/pkg/trait/jolokia.go b/pkg/trait/jolokia.go index cf4dc6959e..4d58863cec 100644 --- a/pkg/trait/jolokia.go +++ b/pkg/trait/jolokia.go @@ -35,6 +35,7 @@ const ( jolokiaTraitOrder = 1800 defaultJolokiaPort = 8778 + trueString = "true" ) type jolokiaTrait struct { @@ -79,35 +80,9 @@ func (t *jolokiaTrait) Apply(e *Environment) error { return err } - t.setDefaultJolokiaOption(options, &t.Host, "host", "*") - t.setDefaultJolokiaOption(options, &t.DiscoveryEnabled, "discoveryEnabled", false) - - // Configure HTTPS by default for OpenShift - if e.DetermineProfile() == v1.TraitProfileOpenShift { - t.setDefaultJolokiaOption(options, &t.Protocol, "protocol", "https") - t.setDefaultJolokiaOption(options, &t.CaCert, "caCert", "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt") - t.setDefaultJolokiaOption(options, &t.ExtendedClientCheck, "extendedClientCheck", true) - t.setDefaultJolokiaOption(options, &t.UseSslClientAuthentication, "useSslClientAuthentication", true) - t.setDefaultJolokiaOption(options, &t.ClientPrincipal, "clientPrincipal", []string{ - // Master API proxy for OpenShift 3 - "cn=system:master-proxy", - // Default Hawtio and Fuse consoles for OpenShift 4 - "cn=hawtio-online.hawtio.svc", - "cn=fuse-console.fuse.svc", - }) - } - + t.setDefaultJolokiaOption(options, e.DetermineProfile()) // Then add explicitly set trait configuration properties - t.addToJolokiaOptions(options, "caCert", t.CaCert) - t.addToJolokiaOptions(options, "clientPrincipal", t.ClientPrincipal) - t.addToJolokiaOptions(options, "discoveryEnabled", t.DiscoveryEnabled) - t.addToJolokiaOptions(options, "extendedClientCheck", t.ExtendedClientCheck) - t.addToJolokiaOptions(options, "host", t.Host) - t.addToJolokiaOptions(options, "password", t.Password) - t.addToJolokiaOptions(options, "port", t.getPort()) - t.addToJolokiaOptions(options, "protocol", t.Protocol) - t.addToJolokiaOptions(options, "user", t.User) - t.addToJolokiaOptions(options, "useSslClientAuthentication", t.UseSslClientAuthentication) + t.addToJolokiaOptions(options) // Options must be sorted so that the environment variable value is consistent over iterations, // otherwise the value changes which results in triggering a new deployment. @@ -128,7 +103,7 @@ func (t *jolokiaTrait) Apply(e *Environment) error { containerPort := corev1.ContainerPort{ Name: "jolokia", - ContainerPort: int32(t.getPort()), + ContainerPort: t.getPort(), Protocol: corev1.ProtocolTCP, } @@ -144,7 +119,7 @@ func (t *jolokiaTrait) Apply(e *Environment) error { return nil } -func (t *jolokiaTrait) getPort() int { +func (t *jolokiaTrait) getPort() int32 { if t.Port == 0 { return defaultJolokiaPort } @@ -152,61 +127,66 @@ func (t *jolokiaTrait) getPort() int { return t.Port } -func (t *jolokiaTrait) setDefaultJolokiaOption(options map[string]string, option interface{}, key string, value interface{}) { +func (t *jolokiaTrait) setDefaultJolokiaOption(options map[string]string, profile v1.TraitProfile) { // Do not override existing option - if _, ok := options[key]; ok { - return - } - switch o := option.(type) { - case **bool: - if *o == nil { - v, _ := value.(bool) - *o = &v - } - case **int: - if *o == nil { - v, _ := value.(int) - *o = &v + if options["host"] == "" { + options["host"] = "*" + } + if options["discoveryEnabled"] == "" { + options["discoveryEnabled"] = "false" + } + //nolint:nestif + if profile == v1.TraitProfileOpenShift { + if options["protocol"] == "" { + options["protocol"] = "https" } - case **string: - if *o == nil { - v, _ := value.(string) - *o = &v + if options["caCert"] == "" { + options["caCert"] = "/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt" } - case *[]string: - if len(*o) == 0 { - *o, _ = value.([]string) + if options["extendedClientCheck"] == "" { + options["extendedClientCheck"] = trueString } - } -} - -func (t *jolokiaTrait) addToJolokiaOptions(options map[string]string, key string, value interface{}) { - switch v := value.(type) { - case *bool: - if v != nil { - options[key] = strconv.FormatBool(*v) + if options["useSslClientAuthentication"] == "" { + options["useSslClientAuthentication"] = trueString } - case *int: - if v != nil { - options[key] = strconv.Itoa(*v) + if options["clientPrincipal.1"] == "" { + options["clientPrincipal.1"] = "cn=system:master-proxy" } - case int: - options[key] = strconv.Itoa(v) - case *string: - if v != nil { - options[key] = *v + if options["clientPrincipal.2"] == "" { + options["clientPrincipal.2"] = "cn=hawtio-online.hawtio.svc" } - case string: - if v != "" { - options[key] = v + if options["clientPrincipal.3"] == "" { + options["clientPrincipal.3"] = "cn=fuse-console.fuse.svc" } - case []string: - if len(v) == 1 { - options[key] = v[0] - } else { - for i, vi := range v { - options[key+"."+strconv.Itoa(i+1)] = vi - } + } +} + +func (t *jolokiaTrait) addToJolokiaOptions(options map[string]string) { + if t.CaCert != nil { + options["caCert"] = *t.CaCert + } + if t.ClientPrincipal != nil { + for i, v := range t.ClientPrincipal { + options[fmt.Sprintf("clientPrincipal.%v", i)] = v } } + if t.ExtendedClientCheck != nil { + options["extendedClientCheck"] = strconv.FormatBool(*t.ExtendedClientCheck) + } + if t.Host != nil { + options["host"] = *t.Host + } + if t.Password != nil { + options["password"] = *t.Password + } + options["port"] = fmt.Sprintf("%v", t.getPort()) + if t.Protocol != nil { + options["protocol"] = *t.Protocol + } + if t.User != nil { + options["user"] = *t.User + } + if t.UseSslClientAuthentication != nil { + options["useSslClientAuthentication"] = strconv.FormatBool(*t.UseSslClientAuthentication) + } } diff --git a/pkg/trait/jolokia_test.go b/pkg/trait/jolokia_test.go index cf9b06fdf0..123834b5a9 100644 --- a/pkg/trait/jolokia_test.go +++ b/pkg/trait/jolokia_test.go @@ -28,7 +28,6 @@ import ( "k8s.io/utils/ptr" v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" - "github.com/apache/camel-k/v2/pkg/util/boolean" "github.com/apache/camel-k/v2/pkg/util/kubernetes" ) @@ -82,13 +81,14 @@ func TestApplyJolokiaTraitForOpenShiftProfileShouldSucceed(t *testing.T) { container := environment.Resources.GetContainerByName(defaultContainerName) assert.NotNil(t, container) - assert.Equal(t, container.Args, []string{ + assert.Equal(t, []string{ "-javaagent:dependencies/lib/main/org.jolokia.jolokia-agent-jvm-1.7.1.jar=caCert=/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt," + "clientPrincipal.1=cn=system:master-proxy,clientPrincipal.2=cn=hawtio-online.hawtio.svc," + "clientPrincipal.3=cn=fuse-console.fuse.svc,discoveryEnabled=false,extendedClientCheck=true," + "host=*,port=8778,protocol=https,useSslClientAuthentication=true", - "-cp", "dependencies/lib/main/org.jolokia.jolokia-agent-jvm-1.7.1.jar", - }) + "-cp", "dependencies/lib/main/org.jolokia.jolokia-agent-jvm-1.7.1.jar"}, + container.Args, + ) assert.Len(t, container.Ports, 1) containerPort := container.Ports[0] @@ -150,129 +150,6 @@ func TestApplyJolokiaTraitWithUnparseableOptionShouldReturnError(t *testing.T) { require.Error(t, err) } -func TestSetDefaultJolokiaOptionShouldNotOverrideOptionsMap(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{"key": "value"} - optionValue := "" - - trait.setDefaultJolokiaOption(options, &optionValue, "key", "new-value") - - assert.Equal(t, "", optionValue) -} - -func TestSetDefaultStringJolokiaOptionShouldSucceed(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - var option *string - - trait.setDefaultJolokiaOption(options, &option, "key", "new-value") - - assert.Equal(t, "new-value", *option) -} - -func TestSetDefaultStringJolokiaOptionShouldNotOverrideExistingValue(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - optionValue := "existing-value" - option := &optionValue - - trait.setDefaultJolokiaOption(options, &option, "key", "new-value") - - assert.Equal(t, "existing-value", *option) -} - -func TestSetDefaultIntJolokiaOptionShouldSucceed(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - var option *int - - trait.setDefaultJolokiaOption(options, &option, "key", 2) - - assert.Equal(t, 2, *option) -} - -func TestSetDefaultIntJolokiaOptionShouldNotOverrideExistingValue(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - optionValue := 1 - option := &optionValue - - trait.setDefaultJolokiaOption(options, &option, "key", 2) - - assert.Equal(t, 1, *option) -} - -func TestSetDefaultBoolJolokiaOptionShouldSucceed(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - var option *bool - - trait.setDefaultJolokiaOption(options, &option, "key", true) - - assert.True(t, *option) -} - -func TestSetDefaultBoolJolokiaOptionShouldNotOverrideExistingValue(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - option := ptr.To(false) - - trait.setDefaultJolokiaOption(options, &option, "key", true) - - assert.False(t, *option) -} - -func TestAddStringOptionToJolokiaOptions(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - optionValue := "value" - - trait.addToJolokiaOptions(options, "key", &optionValue) - - assert.Len(t, options, 1) - assert.Equal(t, "value", options["key"]) -} - -func TestAddIntOptionToJolokiaOptions(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - - trait.addToJolokiaOptions(options, "key", 1) - - assert.Len(t, options, 1) - assert.Equal(t, "1", options["key"]) -} - -func TestAddIntPointerOptionToJolokiaOptions(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - optionValue := 1 - - trait.addToJolokiaOptions(options, "key", &optionValue) - - assert.Len(t, options, 1) - assert.Equal(t, "1", options["key"]) -} - -func TestAddBoolPointerOptionToJolokiaOptions(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - - trait.addToJolokiaOptions(options, "key", ptr.To(false)) - - assert.Len(t, options, 1) - assert.Equal(t, boolean.FalseString, options["key"]) -} - -func TestAddWrongTypeOptionToJolokiaOptionsDoesNothing(t *testing.T) { - trait, _ := newJolokiaTrait().(*jolokiaTrait) - options := map[string]string{} - - trait.addToJolokiaOptions(options, "key", new(rune)) - - assert.Len(t, options, 0) -} - func createNominalJolokiaTest() (*jolokiaTrait, *Environment) { trait, _ := newJolokiaTrait().(*jolokiaTrait) trait.Enabled = ptr.To(true) diff --git a/pkg/trait/knative_service.go b/pkg/trait/knative_service.go index dd7c8236f9..f82f44896d 100644 --- a/pkg/trait/knative_service.go +++ b/pkg/trait/knative_service.go @@ -262,11 +262,11 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) (*serving.Service, e isUpdateRequired := false minScale, ok := svc.Spec.Template.Annotations[knativeServingMinScaleAnnotation] if ok { - min, err := strconv.Atoi(minScale) + mnScale, err := strconv.Atoi(minScale) if err != nil { return nil, err } - if replicas == nil || min != int(*replicas) { + if replicas == nil || mnScale != int(*replicas) { isUpdateRequired = true } } else if replicas != nil { @@ -275,11 +275,11 @@ func (t *knativeServiceTrait) getServiceFor(e *Environment) (*serving.Service, e maxScale, ok := svc.Spec.Template.Annotations[knativeServingMaxScaleAnnotation] if ok { - max, err := strconv.Atoi(maxScale) + mxScale, err := strconv.Atoi(maxScale) if err != nil { return nil, err } - if replicas == nil || max != int(*replicas) { + if replicas == nil || mxScale != int(*replicas) { isUpdateRequired = true } } else if replicas != nil { diff --git a/pkg/trait/pdb.go b/pkg/trait/pdb.go index d4a88a0b08..b836eba622 100644 --- a/pkg/trait/pdb.go +++ b/pkg/trait/pdb.go @@ -98,11 +98,11 @@ func (t *pdbTrait) podDisruptionBudgetFor(integration *v1.Integration) *policyv1 } if t.MaxUnavailable != "" { - max := intstr.Parse(t.MaxUnavailable) - pdb.Spec.MaxUnavailable = &max + mx := intstr.Parse(t.MaxUnavailable) + pdb.Spec.MaxUnavailable = &mx } else { - min := intstr.Parse(t.MinAvailable) - pdb.Spec.MinAvailable = &min + mn := intstr.Parse(t.MinAvailable) + pdb.Spec.MinAvailable = &mn } return pdb diff --git a/pkg/trait/resolver.go b/pkg/trait/resolver.go index 05762e0466..a1fd186e9c 100644 --- a/pkg/trait/resolver.go +++ b/pkg/trait/resolver.go @@ -31,7 +31,7 @@ import ( // resolveSources --. func resolveSources(elements []v1.SourceSpec, mapLookup func(string) (*corev1.ConfigMap, error)) ([]v1.SourceSpec, error) { - for i := range len(elements) { + for i := range elements { r := &elements[i] if err := resolve(&r.DataSpec, mapLookup); err != nil { diff --git a/pkg/trait/trait_catalog.go b/pkg/trait/trait_catalog.go index a76339bab3..7537477caf 100644 --- a/pkg/trait/trait_catalog.go +++ b/pkg/trait/trait_catalog.go @@ -180,7 +180,7 @@ func (c *Catalog) executedTraitCondition(executedTrait []Trait) (*TraitCondition } message := fmt.Sprintf("Applied traits: %s", strings.Join(traitIds, ",")) - c.L.Debugf(message) + c.L.Debug(message) return NewIntegrationCondition("", v1.IntegrationConditionTraitInfo, corev1.ConditionTrue, TraitConfigurationReason, message), traits, nil } diff --git a/pkg/trait/trait_configure_test.go b/pkg/trait/trait_configure_test.go index 1c6654e8a9..14943906da 100644 --- a/pkg/trait/trait_configure_test.go +++ b/pkg/trait/trait_configure_test.go @@ -218,7 +218,7 @@ func TestTraitDecode(t *testing.T) { trait := traitToMap(t, traitv1.ContainerTrait{ PlatformBaseTrait: traitv1.PlatformBaseTrait{}, Name: "test-container", - Port: 7071, + Port: int32(7071), Auto: ptr.To(false), }) @@ -228,6 +228,6 @@ func TestTraitDecode(t *testing.T) { require.NoError(t, err) assert.Equal(t, "test-container", target.Name) - assert.Equal(t, 7071, target.Port) + assert.Equal(t, int32(7071), target.Port) assert.False(t, ptr.Deref(target.Auto, true)) } diff --git a/pkg/trait/trait_types.go b/pkg/trait/trait_types.go index ed681f7167..9fbedfe726 100644 --- a/pkg/trait/trait_types.go +++ b/pkg/trait/trait_types.go @@ -558,7 +558,7 @@ func (e *Environment) getIntegrationContainerPort() *corev1.ContainerPort { // createContainerPort creates a new container port with values taken from Container trait or default. func (e *Environment) createContainerPort() *corev1.ContainerPort { var name string - var port int + var port int32 if t := e.Catalog.GetTrait(containerTraitID); t != nil { if ct, ok := t.(*containerTrait); ok { @@ -573,7 +573,7 @@ func (e *Environment) createContainerPort() *corev1.ContainerPort { return &corev1.ContainerPort{ Name: name, - ContainerPort: int32(port), + ContainerPort: port, Protocol: corev1.ProtocolTCP, } } diff --git a/pkg/util/bindings/catalog.go b/pkg/util/bindings/catalog.go index 18bcb24837..b164d34021 100644 --- a/pkg/util/bindings/catalog.go +++ b/pkg/util/bindings/catalog.go @@ -79,7 +79,7 @@ func Translate(ctx BindingContext, endpointCtx EndpointContext, endpoint v1.Endp } else if ptr.Deref(endpoint.URI, "") != "" { errorMessage = fmt.Sprintf("could not find any suitable binding provider for %s", *endpoint.URI) } - return nil, fmt.Errorf(errorMessage) + return nil, errors.New(errorMessage) } func validateEndpoint(ctx BindingContext, e v1.Endpoint) error { diff --git a/pkg/util/camel/camel_dependencies.go b/pkg/util/camel/camel_dependencies.go index e1501fd186..74d23d6d22 100644 --- a/pkg/util/camel/camel_dependencies.go +++ b/pkg/util/camel/camel_dependencies.go @@ -345,7 +345,7 @@ func postProcessDependencies(project *maven.Project, catalog *RuntimeCatalog) { // SanitizeIntegrationDependencies --. func SanitizeIntegrationDependencies(dependencies []maven.Dependency) error { - for i := range len(dependencies) { + for i := range dependencies { dep := dependencies[i] // It may be externalized into runtime provider specific steps diff --git a/pkg/util/envvar/envvar.go b/pkg/util/envvar/envvar.go index f6b668e851..5552de8c68 100644 --- a/pkg/util/envvar/envvar.go +++ b/pkg/util/envvar/envvar.go @@ -24,7 +24,7 @@ import ( // Get --. func Get(vars []corev1.EnvVar, name string) *corev1.EnvVar { - for i := range len(vars) { + for i := range vars { if vars[i].Name == name { return &vars[i] } diff --git a/pkg/util/kubernetes/portforward.go b/pkg/util/kubernetes/portforward.go index 280556ab23..c1568d5da2 100644 --- a/pkg/util/kubernetes/portforward.go +++ b/pkg/util/kubernetes/portforward.go @@ -38,13 +38,12 @@ import ( func PortForward(ctx context.Context, c client.Client, ns, labelSelector string, localPort, remotePort uint, stdOut, stdErr io.Writer) error { log.InitForCmd() var forwardPod *corev1.Pod - var forwardCtx context.Context - var forwardCtxCancel context.CancelFunc + forwardCtx, forwardCtxCancel := context.WithCancel(ctx) + defer forwardCtxCancel() setupPortForward := func(pod *corev1.Pod) error { if forwardPod == nil && podReady(pod) { forwardPod = pod - forwardCtx, forwardCtxCancel = context.WithCancel(ctx) log.Debugf("Setting up Port Forward for pod with name: %q\n", forwardPod.Name) if _, err := portFowardPod(forwardCtx, c.GetConfig(), ns, forwardPod.Name, localPort, remotePort, stdOut, stdErr); err != nil { return err @@ -107,10 +106,7 @@ func PortForward(ctx context.Context, c client.Client, ns, labelSelector string, } log.Debugf("Handling watch.Deleted event for pod with name: %v while Port Forward was active for pod with name: %v\n", deletedPod.Name, forwardPod.Name) if deletedPod.Name == forwardPod.Name { - forwardCtxCancel() forwardPod = nil - forwardCtx = nil - forwardCtxCancel = nil log.Debugf("Handling watch.Deleted event, since the pod with Port Forward enabled has been deleted we try to bootstrap Port Forward with LabelSelector: %v\n", labelSelector) _, err := bootstrapPortForward(ctx, c, ns, labelSelector, setupPortForward) diff --git a/pkg/util/sets/set.go b/pkg/util/sets/set.go index 707ebddab6..b6940a9d21 100644 --- a/pkg/util/sets/set.go +++ b/pkg/util/sets/set.go @@ -84,7 +84,7 @@ func (s *Set) Has(item string) bool { // IsEmpty returns true if the set has no items. func (s *Set) IsEmpty() bool { - return s.keys == nil || len(s.keys) == 0 + return s.Size() == 0 } // Size returns the number if items in the Set. diff --git a/pkg/util/util.go b/pkg/util/util.go index 4842fbceca..ff550bf628 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -25,6 +25,7 @@ import ( "fmt" "io" "io/fs" + "math" "math/rand" "os" "path" @@ -117,7 +118,7 @@ func StringSliceJoin(slices ...[]string) []string { } func StringSliceContains(slice []string, items []string) bool { - for i := range len(items) { + for i := range items { if !StringSliceExists(slice, items[i]) { return false } @@ -127,7 +128,7 @@ func StringSliceContains(slice []string, items []string) bool { } func StringSliceExists(slice []string, item string) bool { - for i := range len(slice) { + for i := range slice { if slice[i] == item { return true } @@ -137,7 +138,7 @@ func StringSliceExists(slice []string, item string) bool { } func StringContainsPrefix(slice []string, prefix string) bool { - for i := range len(slice) { + for i := range slice { if strings.HasPrefix(slice[i], prefix) { return true } @@ -147,8 +148,8 @@ func StringContainsPrefix(slice []string, prefix string) bool { } func StringSliceContainsAnyOf(slice []string, items ...string) bool { - for i := range len(slice) { - for j := range len(items) { + for i := range slice { + for j := range items { if strings.Contains(slice[i], items[j]) { return true } @@ -745,3 +746,23 @@ func NavigateConfigTree(current interface{}, nodes []string) (interface{}, error return nil, errors.New("invalid node type in configuration") } } + +// IToInt32 attempts to convert safely an int to an int32. +func IToInt32(x int) (*int32, error) { + if x < math.MinInt32 || x > math.MaxInt32 { + return nil, fmt.Errorf("integer overflow casting to int32 type") + } + casted := int32(x) + + return &casted, nil +} + +// IToInt8 attempts to convert safely an int to an int8. +func IToInt8(x int) (*int8, error) { + if x < math.MinInt8 || x > math.MaxInt8 { + return nil, fmt.Errorf("integer overflow casting to int8 type") + } + casted := int8(x) + + return &casted, nil +} diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index ce998237d7..6d9c418014 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -19,6 +19,7 @@ package util import ( "fmt" + "math" "os" "path/filepath" "testing" @@ -61,3 +62,33 @@ func TestCopyDir(t *testing.T) { err = CopyDir(srcDir, destDir) require.NoError(t, err) } + +func TestIToInt32(t *testing.T) { + x := 6000 + converted, err := IToInt32(x) + require.NoError(t, err) + assert.Equal(t, int32(6000), *converted) + x = math.MaxInt32 + 1 + converted, err = IToInt32(x) + require.Error(t, err) + assert.Equal(t, "integer overflow casting to int32 type", err.Error()) + x = math.MinInt32 - 1 + converted, err = IToInt32(x) + require.Error(t, err) + assert.Equal(t, "integer overflow casting to int32 type", err.Error()) +} + +func TestIToInt8(t *testing.T) { + x := 2 + converted, err := IToInt8(x) + require.NoError(t, err) + assert.Equal(t, int8(2), *converted) + x = math.MaxInt8 + 1 + converted, err = IToInt8(x) + require.Error(t, err) + assert.Equal(t, "integer overflow casting to int8 type", err.Error()) + x = math.MinInt8 - 1 + converted, err = IToInt8(x) + require.Error(t, err) + assert.Equal(t, "integer overflow casting to int8 type", err.Error()) +} diff --git a/script/Makefile b/script/Makefile index 5257bb604f..e2d42a74ff 100644 --- a/script/Makefile +++ b/script/Makefile @@ -33,7 +33,7 @@ CODEGEN_VERSION := v0.29.7 OPERATOR_SDK_VERSION := v1.30.0 KUSTOMIZE_VERSION := v4.5.4 OPM_VERSION := v1.24.0 -LINTER_VERSION ?= v1.59.1 +LINTER_VERSION ?= v1.61.0 GOVULNCHECK_VERSION ?= latest LINTER ?= $(LOCALBIN)/golangci-lint @@ -699,21 +699,15 @@ $(LOCALBIN): mkdir -p $(LOCALBIN) .PHONY: golangci-lint -golangci-lint: $(LINTER) -$(LINTER): $(LOCALBIN) - @test -s $(LOCALBIN)/golangci-lint || \ +golangci-lint: GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(LINTER_VERSION) .PHONY: govulncheck -govulncheck: $(GOVULNCHECK) -$(GOVULNCHECK): $(LOCALBIN) - @test -s $(GOVULNCHECK) || \ +govulncheck: GOBIN=$(LOCALBIN) go install golang.org/x/vuln/cmd/govulncheck@$(GOVULNCHECK_VERSION) .PHONY: goimport -goimport: $(GOIMPORT) -$(GOIMPORT): $(LOCALBIN) - @test -s $(LOCALBIN)/goimport || \ +goimport: GOBIN=$(LOCALBIN) go install golang.org/x/tools/cmd/goimports@latest #####