diff --git a/internal/backup/backup.go b/internal/backup/backup.go index 6c3e834..6046d88 100644 --- a/internal/backup/backup.go +++ b/internal/backup/backup.go @@ -43,7 +43,7 @@ func (Implementation) Backup( ) (*backup.BackupResult, error) { contextLogger := logging.FromContext(ctx) - helper, err := pluginhelper.NewFromJSONCluster(metadata.Data.Name, request.ClusterDefinition) + helper, err := pluginhelper.NewDataBuilder(metadata.Data.Name, request.ClusterDefinition).Build() if err != nil { contextLogger.Error(err, "Error while decoding cluster definition from CNPG") return nil, err diff --git a/internal/operator/mutations.go b/internal/operator/mutations.go index 35c19d3..125f340 100644 --- a/internal/operator/mutations.go +++ b/internal/operator/mutations.go @@ -32,7 +32,7 @@ func (Implementation) MutateCluster( _ context.Context, request *operator.OperatorMutateClusterRequest, ) (*operator.OperatorMutateClusterResult, error) { - helper, err := pluginhelper.NewFromJSONCluster(metadata.Data.Name, request.Definition) + helper, err := pluginhelper.NewDataBuilder(metadata.Data.Name, request.Definition).Build() if err != nil { return nil, err } @@ -67,7 +67,8 @@ func (Implementation) MutatePod( _ context.Context, request *operator.OperatorMutatePodRequest, ) (*operator.OperatorMutatePodResult, error) { - helper, err := pluginhelper.NewFromClusterAndPod(metadata.Data.Name, request.ClusterDefinition, request.PodDefinition) + helper, err := pluginhelper.NewDataBuilder(metadata.Data.Name, request.ClusterDefinition). + WithPod(request.PodDefinition).Build() if err != nil { return nil, err } diff --git a/internal/operator/validation.go b/internal/operator/validation.go index 157f51a..aa1b64e 100644 --- a/internal/operator/validation.go +++ b/internal/operator/validation.go @@ -41,7 +41,7 @@ func (Implementation) ValidateClusterCreate( ) (*operator.OperatorValidateClusterCreateResult, error) { result := &operator.OperatorValidateClusterCreateResult{} - helper, err := pluginhelper.NewFromJSONCluster(metadata.Data.Name, request.Definition) + helper, err := pluginhelper.NewDataBuilder(metadata.Data.Name, request.Definition).Build() if err != nil { return nil, err } @@ -58,12 +58,12 @@ func (Implementation) ValidateClusterChange( ) (*operator.OperatorValidateClusterChangeResult, error) { result := &operator.OperatorValidateClusterChangeResult{} - oldClusterHelper, err := pluginhelper.NewFromJSONCluster(metadata.Data.Name, request.OldCluster) + oldClusterHelper, err := pluginhelper.NewDataBuilder(metadata.Data.Name, request.OldCluster).Build() if err != nil { return nil, fmt.Errorf("while parsing old cluster: %w", err) } - newClusterHelper, err := pluginhelper.NewFromJSONCluster(metadata.Data.Name, request.NewCluster) + newClusterHelper, err := pluginhelper.NewDataBuilder(metadata.Data.Name, request.NewCluster).Build() if err != nil { return nil, fmt.Errorf("while parsing new cluster: %w", err) } diff --git a/internal/wal/status.go b/internal/wal/status.go index 3ce9dea..cdf62be 100644 --- a/internal/wal/status.go +++ b/internal/wal/status.go @@ -45,7 +45,7 @@ func (Implementation) Status( ) (*wal.WALStatusResult, error) { contextLogger := logging.FromContext(ctx) - helper, err := pluginhelper.NewFromJSONCluster(metadata.Data.Name, request.ClusterDefinition) + helper, err := pluginhelper.NewDataBuilder(metadata.Data.Name, request.ClusterDefinition).Build() if err != nil { contextLogger.Error(err, "Error while decoding cluster definition from CNPG") return nil, err diff --git a/internal/wal/wal.go b/internal/wal/wal.go index 5af4c59..da3fefa 100644 --- a/internal/wal/wal.go +++ b/internal/wal/wal.go @@ -36,7 +36,7 @@ func (Implementation) Archive( ) (*wal.WALArchiveResult, error) { contextLogger := logging.FromContext(ctx) - helper, err := pluginhelper.NewFromJSONCluster(metadata.Data.Name, request.ClusterDefinition) + helper, err := pluginhelper.NewDataBuilder(metadata.Data.Name, request.ClusterDefinition).Build() if err != nil { contextLogger.Error(err, "Error while decoding cluster definition from CNPG") return nil, err @@ -67,7 +67,7 @@ func (Implementation) Restore( ) (*wal.WALRestoreResult, error) { contextLogger := logging.FromContext(ctx) - helper, err := pluginhelper.NewFromJSONCluster(metadata.Data.Name, request.ClusterDefinition) + helper, err := pluginhelper.NewDataBuilder(metadata.Data.Name, request.ClusterDefinition).Build() if err != nil { contextLogger.Error(err, "Error while decoding cluster definition from CNPG") return nil, err diff --git a/pkg/pluginhelper/helper.go b/pkg/pluginhelper/helper.go index 2f114c7..eaa3287 100644 --- a/pkg/pluginhelper/helper.go +++ b/pkg/pluginhelper/helper.go @@ -31,8 +31,6 @@ const ( pluginMountPath = "/plugins" ) -// TODO: introduce a deserialize struct? - // Data is an helper structure to be used by // plugins wanting to enhance the CNPG validating webhooks type Data struct { @@ -44,21 +42,43 @@ type Data struct { pluginIndex int } -// NewFromJSONCluster creates a new validation helper loading -// a cluster definition -func NewFromJSONCluster( - pluginName string, - jsonCluster []byte, -) (*Data, error) { +// DataBuilder a fluent constructor for the Data struct +type DataBuilder struct { + pluginName string + clusterJSON []byte + podJSON []byte +} + +// NewDataBuilder initializes a basic DataBuilder +func NewDataBuilder(pluginName string, clusterJSON []byte) *DataBuilder { + d := DataBuilder{clusterJSON: clusterJSON, pluginName: pluginName} + d.clusterJSON = clusterJSON + return &d +} + +// WithPod adds Pod data to the DataBuilder +func (d *DataBuilder) WithPod(podJSON []byte) *DataBuilder { + d.podJSON = podJSON + return d +} + +// Build returns the constructed Data object and any errors encountered +func (d *DataBuilder) Build() (*Data, error) { result := &Data{} - if err := json.Unmarshal(jsonCluster, &result.cluster); err != nil { + if err := json.Unmarshal(d.clusterJSON, &result.cluster); err != nil { return nil, err } + if len(d.podJSON) > 0 { + if err := json.Unmarshal(d.podJSON, &result.pod); err != nil { + return nil, err + } + } + result.pluginIndex = -1 for idx, cfg := range result.cluster.Spec.Plugins { - if cfg.Name == pluginName { + if cfg.Name == d.pluginName { result.pluginIndex = idx result.Parameters = cfg.Parameters } @@ -67,25 +87,6 @@ func NewFromJSONCluster( return result, nil } -// NewFromClusterAndPod creates a new validation helper loading -// a cluster and a Pod definition -func NewFromClusterAndPod( - pluginName string, - clusterDefinition []byte, - podDefinition []byte, -) (*Data, error) { - result, err := NewFromJSONCluster(pluginName, clusterDefinition) - if err != nil { - return nil, err - } - - if err := json.Unmarshal(podDefinition, &result.pod); err != nil { - return nil, err - } - - return result, nil -} - // GetCluster gets the decoded cluster object func (helper *Data) GetCluster() *apiv1.Cluster { return &helper.cluster