-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
- Loading branch information
1 parent
8b84ab5
commit c180f8a
Showing
20 changed files
with
382 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,6 @@ linters: | |
- paralleltest | ||
- testpackage | ||
- wsl | ||
- wrapcheck | ||
|
||
issues: | ||
exclude-rules: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Copyright The CloudNativePG Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package decoder | ||
|
||
import ( | ||
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
func getClusterGVK() schema.GroupVersionKind { | ||
return schema.GroupVersionKind{ | ||
Group: apiv1.GroupVersion.Group, | ||
Version: apiv1.GroupVersion.Version, | ||
Kind: apiv1.ClusterKind, | ||
} | ||
} | ||
|
||
// DecodeClusterJSON decodes a JSON representation of a cluster. | ||
func DecodeClusterJSON(clusterJSON []byte) (*apiv1.Cluster, error) { | ||
var result apiv1.Cluster | ||
|
||
if err := DecodeObject(clusterJSON, &result, getClusterGVK()); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright The CloudNativePG Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package decoder | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Decode Functions", func() { | ||
DescribeTable( | ||
"Decode Functions", | ||
func(clusterJSON []byte, succeeds bool) { | ||
cluster, err := DecodeClusterJSON(clusterJSON) | ||
if !succeeds { | ||
Expect(err).To(HaveOccurred()) | ||
return | ||
} | ||
|
||
Expect(cluster).NotTo(BeNil()) | ||
Expect(cluster.GroupVersionKind()).To(Equal(getClusterGVK())) | ||
}, | ||
Entry( | ||
"should decode valid cluster JSON", | ||
[]byte(`{"apiVersion":"postgresql.cnpg.io/v1","kind":"Cluster"}`), | ||
true, | ||
), | ||
Entry( | ||
"should return error for invalid cluster JSON", | ||
[]byte(`{"apiVersion":"v1","kind":}`), | ||
false, | ||
), | ||
Entry( | ||
"should fail when the JSON is valid but doesn't represent a Cluster", | ||
[]byte(`{"apiVersion":"postgresql.cnpg.io/v1","kind":"Backup"}`), | ||
false, | ||
), | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright The CloudNativePG Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package decoder | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// WrongObjectTypeError is raised when the GVK of the passed JSON | ||
// object is different from the expected one. | ||
type WrongObjectTypeError struct { | ||
expectedGVK schema.GroupVersionKind | ||
receivedGVK schema.GroupVersionKind | ||
} | ||
|
||
// Error implements the error interface. | ||
func (e *WrongObjectTypeError) Error() string { | ||
return fmt.Sprintf("received wrong GVK '%v' expected '%v'", e.receivedGVK.String(), e.expectedGVK.String()) | ||
} | ||
|
||
// DecodeObject decodes a JSON representation of an object. | ||
func DecodeObject(objectJSON []byte, object client.Object, expectedGVK schema.GroupVersionKind) error { | ||
if err := json.Unmarshal(objectJSON, object); err != nil { | ||
return fmt.Errorf("error unmarshalling object JSON: %w", err) | ||
} | ||
|
||
if object.GetObjectKind().GroupVersionKind() != expectedGVK { | ||
return &WrongObjectTypeError{ | ||
expectedGVK: expectedGVK, | ||
receivedGVK: object.GetObjectKind().GroupVersionKind(), | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
Copyright The CloudNativePG Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package decoder | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Generic decoder", func() { | ||
DescribeTable( | ||
"Generic decoder", | ||
func(objectJSON []byte, succeeds bool) { | ||
var pod corev1.Pod | ||
err := DecodeObject(objectJSON, &pod, getPodGVK()) | ||
if !succeeds { | ||
Expect(err).To(HaveOccurred()) | ||
return | ||
} | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(pod.GetObjectKind().GroupVersionKind()).To(Equal(getPodGVK())) | ||
}, | ||
Entry("should decode valid object JSON", []byte(`{"apiVersion":"v1","kind":"Pod"}`), true), | ||
Entry("should return error for an invalid object type", []byte(`{"apiVersion":"invalid/v1","kind":"Pod"}`), false), | ||
Entry("should return error for invalid object JSON", []byte(`{"apiVersion":"v1","kind":}`), false), | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
Copyright The CloudNativePG Contributors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// Package decoder contains the functions that decode a JSON | ||
// stream into a structured Kubernetes resource | ||
package decoder |
Oops, something went wrong.