Skip to content
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

Add NATS URL in the NATS CR status #239

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/v1alpha1/nats_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,13 @@ func (ns *NATSStatus) Initialize() {
ns.UpdateConditionStatefulSet(metav1.ConditionFalse, ConditionReasonProcessing, "")
ns.UpdateConditionAvailable(metav1.ConditionFalse, ConditionReasonProcessing, "")
}

// ClearURL clears the url.
func (ns *NATSStatus) ClearURL() {
ns.URL = ""
}

// SetURL sets the url.
func (ns *NATSStatus) SetURL(url string) {
ns.URL = url
}
39 changes: 39 additions & 0 deletions api/v1alpha1/nats_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,42 @@ func Test_Initialize(t *testing.T) {
require.Equal(t, expectedAvailableCondition, availableCondition)
})
}

func Test_ClearURL(t *testing.T) {
t.Parallel()
t.Run("should clear the url", func(t *testing.T) {
t.Parallel()

// given
const givenURL = "some.url"
natsStatus := &NATSStatus{URL: givenURL}

// when
natsStatus.ClearURL()

// then
const wantURL = ""

require.NotEqual(t, wantURL, givenURL)
require.Equal(t, wantURL, natsStatus.URL)
})
}

func Test_SetURL(t *testing.T) {
t.Parallel()
t.Run("should set the url", func(t *testing.T) {
t.Parallel()

// given
const givenURL = ""
natsStatus := &NATSStatus{URL: givenURL}

// when
const wantURL = "some.url"
natsStatus.SetURL(wantURL)

// then
require.NotEqual(t, wantURL, givenURL)
require.Equal(t, wantURL, natsStatus.URL)
})
}
1 change: 1 addition & 0 deletions api/v1alpha1/nats_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type NATS struct {
// NATSStatus defines the observed state of NATS.
type NATSStatus struct {
State string `json:"state"`
URL string `json:"url,omitempty"`
Conditions []metav1.Condition `json:"conditions,omitempty"`
}

Expand Down
2 changes: 2 additions & 0 deletions config/crd/bases/operator.kyma-project.io_nats.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ spec:
type: array
state:
type: string
url:
type: string
required:
- state
type: object
Expand Down
67 changes: 32 additions & 35 deletions docs/user/02-configuration.md

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions internal/controller/nats/deprovisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"context"
"fmt"

"github.com/kyma-project/nats-manager/pkg/events"

natsv1alpha1 "github.com/kyma-project/nats-manager/api/v1alpha1"
natspkg "github.com/kyma-project/nats-manager/pkg/nats"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

natsv1alpha1 "github.com/kyma-project/nats-manager/api/v1alpha1"
natsurl "github.com/kyma-project/nats-manager/internal/controller/nats/url"
"github.com/kyma-project/nats-manager/pkg/events"
natspkg "github.com/kyma-project/nats-manager/pkg/nats"
)

const (
StreamExistsErrorMsg = "Cannot delete NATS cluster as customer stream exists"
ConsumerExistsErrorMsg = "Cannot delete NATS cluster as stream consumer exists"
natsClientPort = 4222
InstanceLabelKey = "app.kubernetes.io/instance"
SapStreamName = "sap"
)
Expand Down Expand Up @@ -109,7 +109,7 @@ func (r *Reconciler) createAndConnectNatsClient(nats *natsv1alpha1.NATS) error {
// create a new instance if it does not exist.
if r.getNatsClient(nats) == nil {
r.setNatsClient(nats, natspkg.NewNatsClient(&natspkg.Config{
URL: fmt.Sprintf("nats://%s.%s.svc.cluster.local:%d", nats.Name, nats.Namespace, natsClientPort),
URL: natsurl.Format(nats.Name, nats.Namespace),
}))
}
return r.getNatsClient(nats).Init()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func Test_CreateNATSCR(t *testing.T) {
testEnvironment.EnsureNATSSpecMemStorageReflected(t, *tc.givenNATS)
testEnvironment.EnsureNATSSpecFileStorageReflected(t, *tc.givenNATS)
}

// check the url in the NATS CR status
testEnvironment.EnsureURLInNATSStatus(t, tc.givenNATS.Name, givenNamespace)
})
}
}
Expand Down Expand Up @@ -264,6 +267,9 @@ func Test_UpdateNATSCR(t *testing.T) {
testEnvironment.EnsureK8sStatefulSetHasAnnotations(t, testutils.GetStatefulSetName(*tc.givenNATS),
givenNamespace, tc.givenUpdateNATS.Spec.Annotations)
testEnvironment.EnsureNATSSpecMemStorageReflected(t, *tc.givenUpdateNATS)

// check the url in the NATS CR status
testEnvironment.EnsureURLInNATSStatus(t, tc.givenNATS.Name, givenNamespace)
})
}
}
Expand Down Expand Up @@ -590,6 +596,9 @@ func Test_DoubleReconcileNATSCR(t *testing.T) {

// check NATS CR status again.
testEnvironment.GetNATSAssert(g, tc.givenNATS).Should(tc.wantMatchers)

// check the url in the NATS CR status
testEnvironment.EnsureURLInNATSStatus(t, tc.givenNATS.Name, givenNamespace)
})
}
}
Expand Down
13 changes: 9 additions & 4 deletions internal/controller/nats/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"time"

