Skip to content

Commit

Permalink
Fixed agent operator to publish to the correct namespace (#728)
Browse files Browse the repository at this point in the history
Issue #726
  • Loading branch information
dbrewster committed Sep 9, 2024
1 parent 506513c commit 77a0878
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
12 changes: 6 additions & 6 deletions k8s-operator/internal/controller/agent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ func (r *AgentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
if err != nil {
if errors.IsNotFound(err) {
// The Agent was deleted, we need to update the ConfigMap
return r.updateConfigMap(ctx)
return r.updateConfigMap(ctx, req.Namespace)
}
logger.Error(err, "Unable to fetch Agent")
return ctrl.Result{}, err
}

// Update the ConfigMap with all Agents
return r.updateConfigMap(ctx)
return r.updateConfigMap(ctx, req.Namespace)
}

func (r *AgentReconciler) updateConfigMap(ctx context.Context) (ctrl.Result, error) {
func (r *AgentReconciler) updateConfigMap(ctx context.Context, namespace string) (ctrl.Result, error) {
logger := log.FromContext(ctx)

// List all Agents
Expand Down Expand Up @@ -106,7 +106,7 @@ func (r *AgentReconciler) updateConfigMap(ctx context.Context) (ctrl.Result, err
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "eidolon-agent-cm",
Namespace: "default", // You might want to make this configurable
Namespace: namespace,
},
Data: map[string]string{
"agents.yaml": agentsYAML.String(),
Expand All @@ -116,7 +116,7 @@ func (r *AgentReconciler) updateConfigMap(ctx context.Context) (ctrl.Result, err
foundCM := &corev1.ConfigMap{}
err := r.Get(ctx, types.NamespacedName{Name: cm.Name, Namespace: cm.Namespace}, foundCM)
if err != nil && errors.IsNotFound(err) {
logger.Info("Creating ConfigMap", "Name", cm.Name)
logger.Info("Creating ConfigMap", "Name", cm.Name, "Namespace", cm.Namespace)
err = r.Create(ctx, cm)
if err != nil {
logger.Error(err, "Failed to create ConfigMap")
Expand All @@ -128,7 +128,7 @@ func (r *AgentReconciler) updateConfigMap(ctx context.Context) (ctrl.Result, err
} else {
// Update the existing ConfigMap
foundCM.Data = cm.Data
logger.Info("Updating ConfigMap", "Name", cm.Name)
logger.Info("Updating ConfigMap", "Name", cm.Name, "Namespace", cm.Namespace)
err = r.Update(ctx, foundCM)
if err != nil {
logger.Error(err, "Failed to update ConfigMap")
Expand Down
3 changes: 2 additions & 1 deletion k8s-operator/internal/controller/agent_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = Describe("Agent Reconciler", func() {
Context("When reconciling Agents", func() {
const (
agentName = "test-agent"
namespace = "default"
namespace = "foobar"
configMapName = "eidolon-agent-cm"
)

Expand All @@ -32,6 +32,7 @@ var _ = Describe("Agent Reconciler", func() {
}

BeforeEach(func() {
Expect(createNamespaceIfNotExists(ctx, namespace)).To(Succeed())
// Create a test Agent
agent := &serverv1alpha1.Agent{
ObjectMeta: metav1.ObjectMeta{
Expand Down
2 changes: 2 additions & 0 deletions k8s-operator/internal/controller/machine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ var _ = Describe("Machine Controller", func() {
mgrCtx, mgrCancel = context.WithCancel(ctx)

go func() {
defer GinkgoRecover()
err := mgr.Start(mgrCtx)
if err != nil {
GinkgoWriter.Printf("Error starting server: %v\n", err)
Expect(err).NotTo(HaveOccurred())
}
}()
Expand Down
8 changes: 4 additions & 4 deletions k8s-operator/internal/controller/reference_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ func (r *ReferenceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
if err != nil {
if errors.IsNotFound(err) {
// The Reference was deleted, we need to update the ConfigMap
return r.updateConfigMap(ctx)
return r.updateConfigMap(ctx, req.NamespacedName.Namespace)
}
logger.Error(err, "Unable to fetch Reference")
return ctrl.Result{}, err
}

// Update the ConfigMap with all References
return r.updateConfigMap(ctx)
return r.updateConfigMap(ctx, req.NamespacedName.Namespace)
}

func (r *ReferenceReconciler) updateConfigMap(ctx context.Context) (ctrl.Result, error) {
func (r *ReferenceReconciler) updateConfigMap(ctx context.Context, namespace string) (ctrl.Result, error) {
logger := log.FromContext(ctx)

// List all References
Expand Down Expand Up @@ -106,7 +106,7 @@ func (r *ReferenceReconciler) updateConfigMap(ctx context.Context) (ctrl.Result,
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "eidolon-reference-cm",
Namespace: "default", // You might want to make this configurable
Namespace: namespace,
},
Data: map[string]string{
"references.yaml": referencesYAML.String(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var _ = Describe("Reference Reconciler", func() {
Context("When reconciling References", func() {
const (
referenceName = "test-reference"
namespace = "default"
namespace = "foobar"
configMapName = "eidolon-reference-cm"
)

Expand All @@ -30,6 +30,7 @@ var _ = Describe("Reference Reconciler", func() {
}

BeforeEach(func() {
Expect(createNamespaceIfNotExists(ctx, namespace)).To(Succeed())
// Create a test Reference
reference := &serverv1alpha1.Reference{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit 77a0878

Please sign in to comment.