Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#2845 from gemmahou/mock-target…
Browse files Browse the repository at this point in the history
…grpcproxy

test: Support TargetGrpcProxy for direct ComputeForwardingRule
  • Loading branch information
google-oss-prow[bot] authored Oct 4, 2024
2 parents 2b23da2 + 26d323b commit c0a7791
Show file tree
Hide file tree
Showing 38 changed files with 2,187 additions and 86 deletions.
1 change: 1 addition & 0 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeServiceAttachment"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSSLCertificate"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSubnetwork"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetGRPCProxy"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetHTTPProxy"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetHTTPSProxy"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetVPNGateway"}:
Expand Down
2 changes: 1 addition & 1 deletion mockgcp/mockcompute/globalbackendservicesv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *GlobalBackendServicesV1) Get(ctx context.Context, req *pb.GetBackendSer

obj := &pb.BackendService{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
return nil, status.Errorf(codes.NotFound, "The resource '%s' was not found", fqn)
}

return obj, nil
Expand Down
6 changes: 6 additions & 0 deletions mockgcp/mockcompute/globalforwardingrulesv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func (s *GlobalForwardingRulesV1) Insert(ctx context.Context, req *pb.InsertGlob
}
}

// network field is only used for global internal load balancing.
// If neither subnetwork nor network field is specified, the default network will be used.
if obj.Network == nil && obj.Subnetwork == nil && obj.LoadBalancingScheme != nil && *obj.LoadBalancingScheme != "EXTERNAL" {
obj.Network = PtrTo(fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/${projectId}/global/networks/default"))
}

if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion mockgcp/mockcompute/globalurlmapsv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *GlobalURLMapsV1) Get(ctx context.Context, req *pb.GetUrlMapRequest) (*p

obj := &pb.UrlMap{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
return nil, status.Errorf(codes.NotFound, "The resource '%s' was not found", fqn)
}

return obj, nil
Expand Down
2 changes: 1 addition & 1 deletion mockgcp/mockcompute/regionalbackendservicev1.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *RegionalBackendServicesV1) Get(ctx context.Context, req *pb.GetRegionBa

obj := &pb.BackendService{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
return nil, status.Errorf(codes.NotFound, "The resource '%s' was not found", fqn)
}

return obj, nil
Expand Down
2 changes: 1 addition & 1 deletion mockgcp/mockcompute/regionalforwardingrulev1.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s *RegionalForwardingRulesV1) Insert(ctx context.Context, req *pb.InsertFo
if obj.NetworkTier == nil {
obj.NetworkTier = PtrTo("PREMIUM")
}
if *obj.LoadBalancingScheme == "" {
if obj.LoadBalancingScheme != nil && *obj.LoadBalancingScheme == "" {
obj.LoadBalancingScheme = nil
}

Expand Down
2 changes: 1 addition & 1 deletion mockgcp/mockcompute/regionalurlmapsv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *RegionalURLMapsV1) Get(ctx context.Context, req *pb.GetRegionUrlMapRequ

obj := &pb.UrlMap{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
return nil, status.Errorf(codes.NotFound, "The resource '%s' was not found", fqn)
}

return obj, nil
Expand Down
6 changes: 6 additions & 0 deletions mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (s *MockService) Register(grpcServer *grpc.Server) {
pb.RegisterSubnetworksServer(grpcServer, &SubnetsV1{MockService: s})
pb.RegisterVpnGatewaysServer(grpcServer, &VPNGatewaysV1{MockService: s})
pb.RegisterTargetVpnGatewaysServer(grpcServer, &TargetVpnGatewaysV1{MockService: s})
pb.RegisterTargetGrpcProxiesServer(grpcServer, &TargetGrpcProxyV1{MockService: s})

pb.RegisterTargetHttpProxiesServer(grpcServer, &GlobalTargetHTTPProxiesV1{MockService: s})
pb.RegisterRegionTargetHttpProxiesServer(grpcServer, &RegionalTargetHTTPProxiesV1{MockService: s})
Expand Down Expand Up @@ -127,6 +128,11 @@ func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (ht
if err := pb.RegisterTargetVpnGatewaysHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}

if err := pb.RegisterTargetGrpcProxiesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}

if err := pb.RegisterTargetHttpProxiesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}
Expand Down
141 changes: 141 additions & 0 deletions mockgcp/mockcompute/targetgrpcproxyv1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mockcompute

import (
"context"
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/compute/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)

type TargetGrpcProxyV1 struct {
*MockService
pb.UnimplementedTargetGrpcProxiesServer
}

func (s *TargetGrpcProxyV1) Get(ctx context.Context, req *pb.GetTargetGrpcProxyRequest) (*pb.TargetGrpcProxy, error) {
reqName := "projects/" + req.GetProject() + "/global/targetGrpcProxies/" + req.GetTargetGrpcProxy()
name, err := s.parseTargetGrpcProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

obj := &pb.TargetGrpcProxy{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, status.Errorf(codes.NotFound, "The resource '%s' was not found", fqn)
}

return obj, nil
}

func (s *TargetGrpcProxyV1) Insert(ctx context.Context, req *pb.InsertTargetGrpcProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/global/targetGrpcProxies/" + req.GetTargetGrpcProxyResource().GetName()
name, err := s.parseTargetGrpcProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

id := s.generateID()

obj := proto.Clone(req.GetTargetGrpcProxyResource()).(*pb.TargetGrpcProxy)
obj.SelfLink = PtrTo("https://www.googleapis.com/compute/v1/" + name.String())
obj.SelfLinkWithId = PtrTo(fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/targetGrpcProxies/%d", name.Project.ID, id))
obj.CreationTimestamp = PtrTo(s.nowString())
obj.Id = &id
obj.Kind = PtrTo("compute#targetGrpcProxy")
if obj.Fingerprint == nil {
obj.Fingerprint = PtrTo(computeFingerprint(obj))
}

if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, err
}

op := &pb.Operation{
TargetId: obj.Id,
TargetLink: obj.SelfLink,
OperationType: PtrTo("compute.targetGrpcProxies.insert"),
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return obj, nil
})
}

