forked from oceanbase/ob-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
22 changed files
with
468 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright (c) 2024 OceanBase | ||
ob-operator is licensed under Mulan PSL v2. | ||
You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
You may obtain a copy of Mulan PSL v2 at: | ||
http://license.coscl.org.cn/MulanPSL2 | ||
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
See the Mulan PSL v2 for more details. | ||
*/ | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
oceanbaseconst "github.com/oceanbase/ob-operator/internal/const/oceanbase" | ||
clusterstatus "github.com/oceanbase/ob-operator/internal/const/status/obcluster" | ||
"github.com/oceanbase/ob-operator/pkg/oceanbase-sdk/connector" | ||
"github.com/oceanbase/ob-operator/pkg/oceanbase-sdk/operation" | ||
) | ||
|
||
func readPassword(c client.Client, namespace, secretName string) (string, error) { | ||
secret := &corev1.Secret{} | ||
err := c.Get(context.Background(), types.NamespacedName{ | ||
Namespace: namespace, | ||
Name: secretName, | ||
}, secret) | ||
if err != nil { | ||
return "", errors.Wrapf(err, "Get password from secret %s failed", secretName) | ||
} | ||
return string(secret.Data["password"]), err | ||
} | ||
|
||
func getSysClient(c client.Client, logger *logr.Logger, obcluster *OBCluster, userName, tenantName, secretName string) (*operation.OceanbaseOperationManager, error) { | ||
observerList := &OBServerList{} | ||
err := c.List(context.Background(), observerList, client.MatchingLabels{ | ||
oceanbaseconst.LabelRefOBCluster: obcluster.Name, | ||
}, client.InNamespace(obcluster.Namespace)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Get observer list") | ||
} | ||
if len(observerList.Items) == 0 { | ||
return nil, errors.Errorf("No observer belongs to cluster %s", obcluster.Name) | ||
} | ||
|
||
var s *connector.OceanBaseDataSource | ||
password, err := readPassword(c, obcluster.Namespace, secretName) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Read password to get oceanbase operation manager of cluster %s", obcluster.Name) | ||
} | ||
for _, observer := range observerList.Items { | ||
address := observer.Status.GetConnectAddr() | ||
switch obcluster.Status.Status { | ||
case clusterstatus.New: | ||
s = connector.NewOceanBaseDataSource(address, oceanbaseconst.SqlPort, oceanbaseconst.RootUser, tenantName, "", "") | ||
case clusterstatus.Bootstrapped: | ||
s = connector.NewOceanBaseDataSource(address, oceanbaseconst.SqlPort, oceanbaseconst.RootUser, tenantName, "", oceanbaseconst.DefaultDatabase) | ||
default: | ||
s = connector.NewOceanBaseDataSource(address, oceanbaseconst.SqlPort, userName, tenantName, password, oceanbaseconst.DefaultDatabase) | ||
} | ||
// if err is nil, db connection is already checked available | ||
sysClient, err := operation.GetOceanbaseOperationManager(s) | ||
if err == nil && sysClient != nil { | ||
sysClient.Logger = logger | ||
return sysClient, nil | ||
} | ||
} | ||
return nil, errors.Errorf("Can not get oceanbase operation manager of obcluster %s after checked all servers", obcluster.Name) | ||
} |
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
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
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
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
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
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
Oops, something went wrong.