Skip to content

Commit

Permalink
Update to use NATS v2 image by default
Browse files Browse the repository at this point in the history
Signed-off-by: Waldemar Quevedo <wally@synadia.com>
  • Loading branch information
wallyqs committed Jun 14, 2019
1 parent 7ecd1fc commit 428b2ee
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 15 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
}
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
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 428b2ee

Please sign in to comment.