Skip to content

Commit

Permalink
mock targettcpproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Sep 5, 2024
1 parent d467c54 commit 46e0f13
Show file tree
Hide file tree
Showing 12 changed files with 2,558 additions and 0 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 @@ -677,6 +677,7 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeVPNGateway"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetHTTPProxy"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetSSLProxy"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetTCPProxy"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeURLMap"}:
// ok

Expand Down
166 changes: 166 additions & 0 deletions mockgcp/mockcompute/globaltargettcpproxyv1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// 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"
"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 GlobalTargetTcpProxyV1 struct {
*MockService
pb.UnimplementedTargetTcpProxiesServer
}

func (s *GlobalTargetTcpProxyV1) Get(ctx context.Context, req *pb.GetTargetTcpProxyRequest) (*pb.TargetTcpProxy, error) {
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxy()
name, err := s.parseTargetTcpProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

obj := &pb.TargetTcpProxy{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
}

return obj, nil
}

func (s *GlobalTargetTcpProxyV1) Insert(ctx context.Context, req *pb.InsertTargetTcpProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxyResource().GetName()
name, err := s.parseTargetTcpProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

id := s.generateID()

obj := proto.Clone(req.GetTargetTcpProxyResource()).(*pb.TargetTcpProxy)
obj.SelfLink = PtrTo("https://www.googleapis.com/compute/v1/" + name.String())
obj.CreationTimestamp = PtrTo(s.nowString())
obj.Id = &id
obj.Kind = PtrTo("compute#targetTcpProxy")

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

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

func (s *GlobalTargetTcpProxyV1) Delete(ctx context.Context, req *pb.DeleteTargetTcpProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxy()
name, err := s.parseTargetTcpProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

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

func (s *GlobalTargetTcpProxyV1) SetProxyHeader(ctx context.Context, req *pb.SetProxyHeaderTargetTcpProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/global/targetTcpProxies/" + req.GetTargetTcpProxy()
name, err := s.parseTargetTcpProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

obj := &pb.TargetTcpProxy{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
}

obj.ProxyHeader = req.GetTargetTcpProxiesSetProxyHeaderRequestResource().ProxyHeader
if err := s.storage.Update(ctx, fqn, obj); err != nil {
return nil, err
}

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

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

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

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

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

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

return name, nil
} else {
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
}
7 changes: 7 additions & 0 deletions mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (s *MockService) Register(grpcServer *grpc.Server) {
pb.RegisterSslCertificatesServer(grpcServer, &GlobalSSLCertificatesV1{MockService: s})
pb.RegisterRegionSslCertificatesServer(grpcServer, &RegionalSSLCertificatesV1{MockService: s})
pb.RegisterTargetSslProxiesServer(grpcServer, &TargetSslProxyV1{MockService: s})
pb.RegisterTargetTcpProxiesServer(grpcServer, &GlobalTargetTcpProxyV1{MockService: s})

pb.RegisterServiceAttachmentsServer(grpcServer, &RegionalServiceAttachmentV1{MockService: s})

Expand Down Expand Up @@ -132,6 +133,12 @@ func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (ht
if err := pb.RegisterTargetSslProxiesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}
if err := pb.RegisterTargetTcpProxiesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}
if err := pb.RegisterRegionTargetTcpProxiesHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
}

if err := pb.RegisterUrlMapsHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/direct/compute/forwardingrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ func (m *forwardingRuleModel) AdapterForObject(ctx context.Context, reader clien
}
obj.Spec.Target.TargetSSLProxyRef.External = targetSSLProxyRef.External
}

// Get target TCPProxy
if obj.Spec.Target.TargetTCPProxyRef != nil {
targetTCPProxyRef, err := ResolveComputeTargetTCPProxy(ctx, reader, obj, obj.Spec.Target.TargetTCPProxyRef)
if err != nil {
return nil, err

}
obj.Spec.Target.TargetTCPProxyRef.External = targetTCPProxyRef.External
}
}

// Get location
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: computeglobalforwardingrule-${uniqueId}
namespace: ${uniqueId}
spec:
description: A global forwarding rule
ipAddress:
addressRef:
name: computeaddress-${uniqueId}
ipProtocol: TCP
loadBalancingScheme: EXTERNAL
location: global
portRange: "110"
target:
targetTCPProxyRef:
name: computetargettcpproxy2-${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"
labelFingerprint: abcdef0123A=
observedGeneration: 2
selfLink: https://www.googleapis.com/compute/v1/projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}
Loading

0 comments on commit 46e0f13

Please sign in to comment.