-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
178 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package database | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/Azure/azure-service-operator/api/v1alpha1" | ||
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" | ||
"github.com/Azure/azure-service-operator/pkg/errhelp" | ||
"github.com/Azure/azure-service-operator/pkg/helpers" | ||
"github.com/Azure/azure-service-operator/pkg/resourcemanager" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
func (m *MySQLDatabaseClient) Ensure(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) { | ||
|
||
instance, err := m.convert(obj) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
client := getMySQLDatabasesClient() | ||
|
||
instance.Status.Provisioning = true | ||
// Check if this database already exists. This is required | ||
// to overcome the issue with the lack of idempotence of the Create call | ||
|
||
_, err = m.GetDatabase(ctx, instance.Spec.ResourceGroup, instance.Spec.Server, instance.Name) | ||
if err == nil { | ||
instance.Status.Provisioned = true | ||
instance.Status.Provisioning = false | ||
instance.Status.Message = resourcemanager.SuccessMsg | ||
return true, nil | ||
} | ||
future, err := m.CreateDatabaseIfValid( | ||
ctx, | ||
instance.Name, | ||
instance.Spec.Server, | ||
instance.Spec.ResourceGroup, | ||
) | ||
|
||
if err != nil { | ||
// let the user know what happened | ||
instance.Status.Message = err.Error() | ||
instance.Status.Provisioning = false | ||
// errors we expect might happen that we are ok with waiting for | ||
catch := []string{ | ||
errhelp.ResourceGroupNotFoundErrorCode, | ||
errhelp.ParentNotFoundErrorCode, | ||
errhelp.NotFoundErrorCode, | ||
errhelp.AsyncOpIncompleteError, | ||
errhelp.ResourceNotFound, | ||
} | ||
|
||
azerr := errhelp.NewAzureErrorAzureError(err) | ||
if helpers.ContainsString(catch, azerr.Type) { | ||
// most of these error technically mean the resource is actually not provisioning | ||
switch azerr.Type { | ||
case errhelp.AsyncOpIncompleteError: | ||
instance.Status.Provisioning = true | ||
} | ||
// reconciliation is not done but error is acceptable | ||
return false, nil | ||
} | ||
// reconciliation not done and we don't know what happened | ||
return false, err | ||
} | ||
|
||
_, err = future.Result(client) | ||
if err != nil { | ||
// let the user know what happened | ||
instance.Status.Message = err.Error() | ||
instance.Status.Provisioning = false | ||
// errors we expect might happen that we are ok with waiting for | ||
catch := []string{ | ||
errhelp.ResourceGroupNotFoundErrorCode, | ||
errhelp.ParentNotFoundErrorCode, | ||
errhelp.NotFoundErrorCode, | ||
errhelp.AsyncOpIncompleteError, | ||
errhelp.SubscriptionDoesNotHaveServer, | ||
} | ||
|
||
azerr := errhelp.NewAzureErrorAzureError(err) | ||
if helpers.ContainsString(catch, azerr.Type) { | ||
// most of these error technically mean the resource is actually not provisioning | ||
switch azerr.Type { | ||
case errhelp.AsyncOpIncompleteError: | ||
instance.Status.Provisioning = true | ||
case errhelp.SubscriptionDoesNotHaveServer: | ||
instance.Status.Message = fmt.Sprintf("The PostgreSQL Server %s has not been provisioned yet. ", instance.Spec.Server) | ||
} | ||
// reconciliation is not done but error is acceptable | ||
return false, nil | ||
} | ||
// reconciliation not done and we don't know what happened | ||
return false, err | ||
} | ||
|
||
if instance.Status.Provisioning { | ||
instance.Status.Provisioned = true | ||
instance.Status.Provisioning = false | ||
instance.Status.Message = resourcemanager.SuccessMsg | ||
} else { | ||
instance.Status.Provisioned = false | ||
instance.Status.Provisioning = true | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (m *MySQLDatabaseClient) Delete(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) { | ||
|
||
instance, err := m.convert(obj) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
status, err := m.DeleteDatabase(ctx, instance.Name, instance.Spec.Server, instance.Spec.ResourceGroup) | ||
if err != nil { | ||
if !errhelp.IsAsynchronousOperationNotComplete(err) { | ||
return true, err | ||
} | ||
} | ||
|
||
if err == nil { | ||
if status != "InProgress" { | ||
return false, nil | ||
} | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (m *MySQLDatabaseClient) GetParents(obj runtime.Object) ([]resourcemanager.KubeParent, error) { | ||
|
||
instance, err := m.convert(obj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []resourcemanager.KubeParent{ | ||
{ | ||
Key: types.NamespacedName{ | ||
Namespace: instance.Namespace, | ||
Name: instance.Spec.Server, | ||
}, | ||
Target: &azurev1alpha1.PostgreSQLServer{}, | ||
}, | ||
{ | ||
Key: types.NamespacedName{ | ||
Namespace: instance.Namespace, | ||
Name: instance.Spec.ResourceGroup, | ||
}, | ||
Target: &azurev1alpha1.ResourceGroup{}, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (m *MySQLDatabaseClient) GetStatus(obj runtime.Object) (*v1alpha1.ASOStatus, error) { | ||
|
||
instance, err := m.convert(obj) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &instance.Status, nil | ||
} | ||
|
||
func (m *MySQLDatabaseClient) convert(obj runtime.Object) (*v1alpha1.MySQLDatabase, error) { | ||
local, ok := obj.(*v1alpha1.MySQLDatabase) | ||
if !ok { | ||
return nil, fmt.Errorf("failed type assertion on kind: %s", obj.GetObjectKind().GroupVersionKind().String()) | ||
} | ||
return local, nil | ||
} |