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

KO-357: Added hot-reload support in backup-service #327

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions api/v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"regexp"
"strings"

"k8s.io/apimachinery/pkg/util/sets"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/ptr"

internalerrors "github.com/aerospike/aerospike-kubernetes-operator/errors"
Expand Down
127 changes: 51 additions & 76 deletions api/v1beta1/aerospikebackup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,20 @@ limitations under the License.
package v1beta1

import (
"context"
"fmt"
"reflect"
"strings"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilRuntime "k8s.io/apimachinery/pkg/util/runtime"
clientGoScheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/yaml"

"github.com/aerospike/aerospike-backup-service/pkg/model"
asdbv1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1"
"github.com/aerospike/aerospike-kubernetes-operator/internal/controller/common"
"github.com/aerospike/aerospike-backup-service/v2/pkg/dto"
"github.com/aerospike/aerospike-backup-service/v2/pkg/validation"
)

func (r *AerospikeBackup) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand Down Expand Up @@ -65,12 +60,12 @@ func (r *AerospikeBackup) ValidateCreate() (admission.Warnings, error) {

abLog.Info("Validate create")

if len(r.Spec.OnDemandBackups) != 0 {
return nil, fmt.Errorf("onDemand backups config cannot be specified while creating backup")
if err := r.validate(); err != nil {
return nil, err
}

if err := r.validateBackupConfig(); err != nil {
return nil, err
if len(r.Spec.OnDemandBackups) != 0 {
return nil, fmt.Errorf("onDemand backups config cannot be specified while creating backup")
}

return nil, nil
Expand All @@ -88,7 +83,7 @@ func (r *AerospikeBackup) ValidateUpdate(old runtime.Object) (admission.Warnings
return nil, fmt.Errorf("backup service cannot be updated")
}

if err := r.validateBackupConfig(); err != nil {
if err := r.validate(); err != nil {
return nil, err
}

Expand All @@ -103,6 +98,22 @@ func (r *AerospikeBackup) ValidateUpdate(old runtime.Object) (admission.Warnings
return nil, nil
}

func (r *AerospikeBackup) validate() error {
k8sClient, gErr := getK8sClient()
if gErr != nil {
return gErr
}

if err := validateBackupSvcSupportedVersion(k8sClient,
r.Spec.BackupService.Name,
r.Spec.BackupService.Namespace,
); err != nil {
return err
}

return r.validateBackupConfig(k8sClient)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *AerospikeBackup) ValidateDelete() (admission.Warnings, error) {
abLog := logf.Log.WithName(namespacedName(r))
Expand All @@ -113,45 +124,32 @@ func (r *AerospikeBackup) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

func (r *AerospikeBackup) validateBackupConfig() error {
func (r *AerospikeBackup) validateBackupConfig(k8sClient client.Client) error {
backupConfig := make(map[string]interface{})

if err := yaml.Unmarshal(r.Spec.Config.Raw, &backupConfig); err != nil {
return err
}

if _, ok := backupConfig[common.ServiceKey]; ok {
if _, ok := backupConfig[ServiceKey]; ok {
return fmt.Errorf("service field cannot be specified in backup config")
}

if _, ok := backupConfig[common.BackupPoliciesKey]; ok {
if _, ok := backupConfig[BackupPoliciesKey]; ok {
return fmt.Errorf("backup-policies field cannot be specified in backup config")
}

if _, ok := backupConfig[common.StorageKey]; ok {
if _, ok := backupConfig[StorageKey]; ok {
return fmt.Errorf("storage field cannot be specified in backup config")
}

if _, ok := backupConfig[common.SecretAgentsKey]; ok {
if _, ok := backupConfig[SecretAgentsKey]; ok {
return fmt.Errorf("secret-agent field cannot be specified in backup config")
}

var backupSvc AerospikeBackupService

cl, gErr := getK8sClient()
if gErr != nil {
return gErr
}

if err := cl.Get(context.TODO(),
types.NamespacedName{Name: r.Spec.BackupService.Name, Namespace: r.Spec.BackupService.Namespace},
&backupSvc); err != nil {
return err
}

var backupSvcConfig model.Config

if err := yaml.UnmarshalStrict(backupSvc.Spec.Config.Raw, &backupSvcConfig); err != nil {
backupSvcConfig, err := getBackupServiceFullConfig(k8sClient, r.Spec.BackupService.Name,
r.Spec.BackupService.Namespace)
if err != nil {
return err
}

Expand All @@ -165,7 +163,7 @@ func (r *AerospikeBackup) validateBackupConfig() error {
return err
}

err = updateValidateBackupSvcConfig(aeroClusters, backupRoutines, &backupSvcConfig)
err = updateValidateBackupSvcConfig(aeroClusters, backupRoutines, backupSvcConfig)
if err != nil {
return err
}
Expand All @@ -181,32 +179,13 @@ func (r *AerospikeBackup) validateBackupConfig() error {
return nil
}

func getK8sClient() (client.Client, error) {
restConfig := ctrl.GetConfigOrDie()

scheme := runtime.NewScheme()

utilRuntime.Must(asdbv1.AddToScheme(scheme))
utilRuntime.Must(clientGoScheme.AddToScheme(scheme))
utilRuntime.Must(AddToScheme(scheme))

cl, err := client.New(restConfig, client.Options{
Scheme: scheme,
})
if err != nil {
return nil, err
}

return cl, nil
}

func (r *AerospikeBackup) getValidatedAerospikeClusters(backupConfig map[string]interface{},
) (map[string]*model.AerospikeCluster, error) {
if _, ok := backupConfig[common.AerospikeClusterKey]; !ok {
) (map[string]*dto.AerospikeCluster, error) {
if _, ok := backupConfig[AerospikeClusterKey]; !ok {
return nil, fmt.Errorf("aerospike-cluster field is required field in backup config")
}

cluster, ok := backupConfig[common.AerospikeClusterKey].(map[string]interface{})
cluster, ok := backupConfig[AerospikeClusterKey].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("aerospike-cluster field is not in the right format")
}
Expand All @@ -216,7 +195,7 @@ func (r *AerospikeBackup) getValidatedAerospikeClusters(backupConfig map[string]
return nil, cErr
}

aeroClusters := make(map[string]*model.AerospikeCluster)
aeroClusters := make(map[string]*dto.AerospikeCluster)

if err := yaml.UnmarshalStrict(clusterBytes, &aeroClusters); err != nil {
return nil, err
Expand Down Expand Up @@ -272,8 +251,8 @@ func (r *AerospikeBackup) validateAerospikeClusterUpdate(oldObj *AerospikeBackup
return err
}

oldCluster := oldObjConfig[common.AerospikeClusterKey].(map[string]interface{})
newCluster := currentConfig[common.AerospikeClusterKey].(map[string]interface{})
oldCluster := oldObjConfig[AerospikeClusterKey].(map[string]interface{})
newCluster := currentConfig[AerospikeClusterKey].(map[string]interface{})

for clusterName := range newCluster {
if _, ok := oldCluster[clusterName]; !ok {
Expand All @@ -286,13 +265,13 @@ func (r *AerospikeBackup) validateAerospikeClusterUpdate(oldObj *AerospikeBackup

func (r *AerospikeBackup) getValidatedBackupRoutines(
backupConfig map[string]interface{},
aeroClusters map[string]*model.AerospikeCluster,
) (map[string]*model.BackupRoutine, error) {
if _, ok := backupConfig[common.BackupRoutinesKey]; !ok {
aeroClusters map[string]*dto.AerospikeCluster,
) (map[string]*dto.BackupRoutine, error) {
if _, ok := backupConfig[BackupRoutinesKey]; !ok {
return nil, fmt.Errorf("backup-routines field is required in backup config")
}

routines, ok := backupConfig[common.BackupRoutinesKey].(map[string]interface{})
routines, ok := backupConfig[BackupRoutinesKey].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("backup-routines field is not in the right format")
}
Expand All @@ -302,7 +281,7 @@ func (r *AerospikeBackup) getValidatedBackupRoutines(
return nil, rErr
}

backupRoutines := make(map[string]*model.BackupRoutine)
backupRoutines := make(map[string]*dto.BackupRoutine)

if err := yaml.UnmarshalStrict(routineBytes, &backupRoutines); err != nil {
return nil, err
Expand All @@ -329,40 +308,36 @@ func (r *AerospikeBackup) getValidatedBackupRoutines(
}

func updateValidateBackupSvcConfig(
clusters map[string]*model.AerospikeCluster,
routines map[string]*model.BackupRoutine,
backupSvcConfig *model.Config,
clusters map[string]*dto.AerospikeCluster,
routines map[string]*dto.BackupRoutine,
backupSvcConfig *dto.Config,
) error {
if len(backupSvcConfig.AerospikeClusters) == 0 {
backupSvcConfig.AerospikeClusters = make(map[string]*model.AerospikeCluster)
backupSvcConfig.AerospikeClusters = make(map[string]*dto.AerospikeCluster)
}

for name, cluster := range clusters {
backupSvcConfig.AerospikeClusters[name] = cluster
}

if len(backupSvcConfig.BackupRoutines) == 0 {
backupSvcConfig.BackupRoutines = make(map[string]*model.BackupRoutine)
backupSvcConfig.BackupRoutines = make(map[string]*dto.BackupRoutine)
}

for name, routine := range routines {
backupSvcConfig.BackupRoutines[name] = routine
}

// Add empty placeholders for missing backupSvcConfig sections. This is required for validation to work.
if backupSvcConfig.ServiceConfig == nil {
backupSvcConfig.ServiceConfig = &model.BackupServiceConfig{}
}

if backupSvcConfig.ServiceConfig.HTTPServer == nil {
backupSvcConfig.ServiceConfig.HTTPServer = &model.HTTPServerConfig{}
backupSvcConfig.ServiceConfig.HTTPServer = &dto.HTTPServerConfig{}
}

if backupSvcConfig.ServiceConfig.Logger == nil {
backupSvcConfig.ServiceConfig.Logger = &model.LoggerConfig{}
backupSvcConfig.ServiceConfig.Logger = &dto.LoggerConfig{}
}

return backupSvcConfig.Validate()
return validation.ValidateConfiguration(backupSvcConfig)
}

func (r *AerospikeBackup) NamePrefix() string {
Expand Down
53 changes: 26 additions & 27 deletions api/v1beta1/aerospikebackupservice_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"sigs.k8s.io/yaml"

"github.com/aerospike/aerospike-backup-service/pkg/model"
"github.com/aerospike/aerospike-backup-service/v2/pkg/dto"
"github.com/aerospike/aerospike-backup-service/v2/pkg/validation"
)

const minSupportedVersion = "3.0.0"

func (r *AerospikeBackupService) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Expand Down Expand Up @@ -57,15 +60,7 @@ func (r *AerospikeBackupService) ValidateCreate() (admission.Warnings, error) {

absLog.Info("Validate create")

if err := r.validateBackupServiceConfig(); err != nil {
return nil, err
}

if err := r.validateBackupServiceSecrets(); err != nil {
return nil, err
}

return nil, nil
return r.validate()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
Expand All @@ -74,15 +69,7 @@ func (r *AerospikeBackupService) ValidateUpdate(_ runtime.Object) (admission.War

absLog.Info("Validate update")

if err := r.validateBackupServiceConfig(); err != nil {
return nil, err
}

if err := r.validateBackupServiceSecrets(); err != nil {
return nil, err
}

return nil, nil
return r.validate()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
Expand All @@ -95,8 +82,24 @@ func (r *AerospikeBackupService) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

func (r *AerospikeBackupService) validate() (admission.Warnings, error) {
if err := ValidateBackupSvcVersion(r.Spec.Image); err != nil {
return nil, err
}

if err := r.validateBackupServiceConfig(); err != nil {
return nil, err
}

if err := r.validateBackupServiceSecrets(); err != nil {
return nil, err
}

return nil, nil
}

func (r *AerospikeBackupService) validateBackupServiceConfig() error {
var config model.Config
var config dto.Config

if err := yaml.UnmarshalStrict(r.Spec.Config.Raw, &config); err != nil {
return err
Expand All @@ -111,19 +114,15 @@ func (r *AerospikeBackupService) validateBackupServiceConfig() error {
}

// Add empty placeholders for missing config sections. This is required for validation to work.
if config.ServiceConfig == nil {
config.ServiceConfig = &model.BackupServiceConfig{}
}

if config.ServiceConfig.HTTPServer == nil {
config.ServiceConfig.HTTPServer = &model.HTTPServerConfig{}
config.ServiceConfig.HTTPServer = &dto.HTTPServerConfig{}
}

if config.ServiceConfig.Logger == nil {
config.ServiceConfig.Logger = &model.LoggerConfig{}
config.ServiceConfig.Logger = &dto.LoggerConfig{}
}

return config.Validate()
return validation.ValidateConfiguration(&config)
}

func (r *AerospikeBackupService) validateBackupServiceSecrets() error {
Expand Down
Loading
Loading