Skip to content

Commit

Permalink
Merge pull request #195 from nats-io/nats-v2-default
Browse files Browse the repository at this point in the history
Update to use NATS v2 image by default
  • Loading branch information
wallyqs authored Jun 14, 2019
2 parents 7ecd1fc + 0470f2d commit 6f3e8b2
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 25 deletions.
12 changes: 7 additions & 5 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/nats-io/nats-operator/pkg/debug"
kubernetesutil "github.com/nats-io/nats-operator/pkg/util/kubernetes"
stringutil "github.com/nats-io/nats-operator/pkg/util/strings"
"github.com/nats-io/nats-operator/pkg/util/versionCheck"
)

var (
Expand Down Expand Up @@ -354,8 +355,8 @@ func (c *Cluster) createPod() (*v1.Pod, error) {
// It does this by trying to make the "gnatsd" process enter the "lame duck" mode before actually attempting to delete the pod.
// This is done in a best-effort basis, since the NATS version running in the pod may not support this mode.
// For that reason, we just log any errors without actually failing and proceed to the actual deletion of the pod.
func (c *Cluster) tryGracefulPodDeletion(pod *v1.Pod) error {
if err := c.enterLameDuckModeAndWaitTermination(pod); err != nil {
func (c *Cluster) tryGracefulPodDeletion(pod *v1.Pod, version string) error {
if err := c.enterLameDuckModeAndWaitTermination(pod, version); err != nil {
c.logger.Warn(err)
}
return c.deletePod(pod)
Expand Down Expand Up @@ -522,13 +523,14 @@ func (c *Cluster) isDebugLoggerEnabled() bool {
// enterLameDuckModeAndWaitTermination execs into the "nats" container of the specified pod and attempts to send the "ldm" signal to the "gnatsd" process.
// In case this succeeds, the funcion blocks until the "nats" container reaches the "Terminated" state (indicating that the "lame duck" mode has been entered and NATS is ready to shutdown) or until a timeout is reached.
// Otherwise, it returns an error which should be handled by the caller.
func (c *Cluster) enterLameDuckModeAndWaitTermination(pod *v1.Pod) error {
// Try to place NATS in "lame duck" mode by sending the "gnatsd" process the "ldm" signal.
func (c *Cluster) enterLameDuckModeAndWaitTermination(pod *v1.Pod, version string) error {
// Try to place NATS in "lame duck" mode by sending the process the "ldm" signal.
// We wait for at most "podExecTimeout" for the "exec" command to return a result.
ctx, fn := context.WithTimeout(context.Background(), podExecTimeout)
defer fn()

args := []string{
constants.NatsBinaryPath,
versionCheck.ServerBinaryPath(version),
"-sl",
fmt.Sprintf("ldm=%s", constants.PidFilePath),
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *Cluster) reconcileSize() error {
// Remove extra pods as required in order to meet the desired size.
// As we remove each pod, we must update the config secret so that routes are re-computed.
for idx := currentSize - 1; idx >= desiredSize; idx-- {
if err := c.tryGracefulPodDeletion(pods[idx]); err != nil {
if err := c.tryGracefulPodDeletion(pods[idx], c.cluster.Spec.Version); err != nil {
return err
}
if err := c.updateConfigSecret(); err != nil {
Expand Down Expand Up @@ -94,7 +94,7 @@ func (c *Cluster) reconcileVersion() error {
for _, pod := range pods {
if kubernetesutil.GetNATSVersion(pod) != c.cluster.Spec.Version {
c.maybeUpgradeMgmtService()
return c.upgradePod(pod)
return c.upgradePod(pod, desiredVersion)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
// In order to do that, we first try to make NATS enter the "lame duck" mode.
// If we succeed, we adopt a special upgrade procedure since the pod (or at least its "nats" container) will have been terminated and can't be upgraded directly.
// If we fail, we stick to the usual method of upgrading the container's "image" field to the desired version.
func (c *Cluster) upgradePod(pod *v1.Pod) error {
if err := c.enterLameDuckModeAndWaitTermination(pod); err != nil {
func (c *Cluster) upgradePod(pod *v1.Pod, version string) error {
if err := c.enterLameDuckModeAndWaitTermination(pod, version); err != nil {
c.logger.Warn(err)
return c.upgradeRunningPod(pod)
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package constants

const (
// DefaultNatsVersion is the nats server version to use.
DefaultNatsVersion = "1.4.0"
DefaultNatsVersion = "2.0.0"

// ClientPort is the port for the clients.
ClientPort = 4222
Expand Down Expand Up @@ -58,7 +58,7 @@ const (
PidFileVolumeName = "pid"

// PidFileName is the pid file name.
PidFileName = "gnatsd.pid"
PidFileName = "nats.pid"

// PidFileMountPath is the absolute path to the directory where NATS
// will be leaving its pid file.
Expand Down Expand Up @@ -128,8 +128,6 @@ const (
DefaultBootconfigImage = "connecteverything/nats-boot-config"
DefaultBootconfigImageTag = "0.5.2"

// NatsBinaryPath is the path to the NATS binary inside the main container.
NatsBinaryPath = "/gnatsd"
// NatsContainerName is the name of the main container.
NatsContainerName = "nats"

Expand Down
3 changes: 2 additions & 1 deletion pkg/util/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/nats-io/nats-operator/pkg/conf"
"github.com/nats-io/nats-operator/pkg/constants"
"github.com/nats-io/nats-operator/pkg/util/retryutil"
"github.com/nats-io/nats-operator/pkg/util/versionCheck"
)

const (
Expand Down Expand Up @@ -920,7 +921,7 @@ func NewNatsPodSpec(namespace, name, clusterName string, cs v1alpha2.ClusterSpec
// Rely on the shared configuration map for configuring the cluster.
retries := strconv.Itoa(constants.ConnectRetries)
cmd := []string{
constants.NatsBinaryPath,
versionCheck.ServerBinaryPath(cs.Version),
"-c",
constants.ConfigFilePath,
"-P",
Expand Down
29 changes: 29 additions & 0 deletions pkg/util/versionCheck/versionCheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package versionCheck

import (
"strconv"
"strings"
)

const (
// OldNatsBinaryPath is the path to the NATS binary inside the
// main container, before NATS Server v2.
OldNatsBinaryPath = "/gnatsd"

// NatsBinaryPath after v2 release.
NatsBinaryPath = "/nats-server"
)

func ServerBinaryPath(version string) string {
v := strings.Split(version, ".")
if len(v) > 0 {
majorVersion, err := strconv.Atoi(v[0])
if err != nil {
return NatsBinaryPath
}
if majorVersion < 2 {
return OldNatsBinaryPath
}
}
return NatsBinaryPath
}
37 changes: 37 additions & 0 deletions pkg/util/versionCheck/versionCheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package versionCheck

import (
"testing"
)

func TestServerBinaryPath(t *testing.T) {
got := ServerBinaryPath("2.0.0")
expected := NatsBinaryPath
if got != expected {
t.Fatalf("Expected %q, got: %q", expected, got)
}

got = ServerBinaryPath("2.1.8")
expected = NatsBinaryPath
if got != expected {
t.Fatalf("Expected %q, got: %q", expected, got)
}

got = ServerBinaryPath("2.1.8-RC18")
expected = NatsBinaryPath
if got != expected {
t.Fatalf("Expected %q, got: %q", expected, got)
}

got = ServerBinaryPath("0.1.8")
expected = OldNatsBinaryPath
if got != expected {
t.Fatalf("Expected %q, got: %q", expected, got)
}

got = ServerBinaryPath("1.4.1")
expected = OldNatsBinaryPath
if got != expected {
t.Fatalf("Expected %q, got: %q", expected, got)
}
}
31 changes: 31 additions & 0 deletions test/e2e/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,37 @@ func TestCreateCluster(t *testing.T) {
}
}

// TestCreateClusterV2 creates a NatsCluster resource and waits for the full mesh to be formed.
func TestCreateClusterV2(t *testing.T) {
var (
size = 3
version = "2.0.0"
)

var (
natsCluster *natsv1alpha2.NatsCluster
err error
)

// Create a NatsCluster resource with three members.
if natsCluster, err = f.CreateCluster(f.Namespace, "test-nats-", size, version); err != nil {
t.Fatal(err)
}
// Make sure we cleanup the NatsCluster resource after we're done testing.
defer func() {
if err = f.DeleteCluster(natsCluster); err != nil {
t.Error(err)
}
}()

// Wait until the full mesh is formed.
ctx, fn := context.WithTimeout(context.Background(), waitTimeout)
defer fn()
if err = f.WaitUntilFullMeshWithVersion(ctx, natsCluster, size, version); err != nil {
t.Fatal(err)
}
}

// TestPauseControl creates a NatsCluster resource and waits for the full mesh to be formed.
// Then, it pauses control of the NatsCluster resource and scales it up to five nodes, expecting the operation to NOT be performed.
// Finally, it resumes control of the NatsCluster resource and waits for the full five-node mesh to be formed.
Expand Down
6 changes: 2 additions & 4 deletions test/e2e/lame_duck_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ func TestLameDuckModeWhenScalingDown(t *testing.T) {
var (
initialSize = 3
finalSize = 1
// TODO Replace with an adequate stable tag once there is one.
version = "5d86964"
// TODO Remove once the "nats" image has an adequate stable tag.
serverImage = "natsop2018/gnatsd"
serverImage = "synadia/nats-server"
version = "2.0.0"
)

var (
Expand Down
6 changes: 2 additions & 4 deletions test/e2e/super_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ import (
func TestCreateServerWithGateways(t *testing.T) {
var (
size = 1
image = "synadia/nats-server"
version = "edge-v2.0.0-RC12"
version = "2.0.0"
nc *natsv1alpha2.NatsCluster
err error
)
nc, err = f.CreateCluster(f.Namespace, "", size, version,
func(cluster *natsv1alpha2.NatsCluster) {
cluster.Name = "test-nats-gw"
cluster.Spec.ServerImage = image

cluster.Spec.ServerConfig = &natsv1alpha2.ServerConfig{
Debug: true,
Expand Down Expand Up @@ -138,7 +136,7 @@ func TestCreateServerWithGatewayAndLeafnodes(t *testing.T) {
var (
size = 1
image = "synadia/nats-server"
version = "edge-v2.0.0-RC12"
version = "2.0.0"
nc *natsv1alpha2.NatsCluster
err error

Expand Down
3 changes: 1 addition & 2 deletions test/e2e/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ func TestCreateClusterWithVerifyAndMap(t *testing.T) {
// The NatsCluster resource must be called "nats" in
// order for the pre-provisioned certificates to work.
natsCluster.Name = "nats-verify"
natsCluster.Spec.ServerImage = "wallyqs/nats-server"
natsCluster.Spec.Version = "edge-2.0.0-RC5"
natsCluster.Spec.Version = "2.0.0"

// Enable TLS using pre-provisioned certificates.
natsCluster.Spec.TLS = &natsv1alpha2.TLSConfig{
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
package version

var (
OperatorVersion = "0.5.0-v1alpha2+git"
OperatorVersion = "0.6.0-v1alpha2+git"
GitSHA = "Not provided"
)

0 comments on commit 6f3e8b2

Please sign in to comment.