diff --git a/example/leafnode-remotes-gateways.yaml b/example/leafnode-remotes-gateways.yaml new file mode 100644 index 00000000..394612fb --- /dev/null +++ b/example/leafnode-remotes-gateways.yaml @@ -0,0 +1,49 @@ +# NOTE: Replace the 1.2.3.4 IPs with real IPs or a domain. Also include a real +# base64 encoded credentials file. + +--- +apiVersion: v1 +kind: Secret +metadata: + name: user-credentials +data: + user.ncreds: ...base64-encoded-credentials... + +--- +apiVersion: nats.io/v1alpha2 +kind: NatsCluster +metadata: + name: example-nats-cluster +spec: + size: 3 + version: "2.1.7" + + natsConfig: + debug: true + trace: true + + pod: + volumeMounts: + - name: user-credentials + mountPath: /etc/nats-creds + readOnl: true + + leafnodeConfig: + remotes: + - url: nats://1.2.3.4:7422 + credentials: /etc/nats-creds/user.ncreds + + gatewayConfig: + name: gateway_b + hostPort: 7222 + rejectUnknown: true + gateways: + - name: gateway_a + url: nats://guser:gpassword@1.2.3.4:7222 + + template: + spec: + volumes: + - name: user-credentials + secret: + secretName: user-credentials diff --git a/pkg/apis/nats/v1alpha2/cluster.go b/pkg/apis/nats/v1alpha2/cluster.go index 8029faa8..7dad72ce 100644 --- a/pkg/apis/nats/v1alpha2/cluster.go +++ b/pkg/apis/nats/v1alpha2/cluster.go @@ -201,9 +201,17 @@ type RemoteGatewayOpts struct { URL string `json:"url,omitempty"` } +// LeafNodeRemote is the URL for remote NATS system. +type LeafNodeRemote struct { + URL string `json:"url,omitempty"` + URLs []string `json:"urls,omitempty"` + Credentials string `json:"credentials,omitempty"` +} + // LeafNodeConfig is the configuration for leafnodes. type LeafNodeConfig struct { - Port int `json:"hostPort,omitempty"` + Port int `json:"hostPort,omitempty"` + Remotes []LeafNodeRemote `json:"remotes,omitempty"` } // TLSConfig is the optional TLS configuration for the cluster. diff --git a/pkg/apis/nats/v1alpha2/zz_generated.deepcopy.go b/pkg/apis/nats/v1alpha2/zz_generated.deepcopy.go index b4140eed..78a1f558 100644 --- a/pkg/apis/nats/v1alpha2/zz_generated.deepcopy.go +++ b/pkg/apis/nats/v1alpha2/zz_generated.deepcopy.go @@ -107,7 +107,7 @@ func (in *ClusterSpec) DeepCopyInto(out *ClusterSpec) { if in.LeafNodeConfig != nil { in, out := &in.LeafNodeConfig, &out.LeafNodeConfig *out = new(LeafNodeConfig) - **out = **in + (*in).DeepCopyInto(*out) } if in.OperatorConfig != nil { in, out := &in.OperatorConfig, &out.OperatorConfig @@ -194,6 +194,13 @@ func (in *GatewayConfig) DeepCopy() *GatewayConfig { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *LeafNodeConfig) DeepCopyInto(out *LeafNodeConfig) { *out = *in + if in.Remotes != nil { + in, out := &in.Remotes, &out.Remotes + *out = make([]LeafNodeRemote, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } return } @@ -207,6 +214,27 @@ func (in *LeafNodeConfig) DeepCopy() *LeafNodeConfig { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *LeafNodeRemote) DeepCopyInto(out *LeafNodeRemote) { + *out = *in + if in.URLs != nil { + in, out := &in.URLs, &out.URLs + *out = make([]string, len(*in)) + copy(*out, *in) + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new LeafNodeRemote. +func (in *LeafNodeRemote) DeepCopy() *LeafNodeRemote { + if in == nil { + return nil + } + out := new(LeafNodeRemote) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NatsCluster) DeepCopyInto(out *NatsCluster) { *out = *in diff --git a/pkg/conf/natsconf.go b/pkg/conf/natsconf.go index d147b901..f5f8ebcd 100644 --- a/pkg/conf/natsconf.go +++ b/pkg/conf/natsconf.go @@ -54,12 +54,19 @@ type GatewayConfig struct { Authorization *AuthorizationConfig `json:"authorization,omitempty"` } +// LeafNodeRemote is the URL for remote NATS system. +type LeafNodeRemote struct { + URLs []string `json:"urls,omitempty"` + Credentials string `json:"credentials,omitempty"` +} + type LeafNodeServerConfig struct { - Port int `json:"port,omitempty"` - TLS *TLSConfig `json:"tls,omitempty"` - TLSTimeout float64 `json:"tls_timeout,omitempty"` - Advertise string `json:"advertise,omitempty"` - Include string `json:"include,omitempty"` + Port int `json:"port,omitempty"` + TLS *TLSConfig `json:"tls,omitempty"` + TLSTimeout float64 `json:"tls_timeout,omitempty"` + Advertise string `json:"advertise,omitempty"` + Include string `json:"include,omitempty"` + Remotes []LeafNodeRemote `json:"remotes,omitempty"` } type RemoteGatewayOpts struct { diff --git a/pkg/util/kubernetes/kubernetes.go b/pkg/util/kubernetes/kubernetes.go index e730180b..816f1ce8 100644 --- a/pkg/util/kubernetes/kubernetes.go +++ b/pkg/util/kubernetes/kubernetes.go @@ -395,6 +395,21 @@ func addGatewayConfig(sconfig *natsconf.ServerConfig, cluster v1alpha2.ClusterSp sconfig.LeafNode = &natsconf.LeafNodeServerConfig{ Port: cluster.LeafNodeConfig.Port, } + for _, r := range cluster.LeafNodeConfig.Remotes { + var urls []string + if r.URL != "" { + urls = append(urls, r.URL) + } + if len(r.URLs) > 0 { + urls = append(urls, r.URLs...) + } + + sconfig.LeafNode.Remotes = append(sconfig.LeafNode.Remotes, natsconf.LeafNodeRemote{ + URLs: urls, + Credentials: r.Credentials, + }) + } + if cluster.Pod != nil && cluster.Pod.AdvertiseExternalIP { sconfig.LeafNode.Include = filepath.Join(".", constants.BootConfigGatewayFilePath) }