Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support mapping kind correctly #86

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- correctly manage resources with kind Mapping

## [1.2.2] - 2023-06-05

### Fixed

- gvk field tag to kind in resource-deployment secret

## [1.2.1] - 2023-06-01

### Fixed
Expand Down Expand Up @@ -132,7 +142,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Initial Release 🎉🎉🎉

[Unreleased]: https://github.com/mia-platform/mlp/compare/v1.2.1...HEAD
[Unreleased]: https://github.com/mia-platform/mlp/compare/v1.2.2...HEAD
[1.2.2]: https://github.com/mia-platform/mlp/compare/v1.2.1...v1.2.2
[1.2.1]: https://github.com/mia-platform/mlp/compare/v1.2.0...v1.2.1
[1.2.0]: https://github.com/mia-platform/mlp/compare/v1.1.0...v1.2.0
[1.1.0]: https://github.com/mia-platform/mlp/compare/v1.0.3...v1.1.0
Expand Down
17 changes: 8 additions & 9 deletions pkg/deploy/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package deploy
import (
"context"
"encoding/json"
"strings"
"fmt"

"github.com/mia-platform/mlp/pkg/resourceutil"
batchv1 "k8s.io/api/batch/v1"
Expand Down Expand Up @@ -117,15 +117,14 @@ func getOldResourceMap(clients *k8sClients, namespace string) (map[string]*Resou
res := make(map[string]*ResourceList)

resources := secret.Data[resourceField]
if strings.Contains(string(resources), "\"Mapping\":{") {
res, err = convertSecretFormat(resources)
} else {
err = json.Unmarshal(resources, &res)
}
if err != nil {
return nil, err
}

if err := json.Unmarshal(resources, &res); err != nil {
var convertError error
res, convertError = convertSecretFormat(resources)
if convertError != nil {
return nil, fmt.Errorf("error unmarshalling resource map in secret %s: %s in namespace %s. Try to convert format from v0, but fails", resourceSecretName, err, namespace)
}
}
return res, nil
}

Expand Down
20 changes: 19 additions & 1 deletion pkg/deploy/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestGetOldResourceMap(t *testing.T) {
configMapGvk := &schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ConfigMap"}
deploymentGvk := &schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}
certGvk := &schema.GroupVersionKind{Group: "cert-manager.io", Version: "v1", Kind: "Certificate"}
mappingGvk := &schema.GroupVersionKind{Group: "getambassador.io", Version: "v2", Kind: "Mapping"}

testcases := []struct {
description string
Expand Down Expand Up @@ -125,6 +126,23 @@ func TestGetOldResourceMap(t *testing.T) {
require.Nil(t, err)
},
},
{
description: "with a Mapping kind",
input: &corev1.Secret{
Data: map[string][]byte{"resources": []byte(`{"Mapping":{"kind":{"Group":"getambassador.io","Version":"v2","Kind":"Mapping"},"resources":["a-resource","another-resource"]}}`)},
ObjectMeta: metav1.ObjectMeta{
Name: resourceSecretName,
Namespace: "foo",
},
TypeMeta: metav1.TypeMeta{Kind: "Secret", APIVersion: "v1"},
},
expected: map[string]*ResourceList{
"Mapping": {Gvk: mappingGvk, Resources: []string{"a-resource", "another-resource"}},
},
error: func(t *testing.T, err error) {
require.Nil(t, err)
},
},
{
description: "resources in in v0 format",
input: &corev1.Secret{
Expand Down Expand Up @@ -194,8 +212,8 @@ func TestGetOldResourceMap(t *testing.T) {
t.Run(tt.description, func(t *testing.T) {
dynamicClient := fake.NewSimpleDynamicClient(scheme, tt.input)
actual, err := getOldResourceMap(&k8sClients{dynamic: dynamicClient}, "foo")
require.Equal(t, tt.expected, actual)
tt.error(t, err)
require.Equal(t, tt.expected, actual)
})
}
}
Expand Down