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

Add some features that ob-operator supports to dashboard #659

Merged
merged 5 commits into from
Dec 3, 2024
Merged
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
1 change: 0 additions & 1 deletion internal/cli/utils/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func MapZonesToTopology(zones map[string]string) ([]param.ZoneTopology, error) {
Zone: zoneName,
Replicas: replica,
NodeSelector: make([]common.KVPair, 0),
Tolerations: make([]common.KVPair, 0),
Copy link
Member

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

Copy link
Collaborator Author

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.

Affinities: make([]common.AffinitySpec, 0),
})
}
Expand Down
196 changes: 192 additions & 4 deletions internal/dashboard/business/oceanbase/obcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will overwrite all the parameters

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
}
8 changes: 5 additions & 3 deletions internal/dashboard/business/oceanbase/obcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ func getMockedCreateClusterParam() *param.CreateOBClusterParam {
Key: "test-node-selector",
Value: "test-node-selector-value",
}},
Tolerations: []common.KVPair{{
Key: "test-toleration",
Value: "test-toleration-value",
Tolerations: []common.TolerationSpec{{
KVPair: common.KVPair{
Key: "test-toleration",
Value: "test-toleration-value",
},
}},
Affinities: []common.AffinitySpec{{
SelectorExpression: common.SelectorExpression{
Expand Down
60 changes: 57 additions & 3 deletions internal/dashboard/business/oceanbase/obtenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -58,6 +61,8 @@ func buildOBTenantApiType(nn types.NamespacedName, p *param.CreateOBTenantParam)

// guard non-nil
Pools: []v1alpha1.ResourcePoolSpec{},

Scenario: p.Scenario,
},
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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{
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overwrite all the parameters and variables?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Expand Down
Loading
Loading