-
Notifications
You must be signed in to change notification settings - Fork 47
feat: auto-label namespaces with Istio ambient mesh on AgentRuntime reconcile #403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pdettori
merged 4 commits into
rossoctl:main
from
r3v5:auto-label-namespaces-with-istio-for-agent-runtime
Jun 8, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b31f180
feat: auto-label namespaces with Istio ambient mesh labels
r3v5 caae23a
test: add unit tests for Istio mesh enrollment
r3v5 6e064af
test: add e2e tests for Istio mesh enrollment
r3v5 8cb9a4c
docs: add IstioMeshEnrolled condition and namespace RBAC
r3v5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
kagenti-operator/internal/controller/agentruntime_istio.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| Copyright 2026. | ||
|
|
||
| Licensed 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 controller | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
| ) | ||
|
|
||
| const ( | ||
| ConditionTypeIstioMeshEnrolled = "IstioMeshEnrolled" | ||
|
|
||
| AnnotationIstioMeshOptOut = "kagenti.io/istio-mesh" | ||
|
|
||
| LabelIstioDiscovery = "istio-discovery" | ||
| LabelIstioDataplaneMode = "istio.io/dataplane-mode" | ||
| ) | ||
|
|
||
| // +kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch;patch | ||
|
|
||
| // ensureIstioMeshLabels patches the namespace with Istio ambient mesh labels | ||
| // unless the namespace has opted out via the kagenti.io/istio-mesh annotation. | ||
| // Returns true if labels were applied (or already present), false if opted out. | ||
| func (r *AgentRuntimeReconciler) ensureIstioMeshLabels(ctx context.Context, namespace string) (bool, error) { | ||
| logger := log.FromContext(ctx) | ||
|
|
||
| ns := &corev1.Namespace{} | ||
| if err := r.Get(ctx, client.ObjectKey{Name: namespace}, ns); err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| if ns.Annotations[AnnotationIstioMeshOptOut] == "disabled" { | ||
| logger.V(1).Info("Namespace opted out of Istio mesh enrollment", "namespace", namespace) | ||
| return false, nil | ||
| } | ||
|
|
||
| if ns.Labels[LabelIstioDiscovery] == "enabled" && ns.Labels[LabelIstioDataplaneMode] == "ambient" { | ||
| return true, nil | ||
| } | ||
|
|
||
| patch := client.MergeFrom(ns.DeepCopy()) | ||
| if ns.Labels == nil { | ||
| ns.Labels = make(map[string]string) | ||
| } | ||
| ns.Labels[LabelIstioDiscovery] = "enabled" | ||
| ns.Labels[LabelIstioDataplaneMode] = "ambient" | ||
|
|
||
| if err := r.Patch(ctx, ns, patch); err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| logger.V(1).Info("Labeled namespace for Istio ambient mesh", "namespace", namespace) | ||
| return true, nil | ||
| } |
131 changes: 131 additions & 0 deletions
131
kagenti-operator/internal/controller/agentruntime_istio_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| Copyright 2026. | ||
|
|
||
| Licensed 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 controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| var _ = Describe("ensureIstioMeshLabels", func() { | ||
| var ( | ||
| reconciler *AgentRuntimeReconciler | ||
| nsCounter int | ||
| ) | ||
|
|
||
| newTestNamespace := func(labels map[string]string, annotations map[string]string) *corev1.Namespace { | ||
| nsCounter++ | ||
| ns := &corev1.Namespace{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: fmt.Sprintf("istio-test-%d", nsCounter), | ||
| Labels: labels, | ||
| Annotations: annotations, | ||
| }, | ||
| } | ||
| Expect(k8sClient.Create(context.Background(), ns)).To(Succeed()) | ||
| return ns | ||
| } | ||
|
|
||
| BeforeEach(func() { | ||
| reconciler = &AgentRuntimeReconciler{ | ||
| Client: k8sClient, | ||
| } | ||
| }) | ||
|
|
||
| It("should add Istio labels to a bare namespace", func() { | ||
| ns := newTestNamespace(nil, nil) | ||
|
|
||
| labeled, err := reconciler.ensureIstioMeshLabels(ctx, ns.Name) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(labeled).To(BeTrue()) | ||
|
|
||
| updated := &corev1.Namespace{} | ||
| Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(ns), updated)).To(Succeed()) | ||
| Expect(updated.Labels[LabelIstioDiscovery]).To(Equal("enabled")) | ||
| Expect(updated.Labels[LabelIstioDataplaneMode]).To(Equal("ambient")) | ||
| }) | ||
|
|
||
| It("should be idempotent on already-labeled namespace", func() { | ||
| ns := newTestNamespace(map[string]string{ | ||
| LabelIstioDiscovery: "enabled", | ||
| LabelIstioDataplaneMode: "ambient", | ||
| }, nil) | ||
|
|
||
| labeled, err := reconciler.ensureIstioMeshLabels(ctx, ns.Name) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(labeled).To(BeTrue()) | ||
|
|
||
| updated := &corev1.Namespace{} | ||
| Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(ns), updated)).To(Succeed()) | ||
| Expect(updated.Labels[LabelIstioDiscovery]).To(Equal("enabled")) | ||
| Expect(updated.Labels[LabelIstioDataplaneMode]).To(Equal("ambient")) | ||
| }) | ||
|
|
||
| It("should skip namespace with opt-out annotation", func() { | ||
| ns := newTestNamespace(nil, map[string]string{ | ||
| AnnotationIstioMeshOptOut: "disabled", | ||
| }) | ||
|
|
||
| labeled, err := reconciler.ensureIstioMeshLabels(ctx, ns.Name) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(labeled).To(BeFalse()) | ||
|
|
||
| updated := &corev1.Namespace{} | ||
| Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(ns), updated)).To(Succeed()) | ||
| Expect(updated.Labels).NotTo(HaveKey(LabelIstioDiscovery)) | ||
| Expect(updated.Labels).NotTo(HaveKey(LabelIstioDataplaneMode)) | ||
| }) | ||
|
|
||
| It("should preserve existing labels", func() { | ||
| ns := newTestNamespace(map[string]string{ | ||
| "team": "platform", | ||
| "env": "test", | ||
| }, nil) | ||
|
|
||
| labeled, err := reconciler.ensureIstioMeshLabels(ctx, ns.Name) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(labeled).To(BeTrue()) | ||
|
|
||
| updated := &corev1.Namespace{} | ||
| Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(ns), updated)).To(Succeed()) | ||
| Expect(updated.Labels["team"]).To(Equal("platform")) | ||
| Expect(updated.Labels["env"]).To(Equal("test")) | ||
| Expect(updated.Labels[LabelIstioDiscovery]).To(Equal("enabled")) | ||
| Expect(updated.Labels[LabelIstioDataplaneMode]).To(Equal("ambient")) | ||
| }) | ||
|
|
||
| It("should label namespace when annotation value is not 'disabled'", func() { | ||
| ns := newTestNamespace(nil, map[string]string{ | ||
| AnnotationIstioMeshOptOut: "enabled", | ||
| }) | ||
|
|
||
| labeled, err := reconciler.ensureIstioMeshLabels(ctx, ns.Name) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(labeled).To(BeTrue()) | ||
|
|
||
| updated := &corev1.Namespace{} | ||
| Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(ns), updated)).To(Succeed()) | ||
| Expect(updated.Labels[LabelIstioDiscovery]).To(Equal("enabled")) | ||
| Expect(updated.Labels[LabelIstioDataplaneMode]).To(Equal("ambient")) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion:
SkillsMountedcondition docs (lines 483-485) appear unrelated to the Istio mesh enrollment feature — this condition isn't implemented anywhere in this diff. Consider moving to a separate commit or the PR that introduces the SkillsMounted logic, to keep this PR focused on its stated scope.