Skip to content

Commit

Permalink
fix runSelfCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
kobzonega committed Oct 15, 2023
1 parent b20a6b1 commit ea8e1aa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 42 deletions.
33 changes: 7 additions & 26 deletions internal/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,20 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"strings"
"time"

"github.com/ydb-platform/ydb-go-sdk/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"sigs.k8s.io/controller-runtime/pkg/log"

ydbv1alpha1 "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
)

func Open(ctx context.Context, endpoint string, opts ...ydb.Option) (*ydb.Driver, error) {
logger := log.FromContext(ctx)
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

opts = append(
opts,
buildYDBTLSOption(endpoint),
)
db, err := ydb.Open(
ctx,
endpoint,
Expand All @@ -50,25 +43,13 @@ func Close(ctx context.Context, db *ydb.Driver) {
}
}

func buildYDBTLSOption(endpoint string) ydb.Option {
certPool, _ := x509.SystemCertPool()
// TODO(shmel1k@): figure out min allowed TLS version?
tlsConfig := &tls.Config{ //nolint
RootCAs: certPool,
}
if strings.HasPrefix(endpoint, ydbv1alpha1.GRPCSProto) {
return ydb.WithTLSConfig(tlsConfig)
}
return ydb.WithTLSSInsecureSkipVerify()
}

func BuildGRPCTLSOption(endpoint string) grpc.DialOption {
certPool, _ := x509.SystemCertPool()
// TODO(shmel1k@): figure out min allowed TLS version?
tlsConfig := &tls.Config{ //nolint
RootCAs: certPool,
}
if strings.HasPrefix(endpoint, ydbv1alpha1.GRPCSProto) {
func LoadTLSCredentials(secure bool) grpc.DialOption {
if secure {
certPool, _ := x509.SystemCertPool()
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: certPool,
}
return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
}
return grpc.WithTransportCredentials(insecure.NewCredentials())
Expand Down
12 changes: 6 additions & 6 deletions internal/controllers/database/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ func (r *Reconciler) Sync(ctx context.Context, ydbCr *v1alpha1.Database) (ctrl.R
if stop {
return result, err
}
auth, result, err := r.getYDBCredentials(ctx, &database)
if auth == nil {
return result, err
}

if !meta.IsStatusConditionTrue(database.Status.Conditions, TenantInitializedCondition) {
stop, result, err = r.setInitialStatus(ctx, &database)
if stop {
return result, err
}
auth, result, err := r.getYDBCredentials(ctx, &database)
if auth == nil {
return result, err
}
stop, result, err = r.handleTenantCreation(ctx, &database, auth)
if stop {
return result, err
Expand Down Expand Up @@ -529,8 +529,8 @@ func (r *Reconciler) getYDBCredentials(
}
}
endpoint := database.GetStorageEndpoint()
secureOpt := connection.BuildGRPCTLSOption(endpoint)
return ydbCredentials.NewStaticCredentials(username, password, endpoint, secureOpt), ctrl.Result{Requeue: false}, nil
secure := connection.LoadTLSCredentials(resources.IsGrpcSecure(database.Storage))
return ydbCredentials.NewStaticCredentials(username, password, endpoint, secure), ctrl.Result{Requeue: false}, nil
}
}
return ydbCredentials.NewAnonymousCredentials(), ctrl.Result{Requeue: false}, nil
Expand Down
16 changes: 8 additions & 8 deletions internal/controllers/storage/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ func (r *Reconciler) initializeStorage(
fmt.Sprintf("%s/%s", v1alpha1.BinariesDir, v1alpha1.DaemonBinaryName),
}

if resources.IsGrpcSecure(storage.Storage) {
cmd = append(
cmd,
"-s",
storage.GetGRPCEndpointWithProto(),
)
}

if storage.Spec.OperatorConnection != nil {
ydbCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
Expand All @@ -139,6 +131,14 @@ func (r *Reconciler) initializeStorage(
)
}

if resources.IsGrpcSecure(storage.Storage) {
cmd = append(
cmd,
"-s",
storage.GetGRPCEndpointWithProto(),
)
}

cmd = append(
cmd,
"admin", "blobstorage", "config", "init",
Expand Down
8 changes: 6 additions & 2 deletions internal/controllers/storage/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (r *Reconciler) Sync(ctx context.Context, cr *ydbv1alpha1.Storage) (ctrl.Re
if stop {
return result, err
}
stop, result, err = r.runSelfCheck(ctx, &storage, auth, false)
if stop {
return result, err
}
stop, result, err = r.initializeStorage(ctx, &storage, auth)
if stop {
return result, err
Expand Down Expand Up @@ -321,8 +325,8 @@ func (r *Reconciler) getYDBCredentials(
}
}
endpoint := storage.GetGRPCEndpoint()
optSecure := connection.BuildGRPCTLSOption(endpoint)
return ydbCredentials.NewStaticCredentials(username, password, endpoint, optSecure), ctrl.Result{Requeue: false}, nil
secure := connection.LoadTLSCredentials(resources.IsGrpcSecure(storage.Storage))
return ydbCredentials.NewStaticCredentials(username, password, endpoint, secure), ctrl.Result{Requeue: false}, nil
}
}
return ydbCredentials.NewAnonymousCredentials(), ctrl.Result{Requeue: false}, nil
Expand Down

0 comments on commit ea8e1aa

Please sign in to comment.