Skip to content

Commit

Permalink
support target google api bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Sep 10, 2024
1 parent 896ce65 commit 71620f3
Show file tree
Hide file tree
Showing 21 changed files with 1,025 additions and 16 deletions.
3 changes: 3 additions & 0 deletions apis/compute/v1beta1/computeforwardingrule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ type ForwardingruleServiceDirectoryRegistrations struct {
}

type ForwardingruleTarget struct {
// +optional
GoogleApiBundle *string `json:"googleApiBundle,omitempty"`

// +optional
ServiceAttachmentRef *refs.ComputeServiceAttachmentRef `json:"serviceAttachmentRef,omitempty"`

Expand Down
5 changes: 5 additions & 0 deletions apis/compute/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 @@ -473,6 +473,8 @@ spec:
For INTERNAL_SELF_MANAGED load balancing, only HTTP and HTTPS targets
are valid.
properties:
googleApiBundle:
type: string
serviceAttachmentRef:
oneOf:
- not:
Expand Down
41 changes: 37 additions & 4 deletions mockgcp/mockcompute/globalforwardingrulesv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ func (s *GlobalForwardingRulesV1) Insert(ctx context.Context, req *pb.InsertGlob
if obj.NetworkTier == nil {
obj.NetworkTier = PtrTo("PREMIUM")
}
if isPSCForwardingRule(obj) {
var num uint64 = 111111111111
obj.PscConnectionId = &num
obj.ServiceDirectoryRegistrations = []*pb.ForwardingRuleServiceDirectoryRegistration{
{
Namespace: PtrTo("goog-psc-${networkID}-${networkID}"),
ServiceDirectoryRegion: PtrTo("us-central1"),
},
}

}

// pattern: \d+(?:-\d+)?
if obj.PortRange != nil {
Expand All @@ -102,10 +113,17 @@ func (s *GlobalForwardingRulesV1) Insert(ctx context.Context, req *pb.InsertGlob
return nil, err
}

var opType *string
if isPSCForwardingRule(obj) {
opType = PtrTo("createPSCServiceEndpoint")
} else {
opType = PtrTo("insert")
}

op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("insert"),
OperationType: opType,
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
Expand All @@ -127,10 +145,17 @@ func (s *GlobalForwardingRulesV1) Delete(ctx context.Context, req *pb.DeleteGlob
return nil, err
}

var opType *string
if isPSCForwardingRule(deleted) {
opType = PtrTo("deletePSCServiceEndpoint")
} else {
opType = PtrTo("delete")
}

op := &pb.Operation{
TargetId: deleted.Id,
TargetLink: deleted.SelfLink,
OperationType: PtrTo("delete"),
OperationType: opType,
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
Expand Down Expand Up @@ -160,7 +185,7 @@ func (s *GlobalForwardingRulesV1) SetLabels(ctx context.Context, req *pb.SetLabe
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetLabels"),
OperationType: PtrTo("setLabels"),
User: PtrTo("[email protected]"),
// SetLabels operation has EndTime in response
EndTime: PtrTo("2024-04-01T12:34:56.123456Z"),
Expand Down Expand Up @@ -192,7 +217,7 @@ func (s *GlobalForwardingRulesV1) SetTarget(ctx context.Context, req *pb.SetTarg
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetTarget"),
OperationType: PtrTo("setTarget"),
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
Expand Down Expand Up @@ -230,3 +255,11 @@ func (s *MockService) parseGlobalForwardingRuleName(name string) (*globalForward
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
}

func isPSCForwardingRule(obj *pb.ForwardingRule) bool {
target := *obj.Target
if target == "all-apis" || target == "vpc-sc" || strings.Contains(target, "/serviceAttachments/") {
return true
}
return false
}
4 changes: 2 additions & 2 deletions mockgcp/mockcompute/regionalforwardingrulev1.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (s *RegionalForwardingRulesV1) SetLabels(ctx context.Context, req *pb.SetLa
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetLabels"),
OperationType: PtrTo("setLabels"),
User: PtrTo("[email protected]"),
// SetLabels operation has EndTime in response
EndTime: PtrTo("2024-04-01T12:34:56.123456Z"),
Expand Down Expand Up @@ -193,7 +193,7 @@ func (s *RegionalForwardingRulesV1) SetTarget(ctx context.Context, req *pb.SetTa
op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("SetTarget"),
OperationType: PtrTo("setTarget"),
User: PtrTo("[email protected]"),
}
return s.startRegionalLRO(ctx, name.Project.ID, name.Region, op, func() (proto.Message, error) {
Expand Down

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

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

11 changes: 9 additions & 2 deletions pkg/controller/direct/compute/forwardingrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"reflect"
"strings"

"k8s.io/klog/v2"

Expand Down Expand Up @@ -110,7 +111,7 @@ func (m *forwardingRuleModel) AdapterForObject(ctx context.Context, reader clien
obj.Spec.BackendServiceRef.External = backendServiceRef.External
}

// Get ip address, ip address is optional
// Get compute address, address is optional
if obj.Spec.IpAddress != nil && obj.Spec.IpAddress.AddressRef != nil {
computeAddressRef, err := ResolveComputeAddress(ctx, reader, obj, obj.Spec.IpAddress.AddressRef)
if err != nil {
Expand Down Expand Up @@ -282,7 +283,13 @@ func (a *forwardingRuleAdapter) Create(ctx context.Context, createOp *directbase
return mapCtx.Err()
}
forwardingRule.Name = direct.LazyPtr(a.id.forwardingRule)
forwardingRule.Labels = desired.Labels
target := direct.ValueOf(forwardingRule.Target)

// API restriction: Labels are invalid in Private Service Connect Forwarding Rule.
// TF workaround: https://github.com/GoogleCloudPlatform/k8s-config-connector/pull/944
if target != "all-apis" && target != "vpc-sc" && !strings.Contains(target, "/serviceAttachments/") {
forwardingRule.Labels = desired.Labels
}

var err error
op := &gcp.Operation{}
Expand Down
5 changes: 4 additions & 1 deletion pkg/controller/direct/compute/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ComputeForwardingRuleSpec_IpAddress_ToProto(mapCtx *direct.MapContext, in *
}
out = direct.LazyPtr(oneof.External)
}
if in.Ip != nil {
if oneof := in.Ip; oneof != nil {
out = in.Ip
}
return out
Expand Down Expand Up @@ -115,6 +115,9 @@ func ComputeForwardingRuleSpec_Target_ToProto(mapCtx *direct.MapContext, in *krm
}

var out *string
if oneof := in.GoogleApiBundle; oneof != nil {
out = in.GoogleApiBundle
}
if oneof := in.ServiceAttachmentRef; oneof != nil {
if oneof.External == "" {
mapCtx.Errorf("reference %s was not pre-resolved", oneof.Name)
Expand Down
6 changes: 5 additions & 1 deletion pkg/randomid/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ type ID struct {
// New builds a random identifier.
// We always generate a more secure random value; we could in future expose less secure options if this becomes a bottleneck.
func New() ID {
b := make([]byte, 12)
// The forwarding rule name for PSC Google APIs must be an 1-20 characters string with lowercase letters and numbers and must start with a letter.
// 9 bytes is equivalent to 72 bits of data. Each character in Base32 represents 5 bits.
// Calculation: 72 bits / 5 bits per character = 14.4 characters.
// Round up to the nearest whole number, which is 15. the ID string will have 15 characters.
b := make([]byte, 9)
if _, err := cryptorand.Read(b); err != nil {
klog.Fatalf("failed to read from crypto/rand: %v", err)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/test/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ var TestNameRegexesToSkip = []string{
// Disable due to TF bug https://github.com/hashicorp/terraform-provider-google/issues/16255.
// We can't specify labels in the create operation, that causes AssertLabelsMatchAndHaveManagedLabel check to fail.
".*(regionalforwardingrulepsc).*",
// This test only works with direct controller. Re-enable it when we turn on direct ComputeForwardingRule.
".*(globalforwardingrulepscgoogleapis).*",
}

// TestNameRegexToSkipForTestCRUD is similar to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"operationType": "setLabels",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
Expand Down Expand Up @@ -929,7 +929,7 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetLabels",
"operationType": "setLabels",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
Expand Down Expand Up @@ -1007,7 +1007,7 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetTarget",
"operationType": "setTarget",
"progress": 0,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
Expand Down Expand Up @@ -1041,7 +1041,7 @@ X-Xss-Protection: 0
"insertTime": "2024-04-01T12:34:56.123456Z",
"kind": "compute#operation",
"name": "${operationID}",
"operationType": "SetTarget",
"operationType": "setTarget",
"progress": 100,
"selfLink": "https://www.googleapis.com/compute/v1/projects/${projectId}/global/operations/${operationID}",
"startTime": "2024-04-01T12:34:56.123456Z",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: compute.cnrm.cloud.google.com/v1beta1
kind: ComputeForwardingRule
metadata:
annotations:
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/project-id: ${projectId}
finalizers:
- cnrm.cloud.google.com/finalizer
- cnrm.cloud.google.com/deletion-defender
generation: 1
labels:
cnrm-test: "true"
label-one: value-two
name: rule${uniqueId}
namespace: ${uniqueId}
spec:
description: A global forwarding rule
ipAddress:
addressRef:
name: default
loadBalancingScheme: ""
location: global
networkRef:
name: default
target:
googleApiBundle: all-apis
status:
conditions:
- lastTransitionTime: "1970-01-01T00:00:00Z"
message: The resource is up to date
reason: UpToDate
status: "True"
type: Ready
creationTimestamp: "1970-01-01T00:00:00Z"
externalRef: //compute.googleapis.com/projects/${projectId}/global/forwardingrules/rule${uniqueId}
labelFingerprint: abcdef0123A=
observedGeneration: 1
selfLink: https://www.googleapis.com/compute/v1/projects/${projectId}/global/forwardingRules/rule${uniqueId}
Loading

0 comments on commit 71620f3

Please sign in to comment.