Skip to content

Commit

Permalink
fix: use hash as revision instead of generation number (#1243)
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
odubajDT committed Apr 18, 2023
1 parent a84cbc7 commit 2ad5d81
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 35 deletions.
10 changes: 10 additions & 0 deletions operator/apis/lifecycle/v1alpha3/common/common.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package common

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"math/rand"
"strconv"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric/instrument/syncfloat64"
Expand Down Expand Up @@ -120,6 +123,13 @@ func TruncateString(s string, max int) string {
return s
}

func Hash(num int64) string {
// generate the SHA-256 hash of the bytes
hash := sha256.Sum256([]byte(strconv.FormatInt(num, 10)))
// take the first 4 bytes of the hash and convert to hex
return hex.EncodeToString(hash[:4])
}

type CheckType string

const PreDeploymentCheckType CheckType = "pre"
Expand Down
21 changes: 21 additions & 0 deletions operator/apis/lifecycle/v1alpha3/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ func TestKeptnState_IsFailed(t *testing.T) {
}
}

func TestHash(t *testing.T) {
tests := []struct {
in int64
out string
}{
{
in: int64(1),
out: "6b86b273",
},
{
in: int64(2),
out: "d4735e3a",
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
require.Equal(t, tt.out, Hash(tt.in))
})
}
}

