Skip to content

Commit

Permalink
Add swap compute field for spanner instance
Browse files Browse the repository at this point in the history
  • Loading branch information
anhdle-sso committed Dec 13, 2024
1 parent 55164ad commit dc25bc4
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 9 deletions.
30 changes: 21 additions & 9 deletions mockgcp/mockspanner/admin/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ package mockspanner
import (
"context"
"reflect"
"slices"
"strings"

"cloud.google.com/go/longrunning/autogen/longrunningpb"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/spanner/admin/instance/v1"
"google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -69,7 +71,7 @@ func (s *SpannerInstanceV1) CreateInstance(ctx context.Context, req *pb.CreateIn

obj := proto.Clone(req.GetInstance()).(*pb.Instance)
obj.Name = fqn
s.populateDefaultsForSpannerInstance(obj, obj)
s.populateDefaultsForSpannerInstance(obj, obj, nil)
obj.State = pb.Instance_READY

// Metadata instance include ReplicaComputeCapacity even if not specify
Expand All @@ -96,16 +98,27 @@ func (s *SpannerInstanceV1) CreateInstance(ctx context.Context, req *pb.CreateIn
})
}

func (s *SpannerInstanceV1) populateDefaultsForSpannerInstance(update, obj *pb.Instance) {
func (s *SpannerInstanceV1) populateDefaultsForSpannerInstance(update, obj *pb.Instance, f *field_mask.FieldMask) {
// At most one of either node_count or processing_units should be present.
// https://cloud.google.com/spanner/docs/compute-capacity
// 1 nodeCount equals 1000 processingUnits
if 1000*update.NodeCount > update.ProcessingUnits {
obj.ProcessingUnits = 1000 * update.NodeCount
obj.NodeCount = update.NodeCount
if f != nil && len(f.Paths) > 0 {
if slices.Contains[[]string](f.Paths, "node_count") {
obj.NodeCount = update.NodeCount
obj.ProcessingUnits = update.NodeCount * 1000
}
if slices.Contains[[]string](f.Paths, "processing_units") {
obj.ProcessingUnits = update.ProcessingUnits
obj.NodeCount = update.ProcessingUnits / 1000
}
} else {
obj.ProcessingUnits = update.ProcessingUnits
obj.NodeCount = update.ProcessingUnits / 1000
if 1000*update.NodeCount > update.ProcessingUnits {
obj.ProcessingUnits = 1000 * update.NodeCount
obj.NodeCount = update.NodeCount
} else {
obj.ProcessingUnits = update.ProcessingUnits
obj.NodeCount = update.ProcessingUnits / 1000
}
}
}

Expand Down Expand Up @@ -152,8 +165,7 @@ func (s *SpannerInstanceV1) UpdateInstance(ctx context.Context, req *pb.UpdateIn

}
}

s.populateDefaultsForSpannerInstance(req.Instance, obj)
s.populateDefaultsForSpannerInstance(req.Instance, obj, req.FieldMask)
// Metadata instance include ReplicaComputeCapacity even if not specify
cloneObj := proto.Clone(obj).(*pb.Instance)
s.populateReplicaComputeCapacityForSpannerInstance(cloneObj)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: spanner.cnrm.cloud.google.com/v1beta1
kind: SpannerInstance
metadata:
annotations:
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/project-id: ${projectId}
cnrm.cloud.google.com/state-into-spec: absent
finalizers:
- cnrm.cloud.google.com/finalizer
- cnrm.cloud.google.com/deletion-defender
generation: 3
labels:
cnrm-test: "true"
label-one: value-one
name: spannerinstance-sample-${uniqueId}
namespace: ${uniqueId}
spec:
config: regional-us-west1
displayName: New spanner Instance Sample
numNodes: 2
processingUnits: 900
resourceID: spannerinstance-sample-${uniqueId}
status:
conditions:
- lastTransitionTime: "1970-01-01T00:00:00Z"
message: The resource is up to date
reason: UpToDate
status: "True"
type: Ready
observedGeneration: 3
state: READY
Loading

0 comments on commit dc25bc4

Please sign in to comment.