Skip to content

Commit

Permalink
Use semver on the upgrade process (#1034)
Browse files Browse the repository at this point in the history
* Use semver on the upgrade process

Signed-off-by: Ruben Vargas <[email protected]>

* More tests

Signed-off-by: Ruben Vargas <[email protected]>

* Simplify upgrade ManagedInstance function, address comments

Signed-off-by: Ruben Vargas <[email protected]>

* Address some small changes

Signed-off-by: Ruben Vargas <[email protected]>

* Create function that parse semver and sort it

Signed-off-by: Ruben Vargas <[email protected]>

* Validate to upper versions and update tests

Signed-off-by: Ruben Vargas <[email protected]>

* Improve logging messages

Signed-off-by: Ruben Vargas <[email protected]>

* Change version to initialized on version package init

Signed-off-by: Ruben Vargas <[email protected]>

* Panic on semver parsing failure, more log messages improvments

Signed-off-by: Ruben Vargas <[email protected]>

* Move some stuff to versions main.go, added tests

Signed-off-by: Ruben Vargas <[email protected]>

* Change one log field name

Signed-off-by: Ruben Vargas <[email protected]>

* More movements, little improvments

Signed-off-by: Ruben Vargas <[email protected]>
  • Loading branch information
rubenvp8510 authored Apr 29, 2020
1 parent d51756e commit db31a0c
Show file tree
Hide file tree
Showing 22 changed files with 247 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ endif
.PHONY: unit-tests
unit-tests:
@echo Running unit tests...
@go test $(VERBOSE) $(UNIT_TEST_PACKAGES) -cover -coverprofile=cover.out
@go test $(VERBOSE) $(UNIT_TEST_PACKAGES) -cover -coverprofile=cover.out -ldflags $(LD_FLAGS)

.PHONY: e2e-tests
e2e-tests: prepare-e2e-tests e2e-tests-smoke e2e-tests-cassandra e2e-tests-es e2e-tests-self-provisioned-es e2e-tests-streaming e2e-tests-examples1 e2e-tests-examples2 e2e-tests-examples-openshift
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/jaegertracing/jaeger-operator
go 1.13

require (
github.com/Masterminds/semver v1.5.0
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/go-openapi/spec v0.19.4
github.com/googleapis/gnostic v0.3.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ github.com/JeffAshton/win_pdh v0.0.0-20161109143554-76bb4ee9f0ab/go.mod h1:3VYc5
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E=
github.com/MakeNowJust/heredoc v0.0.0-20171113091838-e9091a26100e/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.0.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/sprig/v3 v3.0.0/go.mod h1:NEUY/Qq8Gdm2xgYA+NwJM6wmfdRV9xkh8h/Rld20R0U=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/start/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func performUpgrades(ctx context.Context, mgr manager.Manager) {
defer span.End()

// upgrades all the instances managed by this operator
if err := upgrade.ManagedInstances(ctx, mgr.GetClient(), mgr.GetAPIReader()); err != nil {
if err := upgrade.ManagedInstances(ctx, mgr.GetClient(), mgr.GetAPIReader(), version.Get().Jaeger); err != nil {
log.WithError(err).Warn("failed to upgrade managed instances")
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/jaeger/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (r *ReconcileJaeger) applyUpgrades(ctx context.Context, jaeger v1.Jaeger) (
if jaeger.Status.Version != currentVersions.Jaeger {
// in theory, the version from the Status could be higher than currentVersions.Jaeger, but we let the upgrade routine
// check/handle it
upgraded, err := upgrade.ManagedInstance(ctx, r.client, jaeger)
upgraded, err := upgrade.ManagedInstance(ctx, r.client, jaeger, currentVersions.Jaeger)
if err != nil {
return jaeger, tracing.HandleError(err, span)
}
Expand All @@ -32,6 +32,6 @@ func (r *ReconcileJaeger) applyUpgrades(ctx context.Context, jaeger v1.Jaeger) (

// at this point, the Jaeger we are managing is in sync with the Operator's version
// if this is a new object, no upgrade was made, so, we just set the version
jaeger.Status.Version = version.Get().Jaeger
jaeger.Status.Version = currentVersions.Jaeger
return jaeger, nil
}
6 changes: 4 additions & 2 deletions pkg/cronjob/es_index_cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cronjob
import (
"testing"

"github.com/jaegertracing/jaeger-operator/pkg/version"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -201,7 +203,7 @@ func TestDefaultEsIndexCleanerImage(t *testing.T) {

cjob := CreateEsIndexCleaner(jaeger)
assert.Empty(t, jaeger.Spec.Storage.EsIndexCleaner.Image)
assert.Equal(t, "jaegertracing/jaeger-es-index-cleaner:0.0.0", cjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, "jaegertracing/jaeger-es-index-cleaner:"+version.Get().Jaeger, cjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image)
}

func TestCustomEsIndexCleanerImage(t *testing.T) {
Expand All @@ -215,5 +217,5 @@ func TestCustomEsIndexCleanerImage(t *testing.T) {

cjob := CreateEsIndexCleaner(jaeger)
assert.Empty(t, jaeger.Spec.Storage.EsIndexCleaner.Image)
assert.Equal(t, "org/custom-es-index-cleaner-image:0.0.0", cjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, "org/custom-es-index-cleaner-image:"+version.Get().Jaeger, cjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image)
}
6 changes: 4 additions & 2 deletions pkg/cronjob/es_rollover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"
"time"

"github.com/jaegertracing/jaeger-operator/pkg/version"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -333,7 +335,7 @@ func TestDefaultEsRolloverImage(t *testing.T) {

cjob := lookback(jaeger)
assert.Empty(t, jaeger.Spec.Storage.EsRollover.Image)
assert.Equal(t, "jaegertracing/jaeger-es-rollover:0.0.0", cjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, "jaegertracing/jaeger-es-rollover:"+version.Get().Jaeger, cjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image)
}

func TestCustomEsRolloverImage(t *testing.T) {
Expand All @@ -344,5 +346,5 @@ func TestCustomEsRolloverImage(t *testing.T) {

cjob := lookback(jaeger)
assert.Empty(t, jaeger.Spec.Storage.EsRollover.Image)
assert.Equal(t, "org/custom-es-rollover-image:0.0.0", cjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, "org/custom-es-rollover-image:"+version.Get().Jaeger, cjob.Spec.JobTemplate.Spec.Template.Spec.Containers[0].Image)
}
4 changes: 3 additions & 1 deletion pkg/deployment/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"k8s.io/apimachinery/pkg/types"

v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/version"

"github.com/jaegertracing/jaeger-operator/pkg/util"
)

Expand Down Expand Up @@ -46,7 +48,7 @@ func TestDefaultAgentImage(t *testing.T) {

d := NewAgent(jaeger).Get()
assert.Empty(t, jaeger.Spec.Agent.Image)
assert.Equal(t, "org/custom-agent-image:0.0.0", d.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, "org/custom-agent-image:"+version.Get().Jaeger, d.Spec.Template.Spec.Containers[0].Image)
}

func TestGetDefaultAgentDeployment(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/deployment/all_in_one_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package deployment
import (
"testing"

v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/version"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"

v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/util"
)

Expand All @@ -26,7 +28,7 @@ func TestDefaultAllInOneImage(t *testing.T) {

assert.Len(t, d.Spec.Template.Spec.Containers, 1)
assert.Empty(t, jaeger.Spec.AllInOne.Image)
assert.Equal(t, "org/custom-all-in-one-image:0.0.0", d.Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, "org/custom-all-in-one-image:"+version.Get().Jaeger, d.Spec.Template.Spec.Containers[0].Image)

envvars := []corev1.EnvVar{
{
Expand Down
4 changes: 3 additions & 1 deletion pkg/deployment/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"testing"

"github.com/jaegertracing/jaeger-operator/pkg/version"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -71,7 +73,7 @@ func TestDefaultCollectorImage(t *testing.T) {
containers := dep.Spec.Template.Spec.Containers
assert.Len(t, containers, 1)
assert.Empty(t, jaeger.Spec.Collector.Image)
assert.Equal(t, "org/custom-collector-image:0.0.0", containers[0].Image)
assert.Equal(t, "org/custom-collector-image:"+version.Get().Jaeger, containers[0].Image)

envvars := []corev1.EnvVar{
{
Expand Down
4 changes: 3 additions & 1 deletion pkg/deployment/ingester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"testing"

"github.com/jaegertracing/jaeger-operator/pkg/version"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -72,7 +74,7 @@ func TestDefaultIngesterImage(t *testing.T) {
containers := dep.Spec.Template.Spec.Containers
assert.Len(t, containers, 1)
assert.Empty(t, jaeger.Spec.Ingester.Image)
assert.Equal(t, "org/custom-ingester-image:0.0.0", containers[0].Image)
assert.Equal(t, "org/custom-ingester-image:"+version.Get().Jaeger, containers[0].Image)

envvars := []corev1.EnvVar{
{
Expand Down
4 changes: 3 additions & 1 deletion pkg/deployment/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"testing"

"github.com/jaegertracing/jaeger-operator/pkg/version"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -57,7 +59,7 @@ func TestDefaultQueryImage(t *testing.T) {

assert.Len(t, containers, 1)
assert.Empty(t, jaeger.Spec.Query.Image)
assert.Equal(t, "org/custom-query-image:0.0.0", containers[0].Image)
assert.Equal(t, "org/custom-query-image:"+version.Get().Jaeger, containers[0].Image)
}

func TestQueryAnnotations(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/storage/cassandra_dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package storage
import (
"testing"

"github.com/jaegertracing/jaeger-operator/pkg/version"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -29,7 +31,7 @@ func TestDefaultImage(t *testing.T) {
b := cassandraDeps(jaeger)
assert.Len(t, b, 1)
assert.Len(t, b[0].Spec.Template.Spec.Containers, 1)
assert.Equal(t, "jaegertracing/theimage:0.0.0", b[0].Spec.Template.Spec.Containers[0].Image)
assert.Equal(t, "jaegertracing/theimage:"+version.Get().Jaeger, b[0].Spec.Template.Spec.Containers[0].Image)
}

func TestCassandraCreateSchemaDisabled(t *testing.T) {
Expand Down
43 changes: 43 additions & 0 deletions pkg/upgrade/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package upgrade

import (
"sort"

"github.com/Masterminds/semver"
)

var (
semanticVersions []*semver.Version
startUpdatesVersion *semver.Version
)

func init() {
parseSemVer()
}

func parseSemVer() {
semvers, err := versions(upgrades)
if err != nil {
panic(err)
}
semanticVersions = semvers
startUpdatesVersion = semver.MustParse("1.11.0")
}

// Versions return the list of semantic version sorted
func versions(versions map[string]upgradeFunction) ([]*semver.Version, error) {
versionLists := make([]*semver.Version, len(versions))
versionIndex := 0
for v := range versions {
semv, err := semver.NewVersion(v)
if err != nil {
return nil, err
}
versionLists[versionIndex] = semv
versionIndex++
}

// apply the updates in order
sort.Sort(semver.Collection(versionLists))
return versionLists, nil
}
52 changes: 52 additions & 0 deletions pkg/upgrade/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package upgrade

import (
"context"
"testing"

"github.com/Masterminds/semver"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/stretchr/testify/assert"

v1 "github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
)

func noop(ctx context.Context, client client.Client, jaeger v1.Jaeger) (v1.Jaeger, error) {
return jaeger, nil
}

func TestVersions(t *testing.T) {
maptoTest := map[string]upgradeFunction{
"1.17.0": noop,
"1.15.4": noop,
"1.15.0": noop,
"1.16.1": noop,
"1.12.2": noop,
}
sortedSemVersions := []*semver.Version{
semver.MustParse("1.12.2"),
semver.MustParse("1.15.0"),
semver.MustParse("1.15.4"),
semver.MustParse("1.16.1"),
semver.MustParse("1.17.0"),
}

semVersions, err := versions(maptoTest)
assert.NoError(t, err)
assert.Equal(t, semVersions, sortedSemVersions)
}

func TestVersionsError(t *testing.T) {
maptoTest := map[string]upgradeFunction{
"1.17.0": noop,
"1.15.4": noop,
"1.15.0": noop,
"1,16.1": noop, // Bad format, coma instead of point
"1.12.2": noop,
}

_, err := versions(maptoTest)
assert.Error(t, err)
}
Loading

0 comments on commit db31a0c

Please sign in to comment.