Skip to content

Commit

Permalink
Adding func comments and created constant.go for all constants
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekdwivedi3060 committed Jul 8, 2024
1 parent 5a0888e commit c567b49
Show file tree
Hide file tree
Showing 22 changed files with 756 additions and 177 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ help: ## Display this help.
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
cp $(ROOT_DIR)/config/crd/bases/asdb.aerospike.com_aerospikeclusters.yaml $(ROOT_DIR)/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikeclusters.asdb.aerospike.com.yaml
cp $(ROOT_DIR)/config/crd/bases/asdb.aerospike.com_aerospikebackupservices.yaml $(ROOT_DIR)/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikebackupservices.asdb.aerospike.com.yaml
cp $(ROOT_DIR)/config/crd/bases/asdb.aerospike.com_aerospikebackups.yaml $(ROOT_DIR)/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikebackups.asdb.aerospike.com.yaml
cp $(ROOT_DIR)/config/crd/bases/asdb.aerospike.com_aerospikerestores.yaml $(ROOT_DIR)/helm-charts/aerospike-kubernetes-operator/crds/customresourcedefinition_aerospikerestores.asdb.aerospike.com.yaml

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
Expand Down
2 changes: 1 addition & 1 deletion PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ resources:
path: github.com/aerospike/aerospike-kubernetes-operator/api/v1beta1
version: v1beta1
webhooks:
defaulting: true
defaulting: false
validation: true
webhookVersion: v1
- api:
Expand Down
16 changes: 12 additions & 4 deletions api/v1beta1/aerospikebackup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,34 @@ type AerospikeBackupSpec struct {
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Backup Service"
// BackupService is the backup service reference.
BackupService *BackupService `json:"backupService"`

// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Backup Config"
// Config is the configuration for the backup.
Config runtime.RawExtension `json:"config"`
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="On Demand

// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="On Demand"
// OnDemand is the on demand backup configuration.
// +kubebuilder:validation:MaxItems:=1
OnDemand []OnDemandSpec `json:"onDemand,omitempty"`
}

type BackupService struct {
Name string `json:"name"`
// Backup service name
Name string `json:"name"`

// Backup service namespace
Namespace string `json:"namespace"`
}

type OnDemandSpec struct {
// On demand backup ID
ID string `json:"id,omitempty"`
// +kubebuilder:validation:MinLength=1
ID string `json:"id"`

// Backup routine name
RoutineName string `json:"routineName"`
// Delay interval in milliseconds

// Delay interval before starting the backup.
Delay metav1.Duration `json:"delay,omitempty"`
}

Expand Down
75 changes: 64 additions & 11 deletions api/v1beta1/aerospikebackup_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ package v1beta1
import (
"context"
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/types"
utilRuntime "k8s.io/apimachinery/pkg/util/runtime"
clientGoScheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
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/abhishekdwivedi3060/aerospike-backup-service/pkg/model"
asdbv1 "github.com/aerospike/aerospike-kubernetes-operator/api/v1"
"github.com/aerospike/aerospike-kubernetes-operator/controllers/common"
)

// log is for logging in this package.
Expand All @@ -42,9 +47,6 @@ func (r *AerospikeBackup) SetupWebhookWithManager(mgr ctrl.Manager) error {
Complete()
}

