-
Notifications
You must be signed in to change notification settings - Fork 40
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
Add some features that ob-operator supports to dashboard #659
Changes from all commits
68c5162
f870b3e
3c24736
56a39ed
8760bd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,13 @@ import ( | |
|
||
"github.com/google/uuid" | ||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
logger "github.com/sirupsen/logrus" | ||
corev1 "k8s.io/api/core/v1" | ||
apiresource "k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/oceanbase/ob-operator/api/constants" | ||
apitypes "github.com/oceanbase/ob-operator/api/types" | ||
"github.com/oceanbase/ob-operator/api/v1alpha1" | ||
"github.com/oceanbase/ob-operator/internal/clients" | ||
|
@@ -35,7 +37,9 @@ import ( | |
modelcommon "github.com/oceanbase/ob-operator/internal/dashboard/model/common" | ||
"github.com/oceanbase/ob-operator/internal/dashboard/model/param" | ||
"github.com/oceanbase/ob-operator/internal/dashboard/model/response" | ||
"github.com/oceanbase/ob-operator/internal/dashboard/utils" | ||
oberr "github.com/oceanbase/ob-operator/pkg/errors" | ||
models "github.com/oceanbase/ob-operator/pkg/oceanbase-sdk/model" | ||
) | ||
|
||
const ( | ||
|
@@ -115,11 +119,16 @@ func buildOBClusterResponse(ctx context.Context, obcluster *v1alpha1.OBCluster) | |
// TODO: add metrics | ||
Metrics: nil, | ||
} | ||
var parameters []modelcommon.KVPair | ||
var parameters []response.ParameterSpec | ||
statusParameterMap := make(map[string]string, 0) | ||
for _, param := range obcluster.Status.Parameters { | ||
statusParameterMap[param.Name] = param.Value | ||
} | ||
for _, param := range obcluster.Spec.Parameters { | ||
parameters = append(parameters, modelcommon.KVPair{ | ||
Key: param.Name, | ||
Value: param.Value, | ||
parameters = append(parameters, response.ParameterSpec{ | ||
Name: param.Name, | ||
SpecValue: param.Value, | ||
Value: statusParameterMap[param.Name], | ||
}) | ||
} | ||
respCluster.Parameters = parameters | ||
|
@@ -537,6 +546,7 @@ func generateOBClusterInstance(param *param.CreateOBClusterParam) *v1alpha1.OBCl | |
Parameters: parameters, | ||
Topology: topology, | ||
UserSecrets: generateUserSecrets(param.Name, param.ClusterId), | ||
Scenario: param.Scenario, | ||
}, | ||
} | ||
switch param.Mode { | ||
|
@@ -546,6 +556,12 @@ func generateOBClusterInstance(param *param.CreateOBClusterParam) *v1alpha1.OBCl | |
obcluster.Annotations[oceanbaseconst.AnnotationsMode] = oceanbaseconst.ModeService | ||
default: | ||
} | ||
if param.DeletionProtection { | ||
obcluster.Annotations[oceanbaseconst.AnnotationsIgnoreDeletion] = "true" | ||
} | ||
if param.PvcIndependent { | ||
obcluster.Annotations[oceanbaseconst.AnnotationsIndependentPVCLifecycle] = "true" | ||
} | ||
return obcluster | ||
} | ||
|
||
|
@@ -723,3 +739,175 @@ func GetOBClusterStatistic(ctx context.Context) ([]response.OBClusterStatistic, | |
}) | ||
return statisticResult, nil | ||
} | ||
|
||
func PatchOBCluster(ctx context.Context, nn *param.K8sObjectIdentity, param *param.PatchOBClusterParam) (*response.OBCluster, error) { | ||
obcluster, err := clients.GetOBCluster(ctx, nn.Namespace, nn.Name) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Get obcluster %s %s", nn.Namespace, nn.Name) | ||
} | ||
if obcluster.Status.Status != clusterstatus.Running { | ||
return nil, errors.Errorf("OBCluster status is invalid %s", obcluster.Status.Status) | ||
} | ||
alreadyIgnoredDeletion := obcluster.Annotations[oceanbaseconst.AnnotationsIgnoreDeletion] == "true" | ||
|
||
if obcluster.Spec.OBServerTemplate != nil { | ||
// Update resource if specified | ||
obcluster.Spec.OBServerTemplate.Resource = &apitypes.ResourceSpec{ | ||
Cpu: *apiresource.NewQuantity(param.Resource.Cpu, apiresource.DecimalSI), | ||
Memory: *apiresource.NewQuantity(param.Resource.MemoryGB*constant.GB, apiresource.BinarySI), | ||
} | ||
} else if param.Storage != nil && obcluster.Spec.OBServerTemplate != nil { | ||
// Update storage if specified | ||
obcluster.Spec.OBServerTemplate.Storage = &apitypes.OceanbaseStorageSpec{ | ||
DataStorage: &apitypes.StorageSpec{ | ||
StorageClass: param.Storage.Data.StorageClass, | ||
Size: *apiresource.NewQuantity(param.Storage.Data.SizeGB*constant.GB, apiresource.BinarySI), | ||
}, | ||
RedoLogStorage: &apitypes.StorageSpec{ | ||
StorageClass: param.Storage.RedoLog.StorageClass, | ||
Size: *apiresource.NewQuantity(param.Storage.RedoLog.SizeGB*constant.GB, apiresource.BinarySI), | ||
}, | ||
LogStorage: &apitypes.StorageSpec{ | ||
StorageClass: param.Storage.Log.StorageClass, | ||
Size: *apiresource.NewQuantity(param.Storage.Log.SizeGB*constant.GB, apiresource.BinarySI), | ||
}, | ||
} | ||
} else if param.Monitor != nil && obcluster.Spec.MonitorTemplate == nil { | ||
// Update monitor if specified | ||
obcluster.Spec.MonitorTemplate = &apitypes.MonitorTemplate{ | ||
Image: param.Monitor.Image, | ||
Resource: &apitypes.ResourceSpec{ | ||
Cpu: *apiresource.NewQuantity(param.Monitor.Resource.Cpu, apiresource.DecimalSI), | ||
Memory: *apiresource.NewQuantity(param.Monitor.Resource.MemoryGB*constant.GB, apiresource.BinarySI), | ||
}, | ||
} | ||
} else if param.RemoveMonitor { | ||
// Remove monitor if specified | ||
obcluster.Spec.MonitorTemplate = nil | ||
} else if param.BackupVolume != nil && obcluster.Spec.BackupVolume == nil { | ||
// Update backup volume if specified | ||
obcluster.Spec.BackupVolume = &apitypes.BackupVolumeSpec{ | ||
Volume: &corev1.Volume{ | ||
Name: "ob-backup", | ||
VolumeSource: corev1.VolumeSource{ | ||
NFS: &corev1.NFSVolumeSource{ | ||
Server: param.BackupVolume.Address, | ||
Path: param.BackupVolume.Path, | ||
ReadOnly: false, | ||
}, | ||
}, | ||
}, | ||
} | ||
} else if param.RemoveBackupVolume { | ||
// Remove backup volume if specified | ||
obcluster.Spec.BackupVolume = nil | ||
} else if len(param.Parameters) > 0 { | ||
// Update parameters if specified | ||
obcluster.Spec.Parameters = buildOBClusterParameters(param.Parameters) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will overwrite all the parameters There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that's what I want. The front end app does not filter which parameters are added and which are removed. |
||
} | ||
|
||
if param.AddDeletionProtection && !alreadyIgnoredDeletion { | ||
// Update deletion protection if specified | ||
obcluster.Annotations[oceanbaseconst.AnnotationsIgnoreDeletion] = "true" | ||
} else if param.RemoveDeletionProtection && alreadyIgnoredDeletion { | ||
delete(obcluster.Annotations, oceanbaseconst.AnnotationsIgnoreDeletion) | ||
} | ||
|
||
cluster, err := clients.UpdateOBCluster(ctx, obcluster) | ||
if err != nil { | ||
return nil, oberr.NewInternal(err.Error()) | ||
} | ||
return buildOBClusterResponse(ctx, cluster) | ||
} | ||
|
||
func RestartOBServers(ctx context.Context, nn *param.K8sObjectIdentity, param *param.RestartOBServersParam) (*response.OBCluster, error) { | ||
obcluster, err := clients.GetOBCluster(ctx, nn.Namespace, nn.Name) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Get obcluster %s %s", nn.Namespace, nn.Name) | ||
} | ||
if obcluster.Status.Status != clusterstatus.Running { | ||
return nil, errors.Errorf("OBCluster status is invalid %s", obcluster.Status.Status) | ||
} | ||
|
||
// Create OBClusterOperation for restarting observers | ||
operation := &v1alpha1.OBClusterOperation{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "restart-observers-", | ||
Namespace: nn.Namespace, | ||
}, | ||
Spec: v1alpha1.OBClusterOperationSpec{ | ||
Type: constants.ClusterOpTypeRestartOBServers, | ||
OBCluster: nn.Name, | ||
RestartOBServers: &v1alpha1.RestartOBServersConfig{ | ||
OBServers: param.OBServers, | ||
OBZones: param.OBZones, | ||
All: param.All, | ||
}, | ||
}, | ||
} | ||
|
||
_, err = clients.CreateOBClusterOperation(ctx, operation) | ||
if err != nil { | ||
return nil, oberr.NewInternal(err.Error()) | ||
} | ||
|
||
return buildOBClusterResponse(ctx, obcluster) | ||
} | ||
|
||
func DeleteOBServers(ctx context.Context, nn *param.K8sObjectIdentity, param *param.DeleteOBServersParam) (*response.OBCluster, error) { | ||
obcluster, err := clients.GetOBCluster(ctx, nn.Namespace, nn.Name) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Get obcluster %s %s", nn.Namespace, nn.Name) | ||
} | ||
if obcluster.Status.Status != clusterstatus.Running { | ||
return nil, errors.Errorf("OBCluster status is invalid %s", obcluster.Status.Status) | ||
} | ||
|
||
// Create OBClusterOperation for deleting observers | ||
operation := &v1alpha1.OBClusterOperation{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "delete-observers-", | ||
Namespace: nn.Namespace, | ||
}, | ||
Spec: v1alpha1.OBClusterOperationSpec{ | ||
Type: constants.ClusterOpTypeDeleteOBServers, | ||
OBCluster: nn.Name, | ||
DeleteOBServers: &v1alpha1.DeleteOBServersConfig{ | ||
OBServers: param.OBServers, | ||
}, | ||
}, | ||
} | ||
|
||
_, err = clients.CreateOBClusterOperation(ctx, operation) | ||
if err != nil { | ||
return nil, oberr.NewInternal(err.Error()) | ||
} | ||
|
||
return buildOBClusterResponse(ctx, obcluster) | ||
} | ||
|
||
func ListOBClusterParameters(ctx context.Context, nn *param.K8sObjectIdentity) ([]*models.Parameter, error) { | ||
obcluster, err := clients.GetOBCluster(ctx, nn.Namespace, nn.Name) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Get obcluster %s %s", nn.Namespace, nn.Name) | ||
} | ||
observerList := v1alpha1.OBServerList{} | ||
err = clients.ServerClient.List(ctx, nn.Namespace, &observerList, metav1.ListOptions{ | ||
LabelSelector: fmt.Sprintf("%s=%s", oceanbaseconst.LabelRefOBCluster, nn.Name), | ||
}) | ||
if err != nil { | ||
logrus.WithError(err).Error("Failed to list observers") | ||
return nil, errors.Wrap(err, "List observers") | ||
} | ||
conn, err := utils.GetOBConnection(ctx, obcluster, "root", "sys", obcluster.Spec.UserSecrets.Root) | ||
if err != nil { | ||
logrus.Info("Failed to get OceanBase database connection") | ||
return nil, errors.Wrap(err, "Get OceanBase database connection") | ||
} | ||
parameters, err := conn.ListParametersWithTenantID(ctx, 1) | ||
if err != nil { | ||
logrus.WithError(err).Error("Failed to query parameters") | ||
return nil, errors.Wrap(err, "Query parameters") | ||
} | ||
return parameters, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import ( | |
"github.com/oceanbase/ob-operator/api/v1alpha1" | ||
"github.com/oceanbase/ob-operator/internal/clients" | ||
"github.com/oceanbase/ob-operator/internal/clients/schema" | ||
oceanbaseconst "github.com/oceanbase/ob-operator/internal/const/oceanbase" | ||
"github.com/oceanbase/ob-operator/internal/const/status/tenantstatus" | ||
"github.com/oceanbase/ob-operator/internal/dashboard/model/param" | ||
"github.com/oceanbase/ob-operator/internal/dashboard/model/response" | ||
|
@@ -41,8 +42,10 @@ import ( | |
func buildOBTenantApiType(nn types.NamespacedName, p *param.CreateOBTenantParam) (*v1alpha1.OBTenant, error) { | ||
t := &v1alpha1.OBTenant{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: nn.Name, | ||
Namespace: nn.Namespace, | ||
Name: nn.Name, | ||
Namespace: nn.Namespace, | ||
Annotations: make(map[string]string), | ||
Labels: make(map[string]string), | ||
}, | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: schema.OBTenantKind, | ||
|
@@ -58,6 +61,8 @@ func buildOBTenantApiType(nn types.NamespacedName, p *param.CreateOBTenantParam) | |
|
||
// guard non-nil | ||
Pools: []v1alpha1.ResourcePoolSpec{}, | ||
|
||
Scenario: p.Scenario, | ||
}, | ||
} | ||
|
||
|
@@ -141,6 +146,27 @@ func buildOBTenantApiType(nn types.NamespacedName, p *param.CreateOBTenantParam) | |
} | ||
} | ||
} | ||
if len(p.Variables) > 0 { | ||
t.Spec.Variables = make([]apitypes.Variable, 0, len(p.Variables)) | ||
for i := range p.Variables { | ||
t.Spec.Variables = append(t.Spec.Variables, apitypes.Variable{ | ||
Name: p.Variables[i].Key, | ||
Value: p.Variables[i].Value, | ||
}) | ||
} | ||
} | ||
if len(p.Parameters) > 0 { | ||
t.Spec.Parameters = make([]apitypes.Parameter, 0, len(p.Parameters)) | ||
for i := range p.Parameters { | ||
t.Spec.Parameters = append(t.Spec.Parameters, apitypes.Parameter{ | ||
Name: p.Parameters[i].Key, | ||
Value: p.Parameters[i].Value, | ||
}) | ||
} | ||
} | ||
if p.DeletionProtection { | ||
t.Annotations[oceanbaseconst.AnnotationsIgnoreDeletion] = "true" | ||
} | ||
return t, nil | ||
} | ||
|
||
|
@@ -351,7 +377,7 @@ func CreateOBTenant(ctx context.Context, nn types.NamespacedName, p *param.Creat | |
} | ||
|
||
if p.Source.Restore.OSSAccessID != "" && p.Source.Restore.OSSAccessKey != "" { | ||
ossSecretName := p.Name + "-oss-access-" + rand.String(6) | ||
ossSecretName := nn.Name + "-backup-" + strings.ToLower(strings.ReplaceAll(string(p.Source.Restore.Type), "_", "-")) + "-secret-" + rand.String(6) | ||
t.Spec.Source.Restore.ArchiveSource.OSSAccessSecret = ossSecretName | ||
t.Spec.Source.Restore.BakDataSource.OSSAccessSecret = ossSecretName | ||
_, err = k8sclient.ClientSet.CoreV1().Secrets(nn.Namespace).Create(ctx, &corev1.Secret{ | ||
|
@@ -362,6 +388,8 @@ func CreateOBTenant(ctx context.Context, nn types.NamespacedName, p *param.Creat | |
StringData: map[string]string{ | ||
"accessId": p.Source.Restore.OSSAccessID, | ||
"accessKey": p.Source.Restore.OSSAccessKey, | ||
"appId": p.Source.Restore.AppID, | ||
"s3Region": p.Source.Restore.Region, | ||
}, | ||
}, v1.CreateOptions{}) | ||
if err != nil { | ||
|
@@ -548,6 +576,7 @@ func PatchTenant(ctx context.Context, nn types.NamespacedName, p *param.PatchTen | |
if err != nil { | ||
return nil, err | ||
} | ||
alreadyIgnoreDeletion := tenant.Annotations[oceanbaseconst.AnnotationsIgnoreDeletion] == "true" | ||
if p.UnitNumber != nil { | ||
tenant.Spec.UnitNumber = *p.UnitNumber | ||
} | ||
|
@@ -595,6 +624,31 @@ func PatchTenant(ctx context.Context, nn types.NamespacedName, p *param.PatchTen | |
} | ||
} | ||
} | ||
if alreadyIgnoreDeletion && p.RemoveDeletionProtection { | ||
delete(tenant.Annotations, oceanbaseconst.AnnotationsIgnoreDeletion) | ||
} else if !alreadyIgnoreDeletion && p.AddDeletionProtection { | ||
tenant.Annotations[oceanbaseconst.AnnotationsIgnoreDeletion] = "true" | ||
} | ||
if len(p.Variables) > 0 { | ||
newVars := make([]apitypes.Variable, 0, len(p.Variables)) | ||
for i := range p.Variables { | ||
newVars = append(newVars, apitypes.Variable{ | ||
Name: p.Variables[i].Key, | ||
Value: p.Variables[i].Value, | ||
}) | ||
} | ||
tenant.Spec.Variables = newVars | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. overwrite all the parameters and variables? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the same as parameters of obcluster. The front end application will send all parameters here if the user desireds to modify parameters here. |
||
} | ||
if len(p.Parameters) > 0 { | ||
newParameters := make([]apitypes.Parameter, 0, len(p.Parameters)) | ||
for i := range p.Parameters { | ||
newParameters = append(newParameters, apitypes.Parameter{ | ||
Name: p.Parameters[i].Key, | ||
Value: p.Parameters[i].Value, | ||
}) | ||
} | ||
tenant.Spec.Parameters = newParameters | ||
} | ||
tenant, err = clients.UpdateOBTenant(ctx, tenant) | ||
if err != nil { | ||
return nil, err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this line is deleted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is what I need to do. Leaving zero values here is enough.