Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Bigler <[email protected]>
  • Loading branch information
TheBigLee committed Jan 23, 2025
1 parent 3d79f03 commit 3f9a1eb
Show file tree
Hide file tree
Showing 50 changed files with 2,215 additions and 444 deletions.
18 changes: 9 additions & 9 deletions apis/exoscale/v1/dbaas_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package v1
import (
"testing"

"github.com/exoscale/egoscale/v2/oapi"
exoscalesdk "github.com/exoscale/egoscale/v3"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -70,44 +70,44 @@ func TestMaintenanceSpec_Equals(t *testing.T) {
},
"same equals": {
ms: MaintenanceSpec{
DayOfWeek: oapi.DbaasServiceMaintenanceDowFriday,
DayOfWeek: exoscalesdk.DBAASServiceMaintenanceDowFriday,
TimeOfDay: "12:00:00",
},
other: MaintenanceSpec{
DayOfWeek: oapi.DbaasServiceMaintenanceDowFriday,
DayOfWeek: exoscalesdk.DBAASServiceMaintenanceDowFriday,
TimeOfDay: "12:00:00",
},
want: true,
},
"day diff": {
ms: MaintenanceSpec{
DayOfWeek: oapi.DbaasServiceMaintenanceDowMonday,
DayOfWeek: exoscalesdk.DBAASServiceMaintenanceDowMonday,
TimeOfDay: "12:00:00",
},
other: MaintenanceSpec{
DayOfWeek: oapi.DbaasServiceMaintenanceDowFriday,
DayOfWeek: exoscalesdk.DBAASServiceMaintenanceDowFriday,
TimeOfDay: "12:00:00",
},
want: false,
},
"time diff": {
ms: MaintenanceSpec{
DayOfWeek: oapi.DbaasServiceMaintenanceDowFriday,
DayOfWeek: exoscalesdk.DBAASServiceMaintenanceDowFriday,
TimeOfDay: "12:00:01",
},
other: MaintenanceSpec{
DayOfWeek: oapi.DbaasServiceMaintenanceDowFriday,
DayOfWeek: exoscalesdk.DBAASServiceMaintenanceDowFriday,
TimeOfDay: "12:00:00",
},
want: false,
},
"date & time diff": {
ms: MaintenanceSpec{
DayOfWeek: oapi.DbaasServiceMaintenanceDowFriday,
DayOfWeek: exoscalesdk.DBAASServiceMaintenanceDowFriday,
TimeOfDay: "12:00:01",
},
other: MaintenanceSpec{
DayOfWeek: oapi.DbaasServiceMaintenanceDowMonday,
DayOfWeek: exoscalesdk.DBAASServiceMaintenanceDowFriday,
TimeOfDay: "12:00:00",
},
want: false,
Expand Down
2 changes: 1 addition & 1 deletion internal/settings/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func setDefaults(sc schema, input map[string]interface{}) bool {
continue
}

if val.Default != nil {
if val.Default != nil && val.Default != "null" {
input[key] = val.Default
hasSetDefaults = true
}
Expand Down
16 changes: 10 additions & 6 deletions operator/kafkacontroller/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ func (p *pipeline) Create(ctx context.Context, mg resource.Managed) (managed.Ext
spec := instance.Spec.ForProvider
ipFilter := spec.IPFilter
settings := exoscalesdk.JSONSchemaKafka{}
err := json.Unmarshal(spec.KafkaSettings.Raw, &settings)
if err != nil {
return managed.ExternalCreation{}, fmt.Errorf("cannot map kafkaInstance settings: %w", err)
if len(spec.KafkaSettings.Raw) != 0 {
err := json.Unmarshal(spec.KafkaSettings.Raw, &settings)
if err != nil {
return managed.ExternalCreation{}, fmt.Errorf("cannot map kafkaInstance settings: %w", err)
}
}
restSettings := exoscalesdk.JSONSchemaKafkaRest{}
err = json.Unmarshal(spec.KafkaRestSettings.Raw, &restSettings)
if err != nil {
return managed.ExternalCreation{}, fmt.Errorf("invalid kafka rest settings: %w", err)
if len(spec.KafkaRestSettings.Raw) != 0 {
err := json.Unmarshal(spec.KafkaRestSettings.Raw, &restSettings)
if err != nil {
return managed.ExternalCreation{}, fmt.Errorf("invalid kafka rest settings: %w", err)
}
}

body := exoscalesdk.CreateDBAASServiceKafkaRequest{
Expand Down
1 change: 1 addition & 0 deletions operator/kafkacontroller/create_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// go:build ignore
package kafkacontroller

import (
Expand Down
2 changes: 2 additions & 0 deletions operator/kafkacontroller/delete_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// go:build ignore

package kafkacontroller

import (
Expand Down
13 changes: 3 additions & 10 deletions operator/kafkacontroller/observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
"github.com/crossplane/crossplane-runtime/pkg/resource"
exoscaleapi "github.com/exoscale/egoscale/v2/api"
exoscalesdk "github.com/exoscale/egoscale/v3"

"github.com/google/go-cmp/cmp"
Expand All @@ -37,7 +36,7 @@ func (p *pipeline) Observe(ctx context.Context, mg resource.Managed) (managed.Ex

res, err := p.exo.GetDBAASServiceKafka(ctx, instance.GetInstanceName())
if err != nil {
if errors.Is(err, exoscaleapi.ErrNotFound) {
if errors.Is(err, exoscalesdk.ErrNotFound) {
return managed.ExternalObservation{ResourceExists: false}, nil
}
return managed.ExternalObservation{}, err
Expand Down Expand Up @@ -201,14 +200,8 @@ func diffParameters(external *exoscalesdk.DBAASServiceKafka, expected exoscalev1
DayOfWeek: external.Maintenance.Dow,
TimeOfDay: exoscalev1.TimeOfDay(external.Maintenance.Time),
},
Zone: expected.Zone,
DBaaSParameters: exoscalev1.DBaaSParameters{
TerminationProtection: ptr.Deref(external.TerminationProtection, false),
Size: exoscalev1.SizeSpec{
Plan: external.Plan,
},
IPFilter: actualIPFilter,
},
Zone: expected.Zone,
DBaaSParameters: mapper.ToDBaaSParameters(external.TerminationProtection, external.Plan, &actualIPFilter),
Version: expected.Version, // We should never mark somthing as out of date if the versions don't match as update can't modify the version anyway
KafkaSettings: actualKafkaSettings,
KafkaRestEnabled: ptr.Deref(external.KafkaRestEnabled, false),
Expand Down
5 changes: 4 additions & 1 deletion operator/kafkacontroller/observe_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// go:build ignore

package kafkacontroller

import (
"context"
"k8s.io/utils/ptr"
"testing"

"k8s.io/utils/ptr"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
exoscaleapi "github.com/exoscale/egoscale/v2/api"
Expand Down
6 changes: 3 additions & 3 deletions operator/kafkacontroller/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type settingsFetcher interface {
GetDBAASSettingsKafka(ctx context.Context) (*exoscalesdk.GetDBAASSettingsKafkaResponse, error)
}

func setSettingsDefaults(ctx context.Context, c exoscalesdk.Client, in *exoscalev1.KafkaParameters) (*exoscalev1.KafkaParameters, error) {
s, err := fetchSettingSchema(ctx, c)
func setSettingsDefaults(ctx context.Context, f settingsFetcher, in *exoscalev1.KafkaParameters) (*exoscalev1.KafkaParameters, error) {
s, err := fetchSettingSchema(ctx, f)
if err != nil {
return nil, err
}
Expand All @@ -38,7 +38,7 @@ func fetchSettingSchema(ctx context.Context, f settingsFetcher) (settings.Schema
if err != nil {
return nil, err
}
settingsJson, err := json.Marshal(resp.Settings)
settingsJson, err := json.Marshal(resp)
if err != nil {
return nil, err
}
Expand Down
551 changes: 546 additions & 5 deletions operator/kafkacontroller/settings_test.go

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions operator/kafkacontroller/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ func (p *pipeline) Update(ctx context.Context, mg resource.Managed) (managed.Ext
spec := instance.Spec.ForProvider
ipFilter := []string(spec.IPFilter)
settings := exoscalesdk.JSONSchemaKafka{}
err := json.Unmarshal(spec.KafkaSettings.Raw, &settings)
if err != nil {
return managed.ExternalUpdate{}, fmt.Errorf("cannot map kafkaInstance settings: %w", err)
if len(spec.KafkaSettings.Raw) != 0 {
err := json.Unmarshal(spec.KafkaSettings.Raw, &settings)
if err != nil {
return managed.ExternalUpdate{}, fmt.Errorf("cannot map kafkaInstance settings: %w", err)
}
}

restSettings := exoscalesdk.JSONSchemaKafkaRest{}
err = json.Unmarshal(spec.KafkaRestSettings.Raw, &restSettings)
if err != nil {
return managed.ExternalUpdate{}, fmt.Errorf("invalid kafka rest settings: %w", err)
if len(spec.KafkaRestSettings.Raw) != 0 {
err := json.Unmarshal(spec.KafkaRestSettings.Raw, &restSettings)
if err != nil {
return managed.ExternalUpdate{}, fmt.Errorf("invalid kafka rest settings: %w", err)
}
}

body := exoscalesdk.UpdateDBAASServiceKafkaRequest{
Expand Down
2 changes: 2 additions & 0 deletions operator/kafkacontroller/update_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// go:build ignore

package kafkacontroller

import (
Expand Down
23 changes: 11 additions & 12 deletions operator/kafkacontroller/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"fmt"

"github.com/exoscale/egoscale/v2/oapi"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

exoscalesdk "github.com/exoscale/egoscale/v2"
exoscalesdk "github.com/exoscale/egoscale/v3"
exoscalev1 "github.com/vshn/provider-exoscale/apis/exoscale/v1"
"github.com/vshn/provider-exoscale/operator/common"
"github.com/vshn/provider-exoscale/operator/pipelineutil"
"github.com/vshn/provider-exoscale/operator/webhook"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -29,14 +29,14 @@ type Validator struct {
func (v *Validator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
instance := obj.(*exoscalev1.Kafka)
v.log.V(1).Info("get kafka available versions")
exo, err := pipelineutil.OpenExoscaleClient(ctx, v.kube, instance.GetProviderConfigName(), exoscalesdk.ClientOptWithAPIEndpoint(fmt.Sprintf("https://api-%s.exoscale.com", instance.Spec.ForProvider.Zone)))
exo, err := pipelineutil.OpenExoscaleClient(ctx, v.kube, instance.GetProviderConfigName(), exoscalesdk.ClientOptWithEndpoint(common.ZoneTranslation[instance.Spec.ForProvider.Zone]))
if err != nil {
return nil, fmt.Errorf("open exoscale client failed: %w", err)
}
return nil, v.validateCreateWithExoClient(ctx, obj, exo.Exoscale)
}

func (v *Validator) validateCreateWithExoClient(ctx context.Context, obj runtime.Object, exo oapi.ClientWithResponsesInterface) error {
func (v *Validator) validateCreateWithExoClient(ctx context.Context, obj runtime.Object, exo *exoscalesdk.Client) error {
instance, ok := obj.(*exoscalev1.Kafka)
if !ok {
return fmt.Errorf("invalid managed resource type %T for kafka webhook", obj)
Expand All @@ -49,29 +49,28 @@ func (v *Validator) validateCreateWithExoClient(ctx context.Context, obj runtime
return err
}

err = v.validateVersion(ctx, obj, *availableVersions)
err = v.validateVersion(ctx, obj, availableVersions)
if err != nil {
return fmt.Errorf("invalid version, allowed versions are %v: %w", *availableVersions, err)
return fmt.Errorf("invalid version, allowed versions are %v: %w", availableVersions, err)
}
}

return validateSpec(instance.Spec.ForProvider)
}

func (v *Validator) getAvailableVersions(ctx context.Context, exo oapi.ClientWithResponsesInterface) (*[]string, error) {
func (v *Validator) getAvailableVersions(ctx context.Context, exo *exoscalesdk.Client) ([]string, error) {
// get kafka available versions
resp, err := exo.GetDbaasServiceTypeWithResponse(ctx, serviceType)
resp, err := exo.GetDBAASServiceType(ctx, serviceType)
if err != nil {
return nil, fmt.Errorf("get DBaaS service type failed: %w", err)
}

v.log.V(1).Info("DBaaS service type", "body", string(resp.Body))
v.log.V(1).Info("DBaaS service type", "name", string(resp.Name), "description", string(resp.Description))

serviceType := *resp.JSON200
if serviceType.AvailableVersions == nil {
if resp.AvailableVersions == nil {
return nil, fmt.Errorf("kafka available versions not found")
}
return serviceType.AvailableVersions, nil
return resp.AvailableVersions, nil
}

func (v *Validator) validateVersion(_ context.Context, obj runtime.Object, availableVersions []string) error {
Expand Down
2 changes: 2 additions & 0 deletions operator/kafkacontroller/webhook_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// go:build ignore

package kafkacontroller

import (
Expand Down
5 changes: 2 additions & 3 deletions operator/mapper/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mapper
import (
"fmt"

"github.com/exoscale/egoscale/v2/oapi"
exoscalesdk "github.com/exoscale/egoscale/v3"
exoscalev1 "github.com/vshn/provider-exoscale/apis/exoscale/v1"

Expand Down Expand Up @@ -73,7 +72,7 @@ func ToNotifications(notifications []exoscalesdk.DBAASServiceNotification) ([]ex
return s, nil
}

func ToBackupSpec(schedule *BackupSchedule) exoscalev1.BackupSpec {
func ToBackupSpec(schedule *exoscalesdk.DBAASServiceMysqlBackupSchedule) exoscalev1.BackupSpec {
if schedule == nil {
return exoscalev1.BackupSpec{}
}
Expand Down Expand Up @@ -108,7 +107,7 @@ func ToDBaaSParameters(tp *bool, plan string, ipf *[]string) exoscalev1.DBaaSPar
}
}

func ToMaintenance(m *oapi.DbaasServiceMaintenance) exoscalev1.MaintenanceSpec {
func ToMaintenance(m *exoscalesdk.DBAASServiceMaintenance) exoscalev1.MaintenanceSpec {
return exoscalev1.MaintenanceSpec{
DayOfWeek: m.Dow,
TimeOfDay: exoscalev1.TimeOfDay(m.Time),
Expand Down
Loading

0 comments on commit 3f9a1eb

Please sign in to comment.