"github.com/kyma-project/nats-manager/pkg/events"

natsv1alpha1 "github.com/kyma-project/nats-manager/api/v1alpha1"
"github.com/kyma-project/nats-manager/pkg/k8s/chart"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"

natsv1alpha1 "github.com/kyma-project/nats-manager/api/v1alpha1"
natsurl "github.com/kyma-project/nats-manager/internal/controller/nats/url"
"github.com/kyma-project/nats-manager/pkg/events"
"github.com/kyma-project/nats-manager/pkg/k8s/chart"
)

const RequeueTimeForStatusCheck = 10
Expand Down Expand Up @@ -66,6 +67,9 @@ func (r *Reconciler) handleNATSReconcile(ctx context.Context,
// It also syncs the NATS CR status.
func (r *Reconciler) handleNATSState(ctx context.Context, nats *natsv1alpha1.NATS, instance *chart.ReleaseInstance,
log *zap.SugaredLogger) (ctrl.Result, error) {
// Clear the url until the StatefulSet is ready.
nats.Status.ClearURL()

// checking if statefulSet is ready.
isSTSReady, err := r.natsManager.IsNATSStatefulSetReady(ctx, instance)
if err != nil {
Expand All @@ -78,6 +82,7 @@ func (r *Reconciler) handleNATSState(ctx context.Context, nats *natsv1alpha1.NAT

if isSTSReady {
nats.Status.SetStateReady()
nats.Status.SetURL(natsurl.Format(nats.Name, nats.Namespace))
events.Normal(r.recorder, nats, natsv1alpha1.ConditionReasonDeployed, "StatefulSet is ready and NATS is deployed.")
} else {
nats.Status.SetWaitingStateForStatefulSet()
Expand Down
15 changes: 15 additions & 0 deletions internal/controller/nats/url/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package url

import (
"fmt"
)

const (
format = "%s://%s.%s.svc.cluster.local:%d"
protocol = "nats"
port = 4222
)

func Format(name, namespace string) string {
return fmt.Sprintf(format, protocol, name, namespace, port)
}
38 changes: 38 additions & 0 deletions internal/controller/nats/url/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package url

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestFormat(t *testing.T) {
// given
type args struct {
name string
namespace string
}
tests := []struct {
name string
args args
want string
}{
{
name: "should return the correct nats url",
args: args{
name: "test-name",
namespace: "test-namespace",
},
want: "nats://test-name.test-namespace.svc.cluster.local:4222",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// when
got := Format(tt.args.name, tt.args.namespace)

// then
require.Equal(t, tt.want, got)
})
}
}
19 changes: 19 additions & 0 deletions testutils/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,25 @@ func (env TestEnvironment) EnsureNATSSpecMemStorageReflected(t *testing.T, nats
}, SmallTimeOut, SmallPollingInterval, "failed to ensure NATS CR Spec.jetStream.memStorage")
}

func (env TestEnvironment) EnsureURLInNATSStatus(t *testing.T, name, namespace string) {
natsCR, err := env.GetNATSFromK8s(name, namespace)
require.NoError(t, err)
require.NotNil(t, natsCR)

switch natsCR.Status.State {
case natsv1alpha1.StateReady:
{
wantURL := fmt.Sprintf("nats://%s.%s.svc.cluster.local:4222", natsCR.Name, natsCR.Namespace)
require.Equal(t, wantURL, natsCR.Status.URL)
}
default:
{
const wantURL = ""
require.Equal(t, wantURL, natsCR.Status.URL)
}
}
}

func (env TestEnvironment) GetNATSFromK8s(name, namespace string) (natsv1alpha1.NATS, error) {
var nats natsv1alpha1.NATS
err := env.k8sClient.Get(env.Context, k8stypes.NamespacedName{
Expand Down