Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#2699 from hankfreund/attached_…
Browse files Browse the repository at this point in the history
…mock_gcp

Update mockgcp and tests for attached clusters.
  • Loading branch information
google-oss-prow[bot] authored Sep 16, 2024
2 parents 7d20d25 + 3dee86f commit 0ab6bd2
Show file tree
Hide file tree
Showing 12 changed files with 1,138 additions and 89 deletions.
64 changes: 59 additions & 5 deletions mockgcp/mockgkemulticloud/attachedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ package mockgkemulticloud

import (
"context"
"fmt"
"time"

"google.golang.org/genproto/googleapis/longrunning"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"

pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/gkemulticloud/v1"
)
Expand All @@ -40,6 +44,9 @@ func (s *GKEMulticloudV1) GetAttachedCluster(ctx context.Context, req *pb.GetAtt

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

Expand All @@ -53,6 +60,7 @@ func (s *GKEMulticloudV1) CreateAttachedCluster(ctx context.Context, req *pb.Cre
return nil, err
}

now := time.Now()
fqn := name.String()

obj := proto.Clone(req.AttachedCluster).(*pb.AttachedCluster)
Expand All @@ -62,7 +70,18 @@ func (s *GKEMulticloudV1) CreateAttachedCluster(ctx context.Context, req *pb.Cre
return nil, err
}

return s.operations.NewLRO(ctx)
opMetadata := &pb.OperationMetadata{
CreateTime: timestamppb.New(now),
Verb: "create",
RequestedCancellation: false,
Target: fqn,
}
opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location)
return s.operations.StartLRO(ctx, opPrefix, opMetadata, func() (proto.Message, error) {
opMetadata.EndTime = timestamppb.Now()
obj.State = pb.AttachedCluster_RUNNING
return obj, nil
})
}

func (s *GKEMulticloudV1) UpdateAttachedCluster(ctx context.Context, req *pb.UpdateAttachedClusterRequest) (*longrunning.Operation, error) {
Expand All @@ -71,6 +90,7 @@ func (s *GKEMulticloudV1) UpdateAttachedCluster(ctx context.Context, req *pb.Upd
return nil, err
}

now := time.Now()
fqn := name.String()
obj := &pb.AttachedCluster{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
Expand All @@ -81,12 +101,13 @@ func (s *GKEMulticloudV1) UpdateAttachedCluster(ctx context.Context, req *pb.Upd
// fields from
// [AttachedCluster][mockgcp.cloud.gkemulticloud.v1.AttachedCluster]:
//
// - `description`.
// - `annotations`.
// - `platform_version`.
// - `authorization.admin_users`.
// - `binary_authorization.evaluation_mode`.
// - `description`.
// - `logging_config.component_config.enable_components`.
// - `monitoring_config.managed_prometheus_config.enabled`.
// - `platform_version`.

paths := req.GetUpdateMask().GetPaths()
if len(paths) == 0 {
Expand All @@ -96,8 +117,20 @@ func (s *GKEMulticloudV1) UpdateAttachedCluster(ctx context.Context, req *pb.Upd
// TODO: Some sort of helper for fieldmask?
for _, path := range paths {
switch path {
case "annotations":
obj.Annotations = req.GetAttachedCluster().GetAnnotations()
case "authorization.admin_users":
obj.Authorization.AdminUsers = req.GetAttachedCluster().GetAuthorization().GetAdminUsers()
case "binary_authorization.evaluation_mode":
obj.BinaryAuthorization.EvaluationMode = req.GetAttachedCluster().GetBinaryAuthorization().GetEvaluationMode()
case "description":
obj.Description = req.GetAttachedCluster().GetDescription()
case "logging_config.component_config.enable_components":
obj.LoggingConfig.ComponentConfig.EnableComponents = req.GetAttachedCluster().GetLoggingConfig().GetComponentConfig().GetEnableComponents()
case "monitoring_config.managed_prometheus_config.enabled":
obj.MonitoringConfig = req.GetAttachedCluster().GetMonitoringConfig()
case "platformVersion":
obj.PlatformVersion = req.GetAttachedCluster().GetPlatformVersion()
default:
return nil, status.Errorf(codes.InvalidArgument, "update_mask path %q not valid", path)
}
Expand All @@ -106,7 +139,17 @@ func (s *GKEMulticloudV1) UpdateAttachedCluster(ctx context.Context, req *pb.Upd
if err := s.storage.Update(ctx, fqn, obj); err != nil {
return nil, err
}
return s.operations.NewLRO(ctx)
opMetadata := &pb.OperationMetadata{
CreateTime: timestamppb.New(now),
Verb: "update",
RequestedCancellation: false,
Target: fqn,
}
opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location)
return s.operations.StartLRO(ctx, opPrefix, opMetadata, func() (proto.Message, error) {
opMetadata.EndTime = timestamppb.Now()
return obj, nil
})
}

func (s *GKEMulticloudV1) DeleteAttachedCluster(ctx context.Context, req *pb.DeleteAttachedClusterRequest) (*longrunning.Operation, error) {
Expand All @@ -115,12 +158,23 @@ func (s *GKEMulticloudV1) DeleteAttachedCluster(ctx context.Context, req *pb.Del
return nil, err
}

now := time.Now()
fqn := name.String()

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

return s.operations.NewLRO(ctx)
opMetadata := &pb.OperationMetadata{
CreateTime: timestamppb.New(now),
Verb: "delete",
RequestedCancellation: false,
Target: fqn,
}
opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location)
return s.operations.StartLRO(ctx, opPrefix, opMetadata, func() (proto.Message, error) {
opMetadata.EndTime = timestamppb.Now()
return &emptypb.Empty{}, nil
})
}
16 changes: 12 additions & 4 deletions mockgcp/mockgkemulticloud/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"context"
"net/http"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"

"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/httpmux"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/operations"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/gkemulticloud/v1"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
Expand Down Expand Up @@ -57,11 +57,19 @@ func (s *MockService) Register(grpcServer *grpc.Server) {
}

func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (http.Handler, error) {
mux := runtime.NewServeMux()

if err := pb.RegisterAttachedClustersHandler(ctx, mux, conn); err != nil {
mux, err := httpmux.NewServeMux(ctx, conn, httpmux.Options{},
pb.RegisterAttachedClustersHandler,
s.operations.RegisterOperationsPath("/v1/{prefix=**}/operations/{name}"))
if err != nil {
return nil, err
}

// Returns slightly non-standard errors
mux.RewriteError = func(ctx context.Context, error *httpmux.ErrorResponse) {
if error.Code == 404 {
error.Errors = nil
}
}

return mux, nil
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# 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.

apiVersion: containerattached.cnrm.cloud.google.com/v1beta1
kind: ContainerAttachedCluster
metadata:
Expand All @@ -37,7 +23,7 @@ spec:
location: us-west1
oidcConfig:
issuerUrl: https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F
platformVersion: 1.27.0-gke.2
platformVersion: 1.28.0-gke.2
projectRef:
external: ${projectId}
resourceID: kcc-attached-cluster-127
Expand All @@ -49,4 +35,3 @@ status:
status: "True"
type: Ready
observedGeneration: 2
state: STATE_UNSPECIFIED
Loading

0 comments on commit 0ab6bd2

Please sign in to comment.