Skip to content

Commit

Permalink
chore: review
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
  • Loading branch information
leonardoce committed Aug 29, 2024
1 parent 8b84ab5 commit c180f8a
Show file tree
Hide file tree
Showing 20 changed files with 382 additions and 166 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ linters:
- paralleltest
- testpackage
- wsl
- wrapcheck

issues:
exclude-rules:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package backup
// Package clusterstatus provides a set of primitives to compose
// the SetClusterStatus RPC Response
package clusterstatus
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package http
package clusterstatus

import "errors"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package http
package clusterstatus

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package http
package clusterstatus

import (
. "github.com/onsi/ginkgo/v2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// Package backup provides a set of functions to work with the backup plugins
package backup
package decoder

import (
"encoding/json"
"fmt"

apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

func getBackupGVK() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: apiv1.GroupVersion.Group,
Version: apiv1.GroupVersion.Version,
Kind: apiv1.BackupKind,
}
}

// DecodeBackup decodes a JSON representation of a backup.
func DecodeBackup(backupDefinition []byte) (*apiv1.Backup, error) {
var backup apiv1.Backup

if err := json.Unmarshal(backupDefinition, &backup); err != nil {
return nil, fmt.Errorf("error unmarshalling backup JSON: %w", err)
if err := DecodeObject(backupDefinition, &backup, getBackupGVK()); err != nil {
return nil, err
}

return &backup, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,52 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package backup
package decoder

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("DecodeBackup", func() {
Context("when the backup JSON is valid", func() {
It("should decode the backup JSON successfully", func() {
backupJSON := []byte(`{"apiVersion":"v1","kind":"Backup"}`)
DescribeTable(
"DecodeBackup",
func(backupJSON []byte, succeeds bool) {
backup, err := DecodeBackup(backupJSON)
Expect(err).NotTo(HaveOccurred())
Expect(backup).NotTo(BeNil())
Expect(backup.Kind).To(Equal("Backup"))
})
})
if err != nil {
Expect(succeeds).To(BeFalse())
return
}

Context("when the backup JSON is invalid", func() {
It("should return an error for invalid JSON", func() {
backupJSON := []byte(`{"apiVersion":"v1","kind":}`)
backup, err := DecodeBackup(backupJSON)
Expect(err).To(HaveOccurred())
Expect(backup).To(BeNil())
})
})
Expect(succeeds).To(BeTrue())
Expect(backup.GetObjectKind().GroupVersionKind()).To(Equal(getBackupGVK()))
},
Entry(
"when the backup JSON is valid",
[]byte(`{"apiVersion":"postgresql.cnpg.io/v1","kind":"Backup"}`),
true,
),
Entry(
"when the backup JSON is valid but the Kind is wrong",
[]byte(`{"apiVersion":"postgresql.cnpg.io/v1","kind":"Pooler"}`),
false,
),
Entry(
"when the backup JSON is valid but the object type is wrong",
[]byte(`{"apiVersion":"apps/v1","kind":"Backup"}`),
false,
),
Entry(
"when the backup JSON is invalid",
[]byte(`{"apiVersion":"v1","kind":}`),
false,
),
Entry(
"when the backup JSON is empty",
[]byte(``),
false,
),
)

Context("when the backup JSON is empty", func() {
It("should return an error for empty JSON", func() {
Expand Down
41 changes: 41 additions & 0 deletions pkg/pluginhelper/decoder/cluster.go
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
}
53 changes: 53 additions & 0 deletions pkg/pluginhelper/decoder/cluster_test.go
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,
),
)
})
53 changes: 53 additions & 0 deletions pkg/pluginhelper/decoder/decoder.go
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
}
44 changes: 44 additions & 0 deletions pkg/pluginhelper/decoder/decoder_test.go
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),
)
})
19 changes: 19 additions & 0 deletions pkg/pluginhelper/decoder/doc.go
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
Loading

0 comments on commit c180f8a

Please sign in to comment.