From 91d0fc20b81f3bed226744f63a8fe12a052137f4 Mon Sep 17 00:00:00 2001 From: mgoerens Date: Wed, 20 Jul 2022 12:49:54 +0200 Subject: [PATCH 1/3] Use Services' FQDN for intra Pods communication Synapse, Heisenbridge, and mautrix-signal are now setup to communicate with each others using their respective Services' FQDN, rather than the Service IP. --- controllers/synapse/synapse_controller.go | 12 ++++++++++++ .../synapse/synapse_heisenbridge_configmap.go | 4 ++-- .../synapse/synapse_heisenbridge_deployment.go | 2 +- .../synapse/synapse_mautrixsignal_configmap.go | 10 ++++------ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/controllers/synapse/synapse_controller.go b/controllers/synapse/synapse_controller.go index 3d042e9..6ee4ee3 100644 --- a/controllers/synapse/synapse_controller.go +++ b/controllers/synapse/synapse_controller.go @@ -75,6 +75,18 @@ func (r *SynapseReconciler) GetMautrixSignalResourceName(synapse synapsev1alpha1 return strings.Join([]string{synapse.Name, "mautrixsignal"}, "-") } +func (r *SynapseReconciler) GetSynapseServiceFQDN(synapse synapsev1alpha1.Synapse) string { + return strings.Join([]string{synapse.Name, synapse.Namespace, "svc", "cluster", "local"}, ".") +} + +func (r *SynapseReconciler) GetHeisenbridgeServiceFQDN(synapse synapsev1alpha1.Synapse) string { + return strings.Join([]string{r.GetHeisenbridgeResourceName(synapse), synapse.Namespace, "svc", "cluster", "local"}, ".") +} + +func (r *SynapseReconciler) GetMautrixSignalServiceFQDN(synapse synapsev1alpha1.Synapse) string { + return strings.Join([]string{r.GetMautrixSignalResourceName(synapse), synapse.Namespace, "svc", "cluster", "local"}, ".") +} + // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. // diff --git a/controllers/synapse/synapse_heisenbridge_configmap.go b/controllers/synapse/synapse_heisenbridge_configmap.go index 7b30134..8469ca2 100644 --- a/controllers/synapse/synapse_heisenbridge_configmap.go +++ b/controllers/synapse/synapse_heisenbridge_configmap.go @@ -29,7 +29,7 @@ import ( func (r *SynapseReconciler) configMapForHeisenbridge(s *synapsev1alpha1.Synapse, objectMeta metav1.ObjectMeta) (client.Object, error) { heisenbridgeYaml := ` id: heisenbridge -url: http://` + s.Status.BridgesConfiguration.Heisenbridge.IP + `:9898 +url: http://` + r.GetHeisenbridgeServiceFQDN(*s) + `:9898 as_token: EUFqSPQusV4mXkPKbwdHyIhthELQ1Xf9S5lSEzTrrlb0uz0ZJRHhwEljT71ByObe hs_token: If6r2GGlsNN4MnoW3djToADNdq0JuIJ1WNM4rKHO73WuG5QvVubj1Q4JHrmQBcS6 rate_limited: false @@ -91,6 +91,6 @@ func (r *SynapseReconciler) updateHeisenbridgeWithURL( s synapsev1alpha1.Synapse, heisenbridge map[string]interface{}, ) error { - heisenbridge["url"] = "http://" + s.Status.BridgesConfiguration.Heisenbridge.IP + ":9898" + heisenbridge["url"] = "http://" + r.GetHeisenbridgeServiceFQDN(s) + ":9898" return nil } diff --git a/controllers/synapse/synapse_heisenbridge_deployment.go b/controllers/synapse/synapse_heisenbridge_deployment.go index a4a2cc1..20fcc02 100644 --- a/controllers/synapse/synapse_heisenbridge_deployment.go +++ b/controllers/synapse/synapse_heisenbridge_deployment.go @@ -108,7 +108,7 @@ func (r *SynapseReconciler) craftHeisenbridgeCommad(s synapsev1alpha1.Synapse) [ "/data-heisenbridge/heisenbridge.yaml", "-l", "0.0.0.0", - "http://"+s.Status.IP+":8008", + "http://"+r.GetSynapseServiceFQDN(s)+":8008", ) return command diff --git a/controllers/synapse/synapse_mautrixsignal_configmap.go b/controllers/synapse/synapse_mautrixsignal_configmap.go index 0bb1dfb..b0fafcc 100644 --- a/controllers/synapse/synapse_mautrixsignal_configmap.go +++ b/controllers/synapse/synapse_mautrixsignal_configmap.go @@ -30,13 +30,12 @@ import ( // configMapForSynapse returns a synapse ConfigMap object func (r *SynapseReconciler) configMapForMautrixSignal(s *synapsev1alpha1.Synapse, objectMeta metav1.ObjectMeta) (client.Object, error) { synapseServerName := s.Status.HomeserverConfiguration.ServerName - synapseIP := s.Status.IP configYaml := ` # Homeserver details homeserver: # The address that this appservice can use to connect to the homeserver. - address: http://` + synapseIP + `:8008 + address: http://` + r.GetSynapseServiceFQDN(*s) + `:8008 # The domain of the homeserver (for MXIDs, etc). domain: ` + synapseServerName + ` # Whether or not to verify the SSL certificate of the homeserver. @@ -61,7 +60,7 @@ homeserver: # Changing these values requires regeneration of the registration. appservice: # The address that the homeserver can use to connect to this appservice. - address: http://` + s.Status.BridgesConfiguration.MautrixSignal.IP + `:29328 + address: http://` + r.GetMautrixSignalServiceFQDN(*s) + `:29328 # When using https:// the TLS certificate and key files for the address. tls_cert: false tls_key: false @@ -389,7 +388,6 @@ func (r *SynapseReconciler) updateMautrixSignalData( config map[string]interface{}, ) error { synapseServerName := s.Status.HomeserverConfiguration.ServerName - synapseIP := s.Status.IP // Update the homeserver section so that the bridge can reach Synapse configHomeserver, ok := config["homeserver"].(map[interface{}]interface{}) @@ -397,7 +395,7 @@ func (r *SynapseReconciler) updateMautrixSignalData( err := errors.New("cannot parse mautrix-signal config.yaml: error parsing 'homeserver' section") return err } - configHomeserver["address"] = "http://" + synapseIP + ":8008" + configHomeserver["address"] = "http://" + r.GetSynapseServiceFQDN(s) + ":8008" configHomeserver["domain"] = synapseServerName config["homeserver"] = configHomeserver @@ -407,7 +405,7 @@ func (r *SynapseReconciler) updateMautrixSignalData( err := errors.New("cannot parse mautrix-signal config.yaml: error parsing 'appservice' section") return err } - configAppservice["address"] = "http://" + s.Status.BridgesConfiguration.MautrixSignal.IP + ":29328" + configAppservice["address"] = "http://" + r.GetMautrixSignalServiceFQDN(s) + ":29328" config["appservice"] = configAppservice // Update the path to the signal socket path From fe9e7a307f8dd4c4836f8eaac22bfdc7279a5fd1 Mon Sep 17 00:00:00 2001 From: mgoerens Date: Wed, 20 Jul 2022 15:21:49 +0200 Subject: [PATCH 2/3] Remove Services' IP from Synapse Status This information is not needed anymore, as we are now using the Services' FQDN for intra Pod communication. --- apis/synapse/v1alpha1/synapse_types.go | 24 ---- .../synapse/v1alpha1/zz_generated.deepcopy.go | 48 -------- .../manifests/synapse.opdev.io_synapses.yaml | 22 ---- .../crd/bases/synapse.opdev.io_synapses.yaml | 22 ---- controllers/synapse/synapse_controller.go | 51 -------- .../synapse/synapse_controller_test.go | 110 ++---------------- 6 files changed, 9 insertions(+), 268 deletions(-) diff --git a/apis/synapse/v1alpha1/synapse_types.go b/apis/synapse/v1alpha1/synapse_types.go index 5973671..85ebfa4 100644 --- a/apis/synapse/v1alpha1/synapse_types.go +++ b/apis/synapse/v1alpha1/synapse_types.go @@ -160,18 +160,12 @@ type SynapseStatus struct { // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster // Important: Run "make" to regenerate code after modifying this file - // Configuration of deployed bridges - BridgesConfiguration SynapseStatusBridgesConfiguration `json:"bridgesConfiguration,omitempty"` - // Connection information to the external PostgreSQL Database DatabaseConnectionInfo SynapseStatusDatabaseConnectionInfo `json:"databaseConnectionInfo,omitempty"` // Holds configuration information for Synapse HomeserverConfiguration SynapseStatusHomeserverConfiguration `json:"homeserverConfiguration,omitempty"` - // Synapse IP address (corresponding to the Synapse Service IP address) - IP string `json:"ip,omitempty"` - // State of the Synapse instance State string `json:"state,omitempty"` @@ -179,24 +173,6 @@ type SynapseStatus struct { Reason string `json:"reason,omitempty"` } -type SynapseStatusBridgesConfiguration struct { - // Status of the Heisenbridge - Heisenbridge SynapseStatusHeisenbridge `json:"heisenbridge,omitempty"` - - // Status of the mautrix-signal bridge - MautrixSignal SynapseStatusMautrixSignal `json:"mautrixSignal,omitempty"` -} - -type SynapseStatusHeisenbridge struct { - // IP at which the Heisenbridge is available - IP string `json:"ip,omitempty"` -} - -type SynapseStatusMautrixSignal struct { - // IP at which the mautrix-signal bridge is available - IP string `json:"ip,omitempty"` -} - type SynapseStatusDatabaseConnectionInfo struct { // Endpoint to connect to the PostgreSQL database ConnectionURL string `json:"connectionURL,omitempty"` diff --git a/apis/synapse/v1alpha1/zz_generated.deepcopy.go b/apis/synapse/v1alpha1/zz_generated.deepcopy.go index 4aabf88..40915b6 100644 --- a/apis/synapse/v1alpha1/zz_generated.deepcopy.go +++ b/apis/synapse/v1alpha1/zz_generated.deepcopy.go @@ -238,7 +238,6 @@ func (in *SynapseSpec) DeepCopy() *SynapseSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SynapseStatus) DeepCopyInto(out *SynapseStatus) { *out = *in - out.BridgesConfiguration = in.BridgesConfiguration out.DatabaseConnectionInfo = in.DatabaseConnectionInfo out.HomeserverConfiguration = in.HomeserverConfiguration } @@ -253,23 +252,6 @@ func (in *SynapseStatus) DeepCopy() *SynapseStatus { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *SynapseStatusBridgesConfiguration) DeepCopyInto(out *SynapseStatusBridgesConfiguration) { - *out = *in - out.Heisenbridge = in.Heisenbridge - out.MautrixSignal = in.MautrixSignal -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SynapseStatusBridgesConfiguration. -func (in *SynapseStatusBridgesConfiguration) DeepCopy() *SynapseStatusBridgesConfiguration { - if in == nil { - return nil - } - out := new(SynapseStatusBridgesConfiguration) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SynapseStatusDatabaseConnectionInfo) DeepCopyInto(out *SynapseStatusDatabaseConnectionInfo) { *out = *in @@ -285,21 +267,6 @@ func (in *SynapseStatusDatabaseConnectionInfo) DeepCopy() *SynapseStatusDatabase return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *SynapseStatusHeisenbridge) DeepCopyInto(out *SynapseStatusHeisenbridge) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SynapseStatusHeisenbridge. -func (in *SynapseStatusHeisenbridge) DeepCopy() *SynapseStatusHeisenbridge { - if in == nil { - return nil - } - out := new(SynapseStatusHeisenbridge) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SynapseStatusHomeserverConfiguration) DeepCopyInto(out *SynapseStatusHomeserverConfiguration) { *out = *in @@ -314,18 +281,3 @@ func (in *SynapseStatusHomeserverConfiguration) DeepCopy() *SynapseStatusHomeser in.DeepCopyInto(out) return out } - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *SynapseStatusMautrixSignal) DeepCopyInto(out *SynapseStatusMautrixSignal) { - *out = *in -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SynapseStatusMautrixSignal. -func (in *SynapseStatusMautrixSignal) DeepCopy() *SynapseStatusMautrixSignal { - if in == nil { - return nil - } - out := new(SynapseStatusMautrixSignal) - in.DeepCopyInto(out) - return out -} diff --git a/bundle/manifests/synapse.opdev.io_synapses.yaml b/bundle/manifests/synapse.opdev.io_synapses.yaml index 88312d5..8d1d27e 100644 --- a/bundle/manifests/synapse.opdev.io_synapses.yaml +++ b/bundle/manifests/synapse.opdev.io_synapses.yaml @@ -153,24 +153,6 @@ spec: status: description: SynapseStatus defines the observed state of Synapse properties: - bridgesConfiguration: - description: Configuration of deployed bridges - properties: - heisenbridge: - description: Status of the Heisenbridge - properties: - ip: - description: IP at which the Heisenbridge is available - type: string - type: object - mautrixSignal: - description: Status of the mautrix-signal bridge - properties: - ip: - description: IP at which the mautrix-signal bridge is available - type: string - type: object - type: object databaseConnectionInfo: description: Connection information to the external PostgreSQL Database properties: @@ -201,10 +183,6 @@ spec: description: The public-facing domain of the server type: string type: object - ip: - description: Synapse IP address (corresponding to the Synapse Service - IP address) - type: string reason: description: Reason for the current Synapse State type: string diff --git a/config/crd/bases/synapse.opdev.io_synapses.yaml b/config/crd/bases/synapse.opdev.io_synapses.yaml index d626c61..93e3c41 100644 --- a/config/crd/bases/synapse.opdev.io_synapses.yaml +++ b/config/crd/bases/synapse.opdev.io_synapses.yaml @@ -150,24 +150,6 @@ spec: status: description: SynapseStatus defines the observed state of Synapse properties: - bridgesConfiguration: - description: Configuration of deployed bridges - properties: - heisenbridge: - description: Status of the Heisenbridge - properties: - ip: - description: IP at which the Heisenbridge is available - type: string - type: object - mautrixSignal: - description: Status of the mautrix-signal bridge - properties: - ip: - description: IP at which the mautrix-signal bridge is available - type: string - type: object - type: object databaseConnectionInfo: description: Connection information to the external PostgreSQL Database properties: @@ -198,10 +180,6 @@ spec: description: The public-facing domain of the server type: string type: object - ip: - description: Synapse IP address (corresponding to the Synapse Service - IP address) - type: string reason: description: Reason for the current Synapse State type: string diff --git a/controllers/synapse/synapse_controller.go b/controllers/synapse/synapse_controller.go index 6ee4ee3..d6936d4 100644 --- a/controllers/synapse/synapse_controller.go +++ b/controllers/synapse/synapse_controller.go @@ -219,7 +219,6 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct // We first need to create the Synapse Service as its IP address is potentially // needed by the Bridges - synapseKey := types.NamespacedName{Name: synapse.Name, Namespace: synapse.Namespace} createdService := &corev1.Service{} if err := r.reconcileResource( ctx, @@ -231,23 +230,12 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct return ctrl.Result{}, err } - // Fetch Synapse IP and update the resource status - synapseIP, err := r.getServiceIP(ctx, synapseKey, createdService) - if err != nil { - return ctrl.Result{}, err - } - synapse.Status.IP = synapseIP - if err := r.updateSynapseStatus(ctx, &synapse); err != nil { - return ctrl.Result{}, err - } - if synapse.Spec.Bridges.Heisenbridge.Enabled { log.Info("Heisenbridge is enabled - deploying Heisenbridge") // Heisenbridge is composed of a ConfigMap, a Service and a Deployment. // Resources associated to the Heisenbridge are append with "-heisenbridge" createdHeisenbridgeService := &corev1.Service{} objectMetaHeisenbridge := setObjectMeta(r.GetHeisenbridgeResourceName(synapse), synapse.Namespace, map[string]string{}) - heisenbridgeKey := types.NamespacedName{Name: r.GetHeisenbridgeResourceName(synapse), Namespace: synapse.Namespace} // First create the service as we need its IP address for the // heisenbridge.yaml configuration file @@ -261,16 +249,6 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct return ctrl.Result{}, err } - // Get Service IP and update the Synapse status - heisenbridgeIP, err := r.getServiceIP(ctx, heisenbridgeKey, createdHeisenbridgeService) - if err != nil { - return ctrl.Result{}, err - } - synapse.Status.BridgesConfiguration.Heisenbridge.IP = heisenbridgeIP - if err := r.updateSynapseStatus(ctx, &synapse); err != nil { - return ctrl.Result{}, err - } - // The ConfigMap for Heisenbridge, containing the heisenbridge.yaml // config file. It's either a copy of a user-provided ConfigMap, if // defined in Spec.Bridges.Heisenbridge.ConfigMap, or a new ConfigMap @@ -380,7 +358,6 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct // In addition, a second deployment is needed to run signald. This is append with "-signald" createdMautrixSignalService := &corev1.Service{} objectMetaMautrixSignal := setObjectMeta(r.GetMautrixSignalResourceName(synapse), synapse.Namespace, map[string]string{}) - mautrixSignalKey := types.NamespacedName{Name: r.GetMautrixSignalResourceName(synapse), Namespace: synapse.Namespace} objectMetaSignald := setObjectMeta(r.GetSignaldResourceName(synapse), synapse.Namespace, map[string]string{}) // First create the service as we need its IP address for the @@ -395,16 +372,6 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct return ctrl.Result{}, err } - // Get Service IP and update the Synapse status - mautrixSignalIP, err := r.getServiceIP(ctx, mautrixSignalKey, createdMautrixSignalService) - if err != nil { - return ctrl.Result{}, err - } - synapse.Status.BridgesConfiguration.MautrixSignal.IP = mautrixSignalIP - if err := r.updateSynapseStatus(ctx, &synapse); err != nil { - return ctrl.Result{}, err - } - // The ConfigMap for mautrix-signal, containing the config.yaml config // file. It's either a copy of a user-provided ConfigMap, if defined in // Spec.Bridges.MautrixSignal.ConfigMap, or a new ConfigMap containing @@ -840,24 +807,6 @@ func (r *SynapseReconciler) updateSynapseStatusDatabase( return nil } -func (r *SynapseReconciler) getServiceIP( - ctx context.Context, - synapseKey types.NamespacedName, - service *corev1.Service, -) (string, error) { - if err := r.Get(ctx, synapseKey, service); err != nil { - return "", err - } - - serviceIP := service.Spec.ClusterIP - if serviceIP == "" { - err := errors.New("service IP not set") - return "", err - } - - return serviceIP, nil -} - // SetupWithManager sets up the controller with the Manager. func (r *SynapseReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). diff --git a/controllers/synapse/synapse_controller_test.go b/controllers/synapse/synapse_controller_test.go index 6f8c311..f582c40 100644 --- a/controllers/synapse/synapse_controller_test.go +++ b/controllers/synapse/synapse_controller_test.go @@ -496,17 +496,6 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( }) It("Should should update the Synapse Status", func() { - // Get ServiceIP - var synapseIP string - Eventually(func() bool { - err := k8sClient.Get(ctx, synapseLookupKey, createdService) - if err != nil { - return false - } - synapseIP = createdService.Spec.ClusterIP - return synapseIP != "" - }, timeout, interval).Should(BeTrue()) - expectedStatus := synapsev1alpha1.SynapseStatus{ State: "RUNNING", Reason: "", @@ -514,7 +503,6 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( ServerName: ServerName, ReportStats: ReportStats, }, - IP: synapseIP, } // Status may need some time to be updated Eventually(func() synapsev1alpha1.SynapseStatus { @@ -608,17 +596,6 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( }) It("Should should update the Synapse Status", func() { - // Get ServiceIP - var synapseIP string - Eventually(func() bool { - err := k8sClient.Get(ctx, synapseLookupKey, createdService) - if err != nil { - return false - } - synapseIP = createdService.Spec.ClusterIP - return synapseIP != "" - }, timeout, interval).Should(BeTrue()) - expectedStatus := synapsev1alpha1.SynapseStatus{ State: "RUNNING", Reason: "", @@ -626,7 +603,6 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( ServerName: ServerName, ReportStats: ReportStats, }, - IP: synapseIP, } // Status may need some time to be updated Eventually(func() synapsev1alpha1.SynapseStatus { @@ -890,22 +866,6 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( checkResourcePresence(createdHeisenbridgeService, heisenbridgeLookupKey, expectedOwnerReference) }) - It("Should add the Heisenbridge IP to the Synapse Status", func() { - // Get Heisenbridge IP - var heisenbridgeIP string - Eventually(func() bool { - err := k8sClient.Get(ctx, heisenbridgeLookupKey, createdHeisenbridgeService) - if err != nil { - return false - } - heisenbridgeIP = createdHeisenbridgeService.Spec.ClusterIP - return heisenbridgeIP != "" - }, timeout, interval).Should(BeTrue()) - - Expect(k8sClient.Get(ctx, synapseLookupKey, synapse)).To(Succeed()) - Expect(synapse.Status.BridgesConfiguration.Heisenbridge.IP).To(Equal(heisenbridgeIP)) - }) - It("Should update the Synapse homeserver.yaml", func() { Eventually(func(g Gomega) { g.Expect(k8sClient.Get(ctx, @@ -930,16 +890,14 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( When("The user provides an input ConfigMap", func() { var inputHeisenbridgeConfigMap *corev1.ConfigMap var inputHeisenbridgeConfigMapData map[string]string - var heisenbridgeIP string const InputHeisenbridgeConfigMapName = "heisenbridge-input" + const heisenbridgeFQDN = SynapseName + "-heisenbridge." + SynapseNamespace + ".svc.cluster.local" BeforeAll(func() { initSynapseVariables() initHeisenbridgeVariables() - heisenbridgeIP = "" - inputConfigmapData = map[string]string{ "homeserver.yaml": "server_name: " + ServerName + "\n" + "report_stats: " + strconv.FormatBool(ReportStats), @@ -1009,21 +967,6 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( checkResourcePresence(createdHeisenbridgeService, heisenbridgeLookupKey, expectedOwnerReference) }) - It("Should update the Synapse Status with Heisenbridge configuration information", func() { - // Get Heisenbridge IP - Eventually(func() bool { - err := k8sClient.Get(ctx, heisenbridgeLookupKey, createdHeisenbridgeService) - if err != nil { - return false - } - heisenbridgeIP = createdHeisenbridgeService.Spec.ClusterIP - return heisenbridgeIP != "" - }, timeout, interval).Should(BeTrue()) - - Expect(k8sClient.Get(ctx, synapseLookupKey, synapse)).To(Succeed()) - Expect(synapse.Status.BridgesConfiguration.Heisenbridge.IP).To(Equal(heisenbridgeIP)) - }) - It("Should add url value to the created Heisenbridge ConfigMap", func() { Eventually(func(g Gomega) { g.Expect(k8sClient.Get(ctx, heisenbridgeLookupKey, inputHeisenbridgeConfigMap)).Should(Succeed()) @@ -1037,7 +980,7 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( _, ok = heisenbridge["url"] g.Expect(ok).Should(BeTrue()) - g.Expect(heisenbridge["url"]).To(Equal("http://" + heisenbridgeIP + ":" + strconv.Itoa(heisenbridgePort))) + g.Expect(heisenbridge["url"]).To(Equal("http://" + heisenbridgeFQDN + ":" + strconv.Itoa(heisenbridgePort))) }, timeout, interval).Should(Succeed()) }) }) @@ -1166,22 +1109,6 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( checkResourcePresence(createdMautrixSignalService, mautrixSignalLookupKey, expectedOwnerReference) }) - It("Should add the mautrix-signal IP to the Synapse Status", func() { - // Get mautrix-signal IP - var mautrixSignalIP string - Eventually(func() bool { - err := k8sClient.Get(ctx, mautrixSignalLookupKey, createdMautrixSignalService) - if err != nil { - return false - } - mautrixSignalIP = createdMautrixSignalService.Spec.ClusterIP - return mautrixSignalIP != "" - }, timeout, interval).Should(BeTrue()) - - Expect(k8sClient.Get(ctx, synapseLookupKey, synapse)).To(Succeed()) - Expect(synapse.Status.BridgesConfiguration.MautrixSignal.IP).To(Equal(mautrixSignalIP)) - }) - It("Should update the Synapse homeserver.yaml", func() { Eventually(func(g Gomega) { g.Expect(k8sClient.Get(ctx, @@ -1206,16 +1133,15 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( When("The user provides an input ConfigMap", func() { var inputMautrixSignalConfigMap *corev1.ConfigMap var inputMautrixSignalConfigMapData map[string]string - var mautrixSignalIP string const InputMautrixSignalConfigMapName = "mautrix-signal-input" + const mautrixSignalFQDN = SynapseName + "-mautrixsignal." + SynapseNamespace + ".svc.cluster.local" + const synapseFQDN = SynapseName + "." + SynapseNamespace + ".svc.cluster.local" BeforeAll(func() { initSynapseVariables() initMautrixSignalVariables() - mautrixSignalIP = "" - inputConfigmapData = map[string]string{ "homeserver.yaml": "server_name: " + ServerName + "\n" + "report_stats: " + strconv.FormatBool(ReportStats), @@ -1323,26 +1249,8 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( checkResourcePresence(createdMautrixSignalService, mautrixSignalLookupKey, expectedOwnerReference) }) - It("Should update the Synapse Status with mautrix-signal configuration information", func() { - // Get mautrix-signal IP - Eventually(func() bool { - err := k8sClient.Get(ctx, mautrixSignalLookupKey, createdMautrixSignalService) - if err != nil { - return false - } - mautrixSignalIP = createdMautrixSignalService.Spec.ClusterIP - return mautrixSignalIP != "" - }, timeout, interval).Should(BeTrue()) - - Expect(k8sClient.Get(ctx, synapseLookupKey, synapse)).To(Succeed()) - Expect(synapse.Status.BridgesConfiguration.MautrixSignal.IP).To(Equal(mautrixSignalIP)) - }) - It("Should overwrite necessary values in the created mautrix-signal ConfigMap", func() { Eventually(func(g Gomega) { - synapseIP := synapse.Status.IP - synapseServerName := synapse.Status.HomeserverConfiguration.ServerName - By("Verifying that the mautrixsignal ConfigMap exists") g.Expect(k8sClient.Get(ctx, mautrixSignalLookupKey, inputMautrixSignalConfigMap)).Should(Succeed()) @@ -1355,13 +1263,13 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( By("Verifying that the homeserver configuration has been updated") configHomeserver, ok := config["homeserver"].(map[interface{}]interface{}) g.Expect(ok).Should(BeTrue()) - g.Expect(configHomeserver["address"]).To(Equal("http://" + synapseIP + ":8008")) - g.Expect(configHomeserver["domain"]).To(Equal(synapseServerName)) + g.Expect(configHomeserver["address"]).To(Equal("http://" + synapseFQDN + ":8008")) + g.Expect(configHomeserver["domain"]).To(Equal(ServerName)) By("Verifying that the appservice configuration has been updated") configAppservice, ok := config["appservice"].(map[interface{}]interface{}) g.Expect(ok).Should(BeTrue()) - g.Expect(configAppservice["address"]).To(Equal("http://" + mautrixSignalIP + ":" + strconv.Itoa(mautrixSignalPort))) + g.Expect(configAppservice["address"]).To(Equal("http://" + mautrixSignalFQDN + ":" + strconv.Itoa(mautrixSignalPort))) By("Verifying that the signal configuration has been updated") configSignal, ok := config["signal"].(map[interface{}]interface{}) @@ -1374,8 +1282,8 @@ var _ = Describe("Integration tests for the Synapse controller", Ordered, Label( configBridgePermissions, ok := configBridge["permissions"].(map[interface{}]interface{}) g.Expect(ok).Should(BeTrue()) g.Expect(configBridgePermissions).Should(HaveKeyWithValue("*", "relay")) - g.Expect(configBridgePermissions).Should(HaveKeyWithValue(synapseServerName, "user")) - g.Expect(configBridgePermissions).Should(HaveKeyWithValue("@admin:"+synapseServerName, "admin")) + g.Expect(configBridgePermissions).Should(HaveKeyWithValue(ServerName, "user")) + g.Expect(configBridgePermissions).Should(HaveKeyWithValue("@admin:"+ServerName, "admin")) By("Verifying that the log configuration file path have been updated") configLogging, ok := config["logging"].(map[interface{}]interface{}) From 319433740d9b0ea6b6890fa552f45a3141116f97 Mon Sep 17 00:00:00 2001 From: mgoerens Date: Wed, 20 Jul 2022 15:40:26 +0200 Subject: [PATCH 3/3] Reconciliate Service with related resources Following the use of Services' FQDN instead of IP, it is not needed anymore to create the Service first, and wait for an IP to be set. We can reconcile the Services (for Synapse, Heisenbridge, and mautrix-signal) together with their related resources. This makes the controller reconciliation more trivial. --- controllers/synapse/synapse_controller.go | 71 ++++++++++------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/controllers/synapse/synapse_controller.go b/controllers/synapse/synapse_controller.go index d6936d4..358ec84 100644 --- a/controllers/synapse/synapse_controller.go +++ b/controllers/synapse/synapse_controller.go @@ -217,38 +217,12 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct } } - // We first need to create the Synapse Service as its IP address is potentially - // needed by the Bridges - createdService := &corev1.Service{} - if err := r.reconcileResource( - ctx, - r.serviceForSynapse, - &synapse, - createdService, - objectMetaForSynapse, - ); err != nil { - return ctrl.Result{}, err - } - if synapse.Spec.Bridges.Heisenbridge.Enabled { log.Info("Heisenbridge is enabled - deploying Heisenbridge") // Heisenbridge is composed of a ConfigMap, a Service and a Deployment. // Resources associated to the Heisenbridge are append with "-heisenbridge" - createdHeisenbridgeService := &corev1.Service{} objectMetaHeisenbridge := setObjectMeta(r.GetHeisenbridgeResourceName(synapse), synapse.Namespace, map[string]string{}) - // First create the service as we need its IP address for the - // heisenbridge.yaml configuration file - if err := r.reconcileResource( - ctx, - r.serviceForHeisenbridge, - &synapse, - createdHeisenbridgeService, - objectMetaHeisenbridge, - ); err != nil { - return ctrl.Result{}, err - } - // The ConfigMap for Heisenbridge, containing the heisenbridge.yaml // config file. It's either a copy of a user-provided ConfigMap, if // defined in Spec.Bridges.Heisenbridge.ConfigMap, or a new ConfigMap @@ -328,6 +302,17 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct } } + // Create Service for Heisenbridge + if err := r.reconcileResource( + ctx, + r.serviceForHeisenbridge, + &synapse, + &corev1.Service{}, + objectMetaHeisenbridge, + ); err != nil { + return ctrl.Result{}, err + } + // Create Deployment for Heisenbridge if err := r.reconcileResource( ctx, @@ -356,22 +341,9 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct // mautrix-signal is composed of a ConfigMap, a Service and 1 Deployment. // Resources associated to the mautrix-signal are append with "-mautrixsignal" // In addition, a second deployment is needed to run signald. This is append with "-signald" - createdMautrixSignalService := &corev1.Service{} objectMetaMautrixSignal := setObjectMeta(r.GetMautrixSignalResourceName(synapse), synapse.Namespace, map[string]string{}) objectMetaSignald := setObjectMeta(r.GetSignaldResourceName(synapse), synapse.Namespace, map[string]string{}) - // First create the service as we need its IP address for the - // config.yaml configuration file - if err := r.reconcileResource( - ctx, - r.serviceForMautrixSignal, - &synapse, - createdMautrixSignalService, - objectMetaMautrixSignal, - ); err != nil { - return ctrl.Result{}, err - } - // The ConfigMap for mautrix-signal, containing the config.yaml config // file. It's either a copy of a user-provided ConfigMap, if defined in // Spec.Bridges.MautrixSignal.ConfigMap, or a new ConfigMap containing @@ -495,6 +467,17 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct return ctrl.Result{}, err } + // Create a Service for mautrix-signal + if err := r.reconcileResource( + ctx, + r.serviceForMautrixSignal, + &synapse, + &corev1.Service{}, + objectMetaMautrixSignal, + ); err != nil { + return ctrl.Result{}, err + } + // Create a PVC for mautrix-signal if err := r.reconcileResource( ctx, @@ -541,6 +524,16 @@ func (r *SynapseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct } // Reconcile Synapse resources: PVC, Deployment and Service + if err := r.reconcileResource( + ctx, + r.serviceForSynapse, + &synapse, + &corev1.Service{}, + objectMetaForSynapse, + ); err != nil { + return ctrl.Result{}, err + } + if err := r.reconcileResource( ctx, r.serviceAccountForSynapse,