From 8264a2bdcc0e42a22b949291a0843b2b4b97eaf3 Mon Sep 17 00:00:00 2001 From: Jacopo Andrea Giola Date: Tue, 8 Oct 2024 09:36:29 +0200 Subject: [PATCH] fix: error if a resource group/kind is not available in the cluster anymore --- CHANGELOG.md | 6 +++ go.mod | 2 +- go.sum | 4 +- pkg/cmd/deploy/convert_inventory.go | 3 ++ pkg/cmd/deploy/convert_inventory_test.go | 51 ++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c959cce..0bd51fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- hanging if no resources has been applied successfully in a step +- error if a group/kind of a resource saved in the old inventory format is not availbale anymore in the cluster + now we skip the resource because we cannot retrieve it + ## [v2.0.0-beta.2] - 2024-10-04 ### Changed diff --git a/go.mod b/go.mod index b035e41..f23e032 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/external-secrets/external-secrets v0.10.0 github.com/go-logr/logr v1.4.2 github.com/go-logr/stdr v1.2.2 - github.com/mia-platform/jpl v0.5.0 + github.com/mia-platform/jpl v0.5.1 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 diff --git a/go.sum b/go.sum index c5a3208..8a845ac 100644 --- a/go.sum +++ b/go.sum @@ -117,8 +117,8 @@ github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhn github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/mia-platform/jpl v0.5.0 h1:DF6hMMZvMZxabHfpZ1xvfIDOirufsRWt64vynoGwEaA= -github.com/mia-platform/jpl v0.5.0/go.mod h1:fo3TIOpsOhO4sQLgQxhCy4CBOo0BvLRCzgk2fAggvs4= +github.com/mia-platform/jpl v0.5.1 h1:3KpKtOk3Nf8p9DofpO6uCONMsJpyo9v10Tp/cD7Qgeo= +github.com/mia-platform/jpl v0.5.1/go.mod h1:fo3TIOpsOhO4sQLgQxhCy4CBOo0BvLRCzgk2fAggvs4= github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8= github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c= github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA= diff --git a/pkg/cmd/deploy/convert_inventory.go b/pkg/cmd/deploy/convert_inventory.go index 2b022fa..e423d96 100644 --- a/pkg/cmd/deploy/convert_inventory.go +++ b/pkg/cmd/deploy/convert_inventory.go @@ -132,6 +132,9 @@ func (s *Inventory) oldInventoryObjects(ctx context.Context) (sets.Set[resource. mapping, err := s.mapper.RESTMapping(gvk.GroupKind()) if err != nil { + if meta.IsNoMatchError(err) { + continue + } return nil, err } diff --git a/pkg/cmd/deploy/convert_inventory_test.go b/pkg/cmd/deploy/convert_inventory_test.go index 8d0520f..3099ba9 100644 --- a/pkg/cmd/deploy/convert_inventory_test.go +++ b/pkg/cmd/deploy/convert_inventory_test.go @@ -193,6 +193,57 @@ func TestLoadInventory(t *testing.T) { //nolint: gocyclo }), expectedError: fmt.Sprintf("unexpected call: %q, method GET", secretPath), }, + "unavailable kind in compatibility mode": { + client: restfake.CreateHTTPClient(func(r *http.Request) (*http.Response, error) { + path := r.URL.Path + method := r.Method + switch { + case path == configMapPath && method == http.MethodGet: + return &http.Response{StatusCode: http.StatusNotFound, Header: jpltesting.DefaultHeaders()}, nil + case path == secretPath && method == http.MethodGet: + data, err := json.Marshal(map[string]*oldResourceList{ + "Deployment": { + Kind: "Deployment", + Mapping: schema.GroupVersionResource{ + Group: "apps", + Version: "v1", + Resource: "deployments", + }, + Resources: []string{"example"}, + }, + "Namespace": { + Kind: "Namespace", + Mapping: schema.GroupVersionResource{ + Group: "", + Version: "v1", + Resource: "namespaces", + }, + Resources: []string{"example"}, + }, + "Foo": { + Kind: "Foo", + Mapping: schema.GroupVersionResource{ + Group: "example.com", + Version: "v1alpha1", + Resource: "foos", + }, + }, + }) + require.NoError(t, err) + sec := &corev1.Secret{Data: map[string][]byte{ + oldInventoryKey: data, + }} + body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, sec)))) + return &http.Response{StatusCode: http.StatusOK, Body: body, Header: jpltesting.DefaultHeaders()}, nil + } + + return nil, fmt.Errorf("unexpected call: %q, method %s", path, method) + }), + expectedSet: sets.New( + deployResource, + namespaceResource, + ), + }, } for name, test := range tests {