//nolint:lll // for readability
//+kubebuilder:webhook:path=/mutate-asdb-aerospike-com-v1beta1-aerospikebackup,mutating=true,failurePolicy=fail,sideEffects=None,groups=asdb.aerospike.com,resources=aerospikebackups,verbs=create;update,versions=v1beta1,name=maerospikebackup.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &AerospikeBackup{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
Expand All @@ -61,6 +63,10 @@ var _ webhook.Validator = &AerospikeBackup{}
func (r *AerospikeBackup) ValidateCreate() (admission.Warnings, error) {
aerospikebackuplog.Info("validate create", "name", r.Name)

if len(r.Spec.OnDemand) != 0 && r.Spec.Config.Raw != nil {
return nil, fmt.Errorf("onDemand and backup config cannot be specified together while creating backup")
}

if err := r.validateBackupConfig(); err != nil {
return nil, err
}
Expand All @@ -69,13 +75,30 @@ func (r *AerospikeBackup) ValidateCreate() (admission.Warnings, error) {
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *AerospikeBackup) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
func (r *AerospikeBackup) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
aerospikebackuplog.Info("validate update", "name", r.Name)

oldObj := old.(*AerospikeBackup)

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

if len(r.Spec.OnDemand) > 0 && len(oldObj.Spec.OnDemand) > 0 {
// Check if onDemand backup spec is updated
if r.Spec.OnDemand[0].ID == oldObj.Spec.OnDemand[0].ID &&
!reflect.DeepEqual(r.Spec.OnDemand[0], oldObj.Spec.OnDemand[0]) {
return nil, fmt.Errorf("onDemand backup cannot be updated")
}

// Check if previous onDemand backup is completed before allowing new onDemand backup
if r.Spec.OnDemand[0].ID != oldObj.Spec.OnDemand[0].ID && (len(r.Status.OnDemand) == 0 ||
r.Status.OnDemand[0].ID != oldObj.Spec.OnDemand[0].ID) {
return nil,
fmt.Errorf("can not add new onDemand backup when previous onDemand backup is not completed")
}
}

return nil, nil
}

Expand Down Expand Up @@ -113,11 +136,11 @@ func (r *AerospikeBackup) validateBackupConfig() error {
return err
}

