Skip to content

Commit

Permalink
mock targetsslproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Sep 5, 2024
1 parent e63c5ad commit 6ff9c72
Show file tree
Hide file tree
Showing 14 changed files with 2,634 additions and 12 deletions.
2 changes: 2 additions & 0 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,13 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeNodeGroup"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeNodeTemplate"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeManagedSSLCertificate"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSSLCertificate"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeServiceAttachment"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeSubnetwork"}:
case schema.GroupKind{Group: "compute.cnrm.cloud.google.com", Kind: "ComputeTargetVPNGateway"}:
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: "ComputeURLMap"}:
// ok

Expand Down
20 changes: 18 additions & 2 deletions mockgcp/mockcompute/globaladdress.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ func (s *GlobalAddressesV1) Insert(ctx context.Context, req *pb.InsertGlobalAddr
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
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 *GlobalAddressesV1) Delete(ctx context.Context, req *pb.DeleteGlobalAddressRequest) (*pb.Operation, error) {
Expand All @@ -93,7 +101,15 @@ func (s *GlobalAddressesV1) Delete(ctx context.Context, req *pb.DeleteGlobalAddr
return nil, err
}

return s.newLRO(ctx, name.Project.ID)
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 *GlobalAddressesV1) SetLabels(ctx context.Context, req *pb.SetLabelsGlobalAddressRequest) (*pb.Operation, error) {
Expand Down
14 changes: 4 additions & 10 deletions mockgcp/mockcompute/globalsslcertificatesv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ 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"
apierrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/compute/v1"
)

type GlobalSSLCertificatesV1 struct {
Expand Down Expand Up @@ -67,7 +65,7 @@ func (s *GlobalSSLCertificatesV1) Insert(ctx context.Context, req *pb.InsertSslC
obj.Kind = PtrTo("compute#sslCertificate")

if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, status.Errorf(codes.Internal, "error creating sslCertificate: %v", err)
return nil, err
}

op := &pb.Operation{
Expand All @@ -92,11 +90,7 @@ func (s *GlobalSSLCertificatesV1) Delete(ctx context.Context, req *pb.DeleteSslC

deleted := &pb.SslCertificate{}
if err := s.storage.Delete(ctx, fqn, deleted); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "sslCertificate %q not found", name)
}

return nil, status.Errorf(codes.Internal, "error deleting sslCertificate: %v", err)
return nil, err
}

op := &pb.Operation{
Expand Down
4 changes: 4 additions & 0 deletions mockgcp/mockcompute/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (s *MockService) Register(grpcServer *grpc.Server) {
pb.RegisterAddressesServer(grpcServer, &RegionalAddressesV1{MockService: s})
pb.RegisterGlobalAddressesServer(grpcServer, &GlobalAddressesV1{MockService: s})
pb.RegisterSslCertificatesServer(grpcServer, &GlobalSSLCertificatesV1{MockService: s})
pb.RegisterTargetSslProxiesServer(grpcServer, &TargetSslProxyV1{MockService: s})

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

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

if err := pb.RegisterUrlMapsHandler(ctx, mux.ServeMux, conn); err != nil {
return nil, err
Expand Down
136 changes: 136 additions & 0 deletions mockgcp/mockcompute/targetsslproxyv1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// 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 TargetSslProxyV1 struct {
*MockService
pb.UnimplementedTargetSslProxiesServer
}

func (s *TargetSslProxyV1) Get(ctx context.Context, req *pb.GetTargetSslProxyRequest) (*pb.TargetSslProxy, error) {
reqName := "projects/" + req.GetProject() + "/global/targetSslProxies/" + req.GetTargetSslProxy()
name, err := s.parseTargetSslProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

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

return obj, nil
}

func (s *TargetSslProxyV1) Insert(ctx context.Context, req *pb.InsertTargetSslProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/global/targetSslProxies/" + req.GetTargetSslProxyResource().GetName()
name, err := s.parseTargetSslProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

id := s.generateID()

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

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 *TargetSslProxyV1) Delete(ctx context.Context, req *pb.DeleteTargetSslProxyRequest) (*pb.Operation, error) {
reqName := "projects/" + req.GetProject() + "/global/targetSslProxies/" + req.GetTargetSslProxy()
name, err := s.parseTargetSslProxyName(reqName)
if err != nil {
return nil, err
}

fqn := name.String()

deleted := &pb.TargetSslProxy{}
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
})
}

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

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

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

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

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

return name, nil
} else {
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
}
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 @@ -151,6 +151,16 @@ func (m *forwardingRuleModel) AdapterForObject(ctx context.Context, reader clien
}
obj.Spec.Target.TargetVPNGatewayRef.External = targetVPNGatewayRef.External
}

// Get target SSLProxy
if obj.Spec.Target.TargetSSLProxyRef != nil {
targetSSLProxyRef, err := ResolveComputeTargetSSLProxy(ctx, reader, obj, obj.Spec.Target.TargetSSLProxyRef)
if err != nil {
return nil, err

}
obj.Spec.Target.TargetSSLProxyRef.External = targetSSLProxyRef.External
}
}

// Get location
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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: computeglobalforwardingrule-${uniqueId}
namespace: ${uniqueId}
spec:
description: A global forwarding rule
ipAddress:
addressRef:
name: computeaddress-${uniqueId}
loadBalancingScheme: EXTERNAL
location: global
portRange: "443"
target:
targetSSLProxyRef:
name: computetargetsslproxy-${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: 1
selfLink: https://www.googleapis.com/compute/v1/projects/${projectId}/global/forwardingRules/computeglobalforwardingrule-${uniqueId}
Loading

0 comments on commit 6ff9c72

Please sign in to comment.