func (s *TargetGrpcProxyV1) Delete(ctx context.Context, req *pb.DeleteTargetGrpcProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/global/targetGrpcProxies/" + req.GetTargetGrpcProxy()
name, err := s.parseTargetGrpcProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

deleted := &pb.TargetGrpcProxy{}
if err := s.storage.Delete(ctx, fqn, deleted); err != nil {
return nil, err
}

op := &pb.Operation{
TargetId: deleted.Id,
TargetLink: deleted.SelfLink,
OperationType: PtrTo("compute.targetGrpcProxies.delete"),
User: PtrTo("[email protected]"),
}
return s.startGlobalLRO(ctx, name.Project.ID, op, func() (proto.Message, error) {
return deleted, nil
})
}

type targetGrpcProxyName struct {
Project *projects.ProjectData
Name string
}

func (n *targetGrpcProxyName) String() string {
return "projects/" + n.Project.ID + "/global/targetGrpcProxies/" + n.Name
}

// parseTargetGrpcProxyName parses a string into a targetGrpcProxyName.
// The expected form is `projects/*/global/parseTargetGrpcProxies/*`.
func (s *MockService) parseTargetGrpcProxyName(name string) (*targetGrpcProxyName, error) {
tokens := strings.Split(name, "/")

if len(tokens) == 5 && tokens[0] == "projects" && tokens[2] == "global" && tokens[3] == "targetGrpcProxies" {
project, err := s.Projects.GetProjectByID(tokens[1])
if err != nil {
return nil, err
}

name := &targetGrpcProxyName{
Project: project,
Name: tokens[4],
}

return name, nil
} else {
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
}
12 changes: 11 additions & 1 deletion pkg/controller/direct/compute/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func ResolveComputeTargetGrpcProxy(ctx context.Context, reader client.Reader, sr
computeTargetGrpcProxy, err := resolveResourceName(ctx, reader, key, schema.GroupVersionKind{
Group: "compute.cnrm.cloud.google.com",
Version: "v1beta1",
Kind: "ComputeTargetGrpcProxy",
Kind: "ComputeTargetGRPCProxy",
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -577,6 +577,16 @@ func resolveDependencies(ctx context.Context, reader client.Reader, obj *krm.Com
obj.Spec.Target.ServiceAttachmentRef.External = serviceAttachmentRef.External
}

// Get target ComputeTargetGRPCProxyRef
if obj.Spec.Target.TargetGRPCProxyRef != nil {
targetGRPCProxyRef, err := ResolveComputeTargetGrpcProxy(ctx, reader, obj, obj.Spec.Target.TargetGRPCProxyRef)
if err != nil {
return err

}
obj.Spec.Target.TargetGRPCProxyRef.External = targetGRPCProxyRef.External
}

// Get target ComputeTargetHTTPProxy
if obj.Spec.Target.TargetHTTPProxyRef != nil {
targetHTTPProxyRef, err := ResolveComputeTargetHTTPProxy(ctx, reader, obj, obj.Spec.Target.TargetHTTPProxyRef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ X-Xss-Protection: 0
"errors": [
{
"domain": "global",
"message": "backendService \"projects/${projectId}/global/backendServices/computebackendservice-${uniqueId}\" not found",
"message": "The resource 'projects/${projectId}/global/backendServices/computebackendservice-${uniqueId}' was not found",
"reason": "notFound"
}
],
"message": "backendService \"projects/${projectId}/global/backendServices/computebackendservice-${uniqueId}\" not found"
"message": "The resource 'projects/${projectId}/global/backendServices/computebackendservice-${uniqueId}' was not found"
}
}

Expand Down Expand Up @@ -456,11 +456,11 @@ X-Xss-Protection: 0
"errors": [
{
"domain": "global",
"message": "urlMap \"projects/${projectId}/global/urlMaps/computeurlmap-${uniqueId}\" not found",
"message": "The resource 'projects/${projectId}/global/urlMaps/computeurlmap-${uniqueId}' was not found",
"reason": "notFound"
}
],
"message": "urlMap \"projects/${projectId}/global/urlMaps/computeurlmap-${uniqueId}\" not found"
"message": "The resource 'projects/${projectId}/global/urlMaps/computeurlmap-${uniqueId}' was not found"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ X-Xss-Protection: 0
"errors": [
{
"domain": "global",
"message": "backendService \"projects/${projectId}/global/backendServices/computebackendservice-${uniqueId}\" not found",
"message": "The resource 'projects/${projectId}/global/backendServices/computebackendservice-${uniqueId}' was not found",
"reason": "notFound"
}
],
"message": "backendService \"projects/${projectId}/global/backendServices/computebackendservice-${uniqueId}\" not found"
"message": "The resource 'projects/${projectId}/global/backendServices/computebackendservice-${uniqueId}' was not found"
}
}

Expand Down Expand Up @@ -456,11 +456,11 @@ X-Xss-Protection: 0
"errors": [
{
"domain": "global",
"message": "urlMap \"projects/${projectId}/global/urlMaps/computeurlmap-${uniqueId}\" not found",
"message": "The resource 'projects/${projectId}/global/urlMaps/computeurlmap-${uniqueId}' was not found",
"reason": "notFound"
}
],
"message": "urlMap \"projects/${projectId}/global/urlMaps/computeurlmap-${uniqueId}\" not found"
"message": "The resource 'projects/${projectId}/global/urlMaps/computeurlmap-${uniqueId}' was not found"
}
}

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: 2
labels:
cnrm-test: "true"
label-one: value-two
name: computeregionalforwardingrule-${uniqueId}
namespace: ${uniqueId}
spec:
description: A global forwarding rule
ipAddress:
ip: 0.0.0.0
ipProtocol: TCP
loadBalancingScheme: INTERNAL_SELF_MANAGED
location: global
portRange: "100"
target:
targetGRPCProxyRef:
name: computetargetgrpcproxy-2-${uniqueId}
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/computeregionalforwardingrule-${uniqueId}
labelFingerprint: abcdef0123A=
observedGeneration: 2
selfLink: https://www.googleapis.com/compute/v1/projects/${projectId}/global/forwardingRules/computeregionalforwardingrule-${uniqueId}
Loading

0 comments on commit c0a7791

Please sign in to comment.