Skip to content

Commit

Permalink
Merge pull request #2429 from jasonvigil/cloudsql-clone
Browse files Browse the repository at this point in the history
Add CloudSQL support for "create from clone"
  • Loading branch information
google-oss-prow[bot] committed Aug 23, 2024
2 parents 776ea44 + 395fef7 commit 1d4eebe
Show file tree
Hide file tree
Showing 16 changed files with 1,326 additions and 58 deletions.
34 changes: 30 additions & 4 deletions apis/sql/v1beta1/sqlinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,6 @@ type InstanceMaintenanceWindow struct {
UpdateTrack *string `json:"updateTrack,omitempty"`
}

// +kubebuilder:validation:XValidation:rule="has(self.value) ? !has(self.valueFrom) : true",message="valueFrom is forbidden when value is specified"
// +kubebuilder:validation:XValidation:rule="has(self.valueFrom) ? !has(self.value): true",message="value is forbidden when valueFrom is specified"
type InstancePassword struct {
/* Value of the field. Cannot be used if 'valueFrom' is specified. */
// +optional
Expand Down Expand Up @@ -297,8 +295,6 @@ type InstanceReplicaConfiguration struct {
VerifyServerCertificate *bool `json:"verifyServerCertificate,omitempty"`
}

// +kubebuilder:validation:XValidation:rule="has(self.value) ? !has(self.valueFrom) : true",message="valueFrom is forbidden when value is specified"
// +kubebuilder:validation:XValidation:rule="has(self.valueFrom) ? !has(self.value): true",message="value is forbidden when valueFrom is specified"
type InstanceRootPassword struct {
/* Value of the field. Cannot be used if 'valueFrom' is specified. */
// +optional
Expand Down Expand Up @@ -441,7 +437,37 @@ type InstanceValueFrom struct {
SecretKeyRef *v1alpha1.SecretKeyRef `json:"secretKeyRef,omitempty"`
}

type BinLogCoordinates struct {
/* Name of the binary log file for a Cloud SQL instance. */
BinLogFileName string `json:"binLogFileName,omitempty"`

/* Position (offset) within the binary log file. */
BinLogPosition int64 `json:"binLogPosition,omitempty,string"`
}

type CloneSource struct {
/* Binary log coordinates, if specified, identify the position up to which the source instance is
cloned. If not specified, the source instance is cloned up to the most recent binary log coordinates. */
// +optional
BinLogCoordinates *BinLogCoordinates `json:"binLogCoordinates,omitempty"`

/* (SQL Server only) Clone only the specified databases from the source instance. Clone all databases if empty. */
// +optional
DatabaseNames []string `json:"databaseNames,omitempty"`

/* Timestamp, if specified, identifies the time to which the source instance is cloned. */
// +optional
PointInTime *string `json:"pointInTime,omitempty"`

/* The source SQLInstance to clone */
SQLInstanceRef refsv1beta1.SQLInstanceRef `json:"sqlInstanceRef,omitempty"`
}

type SQLInstanceSpec struct {
/* Create this database as a clone of a source instance. Immutable. */
// +optional
CloneSource *CloneSource `json:"cloneSource,omitempty"`

/* The MySQL, PostgreSQL or SQL Server (beta) version to use. Supported values include MYSQL_5_6, MYSQL_5_7, MYSQL_8_0, POSTGRES_9_6, POSTGRES_10, POSTGRES_11, POSTGRES_12, POSTGRES_13, POSTGRES_14, POSTGRES_15, SQLSERVER_2017_STANDARD, SQLSERVER_2017_ENTERPRISE, SQLSERVER_2017_EXPRESS, SQLSERVER_2017_WEB. Database Version Policies includes an up-to-date reference of supported versions. */
// +optional
DatabaseVersion *string `json:"databaseVersion,omitempty"`
Expand Down
53 changes: 53 additions & 0 deletions apis/sql/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,63 @@ spec:
type: object
spec:
properties:
cloneSource:
description: Create this database as a clone of a source instance.
Immutable.
properties:
binLogCoordinates:
description: Binary log coordinates, if specified, identify the
position up to which the source instance is cloned. If not specified,
the source instance is cloned up to the most recent binary log
coordinates.
properties:
binLogFileName:
description: Name of the binary log file for a Cloud SQL instance.
type: string
binLogPosition:
description: Position (offset) within the binary log file.
format: int64
type: integer
type: object
databaseNames:
description: (SQL Server only) Clone only the specified databases
from the source instance. Clone all databases if empty.
items:
type: string
type: array
pointInTime:
description: Timestamp, if specified, identifies the time to which
the source instance is cloned.
type: string
sqlInstanceRef:
description: The source SQLInstance to clone
oneOf:
- not:
required:
- external
required:
- name
- not:
anyOf:
- required:
- name
- required:
- namespace
required:
- external
properties:
external:
description: The SQLInstance selfLink, when not managed by
Config Connector.
type: string
name:
description: The `name` field of a `SQLInstance` resource.
type: string
namespace:
description: The `namespace` field of a `SQLInstance` resource.
type: string
type: object
type: object
databaseVersion:
description: The MySQL, PostgreSQL or SQL Server (beta) version to
use. Supported values include MYSQL_5_6, MYSQL_5_7, MYSQL_8_0, POSTGRES_9_6,
Expand Down Expand Up @@ -201,11 +258,6 @@ spec:
type: object
type: object
type: object
x-kubernetes-validations:
- message: valueFrom is forbidden when value is specified
rule: 'has(self.value) ? !has(self.valueFrom) : true'
- message: value is forbidden when valueFrom is specified
rule: 'has(self.valueFrom) ? !has(self.value): true'
sslCipher:
description: Immutable. Permissible ciphers for use in SSL encryption.
type: string
Expand Down Expand Up @@ -249,11 +301,6 @@ spec:
type: object
type: object
type: object
x-kubernetes-validations:
- message: valueFrom is forbidden when value is specified
rule: 'has(self.value) ? !has(self.valueFrom) : true'
- message: value is forbidden when valueFrom is specified
rule: 'has(self.valueFrom) ? !has(self.value): true'
settings:
description: The settings to use for the database. The configuration
is detailed below.
Expand Down
35 changes: 35 additions & 0 deletions mockgcp/mocksql/sqlinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ func (s *sqlInstancesService) Get(ctx context.Context, req *pb.SqlInstancesGetRe
return obj, nil
}

func (s *sqlInstancesService) Clone(ctx context.Context, req *pb.SqlInstancesCloneRequest) (*pb.Operation, error) {
sourceFQN, err := s.buildInstanceName(req.GetProject(), req.GetInstance())
if err != nil {
return nil, err
}

source := &pb.DatabaseInstance{}
if err := s.storage.Get(ctx, sourceFQN.String(), source); err != nil {
return nil, err
}

cloneName := req.Body.CloneContext.DestinationInstanceName
clone := proto.Clone(source).(*pb.DatabaseInstance)
clone.Name = cloneName

insertReq := &pb.SqlInstancesInsertRequest{
Project: req.GetProject(),
Body: clone,
}

insertOp, err := s.Insert(ctx, insertReq)
if err != nil {
return nil, err
}

cloneOp := &pb.Operation{
TargetProject: insertOp.TargetProject,
OperationType: pb.Operation_CLONE,
}

return s.operations.startLRO(ctx, cloneOp, clone, func() (proto.Message, error) {
return clone, nil
})
}

func (s *sqlInstancesService) Insert(ctx context.Context, req *pb.SqlInstancesInsertRequest) (*pb.Operation, error) {
name, err := s.buildInstanceName(req.GetProject(), req.GetBody().GetName())
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions pkg/clients/generated/apis/sql/v1beta1/sqlinstance_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions pkg/clients/generated/apis/sql/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1d4eebe

Please sign in to comment.