Skip to content

Commit

Permalink
#24 replace DoguVersion with QualifiedDoguVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
nroeske committed Oct 29, 2024
1 parent 10499ad commit 7701f5e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 137 deletions.
42 changes: 21 additions & 21 deletions dogu/doguVersionRegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,27 @@ func NewDoguVersionRegistry(configMapClient configMapClient) *doguVersionRegistr
}
}

func (vr *doguVersionRegistry) GetCurrent(ctx context.Context, name cescommon.SimpleDoguName) (DoguVersion, error) {
func (vr *doguVersionRegistry) GetCurrent(ctx context.Context, name cescommon.SimpleDoguName) (cescommon.QualifiedDoguVersion, error) {
descriptor, err := getDescriptorConfigMapForDogu(ctx, vr.configMapClient, name)
if err != nil {
return DoguVersion{}, err
return cescommon.QualifiedDoguVersion{}, err
}

currentDoguVersion, ok := descriptor.Data[currentVersionKey]
if !ok {
return DoguVersion{}, getDoguRegistryKeyNotFoundError(currentVersionKey, name)
return cescommon.QualifiedDoguVersion{}, getDoguRegistryKeyNotFoundError(currentVersionKey, name)
}

version, err := parseDoguVersion(currentDoguVersion, name)
if err != nil {
return DoguVersion{}, cloudoguerrors.NewGenericError(err)
return cescommon.QualifiedDoguVersion{}, cloudoguerrors.NewGenericError(err)
}

qualifiedDoguName := cescommon.QualifiedDoguName{
SimpleName: name,
}

return DoguVersion{Name: qualifiedDoguName, Version: version}, nil
return cescommon.QualifiedDoguVersion{Name: qualifiedDoguName, Version: version}, nil
}

func parseDoguVersion(version string, name cescommon.SimpleDoguName) (core.Version, error) {
Expand Down Expand Up @@ -89,14 +89,14 @@ func getDescriptorConfigMapForDogu(ctx context.Context, configMapClient configMa
return get, nil
}

func (vr *doguVersionRegistry) GetCurrentOfAll(ctx context.Context) ([]DoguVersion, error) {
func (vr *doguVersionRegistry) GetCurrentOfAll(ctx context.Context) ([]cescommon.QualifiedDoguVersion, error) {
registryList, err := getAllDescriptorConfigMaps(ctx, vr.configMapClient)
if err != nil {
return nil, err
}

var errs []error
doguVersions := make([]DoguVersion, 0, len(registryList.Items))
doguVersions := make([]cescommon.QualifiedDoguVersion, 0, len(registryList.Items))
for _, localRegistry := range registryList.Items {
currentVersion, ok := localRegistry.Data[currentVersionKey]
if !ok {
Expand All @@ -114,7 +114,7 @@ func (vr *doguVersionRegistry) GetCurrentOfAll(ctx context.Context) ([]DoguVersi
SimpleName: doguName,
}

doguVersions = append(doguVersions, DoguVersion{Name: qualifiedDoguName, Version: version})
doguVersions = append(doguVersions, cescommon.QualifiedDoguVersion{Name: qualifiedDoguName, Version: version})
}

err = errors.Join(errs...)
Expand All @@ -138,7 +138,7 @@ func getAllLocalDoguRegistriesSelector() string {
return fmt.Sprintf("%s=%s,%s,%s=%s", appLabelKey, appLabelValueCes, doguNameLabelKey, typeLabelKey, typeLabelValueLocalDoguRegistry)
}

func (vr *doguVersionRegistry) IsEnabled(ctx context.Context, doguVersion DoguVersion) (bool, error) {
func (vr *doguVersionRegistry) IsEnabled(ctx context.Context, doguVersion cescommon.QualifiedDoguVersion) (bool, error) {
descriptorConfigMap, err := getDescriptorConfigMapForDogu(ctx, vr.configMapClient, doguVersion.Name.SimpleName)
if err != nil {
return false, err
Expand All @@ -152,7 +152,7 @@ func (vr *doguVersionRegistry) IsEnabled(ctx context.Context, doguVersion DoguVe
return true, nil
}

func (vr *doguVersionRegistry) Enable(ctx context.Context, doguVersion DoguVersion) error {
func (vr *doguVersionRegistry) Enable(ctx context.Context, doguVersion cescommon.QualifiedDoguVersion) error {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
// do not create the registry here if not existent because it would be an invalid state without the dogu descriptor.
descriptorConfigMap, err := getDescriptorConfigMapForDogu(ctx, vr.configMapClient, doguVersion.Name.SimpleName)
Expand Down Expand Up @@ -315,7 +315,7 @@ func handleDeleteWatchEvent(ctx context.Context, event watch.Event, persistenceC
oldPersistenceContext := copyPersistenceContext(persistenceContext)
delete(persistenceContext, eventDoguVersion.Name.SimpleName)

fireWatchResult(currentVersionsWatchResult, oldPersistenceContext, persistenceContext, []DoguVersion{eventDoguVersion})
fireWatchResult(currentVersionsWatchResult, oldPersistenceContext, persistenceContext, []cescommon.QualifiedDoguVersion{eventDoguVersion})
return nil
}

Expand All @@ -340,7 +340,7 @@ func handleModifiedWatchEvent(ctx context.Context, event watch.Event, persistenc
qualifiedDoguName := cescommon.QualifiedDoguName{
SimpleName: doguName,
}
fireWatchResult(currentVersionsWatchResult, oldPersistenceContext, persistenceContext, []DoguVersion{{Name: qualifiedDoguName, Version: version}})
fireWatchResult(currentVersionsWatchResult, oldPersistenceContext, persistenceContext, []cescommon.QualifiedDoguVersion{{Name: qualifiedDoguName, Version: version}})
delete(persistenceContext, doguName)
} else {
// Detect change
Expand All @@ -356,7 +356,7 @@ func handleModifiedWatchEvent(ctx context.Context, event watch.Event, persistenc
}

persistenceContext[eventDoguVersion.Name.SimpleName] = eventDoguVersion.Version
fireWatchResult(currentVersionsWatchResult, oldPersistenceContext, persistenceContext, []DoguVersion{eventDoguVersion})
fireWatchResult(currentVersionsWatchResult, oldPersistenceContext, persistenceContext, []cescommon.QualifiedDoguVersion{eventDoguVersion})
}

return nil
Expand All @@ -382,7 +382,7 @@ func handleAddWatchEvent(ctx context.Context, event watch.Event, persistenceCont

oldPersistenceContext := copyPersistenceContext(persistenceContext)
persistenceContext[eventDoguVersion.Name.SimpleName] = eventDoguVersion.Version
fireWatchResult(currentVersionsWatchResult, oldPersistenceContext, persistenceContext, []DoguVersion{eventDoguVersion})
fireWatchResult(currentVersionsWatchResult, oldPersistenceContext, persistenceContext, []cescommon.QualifiedDoguVersion{eventDoguVersion})
return nil
}

Expand All @@ -393,11 +393,11 @@ func copyPersistenceContext(persistenceContext map[cescommon.SimpleDoguName]core
return oldPersistenceContext
}

func fireWatchResult(channel chan CurrentVersionsWatchResult, prevPersistenceContext map[cescommon.SimpleDoguName]core.Version, newPersistenceContext map[cescommon.SimpleDoguName]core.Version, diffs []DoguVersion) {
func fireWatchResult(channel chan CurrentVersionsWatchResult, prevPersistenceContext map[cescommon.SimpleDoguName]core.Version, newPersistenceContext map[cescommon.SimpleDoguName]core.Version, diffs []cescommon.QualifiedDoguVersion) {
fireWatchResultWithError(channel, prevPersistenceContext, newPersistenceContext, diffs, nil)
}

func fireWatchResultWithError(channel chan CurrentVersionsWatchResult, prevPersistenceContext map[cescommon.SimpleDoguName]core.Version, newPersistenceContext map[cescommon.SimpleDoguName]core.Version, diffs []DoguVersion, err error) {
func fireWatchResultWithError(channel chan CurrentVersionsWatchResult, prevPersistenceContext map[cescommon.SimpleDoguName]core.Version, newPersistenceContext map[cescommon.SimpleDoguName]core.Version, diffs []cescommon.QualifiedDoguVersion, err error) {
result := CurrentVersionsWatchResult{
PrevVersions: prevPersistenceContext,
Versions: newPersistenceContext,
Expand All @@ -417,25 +417,25 @@ func getDescriptorConfigMapFromEvent(event watch.Event) (*corev1.ConfigMap, erro
return configMap, nil
}

func getCurrentDoguVersionFromDoguDescriptorConfigMap(cm corev1.ConfigMap) (DoguVersion, error) {
func getCurrentDoguVersionFromDoguDescriptorConfigMap(cm corev1.ConfigMap) (cescommon.QualifiedDoguVersion, error) {
doguName, ok := cm.Labels[doguNameLabelKey]
if !ok {
return DoguVersion{}, fmt.Errorf("dogu descriptor configmap does not contain label %q", doguNameLabelKey)
return cescommon.QualifiedDoguVersion{}, fmt.Errorf("dogu descriptor configmap does not contain label %q", doguNameLabelKey)
}

currentVersion, ok := cm.Data[currentVersionKey]
if !ok {
return DoguVersion{}, fmt.Errorf("dogu descriptor configmap does not contain key %q", currentVersionKey)
return cescommon.QualifiedDoguVersion{}, fmt.Errorf("dogu descriptor configmap does not contain key %q", currentVersionKey)
}

version, err := core.ParseVersion(currentVersion)
if err != nil {
return DoguVersion{}, fmt.Errorf("error parsing version %q for dogu version registry %q", currentVersion, cm.Name)
return cescommon.QualifiedDoguVersion{}, fmt.Errorf("error parsing version %q for dogu version registry %q", currentVersion, cm.Name)
}
qualifiedDoguName := cescommon.QualifiedDoguName{
SimpleName: cescommon.SimpleDoguName(doguName),
}
return DoguVersion{Name: qualifiedDoguName, Version: version}, nil
return cescommon.QualifiedDoguVersion{Name: qualifiedDoguName, Version: version}, nil
}

func hasDoguDescriptorConfigMapCurrentKey(cm *corev1.ConfigMap) bool {
Expand Down
48 changes: 24 additions & 24 deletions dogu/doguVersionRegistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestNewDoguVersionRegistry(t *testing.T) {
}

func Test_versionRegistry_GetCurrent(t *testing.T) {
expectedDoguVersion := DoguVersion{
expectedDoguVersion := cescommon.QualifiedDoguVersion{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("cas")},
Version: parseVersionStr(t, casVersionStr),
}
Expand All @@ -58,7 +58,7 @@ func Test_versionRegistry_GetCurrent(t *testing.T) {
name string
configMapClientFn func(t *testing.T) configMapClient
args args
want DoguVersion
want cescommon.QualifiedDoguVersion
wantErr assert.ErrorAssertionFunc
}{
{
Expand All @@ -82,7 +82,7 @@ func Test_versionRegistry_GetCurrent(t *testing.T) {
return configMapClientMock
},
args: casArgs,
want: DoguVersion{},
want: cescommon.QualifiedDoguVersion{},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.True(t, cloudoguerrors.IsGenericError(err), i) &&
assert.ErrorContains(t, err, "failed to get dogu descriptor config map for dogu \"cas\"", i)
Expand All @@ -97,7 +97,7 @@ func Test_versionRegistry_GetCurrent(t *testing.T) {
return configMapClientMock
},
args: casArgs,
want: DoguVersion{},
want: cescommon.QualifiedDoguVersion{},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.True(t, cloudoguerrors.IsNotFoundError(err), i) &&
assert.ErrorContains(t, err, "failed to get value for key \"current\" for dogu registry \"cas\"", i)
Expand All @@ -112,7 +112,7 @@ func Test_versionRegistry_GetCurrent(t *testing.T) {
return configMapClientMock
},
args: casArgs,
want: DoguVersion{},
want: cescommon.QualifiedDoguVersion{},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.True(t, cloudoguerrors.IsGenericError(err), i) &&
assert.ErrorContains(t, err, "failed to parse version \"abc\" for dogu \"cas\"", i)
Expand All @@ -135,11 +135,11 @@ func Test_versionRegistry_GetCurrent(t *testing.T) {
}

func Test_versionRegistry_GetCurrentOfAll(t *testing.T) {
expectedCasDoguVersion := DoguVersion{
expectedCasDoguVersion := cescommon.QualifiedDoguVersion{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("cas")},
Version: parseVersionStr(t, casVersionStr),
}
expectedLdapDoguVersion := DoguVersion{
expectedLdapDoguVersion := cescommon.QualifiedDoguVersion{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("ldap")},
Version: parseVersionStr(t, ldapVersionStr),
}
Expand All @@ -154,7 +154,7 @@ func Test_versionRegistry_GetCurrentOfAll(t *testing.T) {
name string
configMapClientFn func(t *testing.T) configMapClient
args args
want []DoguVersion
want []cescommon.QualifiedDoguVersion
wantErr assert.ErrorAssertionFunc
}{
{
Expand All @@ -166,7 +166,7 @@ func Test_versionRegistry_GetCurrentOfAll(t *testing.T) {
return configMapClientMock
},
args: args{ctx: testCtx},
want: []DoguVersion{expectedCasDoguVersion, expectedLdapDoguVersion},
want: []cescommon.QualifiedDoguVersion{expectedCasDoguVersion, expectedLdapDoguVersion},
wantErr: assert.NoError,
},
{
Expand All @@ -180,7 +180,7 @@ func Test_versionRegistry_GetCurrentOfAll(t *testing.T) {
return configMapClientMock
},
args: args{ctx: testCtx},
want: []DoguVersion{expectedLdapDoguVersion},
want: []cescommon.QualifiedDoguVersion{expectedLdapDoguVersion},
wantErr: assert.NoError,
},
{
Expand Down Expand Up @@ -210,7 +210,7 @@ func Test_versionRegistry_GetCurrentOfAll(t *testing.T) {
return configMapClientMock
},
args: args{ctx: testCtx},
want: []DoguVersion{},
want: []cescommon.QualifiedDoguVersion{},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.True(t, cloudoguerrors.IsGenericError(err), i) &&
assert.ErrorContains(t, err, "failed to get some dogu versions: failed to parse version \"abc\" for dogu \"cas\": failed to parse major version abc: strconv.Atoi: parsing \"abc\": invalid syntax\nfailed to parse version \"abcd\" for dogu \"ldap\": failed to parse major version abcd: strconv.Atoi: parsing \"abcd\": invalid syntax")
Expand All @@ -228,7 +228,7 @@ func Test_versionRegistry_GetCurrentOfAll(t *testing.T) {
return configMapClientMock
},
args: args{ctx: testCtx},
want: []DoguVersion{
want: []cescommon.QualifiedDoguVersion{
{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("ldap")},
Version: parseVersionStr(t, "1.0.0"),
Expand Down Expand Up @@ -259,7 +259,7 @@ func Test_versionRegistry_IsEnabled(t *testing.T) {
casRegistryCmWithOutCurrent := &corev1.ConfigMap{Data: map[string]string{casVersionStr: readCasDoguStr(t)}, ObjectMeta: metav1.ObjectMeta{Labels: casVersionRegistryLabelMap}}
type args struct {
ctx context.Context
doguVersion DoguVersion
doguVersion cescommon.QualifiedDoguVersion
}
tests := []struct {
name string
Expand All @@ -276,7 +276,7 @@ func Test_versionRegistry_IsEnabled(t *testing.T) {

return configMapClientMock
},
args: args{ctx: testCtx, doguVersion: DoguVersion{
args: args{ctx: testCtx, doguVersion: cescommon.QualifiedDoguVersion{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("cas")},
Version: parseVersionStr(t, casVersionStr)}},
want: true,
Expand All @@ -290,7 +290,7 @@ func Test_versionRegistry_IsEnabled(t *testing.T) {

return configMapClientMock
},
args: args{ctx: testCtx, doguVersion: DoguVersion{
args: args{ctx: testCtx, doguVersion: cescommon.QualifiedDoguVersion{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("cas")},
Version: parseVersionStr(t, casVersionStr)}},
want: false,
Expand All @@ -304,7 +304,7 @@ func Test_versionRegistry_IsEnabled(t *testing.T) {

return configMapClientMock
},
args: args{ctx: testCtx, doguVersion: DoguVersion{
args: args{ctx: testCtx, doguVersion: cescommon.QualifiedDoguVersion{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("cas")},
Version: parseVersionStr(t, "7.0.5.1-2")}},
want: false,
Expand All @@ -318,7 +318,7 @@ func Test_versionRegistry_IsEnabled(t *testing.T) {

return configMapClientMock
},
args: args{ctx: testCtx, doguVersion: DoguVersion{
args: args{ctx: testCtx, doguVersion: cescommon.QualifiedDoguVersion{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("cas")},
Version: parseVersionStr(t, casVersionStr)}},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
Expand Down Expand Up @@ -347,11 +347,11 @@ func Test_versionRegistry_Enable(t *testing.T) {

type args struct {
ctx context.Context
doguVersion DoguVersion
doguVersion cescommon.QualifiedDoguVersion
}
casArgs := args{
ctx: testCtx,
doguVersion: DoguVersion{
doguVersion: cescommon.QualifiedDoguVersion{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("cas")}, Version: parseVersionStr(t, casVersionStr)},
}
tests := []struct {
Expand Down Expand Up @@ -522,7 +522,7 @@ func Test_versionRegistry_WatchAllCurrent(t *testing.T) {
assert.Equal(t, initialDoguVersionCtx, result.PrevVersions)
casVersion := parseVersionStr(t, casVersionStr)
assert.Equal(t, result.Versions, map[cescommon.SimpleDoguName]core.Version{"ldap": parseVersionStr(t, ldapVersionStr), "cas": casVersion})
assert.Equal(t, []DoguVersion{{
assert.Equal(t, []cescommon.QualifiedDoguVersion{{
Name: cescommon.QualifiedDoguName{SimpleName: "cas"}, Version: casVersion}}, result.Diff)

addCancelFunc()
Expand Down Expand Up @@ -550,7 +550,7 @@ func Test_versionRegistry_WatchAllCurrent(t *testing.T) {
assert.Equal(t, initialDoguVersionCtx, result.PrevVersions)
casVersion := parseVersionStr(t, casVersionStr)
assert.Equal(t, result.Versions, map[cescommon.SimpleDoguName]core.Version{"ldap": parseVersionStr(t, ldapVersionStr), "cas": casVersion})
assert.Equal(t, []DoguVersion{{
assert.Equal(t, []cescommon.QualifiedDoguVersion{{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("cas")}, Version: casVersion}}, result.Diff)

emptyAddCancelFunc()
Expand All @@ -577,7 +577,7 @@ func Test_versionRegistry_WatchAllCurrent(t *testing.T) {
assert.Equal(t, initialDoguVersionCtx, result.PrevVersions)
upgradedLdapVersion := parseVersionStr(t, upgradeLdapVersionStr)
assert.Equal(t, result.Versions, map[cescommon.SimpleDoguName]core.Version{"ldap": upgradedLdapVersion})
assert.Equal(t, []DoguVersion{{
assert.Equal(t, []cescommon.QualifiedDoguVersion{{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("ldap")}, Version: upgradedLdapVersion}}, result.Diff)

modifyCancelFunc()
Expand All @@ -604,7 +604,7 @@ func Test_versionRegistry_WatchAllCurrent(t *testing.T) {
assert.Equal(t, initialDoguVersionCtx, result.PrevVersions)
ldapVersion := parseVersionStr(t, ldapVersionStr)
assert.Equal(t, result.Versions, map[cescommon.SimpleDoguName]core.Version{})
assert.Equal(t, []DoguVersion{{
assert.Equal(t, []cescommon.QualifiedDoguVersion{{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("ldap")}, Version: ldapVersion}}, result.Diff)

deleteCancelFunc()
Expand Down Expand Up @@ -865,7 +865,7 @@ func Test_handleModifiedWatchEvent(t *testing.T) {
require.NoError(t, result.Err)
assert.Len(t, result.Versions, 0)
assert.Equal(t, result.PrevVersions, expectedOldVersions)
assert.Equal(t, result.Diff, []DoguVersion{{
assert.Equal(t, result.Diff, []cescommon.QualifiedDoguVersion{{
Name: cescommon.QualifiedDoguName{SimpleName: cescommon.SimpleDoguName("ldap")}, Version: parseVersionStr(t, "1.0.0")}})
assert.Len(t, persistentContext, 0)
})
Expand Down
Loading

0 comments on commit 7701f5e

Please sign in to comment.