if _, ok := configMap["aerospike-cluster"]; !ok {
if _, ok := configMap[common.AerospikeClusterKey]; !ok {
return fmt.Errorf("aerospike-cluster field is required in config")
}

cluster, ok := configMap["aerospike-cluster"].(map[string]interface{})
cluster, ok := configMap[common.AerospikeClusterKey].(map[string]interface{})
if !ok {
return fmt.Errorf("aerospike-cluster field is not in the right format")
}
Expand All @@ -141,11 +164,11 @@ func (r *AerospikeBackup) validateBackupConfig() error {
config.AerospikeClusters[name] = aeroCluster
}

if _, ok = configMap["backup-routines"]; !ok {
if _, ok = configMap[common.BackupRoutinesKey]; !ok {
return fmt.Errorf("backup-routines field is required in config")
}

routines, ok := configMap["backup-routines"].(map[string]interface{})
routines, ok := configMap[common.BackupRoutinesKey].(map[string]interface{})
if !ok {
return fmt.Errorf("backup-routines field is not in the right format")
}
Expand All @@ -169,14 +192,44 @@ func (r *AerospikeBackup) validateBackupConfig() error {
config.BackupRoutines[name] = routine
}

return config.Validate()
// 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{}
}

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

if err := config.Validate(); err != nil {
return nil
}

// Validate on-demand backup
if len(r.Spec.OnDemand) > 0 {
if _, ok = config.BackupRoutines[r.Spec.OnDemand[0].RoutineName]; !ok {
return fmt.Errorf("backup routine %s not found", r.Spec.OnDemand[0].RoutineName)
}
}

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: clientGoScheme.Scheme,
Scheme: scheme,
})
if err != nil {
return nil, err
Expand Down
18 changes: 15 additions & 3 deletions api/v1beta1/aerospikebackupservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ import (
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// AerospikeBackupServiceSpec defines the desired state of AerospikeBackupService
//
//nolint:govet // for readability
type AerospikeBackupServiceSpec struct {
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Backup Service Image"
// Image is the image for the backup service.
Image string `json:"image"`
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Backup Service Config

// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Backup Service Config"
// Config is the configuration for the backup service.
Config runtime.RawExtension `json:"config"`

// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Resources"
// Resources is the resource requirements for the backup service.
Resources corev1.ResourceRequirements `json:"resources,omitempty"`

// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Backup Service Volume"
// SecretMounts is the list of secret to be mounted in the backup service.
SecretMounts []SecretMount `json:"secrets,omitempty"`

// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Backup Service"
Expand All @@ -45,9 +52,14 @@ type AerospikeBackupServiceSpec struct {

// AerospikeBackupServiceStatus defines the observed state of AerospikeBackupService
type AerospikeBackupServiceStatus struct {
// Backup Service API context path
ContextPath string `json:"contextPath"`
ConfigHash string `json:"configHash"`
Port int32 `json:"port"`

// Backup service config hash
ConfigHash string `json:"configHash"`

// Backup service listening port
Port int32 `json:"port"`
}

//+kubebuilder:object:root=true
Expand Down
13 changes: 13 additions & 0 deletions api/v1beta1/aerospikebackupservice_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,18 @@ func (r *AerospikeBackupService) validateBackupServiceConfig() error {
return err
}

// 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{}
}

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

return config.Validate()
}
11 changes: 9 additions & 2 deletions api/v1beta1/aerospikerestore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (

// AerospikeRestoreSpec defines the desired state of AerospikeRestore
// +k8s:openapi-gen=true
//
//nolint:govet // for readability
type AerospikeRestoreSpec struct {
// +operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Backup Service"
// BackupService is the backup service reference.
Expand All @@ -61,18 +63,23 @@ type AerospikeRestoreSpec struct {

// AerospikeRestoreStatus defines the observed state of AerospikeRestore
type AerospikeRestoreStatus struct {
// JobID is the restore operation job id.
JobID *int64 `json:"job-id,omitempty"`
// Phase denotes the current phase of Aerospike restore operation.
Phase AerospikeRestorePhase `json:"phase,omitempty"`

// RestoreResult is the result of the restore operation.
RestoreResult runtime.RawExtension `json:"restoreResult,omitempty"`

// Phase denotes the current phase of Aerospike restore operation.
Phase AerospikeRestorePhase `json:"phase,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.restoreResult.status`

// AerospikeRestore is the Schema for the aerospikerestores API
//
//nolint:govet // auto-generated
type AerospikeRestore struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
10 changes: 8 additions & 2 deletions config/crd/bases/asdb.aerospike.com_aerospikebackups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ spec:
description: BackupService is the backup service reference.
properties:
name:
description: Backup service name
type: string
namespace:
description: Backup service namespace
type: string
required:
- name
Expand All @@ -54,15 +56,17 @@ spec:
items:
properties:
delay:
description: Delay interval in milliseconds
description: Delay interval before starting the backup.
type: string
id:
description: On demand backup ID
minLength: 1
type: string
routineName:
description: Backup routine name
type: string
required:
- id
- routineName
type: object
maxItems: 1
Expand All @@ -78,15 +82,17 @@ spec:
items:
properties:
delay:
description: Delay interval in milliseconds
description: Delay interval before starting the backup.
type: string
id:
description: On demand backup ID
minLength: 1
type: string
routineName:
description: Backup routine name
type: string
required:
- id
- routineName
type: object
type: array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ spec:
description: Image is the image for the backup service.
type: string
resources:
description: ResourceRequirements describes the compute resource requirements.
description: Resources is the resource requirements for the backup
service.
properties:
claims:
description: "Claims lists the names of resources, defined in
Expand Down Expand Up @@ -91,6 +92,8 @@ spec:
type: object
type: object
secrets:
description: SecretMounts is the list of secret to be mounted in the
backup service.
items:
properties:
secretName:
Expand Down Expand Up @@ -155,10 +158,13 @@ spec:
AerospikeBackupService
properties:
configHash:
description: Backup service config hash
type: string
contextPath:
description: Backup Service API context path
type: string
port:
description: Backup service listening port
format: int32
type: integer
required:
Expand Down
Loading

0 comments on commit c567b49

Please sign in to comment.