This is an automated email from the ASF dual-hosted git repository. squakez pushed a commit to branch v3 in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 817c9fbe96e05357fd3aba230f75d24214dc6c8b Author: VĂctor Jean Pierre Mandujano Gutierrez <[email protected]> AuthorDate: Wed Nov 11 10:50:11 2026 -0500 feat: I expose CamelMonitor availability on Integrations (#6902) * Fix #6898: I report associated CamelMonitor resources I add an informational condition and kubectl column to show whether an Integration has an associated CamelMonitor. I verify direct ownership or the bounded Deployment/CronJob chain by namespace, name and UID. I reuse unstructured lookup and optional watches to keep the discovery condition current without changing runtime readiness. I add focused regression tests, minimal read permissions and regenerated CRD/Helm assets. Fixes #6898 * Fix #6898: I update discovery through the monitor action I let CamelMonitor events use normal reconciliation and update the availability condition from the existing monitor action. I remove the separate status patch and fixed retry from the reconciler. I clear the condition when the association disappears and keep lookup errors from interrupting normal runtime monitoring. I move the kubectl column last and update the regression tests and documentation. Fixes #6898 * Fix #6898: I simplify monitor discovery with watches and labels * Fix #6898: I check monitor availability during reconciliation * Fix #6898: I cover CamelMonitor discovery with an e2e workflow * Fix #6898: I reconcile monitors after source changes * Fix #6898: I simplify CamelMonitor discovery --- .github/workflows/camel-monitor.yml | 89 +++++++++++ docs/modules/ROOT/pages/contributing/e2e.adoc | 1 + .../ROOT/pages/observability/dashboard.adoc | 5 + e2e/camelmonitor/camel_monitor_test.go | 41 +++++ e2e/camelmonitor/files/timer.yaml | 25 +++ go.mod | 19 +-- go.sum | 42 ++--- helm/camel-k/crds/camel-k-crds.yaml | 4 + helm/camel-k/templates/rbacs-descoped.yaml | 8 + helm/camel-k/templates/rbacs-namespaced.yaml | 8 + pkg/apis/addtoscheme_camel_monitor.go | 24 +++ pkg/apis/camel/v1/integration_types.go | 3 + pkg/controller/integration/camel_monitor.go | 46 ++++++ pkg/controller/integration/camel_monitor_test.go | 177 +++++++++++++++++++++ .../integration/integration_controller.go | 37 ++++- .../integration/integration_controller_test.go | 2 +- pkg/controller/integration/monitor.go | 12 +- pkg/controller/integration/predicate_test.go | 19 +++ .../crd/bases/camel.apache.org_integrations.yaml | 4 + .../rbac/descoped/operator-cluster-role.yaml | 8 + .../config/rbac/namespaced/operator-role.yaml | 8 + script/Makefile | 9 ++ 22 files changed, 556 insertions(+), 35 deletions(-) diff --git a/.github/workflows/camel-monitor.yml b/.github/workflows/camel-monitor.yml new file mode 100644 index 000000000..321a101ca --- /dev/null +++ b/.github/workflows/camel-monitor.yml @@ -0,0 +1,89 @@ +# --------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# --------------------------------------------------------------------------- + +name: camel-monitor + +env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CAMEL_K_TEST_TIMEOUT_SHORT: 2m + CAMEL_K_TEST_TIMEOUT_MEDIUM: 5m + CAMEL_K_TEST_TIMEOUT_LONG: 10m + +on: + pull_request: + branches: + - main + - "release-*" + paths-ignore: + - 'docs/**' + - 'java/**' + - 'proposals/**' + - '**.adoc' + - '**.md' + - 'KEYS' + - 'LICENSE' + - 'NOTICE' + push: + branches: + - main + - "release-*" + paths-ignore: + - 'docs/**' + - 'java/**' + - 'proposals/**' + - '**.adoc' + - '**.md' + - 'KEYS' + - 'LICENSE' + - 'NOTICE' + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} + cancel-in-progress: true + +jobs: + camel-monitor: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v7 + with: + persist-credentials: false + submodules: recursive + + - name: Infra setting + uses: ./.github/actions/infra-setting + + - name: Install Camel Monitor operator + shell: bash + run: | + kubectl create ns camel + kubectl apply -k github.com/camel-tooling/camel-monitor-operator/install/overlays/all-namespaces?ref=v1.0.0 --server-side + kubectl wait --for=condition=Established crd/camelmonitors.camel.apache.org --timeout=60s + kubectl wait --for=condition=available deployment/camel-monitor-operator -n camel --timeout=60s + + - name: Install Camel K operator + shell: bash + run: | + kubectl apply -k install/overlays/all-namespaces/ --server-side --force-conflicts + kubectl wait --for=condition=available deployment/camel-k-operator -n camel --timeout=60s + + - name: Run test + shell: bash + run: | + DO_TEST_PREBUILD=false GOTESTFMT="-json 2>&1 | gotestfmt" make test-camel-monitor diff --git a/docs/modules/ROOT/pages/contributing/e2e.adoc b/docs/modules/ROOT/pages/contributing/e2e.adoc index 82ce1697e..bcecad5d3 100644 --- a/docs/modules/ROOT/pages/contributing/e2e.adoc +++ b/docs/modules/ROOT/pages/contributing/e2e.adoc @@ -34,6 +34,7 @@ This is the list of the groups we are using (please, notice that they can slight * advanced (`make test-advanced`) * install (`make test-install`) * knative (`make test-knative`) + * camelmonitor (`make test-camel-monitor`) * native (`make test-quarkus-native`) * telemetry (`make test-telemetry`) diff --git a/docs/modules/ROOT/pages/observability/dashboard.adoc b/docs/modules/ROOT/pages/observability/dashboard.adoc index 374e92527..4da29436d 100644 --- a/docs/modules/ROOT/pages/observability/dashboard.adoc +++ b/docs/modules/ROOT/pages/observability/dashboard.adoc @@ -97,3 +97,8 @@ items: NOTE: Camel K won't require the availability of Camel Monitor operator to work, neither Camel Monitor operator would require Camel K. They are independent projects which can cooperate to provide a Build and Monitoring tool respectively. Learn more by reading the official https://camel-tooling.github.io/camel-dashboard/[Camel Dashboard documentation]. + +== Discover monitoring from an Integration + +The `CamelMonitorAvailable` condition and `Camel Monitor` column indicate when a +`CamelMonitor` with the Integration's name is found in its namespace. diff --git a/e2e/camelmonitor/camel_monitor_test.go b/e2e/camelmonitor/camel_monitor_test.go new file mode 100644 index 000000000..7036fa3f0 --- /dev/null +++ b/e2e/camelmonitor/camel_monitor_test.go @@ -0,0 +1,41 @@ +//go:build integration +// +build integration + +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package camelmonitor + +import ( + "context" + "testing" + + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + + . "github.com/apache/camel-k/v2/e2e/support" + v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" +) + +func TestCamelMonitorDiscovery(t *testing.T) { + WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) { + name := "monitored-integration" + g.Expect(KamelRun(t, ctx, ns, "files/timer.yaml", "--name", name).Execute()).To(Succeed()) + g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionCamelMonitorAvailable), TestTimeoutLong). + Should(Equal(corev1.ConditionTrue)) + }) +} diff --git a/e2e/camelmonitor/files/timer.yaml b/e2e/camelmonitor/files/timer.yaml new file mode 100644 index 000000000..16069f3f5 --- /dev/null +++ b/e2e/camelmonitor/files/timer.yaml @@ -0,0 +1,25 @@ +# --------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# --------------------------------------------------------------------------- + +- from: + uri: "timer:monitored" + parameters: + period: "5000" + steps: + - setBody: + constant: "Monitoring discovery test" + - to: "log:info" diff --git a/go.mod b/go.mod index 45dd9db5f..c266e187c 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.27.3 require ( dario.cat/mergo v1.0.2 github.com/Masterminds/semver v1.5.0 + github.com/camel-tooling/camel-monitor-operator v1.0.0 github.com/evanphx/json-patch v5.9.11+incompatible github.com/gertd/go-pluralize v0.2.1 github.com/go-git/go-git/v5 v5.19.3 @@ -68,16 +69,16 @@ require ( github.com/go-openapi/jsonreference v1.0.0 // indirect github.com/go-openapi/swag v0.27.1 // indirect github.com/go-openapi/swag/cmdutils v0.27.1 // indirect - github.com/go-openapi/swag/conv v0.27.1 // indirect - github.com/go-openapi/swag/fileutils v0.27.1 // indirect - github.com/go-openapi/swag/jsonutils v0.27.1 // indirect - github.com/go-openapi/swag/loading v0.27.1 // indirect - github.com/go-openapi/swag/mangling v0.27.1 // indirect + github.com/go-openapi/swag/conv v0.27.3 // indirect + github.com/go-openapi/swag/fileutils v0.27.3 // indirect + github.com/go-openapi/swag/jsonutils v0.27.3 // indirect + github.com/go-openapi/swag/loading v0.27.3 // indirect + github.com/go-openapi/swag/mangling v0.27.3 // indirect github.com/go-openapi/swag/netutils v0.27.1 // indirect - github.com/go-openapi/swag/pools v0.27.1 // indirect - github.com/go-openapi/swag/stringutils v0.27.1 // indirect - github.com/go-openapi/swag/typeutils v0.27.1 // indirect - github.com/go-openapi/swag/yamlutils v0.27.1 // indirect + github.com/go-openapi/swag/pools v0.27.3 // indirect + github.com/go-openapi/swag/stringutils v0.27.3 // indirect + github.com/go-openapi/swag/typeutils v0.27.3 // indirect + github.com/go-openapi/swag/yamlutils v0.27.3 // indirect github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect github.com/google/gnostic-models v0.7.1 // indirect github.com/google/go-cmp v0.7.0 // indirect diff --git a/go.sum b/go.sum index 549773a8e..8c01055aa 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= +github.com/camel-tooling/camel-monitor-operator v1.0.0 h1:svk+zojrOvqu9/NbD0jhweMw7V8jL0m04MzcDYqlgho= +github.com/camel-tooling/camel-monitor-operator v1.0.0/go.mod h1:srXAc8o8ka+XFYMKjLqDSAz7/8do95/waFbAQ/Tsyjg= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8= @@ -73,28 +75,28 @@ github.com/go-openapi/swag v0.27.1 h1:VotvOLWW8q/EAxB0YdsBBGC8XYyeL1YwBj2ungAGPN github.com/go-openapi/swag v0.27.1/go.mod h1:GTkJPwHfhJp6MWr4/rCh64HVI3Ofu+tcsbfjfHmTxpE= github.com/go-openapi/swag/cmdutils v0.27.1 h1:I7sYqaWVl5mq0NEmNQkAmFDyNin9ufvMX/p2zwtQaOE= github.com/go-openapi/swag/cmdutils v0.27.1/go.mod h1:Sm1MVFMkF6guJJ+pQqHnQA3N0j9qALV3NxzDSv6bETM= -github.com/go-openapi/swag/conv v0.27.1 h1:8wi9ZG+olmY1wXphl93EWniPtbSPkXM/feH7FgjsvrU= -github.com/go-openapi/swag/conv v0.27.1/go.mod h1:QbqMivkpKhC3g1B1GGGOJ6ANewI3S62dbzYu3Duowqs= -github.com/go-openapi/swag/fileutils v0.27.1 h1:QQqBSoi5mW4XpU85nS0mLcA+zAE6vLzrb0QkmLKf9oM= -github.com/go-openapi/swag/fileutils v0.27.1/go.mod h1:VvJFZLTZS0AI854gEQz5tk7dBESdLjiNUMSZ/th2ry8= -github.com/go-openapi/swag/jsonutils v0.27.1 h1:SVgK3i4USzCU5mibOOS/l4ea2h9UQXy7J7RNLTjuXjU= -github.com/go-openapi/swag/jsonutils v0.27.1/go.mod h1:tdlEpZqdcQ17uj6J4YdK9vd8It5qWMwjWXOs0tjpRlk= -github.com/go-openapi/swag/jsonutils/fixtures_test v0.27.1 h1:mJu3COL9WEaZVp/Kf2PRMi7tPszPEJfSr/OO75ynCs8= -github.com/go-openapi/swag/jsonutils/fixtures_test v0.27.1/go.mod h1:mofwUWx70wvskwESqRJ//k/9kURmCgyJl5m5Ppoh5kY= -github.com/go-openapi/swag/loading v0.27.1 h1:/DxUgDXKbBX4bcn7r9uEXfJyzN5XpiJmZplzQTjrRCY= -github.com/go-openapi/swag/loading v0.27.1/go.mod h1:jvGh3iA2+zyUUycB5fgJWzeHnhrpvGnJJM0RVE9ZShE= -github.com/go-openapi/swag/mangling v0.27.1 h1:yC9D0HyUE8gbP+BfmGx9+AA89ikwZTMjESK3OnnoaqA= -github.com/go-openapi/swag/mangling v0.27.1/go.mod h1:jtBE2+V+3pILxOR7Vgce+Cwp6A2PgZbvVqfNntbVs0w= +github.com/go-openapi/swag/conv v0.27.3 h1:iqJFmGEjmX3AY0lSszABFqRVqOSt99XS0LzNIMJYuhU= +github.com/go-openapi/swag/conv v0.27.3/go.mod h1:nPRmN6jgNme99hpf+nM0auDZGALWIqlwhisKPK/bQhQ= +github.com/go-openapi/swag/fileutils v0.27.3 h1:3UVoZ2RLaIs1lt+2jcKzL8RM3Yk0rmsDE9FLA/HGxFE= +github.com/go-openapi/swag/fileutils v0.27.3/go.mod h1:VvJFZLTZS0AI854gEQz5tk7dBESdLjiNUMSZ/th2ry8= +github.com/go-openapi/swag/jsonutils v0.27.3 h1:1DEz+O82frtSMBcos/7XIn1GnpNTbsD4Bru4Dc/uhRc= +github.com/go-openapi/swag/jsonutils v0.27.3/go.mod h1:qiDCoQvzkMxrV3G8FLEdIU5L+EFYc0zcDOHWT3Yofvo= +github.com/go-openapi/swag/jsonutils/fixtures_test v0.27.3 h1:h/eT9kmGCDdFLJF29lOhzLtF0FmP1AX2MhLJWVebsb8= +github.com/go-openapi/swag/jsonutils/fixtures_test v0.27.3/go.mod h1:mofwUWx70wvskwESqRJ//k/9kURmCgyJl5m5Ppoh5kY= +github.com/go-openapi/swag/loading v0.27.3 h1:L9nQkEgzU7QgFQL+pLEMfGUKxeM4pWwGwbET9Z3weW0= +github.com/go-openapi/swag/loading v0.27.3/go.mod h1:rJ0NeaKsF4CVPnMGjPQl7JlSHzvD0bc2DKXLss1hiuE= +github.com/go-openapi/swag/mangling v0.27.3 h1:gRzzD1PAUoLTtGMgI3KpBmCSOlTuLTFWnviLxLcTnyg= +github.com/go-openapi/swag/mangling v0.27.3/go.mod h1:jtBE2+V+3pILxOR7Vgce+Cwp6A2PgZbvVqfNntbVs0w= github.com/go-openapi/swag/netutils v0.27.1 h1:mICMFoS82F5TZ4Zy3cqmcQk+BFeCp3Uyq3Np7GI0/qU= github.com/go-openapi/swag/netutils v0.27.1/go.mod h1:J+WYyFMLtvtCGqa6jLv+YNUmIKI3ZRQRrvfNDMoQoEQ= -github.com/go-openapi/swag/pools v0.27.1 h1:9LeadcMyb2GJCbXX5hVQDbZ2Lq9TL4dCs/nx1j5DO0E= -github.com/go-openapi/swag/pools v0.27.1/go.mod h1:kVQefhSK5RWuRe7BXsL8htgBPAMpN7HDGpGEknqugeE= -github.com/go-openapi/swag/stringutils v0.27.1 h1:ZXePZ0r2p1qSjo8tD3Un4vFj8+FqlCkczxDrJIhYUp8= -github.com/go-openapi/swag/stringutils v0.27.1/go.mod h1:lzRN95CxXmA03XcDWHLOb6nOMcxCqR5rGY0lOgsfRoM= -github.com/go-openapi/swag/typeutils v0.27.1 h1:KSTdFlfnse4r6dP9IrEnwMldjE+zs71UeEB3//PtVXc= -github.com/go-openapi/swag/typeutils v0.27.1/go.mod h1:Srm0xFNRZ1Y+vCxJclo5qzx8aj+1pAKda/YfFPrG0dQ= -github.com/go-openapi/swag/yamlutils v0.27.1 h1:ftxv6xvXb1E3zohUc+okZ9nSqNb9StQX/FXnKZ98sQA= -github.com/go-openapi/swag/yamlutils v0.27.1/go.mod h1:bnxFIB1qewGRiZHypXGZ3fNgf13/0HfRgnS/iZBDrOo= +github.com/go-openapi/swag/pools v0.27.3 h1:gXjImP3F6/56wRRcFgEPld084Y6u2gs21ikPBt8NKBk= +github.com/go-openapi/swag/pools v0.27.3/go.mod h1:kVQefhSK5RWuRe7BXsL8htgBPAMpN7HDGpGEknqugeE= +github.com/go-openapi/swag/stringutils v0.27.3 h1:Ru28hnbAvN5wycALQYy8IobHvASq+FUFMlp1QzLM0JI= +github.com/go-openapi/swag/stringutils v0.27.3/go.mod h1:lzRN95CxXmA03XcDWHLOb6nOMcxCqR5rGY0lOgsfRoM= +github.com/go-openapi/swag/typeutils v0.27.3 h1:l6SSrx5eR5/WVwrGNzN6bQ9WqL04mrxNBl9YgQ3rcJ4= +github.com/go-openapi/swag/typeutils v0.27.3/go.mod h1:Srm0xFNRZ1Y+vCxJclo5qzx8aj+1pAKda/YfFPrG0dQ= +github.com/go-openapi/swag/yamlutils v0.27.3 h1:cRFCAoYtslYn9L9T0xWryHy1t7c1MACC+DMj3CLvwvs= +github.com/go-openapi/swag/yamlutils v0.27.3/go.mod h1:6JYBGj8sw/NawMllyZY+cTA8Mzk2etS3ZBASdcyPsiU= github.com/go-openapi/testify/enable/yaml/v2 v2.6.0 h1:gGHwAJ0R/5jU8BEGDbfRNR3hL68dAVi84WuOApp29B0= github.com/go-openapi/testify/enable/yaml/v2 v2.6.0/go.mod h1:tY+St1SGq4NFl0QIqdTY4aEdbChAHxhyB77XQi9iJCo= github.com/go-openapi/testify/v2 v2.6.0 h1:5PKH2HE7YJ/LuRPQGvSxBRlFXNQhSetBLlGAgUEu3ug= diff --git a/helm/camel-k/crds/camel-k-crds.yaml b/helm/camel-k/crds/camel-k-crds.yaml index fbb8b4955..275675bf2 100644 --- a/helm/camel-k/crds/camel-k-crds.yaml +++ b/helm/camel-k/crds/camel-k-crds.yaml @@ -13143,6 +13143,10 @@ spec: jsonPath: .status.replicas name: Replicas type: integer + - description: Whether an associated CamelMonitor resource is available + jsonPath: .status.conditions[?(@.type=="CamelMonitorAvailable")].status + name: Camel Monitor + type: string name: v1 schema: openAPIV3Schema: diff --git a/helm/camel-k/templates/rbacs-descoped.yaml b/helm/camel-k/templates/rbacs-descoped.yaml index 2a57524c2..eb11fa0d6 100644 --- a/helm/camel-k/templates/rbacs-descoped.yaml +++ b/helm/camel-k/templates/rbacs-descoped.yaml @@ -22,6 +22,14 @@ metadata: app: camel-k name: {{ include "camel-k.fullname" . }}-operator rules: +- apiGroups: + - camel.apache.org + resources: + - camelmonitors + verbs: + - get + - list + - watch - apiGroups: - camel.apache.org resources: diff --git a/helm/camel-k/templates/rbacs-namespaced.yaml b/helm/camel-k/templates/rbacs-namespaced.yaml index 2b3bbbb9c..5c53106a8 100644 --- a/helm/camel-k/templates/rbacs-namespaced.yaml +++ b/helm/camel-k/templates/rbacs-namespaced.yaml @@ -22,6 +22,14 @@ metadata: app: camel-k name: {{ include "camel-k.fullname" . }}-operator rules: +- apiGroups: + - camel.apache.org + resources: + - camelmonitors + verbs: + - get + - list + - watch - apiGroups: - camel.apache.org resources: diff --git a/pkg/apis/addtoscheme_camel_monitor.go b/pkg/apis/addtoscheme_camel_monitor.go new file mode 100644 index 000000000..bb82349e5 --- /dev/null +++ b/pkg/apis/addtoscheme_camel_monitor.go @@ -0,0 +1,24 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package apis + +import monitorv1alpha1 "github.com/camel-tooling/camel-monitor-operator/pkg/apis/camel/v1alpha1" + +func init() { + AddToSchemes = append(AddToSchemes, monitorv1alpha1.AddToScheme) +} diff --git a/pkg/apis/camel/v1/integration_types.go b/pkg/apis/camel/v1/integration_types.go index ac4c8408d..190c64c59 100644 --- a/pkg/apis/camel/v1/integration_types.go +++ b/pkg/apis/camel/v1/integration_types.go @@ -40,6 +40,7 @@ import ( // +kubebuilder:printcolumn:name="Catalog Version",type=string,JSONPath=`.status.catalog.version`,description="The catalog version" // +kubebuilder:printcolumn:name="Kit",type=string,JSONPath=`.status.integrationKit.name`,description="The integration kit" // +kubebuilder:printcolumn:name="Replicas",type=integer,JSONPath=`.status.replicas`,description="The number of pods" +// +kubebuilder:printcolumn:name="Camel Monitor",type=string,JSONPath=`.status.conditions[?(@.type=="CamelMonitorAvailable")].status`,description="Whether an associated CamelMonitor resource is available" // Integration is the Schema for the integrations API. type Integration struct { @@ -186,6 +187,8 @@ const ( // Deprecated: no longer in use. IntegrationPhaseUnknown IntegrationPhase = "Unknown" + // IntegrationConditionCamelMonitorAvailable reports discovery of an associated CamelMonitor. + IntegrationConditionCamelMonitorAvailable IntegrationConditionType = "CamelMonitorAvailable" // IntegrationConditionReady --. IntegrationConditionReady IntegrationConditionType = "Ready" // IntegrationConditionKitAvailable --. diff --git a/pkg/controller/integration/camel_monitor.go b/pkg/controller/integration/camel_monitor.go new file mode 100644 index 000000000..c2ef09629 --- /dev/null +++ b/pkg/controller/integration/camel_monitor.go @@ -0,0 +1,46 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package integration + +import ( + "context" + + monitorv1alpha1 "github.com/camel-tooling/camel-monitor-operator/pkg/apis/camel/v1alpha1" + corev1 "k8s.io/api/core/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + ctrl "sigs.k8s.io/controller-runtime/pkg/client" + + v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" +) + +func addCamelMonitorCondition(ctx context.Context, c ctrl.Reader, integration *v1.Integration) error { + monitor := &monitorv1alpha1.CamelMonitor{} + if err := c.Get(ctx, ctrl.ObjectKeyFromObject(integration), monitor); err != nil { + if k8serrors.IsNotFound(err) { + integration.Status.RemoveCondition(v1.IntegrationConditionCamelMonitorAvailable) + + return nil + } + + return err + } + integration.Status.SetCondition(v1.IntegrationConditionCamelMonitorAvailable, corev1.ConditionTrue, + "CamelMonitorAvailable", "A CamelMonitor resource is available") + + return nil +} diff --git a/pkg/controller/integration/camel_monitor_test.go b/pkg/controller/integration/camel_monitor_test.go new file mode 100644 index 000000000..fd404918f --- /dev/null +++ b/pkg/controller/integration/camel_monitor_test.go @@ -0,0 +1,177 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package integration + +import ( + "context" + "errors" + "testing" + + monitorv1alpha1 "github.com/camel-tooling/camel-monitor-operator/pkg/apis/camel/v1alpha1" + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/discovery/fake" + k8stesting "k8s.io/client-go/testing" + "sigs.k8s.io/controller-runtime/pkg/builder" + ctrl "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/interceptor" + "sigs.k8s.io/controller-runtime/pkg/manager" + + v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" + "github.com/apache/camel-k/v2/pkg/client" + "github.com/apache/camel-k/v2/pkg/internal" + "github.com/apache/camel-k/v2/pkg/util/log" +) + +func TestCamelMonitorCondition(t *testing.T) { + ctx := context.Background() + integration := v1.NewIntegration("test", "sample") + integration.Status.Phase = v1.IntegrationPhaseRunning + integration.SetReadyCondition(corev1.ConditionTrue, "Ready", "running") + ready := integration.Status.GetCondition(v1.IntegrationConditionReady).DeepCopy() + foreign := monitorv1alpha1.NewCamelMonitor("other", integration.Name) + different := monitorv1alpha1.NewCamelMonitor(integration.Namespace, "different-name") + different.Labels = map[string]string{v1.IntegrationLabel: integration.Name} + c, err := internal.NewFakeClient() + require.NoError(t, err) + require.NoError(t, c.Create(ctx, &foreign)) + require.NoError(t, c.Create(ctx, &different)) + require.NoError(t, addCamelMonitorCondition(ctx, c, &integration)) + require.Nil(t, integration.Status.GetCondition(v1.IntegrationConditionCamelMonitorAvailable)) + + monitor := monitorv1alpha1.NewCamelMonitor(integration.Namespace, integration.Name) + monitor.Status.Conditions = []metav1.Condition{{Type: "Healthy", Status: metav1.ConditionFalse}} + require.NoError(t, c.Create(ctx, &monitor)) + require.NoError(t, addCamelMonitorCondition(ctx, c, &integration)) + condition := integration.Status.GetCondition(v1.IntegrationConditionCamelMonitorAvailable).DeepCopy() + require.Equal(t, corev1.ConditionTrue, condition.Status) + require.NoError(t, addCamelMonitorCondition(ctx, c, &integration)) + require.Equal(t, condition, integration.Status.GetCondition(v1.IntegrationConditionCamelMonitorAvailable)) + require.NoError(t, c.Delete(ctx, &monitor)) + require.NoError(t, addCamelMonitorCondition(ctx, c, &integration)) + require.Nil(t, integration.Status.GetCondition(v1.IntegrationConditionCamelMonitorAvailable)) + require.Equal(t, ready, integration.Status.GetCondition(v1.IntegrationConditionReady)) + require.Equal(t, v1.IntegrationPhaseRunning, integration.Status.Phase) +} + +func TestCamelMonitorLookupFailure(t *testing.T) { + integration := v1.NewIntegration("test", "sample") + integration.Status.SetCondition(v1.IntegrationConditionCamelMonitorAvailable, corev1.ConditionTrue, "Available", "found") + previous := integration.Status.DeepCopy() + c, err := internal.NewFakeClient() + require.NoError(t, err) + lookupErr := errors.New("lookup unavailable") + c.(*internal.FakeClient).Intercept(&interceptor.Funcs{Get: func(context.Context, ctrl.WithWatch, ctrl.ObjectKey, ctrl.Object, ...ctrl.GetOption) error { + return lookupErr + }}) + require.ErrorIs(t, addCamelMonitorCondition(context.Background(), c, &integration), lookupErr) + require.Equal(t, previous, &integration.Status) +} + +func enableCamelMonitorDiscovery(t *testing.T, c client.Client) *fake.FakeDiscovery { + t.Helper() + fakeClient, ok := c.(*internal.FakeClient) + require.True(t, ok) + discovery, ok := fakeClient.Interface.Discovery().(*fake.FakeDiscovery) + require.True(t, ok) + discovery.Resources = []*metav1.APIResourceList{{GroupVersion: monitorv1alpha1.SchemeGroupVersion.String(), + APIResources: []metav1.APIResource{{Kind: monitorv1alpha1.AppKind, Name: "camelmonitors", Namespaced: true}}}} + + return discovery +} + +func TestMonitorActionCachesCamelMonitorAvailability(t *testing.T) { + for _, installed := range []bool{false, true} { + t.Run(map[bool]string{false: "absent", true: "installed"}[installed], func(t *testing.T) { + c, integration, err := nominalEnvironment() + require.NoError(t, err) + if installed { + enableCamelMonitorDiscovery(t, c) + } + monitor := monitorv1alpha1.NewCamelMonitor(integration.Namespace, integration.Name) + require.NoError(t, c.Create(context.Background(), &monitor)) + action := NewMonitorAction(installed) + action.InjectClient(c) + action.InjectLogger(log.Log) + if installed { + c.(*internal.FakeClient).Interface.Discovery().(*fake.FakeDiscovery).Resources = nil + } else { + enableCamelMonitorDiscovery(t, c) + } + discoveryRequests := 0 + c.(*internal.FakeClient).Interface.Discovery().(*fake.FakeDiscovery).PrependReactor("get", "resource", func(k8stesting.Action) (bool, runtime.Object, error) { + discoveryRequests++ + + return true, nil, errors.New("unexpected discovery during reconciliation") + }) + for range 2 { + result, err := action.Handle(context.Background(), integration) + require.NoError(t, err) + require.NotNil(t, result) + condition := result.Status.GetCondition(v1.IntegrationConditionCamelMonitorAvailable) + if installed { + require.NotNil(t, condition) + require.Equal(t, corev1.ConditionTrue, condition.Status) + } else { + require.Nil(t, condition) + } + } + require.Zero(t, discoveryRequests) + }) + } +} + +func TestCamelMonitorOptionalWatch(t *testing.T) { + c, err := internal.NewFakeClient() + require.NoError(t, err) + require.NoError(t, watchCamelMonitorResources(c, nil)) + discovery := enableCamelMonitorDiscovery(t, c) + require.NoError(t, watchCamelMonitorResources(c, &builder.Builder{})) + lookupErr := errors.New("discovery unavailable") + discovery.PrependReactor("get", "resource", func(k8stesting.Action) (bool, runtime.Object, error) { + return true, nil, lookupErr + }) + require.ErrorIs(t, watchCamelMonitorResources(c, &builder.Builder{}), lookupErr) +} + +func TestAddCamelMonitorDiscoveryFailure(t *testing.T) { + c, err := internal.NewFakeClient() + require.NoError(t, err) + discovery := enableCamelMonitorDiscovery(t, c) + lookupErr := errors.New("discovery unavailable") + discovery.PrependReactor("get", "resource", func(k8stesting.Action) (bool, runtime.Object, error) { + return true, nil, lookupErr + }) + require.ErrorIs(t, Add(context.Background(), camelMonitorTestManager{}, c), lookupErr) +} + +type camelMonitorTestManager struct { + manager.Manager +} + +func (camelMonitorTestManager) GetFieldIndexer() ctrl.FieldIndexer { + return camelMonitorTestFieldIndexer{} +} + +type camelMonitorTestFieldIndexer struct{} + +func (camelMonitorTestFieldIndexer) IndexField(context.Context, ctrl.Object, string, ctrl.IndexerFunc) error { + return nil +} diff --git a/pkg/controller/integration/integration_controller.go b/pkg/controller/integration/integration_controller.go index 9f2cf866b..dbc80ca4e 100644 --- a/pkg/controller/integration/integration_controller.go +++ b/pkg/controller/integration/integration_controller.go @@ -37,6 +37,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/predicate" "sigs.k8s.io/controller-runtime/pkg/reconcile" + monitorv1alpha1 "github.com/camel-tooling/camel-monitor-operator/pkg/apis/camel/v1alpha1" + v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" servingv1 "github.com/apache/camel-k/v2/pkg/apis/duck/knative/serving/v1" "github.com/apache/camel-k/v2/pkg/client" @@ -62,10 +64,15 @@ func Add(ctx context.Context, mgr manager.Manager, c client.Client) error { return fmt.Errorf("unable to set up field indexer for status.phase: %w", err) } - return add(mgr, c, newReconciler(mgr, c)) + camelMonitorAvailable, err := kubernetes.IsAPIResourceInstalled(c, monitorv1alpha1.SchemeGroupVersion.String(), monitorv1alpha1.AppKind) + if err != nil { + return err + } + + return add(mgr, c, newReconciler(mgr, c, camelMonitorAvailable)) } -func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler { +func newReconciler(mgr manager.Manager, c client.Client, camelMonitorAvailable bool) reconcile.Reconciler { base := []Action{ NewInitializeAction(), NewBuildAction(), @@ -78,7 +85,7 @@ func newReconciler(mgr manager.Manager, c client.Client) reconcile.Reconciler { client: c, scheme: mgr.GetScheme(), recorder: mgr.GetEventRecorder("camel-k-integration-controller"), - baseActions: append(append([]Action{}, base...), NewMonitorAction()), + baseActions: append(append([]Action{}, base...), NewMonitorAction(camelMonitorAvailable)), }, schema.GroupVersionKind{ Group: v1.SchemeGroupVersion.Group, @@ -276,6 +283,12 @@ func add(mgr manager.Manager, c client.Client, r reconcile.Reconciler) error { return err } + // Watch for the Camel Monitors conditionally + err = watchCamelMonitorResources(c, b) + if err != nil { + return err + } + return b.Complete(r) } @@ -377,6 +390,24 @@ func watchKnativeResources(c client.Client, b *builder.Builder) error { return nil } +func watchCamelMonitorResources(c client.Client, b *builder.Builder) error { + installed, err := kubernetes.IsAPIResourceInstalled(c, monitorv1alpha1.SchemeGroupVersion.String(), monitorv1alpha1.AppKind) + if err != nil { + return err + } + if !installed { + log.Info("CamelMonitor resources are not installed in the cluster. To enable monitoring discovery after installing the CRD, " + + "assign the required RBAC privileges and restart the Camel K operator.") + + return nil + } + log.Info("CamelMonitor resources installed in the cluster: make sure to assign CamelMonitor RBAC privileges") + b.Watches(&monitorv1alpha1.CamelMonitor{}, &handler.EnqueueRequestForObject{}, + builder.WithPredicates(StatusChangedPredicate{})) + + return nil +} + var _ reconcile.Reconciler = &reconcileIntegration{} // reconcileIntegration reconciles an Integration object. diff --git a/pkg/controller/integration/integration_controller_test.go b/pkg/controller/integration/integration_controller_test.go index 17149f185..d591b5896 100644 --- a/pkg/controller/integration/integration_controller_test.go +++ b/pkg/controller/integration/integration_controller_test.go @@ -37,7 +37,7 @@ func TestActions(t *testing.T) { NewBuildCompleteAction(), } reconciler := reconcileIntegration{ - baseActions: append(append([]Action{}, base...), NewMonitorAction()), + baseActions: append(append([]Action{}, base...), NewMonitorAction(false)), } actions := reconciler.baseActions diff --git a/pkg/controller/integration/monitor.go b/pkg/controller/integration/monitor.go index b81ba0ef4..b676279cd 100644 --- a/pkg/controller/integration/monitor.go +++ b/pkg/controller/integration/monitor.go @@ -43,12 +43,14 @@ import ( ) // NewMonitorAction is an action used to monitor manager Integrations. -func NewMonitorAction() Action { - return &monitorAction{} +func NewMonitorAction(camelMonitorAvailable bool) Action { + return &monitorAction{camelMonitorAvailable: camelMonitorAvailable} } type monitorAction struct { baseAction + + camelMonitorAvailable bool } func (action *monitorAction) Name() string { @@ -135,6 +137,12 @@ func (action *monitorAction) Handle(ctx context.Context, integration *v1.Integra return integration, err } + if action.camelMonitorAvailable { + if err := addCamelMonitorCondition(ctx, action.client, integration); err != nil { + return integration, err + } + } + return action.monitorPods(ctx, environment, integration) } diff --git a/pkg/controller/integration/predicate_test.go b/pkg/controller/integration/predicate_test.go index 70f59958f..b06469677 100644 --- a/pkg/controller/integration/predicate_test.go +++ b/pkg/controller/integration/predicate_test.go @@ -20,6 +20,7 @@ package integration import ( "testing" + monitorv1alpha1 "github.com/camel-tooling/camel-monitor-operator/pkg/apis/camel/v1alpha1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -74,3 +75,21 @@ func TestStatusChangedPredicateKnativeService(t *testing.T) { assert.False(t, p.Update(event.UpdateEvent{ObjectOld: nil, ObjectNew: same})) assert.False(t, p.Update(event.UpdateEvent{ObjectOld: old, ObjectNew: nil})) } + +func TestStatusChangedPredicateCamelMonitor(t *testing.T) { + p := StatusChangedPredicate{} + monitor := monitorv1alpha1.NewCamelMonitor("test", "sample") + monitor.Status.Conditions = []metav1.Condition{{Type: "Healthy", Status: metav1.ConditionFalse}} + + assert.True(t, p.Create(event.CreateEvent{Object: &monitor})) + assert.True(t, p.Delete(event.DeleteEvent{Object: &monitor})) + + metadataChanged := monitor.DeepCopy() + metadataChanged.Labels = map[string]string{"app": "sample"} + metadataChanged.Generation++ + assert.False(t, p.Update(event.UpdateEvent{ObjectOld: &monitor, ObjectNew: metadataChanged})) + + statusChanged := monitor.DeepCopy() + statusChanged.Status.Conditions[0].Status = metav1.ConditionTrue + assert.True(t, p.Update(event.UpdateEvent{ObjectOld: &monitor, ObjectNew: statusChanged})) +} 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 e37bf25d0..dcc2c093c 100644 --- a/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml +++ b/pkg/resources/config/crd/bases/camel.apache.org_integrations.yaml @@ -65,6 +65,10 @@ spec: jsonPath: .status.replicas name: Replicas type: integer + - description: Whether an associated CamelMonitor resource is available + jsonPath: .status.conditions[?(@.type=="CamelMonitorAvailable")].status + name: Camel Monitor + type: string name: v1 schema: openAPIV3Schema: diff --git a/pkg/resources/config/rbac/descoped/operator-cluster-role.yaml b/pkg/resources/config/rbac/descoped/operator-cluster-role.yaml index a21bef406..5e51a35c8 100644 --- a/pkg/resources/config/rbac/descoped/operator-cluster-role.yaml +++ b/pkg/resources/config/rbac/descoped/operator-cluster-role.yaml @@ -22,6 +22,14 @@ metadata: labels: app: "camel-k" rules: +- apiGroups: + - camel.apache.org + resources: + - camelmonitors + verbs: + - get + - list + - watch - apiGroups: - camel.apache.org resources: diff --git a/pkg/resources/config/rbac/namespaced/operator-role.yaml b/pkg/resources/config/rbac/namespaced/operator-role.yaml index 0999ba5b0..c9054a877 100644 --- a/pkg/resources/config/rbac/namespaced/operator-role.yaml +++ b/pkg/resources/config/rbac/namespaced/operator-role.yaml @@ -22,6 +22,14 @@ metadata: labels: app: "camel-k" rules: +- apiGroups: + - camel.apache.org + resources: + - camelmonitors + verbs: + - get + - list + - watch - apiGroups: - camel.apache.org resources: diff --git a/script/Makefile b/script/Makefile index 65157c853..ff444993e 100644 --- a/script/Makefile +++ b/script/Makefile @@ -338,6 +338,15 @@ test-knative: gotestfmt-install go test -timeout 60m -v ./e2e/knative -tags=integration $(GOTESTFMT) || ((FAILED++)); \ exit $${FAILED} +# +# CamelMonitor discovery tests require Camel Monitor and Camel K operators. +# +.PHONY: test-camel-monitor +test-camel-monitor: gotestfmt-install + set -o pipefail; FAILED=0; \ + go test -timeout 20m -v ./e2e/camelmonitor -tags=integration $(GOTESTFMT) || ((FAILED++)); \ + exit $${FAILED} + # # Kafka tests that require the presence of Strimzi operator configuration #