func TestKeptnState_IsDeprecated(t *testing.T) {
tests := []struct {
State KeptnState
Expand Down
6 changes: 3 additions & 3 deletions operator/apis/lifecycle/v1alpha3/keptnapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func TestKeptnApp(t *testing.T) {
}

appVersionName := app.GetAppVersionName()
require.Equal(t, "app-version-1", appVersionName)
require.Equal(t, "app-version-6b86b273", appVersionName)

appVersion := app.GenerateAppVersion("prev", map[string]string{})
require.Equal(t, KeptnAppVersion{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
Name: "app-version-1",
Name: "app-version-6b86b273",
Namespace: "namespace",
},
Spec: KeptnAppVersionSpec{
Expand All @@ -48,6 +48,6 @@ func TestKeptnApp(t *testing.T) {
require.Equal(t, map[string]string{
"appName": "app",
"appVersion": "version",
"appRevision": "1",
"appRevision": "6b86b273",
}, app.GetEventAnnotations())
}
5 changes: 2 additions & 3 deletions operator/apis/lifecycle/v1alpha3/keptnapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package v1alpha3

import (
"fmt"
"strconv"
"strings"

"github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3/common"
Expand Down Expand Up @@ -79,7 +78,7 @@ func init() {
}

func (a KeptnApp) GetAppVersionName() string {
return strings.ToLower(fmt.Sprintf("%s-%s-%d", a.Name, a.Spec.Version, a.Generation))
return strings.ToLower(fmt.Sprintf("%s-%s-%s", a.Name, a.Spec.Version, common.Hash(a.Generation)))
}

func (a KeptnApp) SetSpanAttributes(span trace.Span) {
Expand Down Expand Up @@ -112,6 +111,6 @@ func (a KeptnApp) GetEventAnnotations() map[string]string {
return map[string]string{
"appName": a.Name,
"appVersion": a.Spec.Version,
"appRevision": strconv.FormatInt(a.Generation, 10),
"appRevision": common.Hash(a.Generation),
}
}
2 changes: 1 addition & 1 deletion operator/controllers/common/helperfunctions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func Test_setAnnotations(t *testing.T) {
"phase": "AppDeploy",
"appName": "app",
"appVersion": "1.0.0",
"appRevision": "1",
"appRevision": "6b86b273",
"traceparent": "",
},
},
Expand Down
5 changes: 2 additions & 3 deletions operator/controllers/lifecycle/keptnapp/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package keptnapp
import (
"context"
"fmt"
"strconv"

"github.com/go-logr/logr"
klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3"
Expand Down Expand Up @@ -186,9 +185,9 @@ func (r *KeptnAppReconciler) deprecateAppVersions(ctx context.Context, app *klcv
lastResultErr = nil
for i := app.Generation - 1; i > 0; i-- {
deprecatedAppVersion := &klcv1alpha3.KeptnAppVersion{}
if err := r.Get(ctx, types.NamespacedName{Namespace: app.Namespace, Name: app.Name + "-" + app.Spec.Version + "-" + strconv.FormatInt(i, 10)}, deprecatedAppVersion); err != nil {
if err := r.Get(ctx, types.NamespacedName{Namespace: app.Namespace, Name: app.Name + "-" + app.Spec.Version + "-" + common.Hash(i)}, deprecatedAppVersion); err != nil {
if !errors.IsNotFound(err) {
r.Log.Error(err, fmt.Sprintf("Could not get KeptnAppVersion: %s", app.Name+"-"+app.Spec.Version+"-"+strconv.FormatInt(i, 10)))
r.Log.Error(err, fmt.Sprintf("Could not get KeptnAppVersion: %s", app.Name+"-"+app.Spec.Version+"-"+common.Hash(i)))
lastResultErr = err
}
} else if !deprecatedAppVersion.Status.Status.IsDeprecated() {
Expand Down
14 changes: 7 additions & 7 deletions operator/controllers/lifecycle/keptnapp/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestKeptnAppReconciler_createAppVersionSuccess(t *testing.T) {
}
t.Log("Verifying created app")
assert.Equal(t, appVersion.Namespace, app.Namespace)
assert.Equal(t, appVersion.Name, fmt.Sprintf("%s-%s-%d", app.Name, app.Spec.Version, app.Generation))
assert.Equal(t, appVersion.Name, fmt.Sprintf("%s-%s-%s", app.Name, app.Spec.Version, apicommon.Hash(app.Generation)))
}

func TestKeptnAppReconciler_reconcile(t *testing.T) {
Expand All @@ -66,7 +66,7 @@ func TestKeptnAppReconciler_reconcile(t *testing.T) {
},
},
wantErr: nil,
event: `Normal CreateAppVersionAppVersionCreated Create AppVersion: created KeptnAppVersion / Namespace: default, Name: myapp-1.0.0-1, Version: 1.0.0`,
event: `Normal CreateAppVersionAppVersionCreated Create AppVersion: created KeptnAppVersion / Namespace: default, Name: myapp-1.0.0-6b86b273, Version: 1.0.0`,
},
{
name: "test simple notfound should not return error nor event",
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestKeptnAppReconciler_reconcile(t *testing.T) {
require.Nil(t, err)
err = controllercommon.AddApp(r.Client, "myfinishedapp")
require.Nil(t, err)
err = controllercommon.AddAppVersion(r.Client, "default", "myfinishedapp", "1.0.0-1", nil, lfcv1alpha3.KeptnAppVersionStatus{Status: apicommon.StateSucceeded})
err = controllercommon.AddAppVersion(r.Client, "default", "myfinishedapp", "1.0.0-6b86b273", nil, lfcv1alpha3.KeptnAppVersionStatus{Status: apicommon.StateSucceeded})
require.Nil(t, err)

for _, tt := range tests {
Expand All @@ -121,7 +121,7 @@ func TestKeptnAppReconciler_reconcile(t *testing.T) {
// case 1 reconcile and create app ver
assert.Equal(t, tracer.StartCalls()[0].SpanName, "reconcile_app")
assert.Equal(t, tracer.StartCalls()[1].SpanName, "create_app_version")
assert.Equal(t, tracer.StartCalls()[2].SpanName, "myapp-1.0.0-1")
assert.Equal(t, tracer.StartCalls()[2].SpanName, "myapp-1.0.0-6b86b273")
//case 2 creates no span because notfound
//case 3 reconcile finished crd
assert.Equal(t, tracer.StartCalls()[3].SpanName, "reconcile_app")
Expand All @@ -143,7 +143,7 @@ func TestKeptnAppReconciler_deprecateAppVersions(t *testing.T) {
require.Nil(t, err)

event := <-eventChannel
assert.Matches(t, event, `Normal CreateAppVersionAppVersionCreated Create AppVersion: created KeptnAppVersion / Namespace: default, Name: myapp-1.0.0-1, Version: 1.0.0`)
assert.Matches(t, event, `Normal CreateAppVersionAppVersionCreated Create AppVersion: created KeptnAppVersion / Namespace: default, Name: myapp-1.0.0-6b86b273, Version: 1.0.0`)

err = controllercommon.UpdateAppRevision(r.Client, "myapp", 2)
require.Nil(t, err)
Expand All @@ -158,10 +158,10 @@ func TestKeptnAppReconciler_deprecateAppVersions(t *testing.T) {
require.Nil(t, err)

event = <-eventChannel
assert.Matches(t, event, `Normal CreateAppVersionAppVersionCreated Create AppVersion: created KeptnAppVersion / Namespace: default, Name: myapp-1.0.0-2, Version: 1.0.0`)
assert.Matches(t, event, `Normal CreateAppVersionAppVersionCreated Create AppVersion: created KeptnAppVersion / Namespace: default, Name: myapp-1.0.0-d4735e3a, Version: 1.0.0`)

event = <-eventChannel
assert.Matches(t, event, `Normal CreateAppVersionAppVersionDeprecated Create AppVersion: deprecated KeptnAppVersions for KeptnAppVersion: myapp-1.0.0-2 / Namespace: default, Name: myapp, Version: 1.0.0`)
assert.Matches(t, event, `Normal CreateAppVersionAppVersionDeprecated Create AppVersion: deprecated KeptnAppVersions for KeptnAppVersion: myapp-1.0.0-d4735e3a / Namespace: default, Name: myapp, Version: 1.0.0`)
}

func setupReconciler() (*KeptnAppReconciler, chan string, *fake.ITracerMock) {
Expand Down
2 changes: 1 addition & 1 deletion operator/test/component/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func assertAppSpan(instance *klcv1alpha3.KeptnApp, spanRecorder *sdktest.SpanRec
return len(spans) >= 3
}, "10s").Should(BeTrue())

Expect(spans[0].Name()).To(Equal(fmt.Sprintf("%s-%s-%d", instance.Name, instance.Spec.Version, instance.Generation)))
Expect(spans[0].Name()).To(Equal(instance.GetAppVersionName()))
Expect(spans[0].Attributes()).To(ContainElement(apicommon.AppName.String(instance.Name)))
Expect(spans[0].Attributes()).To(ContainElement(apicommon.AppVersion.String(instance.Spec.Version)))

Expand Down
2 changes: 1 addition & 1 deletion operator/test/component/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func AssertResourceUpdated(ctx context.Context, k8sClient client.Client, instanc
func GetAppVersion(ctx context.Context, k8sClient client.Client, instance *klcv1alpha3.KeptnApp) *klcv1alpha3.KeptnAppVersion {
appvName := types.NamespacedName{
Namespace: instance.Namespace,
Name: fmt.Sprintf("%s-%s-%d", instance.Name, instance.Spec.Version, instance.Generation),
Name: instance.GetAppVersionName(),
}

appVersion := &klcv1alpha3.KeptnAppVersion{}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppPostDeployEvaluations
postDeploymentStatus: Succeeded
Expand Down
2 changes: 1 addition & 1 deletion test/integration/app-failing-post-task/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppPostDeployTasks
postDeploymentEvaluationStatus: Deprecated
Expand Down
2 changes: 1 addition & 1 deletion test/integration/app-failing-pre-evaluation/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppPreDeployEvaluations
postDeploymentEvaluationStatus: Deprecated
Expand Down
2 changes: 1 addition & 1 deletion test/integration/app-failing-pre-task-retry/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppPreDeployTasks
postDeploymentEvaluationStatus: Deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppPreDeployTasks
postDeploymentEvaluationStatus: Deprecated
Expand Down
2 changes: 1 addition & 1 deletion test/integration/app-failing-pre-task/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppPreDeployTasks
postDeploymentEvaluationStatus: Deprecated
Expand Down
2 changes: 1 addition & 1 deletion test/integration/podtato-head-application/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: Completed
postDeploymentEvaluationStatus: Succeeded
Expand Down
2 changes: 1 addition & 1 deletion test/integration/restartable-app/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppDeploy
postDeploymentEvaluationStatus: Pending
Expand Down
4 changes: 2 additions & 2 deletions test/integration/restartable-app/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-2
name: podtato-head-1.3-d4735e3a
status:
currentPhase: Completed
postDeploymentEvaluationStatus: Succeeded
Expand All @@ -14,7 +14,7 @@ status:
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppDeploy
postDeploymentEvaluationStatus: Deprecated
Expand Down
6 changes: 3 additions & 3 deletions test/integration/restartable-app/02-assert.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-3
name: podtato-head-1.3-4e074085
status:
currentPhase: Completed
postDeploymentEvaluationStatus: Succeeded
Expand All @@ -14,7 +14,7 @@ status:
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-2
name: podtato-head-1.3-d4735e3a
status:
currentPhase: Completed
postDeploymentEvaluationStatus: Deprecated
Expand All @@ -27,7 +27,7 @@ status:
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppDeploy
postDeploymentEvaluationStatus: Deprecated
Expand Down
2 changes: 1 addition & 1 deletion test/integration/simple-deployment/01-assert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ metadata:
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: waiter-0.4-1
name: waiter-0.4-6b86b273
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppDeploy
postDeploymentEvaluationStatus: Pending
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: lifecycle.keptn.sh/v1alpha3
kind: KeptnAppVersion
metadata:
name: podtato-head-1.3-1
name: podtato-head-1.3-6b86b273
status:
currentPhase: AppDeploy
postDeploymentEvaluationStatus: Pending
Expand Down

0 comments on commit 2ad5d81

Please sign in to comment.