From 6ef45e18c37788c6e786cd85f0547d3205c0ffb8 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Tue, 12 Nov 2024 18:12:01 +0000 Subject: [PATCH 01/22] add repository controller --- .../repository_controller.go | 232 ++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 pkg/controller/direct/securesourcemanager/repository_controller.go diff --git a/pkg/controller/direct/securesourcemanager/repository_controller.go b/pkg/controller/direct/securesourcemanager/repository_controller.go new file mode 100644 index 0000000000..b08f287624 --- /dev/null +++ b/pkg/controller/direct/securesourcemanager/repository_controller.go @@ -0,0 +1,232 @@ +// 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 securesourcemanager + +import ( + "context" + "fmt" + + refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" + krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/securesourcemanager/v1alpha1" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" + + // TODO(user): Update the import with the google cloud client + gcp "cloud.google.com/go/securesourcemanager/apiv1" + + // TODO(user): Update the import with the google cloud client api protobuf + securesourcemanagerpb "cloud.google.com/go/securesourcemanager/apiv1/securesourcemanagerpb" + "google.golang.org/api/option" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/klog/v2" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func init() { + registry.RegisterModel(krm.SecureSourceManagerRepositoryGVK, NewSecureSourceManagerRepositoryModel) +} + +func NewSecureSourceManagerRepositoryModel(ctx context.Context, config *config.ControllerConfig) (directbase.Model, error) { + return &modelSecureSourceManagerRepository{config: *config}, nil +} + +var _ directbase.Model = &modelSecureSourceManagerRepository{} + +type modelSecureSourceManagerRepository struct { + config config.ControllerConfig +} + +func (m *modelSecureSourceManagerRepository) client(ctx context.Context) (*gcp.Client, error) { + var opts []option.ClientOption + opts, err := m.config.RESTClientOptions() + if err != nil { + return nil, err + } + gcpClient, err := gcp.NewRESTClient(ctx, opts...) + if err != nil { + return nil, fmt.Errorf("building Repository client: %w", err) + } + return gcpClient, err +} + +func (m *modelSecureSourceManagerRepository) AdapterForObject(ctx context.Context, reader client.Reader, u *unstructured.Unstructured) (directbase.Adapter, error) { + obj := &krm.SecureSourceManagerRepository{} + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &obj); err != nil { + return nil, fmt.Errorf("error converting to %T: %w", obj, err) + } + + // Resolve SecureSourceManagerInstanceRef + if obj.Spec.InstanceRef != nil { + if obj.Spec.InstanceRef != nil { + instance, err := krm.ResolveSecureSourceManagerInstanceRef(ctx, reader, obj, obj.Spec.InstanceRef) + if err != nil { + return nil, err + } + obj.Spec.InstanceRef.External = instance.String() + } + } + + id, err := krm.NewSecureSourceManagerRepositoryRef(ctx, reader, obj) + if err != nil { + return nil, err + } + + // Get securesourcemanager GCP client + gcpClient, err := m.client(ctx) + if err != nil { + return nil, err + } + return &SecureSourceManagerRepositoryAdapter{ + id: id, + gcpClient: gcpClient, + desired: obj, + }, nil +} + +func (m *modelSecureSourceManagerRepository) AdapterForURL(ctx context.Context, url string) (directbase.Adapter, error) { + // TODO: Support URLs + return nil, nil +} + +type SecureSourceManagerRepositoryAdapter struct { + id *krm.SecureSourceManagerRepositoryRef + gcpClient *gcp.Client + desired *krm.SecureSourceManagerRepository + actual *securesourcemanagerpb.Repository +} + +var _ directbase.Adapter = &SecureSourceManagerRepositoryAdapter{} + +func (a *SecureSourceManagerRepositoryAdapter) Find(ctx context.Context) (bool, error) { + log := klog.FromContext(ctx) + log.V(2).Info("getting SecureSourceManagerRepository", "name", a.id.External) + + req := &securesourcemanagerpb.GetRepositoryRequest{Name: a.id.External} + repositorypb, err := a.gcpClient.GetRepository(ctx, req) + if err != nil { + if direct.IsNotFound(err) { + return false, nil + } + return false, fmt.Errorf("getting SecureSourceManagerRepository %q: %w", a.id.External, err) + } + + a.actual = repositorypb + return true, nil +} + +func (a *SecureSourceManagerRepositoryAdapter) Create(ctx context.Context, createOp *directbase.CreateOperation) error { + log := klog.FromContext(ctx) + log.V(2).Info("creating Repository", "name", a.id.External) + mapCtx := &direct.MapContext{} + + desired := a.desired.DeepCopy() + resource := SecureSourceManagerRepositorySpec_ToProto(mapCtx, &desired.Spec) + if mapCtx.Err() != nil { + return mapCtx.Err() + } + + parent, err := a.id.Parent() + if err != nil { + return err + } + repositoryID, err := a.id.ResourceID() + if err != nil { + return err + } + + req := &securesourcemanagerpb.CreateRepositoryRequest{ + Parent: parent.String(), + Repository: resource, + RepositoryId: repositoryID, + } + op, err := a.gcpClient.CreateRepository(ctx, req) + if err != nil { + return fmt.Errorf("creating Repository %s: %w", a.id.External, err) + } + created, err := op.Wait(ctx) + if err != nil { + return fmt.Errorf("Repository %s waiting creation: %w", a.id.External, err) + } + log.V(2).Info("successfully created Repository", "name", a.id.External) + + status := &krm.SecureSourceManagerRepositoryStatus{} + status.ObservedState = SecureSourceManagerRepositoryObservedState_FromProto(mapCtx, created) + if mapCtx.Err() != nil { + return mapCtx.Err() + } + status.ExternalRef = &a.id.External + return createOp.UpdateStatus(ctx, status, nil) +} + +func (a *SecureSourceManagerRepositoryAdapter) Update(ctx context.Context, updateOp *directbase.UpdateOperation) error { + log := klog.FromContext(ctx) + log.Info("update of SecureSourceManagerRepository not supported") + return nil +} + +func (a *SecureSourceManagerRepositoryAdapter) Export(ctx context.Context) (*unstructured.Unstructured, error) { + if a.actual == nil { + return nil, fmt.Errorf("Find() not called") + } + u := &unstructured.Unstructured{} + + obj := &krm.SecureSourceManagerRepository{} + mapCtx := &direct.MapContext{} + obj.Spec = direct.ValueOf(SecureSourceManagerRepositorySpec_FromProto(mapCtx, a.actual)) + if mapCtx.Err() != nil { + return nil, mapCtx.Err() + } + parent, err := a.id.Parent() + if err != nil { + return nil, err + } + repositoryID, err := a.id.ResourceID() + if err != nil { + return nil, err + } + obj.Spec.ProjectRef = &refs.ProjectRef{Name: parent.ProjectID} + obj.Spec.Location = parent.Location + uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj) + if err != nil { + return nil, err + } + u.SetName(repositoryID) + u.SetGroupVersionKind(krm.SecureSourceManagerRepositoryGVK) + u.Object = uObj + return u, nil +} + +// Delete implements the Adapter interface. +func (a *SecureSourceManagerRepositoryAdapter) Delete(ctx context.Context, deleteOp *directbase.DeleteOperation) (bool, error) { + log := klog.FromContext(ctx) + log.V(2).Info("deleting Repository", "name", a.id.External) + + req := &securesourcemanagerpb.DeleteRepositoryRequest{Name: a.id.External} + op, err := a.gcpClient.DeleteRepository(ctx, req) + if err != nil { + return false, fmt.Errorf("deleting Repository %s: %w", a.id.External, err) + } + log.V(2).Info("successfully deleted Repository", "name", a.id.External) + + err = op.Wait(ctx) + if err != nil { + return false, fmt.Errorf("waiting delete Repository %s: %w", a.id.External, err) + } + return true, nil +} From a29e28601642a59780fa0e1b5a9caff82f12094a Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Tue, 12 Nov 2024 18:15:27 +0000 Subject: [PATCH 02/22] Add delete lro handling hack --- .../securesourcemanager/repository_controller.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pkg/controller/direct/securesourcemanager/repository_controller.go b/pkg/controller/direct/securesourcemanager/repository_controller.go index b08f287624..5a9804745c 100644 --- a/pkg/controller/direct/securesourcemanager/repository_controller.go +++ b/pkg/controller/direct/securesourcemanager/repository_controller.go @@ -17,6 +17,7 @@ package securesourcemanager import ( "context" "fmt" + "strings" refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/securesourcemanager/v1alpha1" @@ -218,15 +219,13 @@ func (a *SecureSourceManagerRepositoryAdapter) Delete(ctx context.Context, delet log.V(2).Info("deleting Repository", "name", a.id.External) req := &securesourcemanagerpb.DeleteRepositoryRequest{Name: a.id.External} - op, err := a.gcpClient.DeleteRepository(ctx, req) + _, err := a.gcpClient.DeleteRepository(ctx, req) + // TODO - remove after the Go protobuf fix is in. https://github.com/golang/protobuf/issues/1620#issuecomment-2402608919 + // Handles the LRO parsing error. if err != nil { - return false, fmt.Errorf("deleting Repository %s: %w", a.id.External, err) - } - log.V(2).Info("successfully deleted Repository", "name", a.id.External) - - err = op.Wait(ctx) - if err != nil { - return false, fmt.Errorf("waiting delete Repository %s: %w", a.id.External, err) + if !strings.Contains(err.Error(), "(line 14:3): missing \"value\" field") { + return false, fmt.Errorf("deleting Repository %s: %w", a.id.External, err) + } } return true, nil } From 2267e07f990bd71c8bd395486e8fbb7ff59de180 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Tue, 12 Nov 2024 18:21:27 +0000 Subject: [PATCH 03/22] Add mock --- config/tests/samples/create/harness.go | 1 + mockgcp/mocksecuresourcemanager/repository.go | 165 ++++++++++++++++++ pkg/controller/direct/registry/registry.go | 2 + .../create.yaml | 26 +++ .../dependencies.yaml | 22 +++ 5 files changed, 216 insertions(+) create mode 100644 mockgcp/mocksecuresourcemanager/repository.go create mode 100644 pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/create.yaml create mode 100644 pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml diff --git a/config/tests/samples/create/harness.go b/config/tests/samples/create/harness.go index 9a999ec696..9e7ea2a226 100644 --- a/config/tests/samples/create/harness.go +++ b/config/tests/samples/create/harness.go @@ -855,6 +855,7 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured case schema.GroupKind{Group: "secretmanager.cnrm.cloud.google.com", Kind: "SecretManagerSecretVersion"}: case schema.GroupKind{Group: "securesourcemanager.cnrm.cloud.google.com", Kind: "SecureSourceManagerInstance"}: + case schema.GroupKind{Group: "securesourcemanager.cnrm.cloud.google.com", Kind: "SecureSourceManagerRepository"}: case schema.GroupKind{Group: "servicedirectory.cnrm.cloud.google.com", Kind: "ServiceDirectoryNamespace"}: case schema.GroupKind{Group: "servicedirectory.cnrm.cloud.google.com", Kind: "ServiceDirectoryService"}: diff --git a/mockgcp/mocksecuresourcemanager/repository.go b/mockgcp/mocksecuresourcemanager/repository.go new file mode 100644 index 0000000000..6b8cd9b0da --- /dev/null +++ b/mockgcp/mocksecuresourcemanager/repository.go @@ -0,0 +1,165 @@ +// 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 mocksecuresourcemanager + +import ( + "context" + "fmt" + "strings" + "time" + + longrunning "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" + + "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects" + pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/securesourcemanager/v1" +) + +func (s *secureSourceManagerServer) GetRepository(ctx context.Context, req *pb.GetRepositoryRequest) (*pb.Repository, error) { + name, err := s.parseRepositoryName(req.Name) + if err != nil { + return nil, err + } + + fqn := name.String() + + obj := &pb.Repository{} + 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 + } + + return obj, nil +} + +func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *pb.CreateRepositoryRequest) (*longrunning.Operation, error) { + reqName := req.Parent + "/repositories/" + req.RepositoryId + name, err := s.parseRepositoryName(reqName) + if err != nil { + return nil, err + } + + fqn := name.String() + + now := time.Now() + + obj := proto.Clone(req.Repository).(*pb.Repository) + obj.Name = fqn + + obj.CreateTime = timestamppb.New(now) + obj.UpdateTime = timestamppb.New(now) + + instanceName, err := s.parseInstanceName(obj.GetInstance()) + if err != nil { + return nil, err + } + + prefix := fmt.Sprintf("%s-%d", instanceName.InstanceID, name.Project.Number) + domain := "." + name.Location + ".sourcemanager.dev" + obj.Uris = &pb.Repository_URIs{ + Html: prefix + domain + fmt.Sprintf("%s/%s", name.Project.ID, req.GetRepositoryId()), + Api: prefix + "-api" + domain + fmt.Sprintf("/v1/projects/%s/locations/%s/repositories/%s", name.Project.ID, name.Location, req.GetRepositoryId()), + GitHttps: prefix + "-git" + domain + fmt.Sprintf("%s/%s.git", name.Project.ID, req.GetRepositoryId()), + } + + if err := s.storage.Create(ctx, fqn, obj); err != nil { + return nil, err + } + + op := &pb.OperationMetadata{ + CreateTime: timestamppb.New(now), + Target: name.String(), + Verb: "create", + ApiVersion: "v1", + } + opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location) + return s.operations.StartLRO(ctx, opPrefix, op, func() (proto.Message, error) { + op.EndTime = timestamppb.Now() + return obj, nil + }) +} + +func (s *secureSourceManagerServer) DeleteRepository(ctx context.Context, req *pb.DeleteRepositoryRequest) (*longrunning.Operation, error) { + name, err := s.parseRepositoryName(req.GetName()) + if err != nil { + return nil, err + } + + fqn := name.String() + now := time.Now() + + deleted := &pb.Repository{} + if err := s.storage.Delete(ctx, fqn, deleted); err != nil { + return nil, err + } + + op := &pb.OperationMetadata{ + CreateTime: timestamppb.New(now), + Target: name.String(), + Verb: "delete", + ApiVersion: "v1", + } + opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location) + return s.operations.StartLRO(ctx, opPrefix, op, func() (proto.Message, error) { + op.EndTime = timestamppb.Now() + return &emptypb.Empty{}, nil + }) +} + +type RepositoryName struct { + Project *projects.ProjectData + Location string + RepositoryID string +} + +func (n *RepositoryName) String() string { + return fmt.Sprintf("projects/%s/locations/%s/repositories/%s", n.Project.ID, n.Location, n.RepositoryID) +} + +// func (n *RepositoryName) Target() string { +// return fmt.Sprintf("projects/%s/locations/%s/repositories/%s", n.Project.ID, n.Location, n.RepositoryID) +// } + +// parseRepositoryName parses a string into a RepositoryName. +// The expected form is projects/*/locations/*/repositories/* +func (s *MockService) parseRepositoryName(name string) (*RepositoryName, error) { + tokens := strings.Split(name, "/") + + if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "locations" && tokens[4] == "repositories" { + projectName, err := projects.ParseProjectName(tokens[0] + "/" + tokens[1]) + if err != nil { + return nil, err + } + project, err := s.Projects.GetProject(projectName) + if err != nil { + return nil, err + } + + name := &RepositoryName{ + Project: project, + Location: tokens[3], + RepositoryID: tokens[5], + } + return name, nil + } else { + return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name) + } +} diff --git a/pkg/controller/direct/registry/registry.go b/pkg/controller/direct/registry/registry.go index a2cf44272e..20527466bd 100644 --- a/pkg/controller/direct/registry/registry.go +++ b/pkg/controller/direct/registry/registry.go @@ -143,6 +143,8 @@ func SupportsIAM(groupKind schema.GroupKind) (bool, error) { return false, nil case schema.GroupKind{Group: "securesourcemanager.cnrm.cloud.google.com", Kind: "SecureSourceManagerInstance"}: return false, nil + case schema.GroupKind{Group: "securesourcemanager.cnrm.cloud.google.com", Kind: "SecureSourceManagerRepository"}: + return false, nil case schema.GroupKind{Group: "discoveryengine.cnrm.cloud.google.com", Kind: "DiscoveryEngineDataStore"}: return false, nil } diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/create.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/create.yaml new file mode 100644 index 0000000000..1772dd59f2 --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/create.yaml @@ -0,0 +1,26 @@ +# 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: securesourcemanager.cnrm.cloud.google.com/v1alpha1 +kind: SecureSourceManagerRepository +metadata: + name: ssmrepository-${uniqueId} + annotations: + alpha.cnrm.cloud.google.com/reconciler: "direct" +spec: + location: us-central1 + projectRef: + external: ${projectId} + instanceRef: + name: ssminstance-dep-${uniqueId} \ No newline at end of file diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml new file mode 100644 index 0000000000..4ccd139a65 --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml @@ -0,0 +1,22 @@ +# 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: securesourcemanager.cnrm.cloud.google.com/v1alpha1 +kind: SecureSourceManagerInstance +metadata: + name: ssminstance-dep-${uniqueId} +spec: + location: us-central1 + projectRef: + external: ${projectId} From fcec6c96142fc21568cddd3cb07e54570ae31b03 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Tue, 12 Nov 2024 18:22:29 +0000 Subject: [PATCH 04/22] Fix instance ref in repo controller --- .../securesourcemanager/repository_controller.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pkg/controller/direct/securesourcemanager/repository_controller.go b/pkg/controller/direct/securesourcemanager/repository_controller.go index 5a9804745c..4a8084afa4 100644 --- a/pkg/controller/direct/securesourcemanager/repository_controller.go +++ b/pkg/controller/direct/securesourcemanager/repository_controller.go @@ -73,15 +73,12 @@ func (m *modelSecureSourceManagerRepository) AdapterForObject(ctx context.Contex } // Resolve SecureSourceManagerInstanceRef - if obj.Spec.InstanceRef != nil { - if obj.Spec.InstanceRef != nil { - instance, err := krm.ResolveSecureSourceManagerInstanceRef(ctx, reader, obj, obj.Spec.InstanceRef) - if err != nil { - return nil, err - } - obj.Spec.InstanceRef.External = instance.String() - } + instanceRef := obj.Spec.InstanceRef + normalizedExternal, err := instanceRef.NormalizedExternal(ctx, reader, instanceRef.Namespace) + if err != nil { + return nil, err } + obj.Spec.InstanceRef.External = normalizedExternal id, err := krm.NewSecureSourceManagerRepositoryRef(ctx, reader, obj) if err != nil { From 786053607681890b4fcaa6e0673670b977eaedc5 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Tue, 12 Nov 2024 18:26:25 +0000 Subject: [PATCH 05/22] Add direct annotation to dependencies.yaml --- .../securesourcemanagerrepositorybasic/dependencies.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml index 4ccd139a65..892f8b1032 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml @@ -16,6 +16,8 @@ apiVersion: securesourcemanager.cnrm.cloud.google.com/v1alpha1 kind: SecureSourceManagerInstance metadata: name: ssminstance-dep-${uniqueId} + annotations: + alpha.cnrm.cloud.google.com/reconciler: "direct" spec: location: us-central1 projectRef: From c873b257421af4886bb226fc253d579bb2239663 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Tue, 12 Nov 2024 19:09:17 +0000 Subject: [PATCH 06/22] Fix namespace reference --- .../direct/securesourcemanager/repository_controller.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/controller/direct/securesourcemanager/repository_controller.go b/pkg/controller/direct/securesourcemanager/repository_controller.go index 4a8084afa4..7011c318cd 100644 --- a/pkg/controller/direct/securesourcemanager/repository_controller.go +++ b/pkg/controller/direct/securesourcemanager/repository_controller.go @@ -74,7 +74,7 @@ func (m *modelSecureSourceManagerRepository) AdapterForObject(ctx context.Contex // Resolve SecureSourceManagerInstanceRef instanceRef := obj.Spec.InstanceRef - normalizedExternal, err := instanceRef.NormalizedExternal(ctx, reader, instanceRef.Namespace) + normalizedExternal, err := instanceRef.NormalizedExternal(ctx, reader, obj.Namespace) if err != nil { return nil, err } From a8059a2ec91a50c5bf82b780e2463eae7d51d6e0 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 13 Nov 2024 17:48:28 +0000 Subject: [PATCH 07/22] log: real gcp --- ...resourcemanagerrepositorybasic.golden.yaml | 33 ++ .../_http.log | 353 ++++++++++++++++++ 2 files changed, 386 insertions(+) create mode 100644 pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml create mode 100644 pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml new file mode 100644 index 0000000000..9a5caa72a4 --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml @@ -0,0 +1,33 @@ +apiVersion: securesourcemanager.cnrm.cloud.google.com/v1alpha1 +kind: SecureSourceManagerRepository +metadata: + annotations: + alpha.cnrm.cloud.google.com/reconciler: direct + finalizers: + - cnrm.cloud.google.com/finalizer + - cnrm.cloud.google.com/deletion-defender + generation: 1 + labels: + cnrm-test: "true" + name: ssmrepository-${uniqueId} + namespace: ${uniqueId} +spec: + instanceRef: + external: projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId} + location: us-central1 + projectRef: + external: ${projectId} +status: + conditions: + - lastTransitionTime: "1970-01-01T00:00:00Z" + message: The resource is up to date + reason: UpToDate + status: "True" + type: Ready + externalRef: projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} + observedGeneration: 1 + observedState: + uris: + api: https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} + gitHTTPS: https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git + html: https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId} diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log new file mode 100644 index 0000000000..8749e6a558 --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log @@ -0,0 +1,353 @@ +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} + +404 Not Found +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "error": { + "code": 404, + "message": "Resource 'projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}' was not found", + "status": "NOT_FOUND" + } +} + +--- + +POST https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/instances?%24alt=json%3Benum-encoding%3Dint&instanceId=ssminstance-dep-${uniqueId} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 + +{} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "verb": "create" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "done": true, + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "verb": "create" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", + "response": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Instance", + "createTime": "2024-04-01T12:34:56.123456Z", + "hostConfig": { + "api": "ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev", + "gitHttp": "ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev", + "gitSsh": "ssminstance-dep-${uniqueId}-${projectNumber}-ssh.us-central1.sourcemanager.dev", + "html": "ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev" + }, + "name": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "state": "ACTIVE", + "updateTime": "2024-04-01T12:34:56.123456Z" + } +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} + +404 Not Found +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "error": { + "code": 404, + "message": "Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found", + "status": "NOT_FOUND" + } +} + +--- + +POST https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories?%24alt=json%3Benum-encoding%3Dint&repositoryId=ssmrepository-${uniqueId} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 + +{ + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}" +} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "verb": "create" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "done": true, + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "verb": "create" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", + "response": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Repository", + "createTime": "2024-04-01T12:34:56.123456Z", + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "updateTime": "2024-04-01T12:34:56.123456Z", + "uris": { + "api": "ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "gitHttps": "ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git", + "html": "ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}" + } + } +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "createTime": "2024-04-01T12:34:56.123456Z", + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "updateTime": "2024-04-01T12:34:56.123456Z", + "uris": { + "api": "ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "gitHttps": "ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git", + "html": "ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}" + } +} + +--- + +DELETE https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "verb": "delete" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "createTime": "2024-04-01T12:34:56.123456Z", + "hostConfig": { + "api": "ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev", + "gitHttp": "ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev", + "gitSsh": "ssminstance-dep-${uniqueId}-${projectNumber}-ssh.us-central1.sourcemanager.dev", + "html": "ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev" + }, + "name": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "state": 2, + "updateTime": "2024-04-01T12:34:56.123456Z" +} + +--- + +DELETE https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "verb": "delete" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "done": true, + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "verb": "delete" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", + "response": { + "@type": "type.googleapis.com/google.protobuf.Empty" + } +} \ No newline at end of file From 81540bf4578ab79f74b0c98248108f7ebafd8c47 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 13 Nov 2024 17:57:13 +0000 Subject: [PATCH 08/22] Update log --- mockgcp/mocksecuresourcemanager/repository.go | 2 +- ...ct_securesourcemanagerrepositorybasic.golden.yaml | 6 +++--- .../securesourcemanagerrepositorybasic/_http.log | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/mockgcp/mocksecuresourcemanager/repository.go b/mockgcp/mocksecuresourcemanager/repository.go index 6b8cd9b0da..c9bfdc6260 100644 --- a/mockgcp/mocksecuresourcemanager/repository.go +++ b/mockgcp/mocksecuresourcemanager/repository.go @@ -72,7 +72,7 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p return nil, err } - prefix := fmt.Sprintf("%s-%d", instanceName.InstanceID, name.Project.Number) + prefix := fmt.Sprintf("https://%s-%d", instanceName.InstanceID, name.Project.Number) domain := "." + name.Location + ".sourcemanager.dev" obj.Uris = &pb.Repository_URIs{ Html: prefix + domain + fmt.Sprintf("%s/%s", name.Project.ID, req.GetRepositoryId()), diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml index 9a5caa72a4..ff54dd8b0c 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml @@ -13,7 +13,7 @@ metadata: namespace: ${uniqueId} spec: instanceRef: - external: projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId} + name: ssminstance-dep-${uniqueId} location: us-central1 projectRef: external: ${projectId} @@ -29,5 +29,5 @@ status: observedState: uris: api: https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} - gitHTTPS: https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git - html: https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId} + gitHTTPS: https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git + html: https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId} diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log index 8749e6a558..4e88bb11dd 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log @@ -192,9 +192,9 @@ X-Xss-Protection: 0 "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { - "api": "ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "gitHttps": "ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git", - "html": "ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}" + "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git", + "html": "https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}" } } } @@ -223,9 +223,9 @@ X-Xss-Protection: 0 "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { - "api": "ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "gitHttps": "ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git", - "html": "ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}" + "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git", + "html": "https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}" } } From 9c77998553f79461f87a4f4c8b9175660dae7ed1 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 13 Nov 2024 18:02:32 +0000 Subject: [PATCH 09/22] fix mockgcp repository --- mockgcp/mocksecuresourcemanager/repository.go | 4 ++-- ..._object_securesourcemanagerrepositorybasic.golden.yaml | 4 ++-- .../securesourcemanagerrepositorybasic/_http.log | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mockgcp/mocksecuresourcemanager/repository.go b/mockgcp/mocksecuresourcemanager/repository.go index c9bfdc6260..93185bbba9 100644 --- a/mockgcp/mocksecuresourcemanager/repository.go +++ b/mockgcp/mocksecuresourcemanager/repository.go @@ -75,9 +75,9 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p prefix := fmt.Sprintf("https://%s-%d", instanceName.InstanceID, name.Project.Number) domain := "." + name.Location + ".sourcemanager.dev" obj.Uris = &pb.Repository_URIs{ - Html: prefix + domain + fmt.Sprintf("%s/%s", name.Project.ID, req.GetRepositoryId()), + Html: prefix + domain + fmt.Sprintf("/%s/%s", name.Project.ID, req.GetRepositoryId()), Api: prefix + "-api" + domain + fmt.Sprintf("/v1/projects/%s/locations/%s/repositories/%s", name.Project.ID, name.Location, req.GetRepositoryId()), - GitHttps: prefix + "-git" + domain + fmt.Sprintf("%s/%s.git", name.Project.ID, req.GetRepositoryId()), + GitHttps: prefix + "-git" + domain + fmt.Sprintf("/%s/%s.git", name.Project.ID, req.GetRepositoryId()), } if err := s.storage.Create(ctx, fqn, obj); err != nil { diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml index ff54dd8b0c..e849ab154c 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml @@ -29,5 +29,5 @@ status: observedState: uris: api: https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} - gitHTTPS: https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git - html: https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId} + gitHTTPS: https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git + html: https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId} diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log index 4e88bb11dd..ab9f31955d 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log @@ -193,8 +193,8 @@ X-Xss-Protection: 0 "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git", - "html": "https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}" + "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", + "html": "https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}" } } } @@ -224,8 +224,8 @@ X-Xss-Protection: 0 "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}.git", - "html": "https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev${projectId}/ssmrepository-${uniqueId}" + "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", + "html": "https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}" } } From fb5318b9d32e97020ba5b0906ed15e0f6363dc55 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Tue, 19 Nov 2024 23:55:27 +0000 Subject: [PATCH 10/22] Remove direct annotation --- .../securesourcemanagerrepositorybasic/create.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/create.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/create.yaml index 1772dd59f2..eacf771676 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/create.yaml +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/create.yaml @@ -16,8 +16,6 @@ apiVersion: securesourcemanager.cnrm.cloud.google.com/v1alpha1 kind: SecureSourceManagerRepository metadata: name: ssmrepository-${uniqueId} - annotations: - alpha.cnrm.cloud.google.com/reconciler: "direct" spec: location: us-central1 projectRef: From 073f13d3cb8b33d2deb671a35cd67b309ae98d64 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 20 Nov 2024 05:14:17 +0000 Subject: [PATCH 11/22] Remove another direct annotation --- .../securesourcemanagerrepositorybasic/dependencies.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml index 892f8b1032..4ccd139a65 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/dependencies.yaml @@ -16,8 +16,6 @@ apiVersion: securesourcemanager.cnrm.cloud.google.com/v1alpha1 kind: SecureSourceManagerInstance metadata: name: ssminstance-dep-${uniqueId} - annotations: - alpha.cnrm.cloud.google.com/reconciler: "direct" spec: location: us-central1 projectRef: From 2d1ad3cbd6b42f985c89e4b37741ca16eb05fe7e Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 20 Nov 2024 05:14:54 +0000 Subject: [PATCH 12/22] Remove direct annotation --- ...erated_object_securesourcemanagerrepositorybasic.golden.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml index e849ab154c..e03bfef5d6 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_generated_object_securesourcemanagerrepositorybasic.golden.yaml @@ -1,8 +1,6 @@ apiVersion: securesourcemanager.cnrm.cloud.google.com/v1alpha1 kind: SecureSourceManagerRepository metadata: - annotations: - alpha.cnrm.cloud.google.com/reconciler: direct finalizers: - cnrm.cloud.google.com/finalizer - cnrm.cloud.google.com/deletion-defender From 848cde276b0fa76e654c7b3315c8a879ec8fbed7 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Thu, 21 Nov 2024 16:59:24 +0000 Subject: [PATCH 13/22] Remove some todos --- .../direct/securesourcemanager/repository_controller.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/controller/direct/securesourcemanager/repository_controller.go b/pkg/controller/direct/securesourcemanager/repository_controller.go index 7011c318cd..a74b7bc177 100644 --- a/pkg/controller/direct/securesourcemanager/repository_controller.go +++ b/pkg/controller/direct/securesourcemanager/repository_controller.go @@ -26,10 +26,8 @@ import ( "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" - // TODO(user): Update the import with the google cloud client gcp "cloud.google.com/go/securesourcemanager/apiv1" - // TODO(user): Update the import with the google cloud client api protobuf securesourcemanagerpb "cloud.google.com/go/securesourcemanager/apiv1/securesourcemanagerpb" "google.golang.org/api/option" From 35370c474ef78c8745972ef26d1722c9a968597c Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Thu, 21 Nov 2024 20:28:47 +0000 Subject: [PATCH 14/22] Fix instance controller delete handling, and add a recording for the full repo --- mockgcp/mocksecuresourcemanager/repository.go | 2 + ...uresourcemanagerrepositoryfull.golden.yaml | 37 ++ .../_http.log | 377 ++++++++++++++++++ .../create.yaml | 30 ++ .../dependencies.yaml | 22 + 5 files changed, 468 insertions(+) create mode 100644 pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml create mode 100644 pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log create mode 100644 pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/create.yaml create mode 100644 pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/dependencies.yaml diff --git a/mockgcp/mocksecuresourcemanager/repository.go b/mockgcp/mocksecuresourcemanager/repository.go index 93185bbba9..ee982e9789 100644 --- a/mockgcp/mocksecuresourcemanager/repository.go +++ b/mockgcp/mocksecuresourcemanager/repository.go @@ -72,6 +72,8 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p return nil, err } + obj.InitialConfig = req.GetRepository().GetInitialConfig() + prefix := fmt.Sprintf("https://%s-%d", instanceName.InstanceID, name.Project.Number) domain := "." + name.Location + ".sourcemanager.dev" obj.Uris = &pb.Repository_URIs{ diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml new file mode 100644 index 0000000000..efad373cfc --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml @@ -0,0 +1,37 @@ +apiVersion: securesourcemanager.cnrm.cloud.google.com/v1alpha1 +kind: SecureSourceManagerRepository +metadata: + finalizers: + - cnrm.cloud.google.com/finalizer + - cnrm.cloud.google.com/deletion-defender + generation: 1 + labels: + cnrm-test: "true" + name: ssmrepository-${uniqueId} + namespace: ${uniqueId} +spec: + initialConfig: + defaultBranch: main + gitignores: + - python + license: mit + readme: default + instanceRef: + name: ssminstance-dep-${uniqueId} + location: us-central1 + projectRef: + external: ${projectId} +status: + conditions: + - lastTransitionTime: "1970-01-01T00:00:00Z" + message: The resource is up to date + reason: UpToDate + status: "True" + type: Ready + externalRef: projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} + observedGeneration: 1 + observedState: + uris: + api: https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} + gitHTTPS: https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git + html: https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId} diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log new file mode 100644 index 0000000000..3955a201fd --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log @@ -0,0 +1,377 @@ +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} + +404 Not Found +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "error": { + "code": 404, + "message": "Resource 'projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}' was not found", + "status": "NOT_FOUND" + } +} + +--- + +POST https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/instances?%24alt=json%3Benum-encoding%3Dint&instanceId=ssminstance-dep-${uniqueId} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 + +{} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "verb": "create" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "done": true, + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "verb": "create" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", + "response": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Instance", + "createTime": "2024-04-01T12:34:56.123456Z", + "hostConfig": { + "api": "ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev", + "gitHttp": "ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev", + "gitSsh": "ssminstance-dep-${uniqueId}-${projectNumber}-ssh.us-central1.sourcemanager.dev", + "html": "ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev" + }, + "name": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "state": "ACTIVE", + "updateTime": "2024-04-01T12:34:56.123456Z" + } +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} + +404 Not Found +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "error": { + "code": 404, + "message": "Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found", + "status": "NOT_FOUND" + } +} + +--- + +POST https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories?%24alt=json%3Benum-encoding%3Dint&repositoryId=ssmrepository-${uniqueId} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 + +{ + "initialConfig": { + "defaultBranch": "main", + "gitignores": [ + "python" + ], + "license": "mit", + "readme": "default" + }, + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}" +} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "verb": "create" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "done": true, + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "verb": "create" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", + "response": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Repository", + "createTime": "2024-04-01T12:34:56.123456Z", + "initialConfig": { + "defaultBranch": "main", + "gitignores": [ + "python" + ], + "license": "mit", + "readme": "default" + }, + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "updateTime": "2024-04-01T12:34:56.123456Z", + "uris": { + "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", + "html": "https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}" + } + } +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "createTime": "2024-04-01T12:34:56.123456Z", + "initialConfig": { + "defaultBranch": "main", + "gitignores": [ + "python" + ], + "license": "mit", + "readme": "default" + }, + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "updateTime": "2024-04-01T12:34:56.123456Z", + "uris": { + "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", + "html": "https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}" + } +} + +--- + +DELETE https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "verb": "delete" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "createTime": "2024-04-01T12:34:56.123456Z", + "hostConfig": { + "api": "ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev", + "gitHttp": "ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev", + "gitSsh": "ssminstance-dep-${uniqueId}-${projectNumber}-ssh.us-central1.sourcemanager.dev", + "html": "ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev" + }, + "name": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "state": 2, + "updateTime": "2024-04-01T12:34:56.123456Z" +} + +--- + +DELETE https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}?%24alt=json%3Benum-encoding%3Dint +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "verb": "delete" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + +{ + "done": true, + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "verb": "delete" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", + "response": { + "@type": "type.googleapis.com/google.protobuf.Empty" + } +} \ No newline at end of file diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/create.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/create.yaml new file mode 100644 index 0000000000..dfaccfe948 --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/create.yaml @@ -0,0 +1,30 @@ +# 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: securesourcemanager.cnrm.cloud.google.com/v1alpha1 +kind: SecureSourceManagerRepository +metadata: + name: ssmrepository-${uniqueId} +spec: + location: us-central1 + projectRef: + external: ${projectId} + instanceRef: + name: ssminstance-dep-${uniqueId} + initialConfig: + defaultBranch: main + gitignores: + - python + license: mit + readme: default \ No newline at end of file diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/dependencies.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/dependencies.yaml new file mode 100644 index 0000000000..4ccd139a65 --- /dev/null +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/dependencies.yaml @@ -0,0 +1,22 @@ +# 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: securesourcemanager.cnrm.cloud.google.com/v1alpha1 +kind: SecureSourceManagerInstance +metadata: + name: ssminstance-dep-${uniqueId} +spec: + location: us-central1 + projectRef: + external: ${projectId} From 152eb7f566c106bd863470fdc8d51631381b9fe0 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Sat, 23 Nov 2024 08:39:23 +0000 Subject: [PATCH 15/22] Use ref func --- pkg/controller/direct/securesourcemanager/mapper.generated.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/controller/direct/securesourcemanager/mapper.generated.go b/pkg/controller/direct/securesourcemanager/mapper.generated.go index 12e6552143..4b16df6a73 100644 --- a/pkg/controller/direct/securesourcemanager/mapper.generated.go +++ b/pkg/controller/direct/securesourcemanager/mapper.generated.go @@ -177,7 +177,7 @@ func SecureSourceManagerRepositorySpec_FromProto(mapCtx *direct.MapContext, in * // MISSING: Name // MISSING: Description if in.GetInstance() != "" { - out.InstanceRef = &krm.SecureSourceManagerInstanceRef{External: in.GetInstance()} + out.InstanceRef = SecureSourceManagerRepositorySpec_InstanceRef_FromProto(mapCtx, in.GetInstance()) } // MISSING: Uid // MISSING: CreateTime From f697b939821cb8d9c329e43bf0f6bbb5752af7fa Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Tue, 26 Nov 2024 04:12:04 +0000 Subject: [PATCH 16/22] log realgcp --- .../_http.log | 68 +++++-------------- 1 file changed, 16 insertions(+), 52 deletions(-) diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log index 3955a201fd..082b960fc2 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log @@ -43,10 +43,12 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { + "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -78,6 +80,7 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -118,7 +121,7 @@ X-Xss-Protection: 0 { "error": { "code": 404, - "message": "Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found", + "message": "Resource \"projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}\" was not found", "status": "NOT_FOUND" } } @@ -153,35 +156,6 @@ X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 -{ - "metadata": { - "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", - "apiVersion": "v1", - "createTime": "2024-04-01T12:34:56.123456Z", - "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "verb": "create" - }, - "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" -} - ---- - -GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} -Content-Type: application/json -User-Agent: kcc/controller-manager -x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} - -200 OK -Cache-Control: private -Content-Type: application/json; charset=UTF-8 -Server: ESF -Vary: Origin -Vary: X-Origin -Vary: Referer -X-Content-Type-Options: nosniff -X-Frame-Options: SAMEORIGIN -X-Xss-Protection: 0 - { "done": true, "metadata": { @@ -192,21 +166,12 @@ X-Xss-Protection: 0 "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "create" }, - "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", + "name": "operations/${operationID}", "response": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Repository", "createTime": "2024-04-01T12:34:56.123456Z", - "initialConfig": { - "defaultBranch": "main", - "gitignores": [ - "python" - ], - "license": "mit", - "readme": "default" - }, - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", @@ -235,17 +200,8 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", - "initialConfig": { - "defaultBranch": "main", - "gitignores": [ - "python" - ], - "license": "mit", - "readme": "default" - }, - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", @@ -272,14 +228,19 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { + "done": true, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "delete" }, - "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" + "name": "operations/${operationID}", + "response": { + "@type": "type.googleapis.com/google.protobuf.Empty" + } } --- @@ -332,10 +293,12 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { + "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, @@ -367,6 +330,7 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, From f70eefdd15586f58c75eddce99fd286400db0bf1 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 27 Nov 2024 00:13:22 +0000 Subject: [PATCH 17/22] Try a more accurate mock --- mockgcp/mocksecuresourcemanager/repository.go | 54 ++++++++++++++----- ...uresourcemanagerrepositoryfull.golden.yaml | 6 --- .../_http.log | 19 +++---- 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/mockgcp/mocksecuresourcemanager/repository.go b/mockgcp/mocksecuresourcemanager/repository.go index ee982e9789..f3766ea76b 100644 --- a/mockgcp/mocksecuresourcemanager/repository.go +++ b/mockgcp/mocksecuresourcemanager/repository.go @@ -24,6 +24,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/types/known/anypb" "google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/timestamppb" @@ -65,14 +66,14 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p obj.Name = fqn obj.CreateTime = timestamppb.New(now) - obj.UpdateTime = timestamppb.New(now) instanceName, err := s.parseInstanceName(obj.GetInstance()) if err != nil { return nil, err } - obj.InitialConfig = req.GetRepository().GetInitialConfig() + // Real GCP doesn't include initial config field. + obj.InitialConfig = nil prefix := fmt.Sprintf("https://%s-%d", instanceName.InstanceID, name.Project.Number) domain := "." + name.Location + ".sourcemanager.dev" @@ -86,17 +87,29 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p return nil, err } - op := &pb.OperationMetadata{ + opMetadata := &pb.OperationMetadata{ CreateTime: timestamppb.New(now), Target: name.String(), Verb: "create", ApiVersion: "v1", } - opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location) - return s.operations.StartLRO(ctx, opPrefix, op, func() (proto.Message, error) { - op.EndTime = timestamppb.Now() - return obj, nil - }) + opMetadataAny, err := anypb.New(opMetadata) + if err != nil { + return nil, err + } + resp, err := anypb.New(obj) + if err != nil { + return nil, err + } + op := &longrunning.Operation{ + Done: true, + Metadata: opMetadataAny, + Name: `operations/${operation_id}`, + Result: &longrunning.Operation_Response{ + Response: resp, + }, + } + return op, nil } func (s *secureSourceManagerServer) DeleteRepository(ctx context.Context, req *pb.DeleteRepositoryRequest) (*longrunning.Operation, error) { @@ -113,17 +126,30 @@ func (s *secureSourceManagerServer) DeleteRepository(ctx context.Context, req *p return nil, err } - op := &pb.OperationMetadata{ + opMetadata := &pb.OperationMetadata{ CreateTime: timestamppb.New(now), + EndTime: timestamppb.New(now), Target: name.String(), Verb: "delete", ApiVersion: "v1", } - opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location) - return s.operations.StartLRO(ctx, opPrefix, op, func() (proto.Message, error) { - op.EndTime = timestamppb.Now() - return &emptypb.Empty{}, nil - }) + opMetadataAny, err := anypb.New(opMetadata) + if err != nil { + return nil, err + } + resp, err := anypb.New(&emptypb.Empty{}) + if err != nil { + return nil, err + } + op := &longrunning.Operation{ + Done: true, + Metadata: opMetadataAny, + Name: `operations/${operation_id}`, + Result: &longrunning.Operation_Response{ + Response: resp, + }, + } + return op, nil } type RepositoryName struct { diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml index efad373cfc..9932e612e6 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml @@ -28,10 +28,4 @@ status: reason: UpToDate status: "True" type: Ready - externalRef: projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} observedGeneration: 1 - observedState: - uris: - api: https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} - gitHTTPS: https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git - html: https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId} diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log index 082b960fc2..4491c6e9eb 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log @@ -43,12 +43,10 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -80,7 +78,6 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -121,7 +118,7 @@ X-Xss-Protection: 0 { "error": { "code": 404, - "message": "Resource \"projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}\" was not found", + "message": "Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found", "status": "NOT_FOUND" } } @@ -159,18 +156,17 @@ X-Xss-Protection: 0 { "done": true, "metadata": { - "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "@type": "type.googleapis.com/mockgcp.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "create" }, "name": "operations/${operationID}", "response": { - "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Repository", + "@type": "type.googleapis.com/mockgcp.cloud.securesourcemanager.v1.Repository", "createTime": "2024-04-01T12:34:56.123456Z", - "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", @@ -200,7 +196,7 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", - "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", @@ -230,7 +226,7 @@ X-Xss-Protection: 0 { "done": true, "metadata": { - "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "@type": "type.googleapis.com/mockgcp.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", @@ -293,12 +289,10 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, @@ -330,7 +324,6 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, From cdeffa0f1de0de6fdc5fe365ecca256cb34bd0f1 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 27 Nov 2024 22:05:02 +0000 Subject: [PATCH 18/22] Revert "Try a more accurate mock" This reverts commit 15a401bdf8295a7c97bf250fb8afa683d2df0957. --- mockgcp/mocksecuresourcemanager/repository.go | 54 +++++-------------- ...uresourcemanagerrepositoryfull.golden.yaml | 6 +++ .../_http.log | 19 ++++--- 3 files changed, 33 insertions(+), 46 deletions(-) diff --git a/mockgcp/mocksecuresourcemanager/repository.go b/mockgcp/mocksecuresourcemanager/repository.go index f3766ea76b..ee982e9789 100644 --- a/mockgcp/mocksecuresourcemanager/repository.go +++ b/mockgcp/mocksecuresourcemanager/repository.go @@ -24,7 +24,6 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" - "google.golang.org/protobuf/types/known/anypb" "google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/timestamppb" @@ -66,14 +65,14 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p obj.Name = fqn obj.CreateTime = timestamppb.New(now) + obj.UpdateTime = timestamppb.New(now) instanceName, err := s.parseInstanceName(obj.GetInstance()) if err != nil { return nil, err } - // Real GCP doesn't include initial config field. - obj.InitialConfig = nil + obj.InitialConfig = req.GetRepository().GetInitialConfig() prefix := fmt.Sprintf("https://%s-%d", instanceName.InstanceID, name.Project.Number) domain := "." + name.Location + ".sourcemanager.dev" @@ -87,29 +86,17 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p return nil, err } - opMetadata := &pb.OperationMetadata{ + op := &pb.OperationMetadata{ CreateTime: timestamppb.New(now), Target: name.String(), Verb: "create", ApiVersion: "v1", } - opMetadataAny, err := anypb.New(opMetadata) - if err != nil { - return nil, err - } - resp, err := anypb.New(obj) - if err != nil { - return nil, err - } - op := &longrunning.Operation{ - Done: true, - Metadata: opMetadataAny, - Name: `operations/${operation_id}`, - Result: &longrunning.Operation_Response{ - Response: resp, - }, - } - return op, nil + opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location) + return s.operations.StartLRO(ctx, opPrefix, op, func() (proto.Message, error) { + op.EndTime = timestamppb.Now() + return obj, nil + }) } func (s *secureSourceManagerServer) DeleteRepository(ctx context.Context, req *pb.DeleteRepositoryRequest) (*longrunning.Operation, error) { @@ -126,30 +113,17 @@ func (s *secureSourceManagerServer) DeleteRepository(ctx context.Context, req *p return nil, err } - opMetadata := &pb.OperationMetadata{ + op := &pb.OperationMetadata{ CreateTime: timestamppb.New(now), - EndTime: timestamppb.New(now), Target: name.String(), Verb: "delete", ApiVersion: "v1", } - opMetadataAny, err := anypb.New(opMetadata) - if err != nil { - return nil, err - } - resp, err := anypb.New(&emptypb.Empty{}) - if err != nil { - return nil, err - } - op := &longrunning.Operation{ - Done: true, - Metadata: opMetadataAny, - Name: `operations/${operation_id}`, - Result: &longrunning.Operation_Response{ - Response: resp, - }, - } - return op, nil + opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location) + return s.operations.StartLRO(ctx, opPrefix, op, func() (proto.Message, error) { + op.EndTime = timestamppb.Now() + return &emptypb.Empty{}, nil + }) } type RepositoryName struct { diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml index 9932e612e6..efad373cfc 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_generated_object_securesourcemanagerrepositoryfull.golden.yaml @@ -28,4 +28,10 @@ status: reason: UpToDate status: "True" type: Ready + externalRef: projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} observedGeneration: 1 + observedState: + uris: + api: https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId} + gitHTTPS: https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git + html: https://ssminstance-dep-${uniqueId}-${projectNumber}.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId} diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log index 4491c6e9eb..082b960fc2 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log @@ -43,10 +43,12 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { + "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -78,6 +80,7 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -118,7 +121,7 @@ X-Xss-Protection: 0 { "error": { "code": 404, - "message": "Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found", + "message": "Resource \"projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}\" was not found", "status": "NOT_FOUND" } } @@ -156,17 +159,18 @@ X-Xss-Protection: 0 { "done": true, "metadata": { - "@type": "type.googleapis.com/mockgcp.cloud.securesourcemanager.v1.OperationMetadata", + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "create" }, "name": "operations/${operationID}", "response": { - "@type": "type.googleapis.com/mockgcp.cloud.securesourcemanager.v1.Repository", + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Repository", "createTime": "2024-04-01T12:34:56.123456Z", - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", @@ -196,7 +200,7 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", @@ -226,7 +230,7 @@ X-Xss-Protection: 0 { "done": true, "metadata": { - "@type": "type.googleapis.com/mockgcp.cloud.securesourcemanager.v1.OperationMetadata", + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", @@ -289,10 +293,12 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { + "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, @@ -324,6 +330,7 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, From 813202ce2a6ffa976b87210d48deb62ac0848777 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Fri, 29 Nov 2024 05:31:51 +0000 Subject: [PATCH 19/22] Revert "log realgcp" This reverts commit c8028a196df0cbd8ac69874faf7a46ebc93c0465. --- .../_http.log | 68 ++++++++++++++----- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log index 082b960fc2..3955a201fd 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log @@ -43,12 +43,10 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -80,7 +78,6 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -121,7 +118,7 @@ X-Xss-Protection: 0 { "error": { "code": 404, - "message": "Resource \"projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}\" was not found", + "message": "Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found", "status": "NOT_FOUND" } } @@ -156,6 +153,35 @@ X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 +{ + "metadata": { + "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", + "apiVersion": "v1", + "createTime": "2024-04-01T12:34:56.123456Z", + "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "verb": "create" + }, + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" +} + +--- + +GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} +Content-Type: application/json +User-Agent: kcc/controller-manager +x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} + +200 OK +Cache-Control: private +Content-Type: application/json; charset=UTF-8 +Server: ESF +Vary: Origin +Vary: X-Origin +Vary: Referer +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN +X-Xss-Protection: 0 + { "done": true, "metadata": { @@ -166,12 +192,21 @@ X-Xss-Protection: 0 "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "create" }, - "name": "operations/${operationID}", + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", "response": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Repository", "createTime": "2024-04-01T12:34:56.123456Z", - "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "initialConfig": { + "defaultBranch": "main", + "gitignores": [ + "python" + ], + "license": "mit", + "readme": "default" + }, + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", @@ -200,8 +235,17 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", - "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "initialConfig": { + "defaultBranch": "main", + "gitignores": [ + "python" + ], + "license": "mit", + "readme": "default" + }, + "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", + "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", @@ -228,19 +272,14 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "done": true, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "delete" }, - "name": "operations/${operationID}", - "response": { - "@type": "type.googleapis.com/google.protobuf.Empty" - } + "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" } --- @@ -293,12 +332,10 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, @@ -330,7 +367,6 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, From 35b6755420cc0ea273b66ca371234f40ffff7925 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 18 Dec 2024 04:21:42 +0000 Subject: [PATCH 20/22] fix mock and use project number for instance --- .../v1alpha1/instance_reference.go | 34 ++++++++ mockgcp/mocksecuresourcemanager/repository.go | 27 +++---- .../repository_controller.go | 58 ++++++++++---- .../_http.log | 46 ++++++----- .../_http.log | 78 +++++++------------ 5 files changed, 138 insertions(+), 105 deletions(-) diff --git a/apis/securesourcemanager/v1alpha1/instance_reference.go b/apis/securesourcemanager/v1alpha1/instance_reference.go index 3ea040e01c..62e50515d9 100644 --- a/apis/securesourcemanager/v1alpha1/instance_reference.go +++ b/apis/securesourcemanager/v1alpha1/instance_reference.go @@ -17,8 +17,12 @@ package v1alpha1 import ( "context" "fmt" + "strconv" "strings" + resourcemanager "cloud.google.com/go/resourcemanager/apiv3" + resourcemanagerpb "cloud.google.com/go/resourcemanager/apiv3/resourcemanagerpb" + refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -70,6 +74,36 @@ func parseSecureSourceManagerExternal(external string) (*ProjectIDAndLocation, s return nil, "", fmt.Errorf("format of SecureSourceManagerInstance external=%q was not known (use projects/{{projectId}}/locations/{{location}}/instances/{{instanceID}})", external) } +// ConvertToProjectNumber converts the external reference to use a project number. +func (r *SecureSourceManagerInstanceRef) ConvertToProjectNumber(ctx context.Context, projectsClient *resourcemanager.ProjectsClient) error { + if r == nil { + return nil + } + + parent, id, err := parseSecureSourceManagerExternal(r.External) + + // Check if the project number is already a valid integer + // If not, we need to look it up + projectNumber, err := strconv.ParseInt(id, 10, 64) + if err != nil { + req := &resourcemanagerpb.GetProjectRequest{ + Name: "projects/" + parent.ProjectID, + } + project, err := projectsClient.GetProject(ctx, req) + if err != nil { + return fmt.Errorf("error getting project %q: %w", req.Name, err) + } + n, err := strconv.ParseInt(strings.TrimPrefix(project.Name, "projects/"), 10, 64) + if err != nil { + return fmt.Errorf("error parsing project number for %q: %w", project.Name, err) + } + projectNumber = n + } + parent.ProjectID = strconv.FormatInt(projectNumber, 10) + r.External = fmt.Sprintf("%s/instances/%s", parent.String(), id) + return nil +} + // NormalizedExternal provision the "External" value for other resource that depends on SecureSourceManagerInstance. // If the "External" is given in the other resource's spec.SecureSourceManagerInstanceRef, the given value will be used. // Otherwise, the "Name" and "Namespace" will be used to query the actual SecureSourceManagerInstance object from the cluster. diff --git a/mockgcp/mocksecuresourcemanager/repository.go b/mockgcp/mocksecuresourcemanager/repository.go index ee982e9789..d45d40edff 100644 --- a/mockgcp/mocksecuresourcemanager/repository.go +++ b/mockgcp/mocksecuresourcemanager/repository.go @@ -42,7 +42,7 @@ func (s *secureSourceManagerServer) GetRepository(ctx context.Context, req *pb.G obj := &pb.Repository{} 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, status.Errorf(codes.NotFound, "\"Resource '%s' was not found\"", fqn) } return nil, err } @@ -65,14 +65,13 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p obj.Name = fqn obj.CreateTime = timestamppb.New(now) - obj.UpdateTime = timestamppb.New(now) instanceName, err := s.parseInstanceName(obj.GetInstance()) if err != nil { return nil, err } - - obj.InitialConfig = req.GetRepository().GetInitialConfig() + // Real GCP doesn't include initial config field. + obj.InitialConfig = nil prefix := fmt.Sprintf("https://%s-%d", instanceName.InstanceID, name.Project.Number) domain := "." + name.Location + ".sourcemanager.dev" @@ -82,21 +81,16 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p GitHttps: prefix + "-git" + domain + fmt.Sprintf("/%s/%s.git", name.Project.ID, req.GetRepositoryId()), } - if err := s.storage.Create(ctx, fqn, obj); err != nil { - return nil, err - } - op := &pb.OperationMetadata{ CreateTime: timestamppb.New(now), Target: name.String(), Verb: "create", ApiVersion: "v1", } - opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location) - return s.operations.StartLRO(ctx, opPrefix, op, func() (proto.Message, error) { - op.EndTime = timestamppb.Now() - return obj, nil - }) + if err := s.storage.Create(ctx, fqn, obj); err != nil { + return nil, err + } + return s.operations.DoneLRO(ctx, "", op, obj) } func (s *secureSourceManagerServer) DeleteRepository(ctx context.Context, req *pb.DeleteRepositoryRequest) (*longrunning.Operation, error) { @@ -119,11 +113,8 @@ func (s *secureSourceManagerServer) DeleteRepository(ctx context.Context, req *p Verb: "delete", ApiVersion: "v1", } - opPrefix := fmt.Sprintf("projects/%s/locations/%s", name.Project.ID, name.Location) - return s.operations.StartLRO(ctx, opPrefix, op, func() (proto.Message, error) { - op.EndTime = timestamppb.Now() - return &emptypb.Empty{}, nil - }) + + return s.operations.DoneLRO(ctx, "", op, &emptypb.Empty{}) } type RepositoryName struct { diff --git a/pkg/controller/direct/securesourcemanager/repository_controller.go b/pkg/controller/direct/securesourcemanager/repository_controller.go index a74b7bc177..2474b0755a 100644 --- a/pkg/controller/direct/securesourcemanager/repository_controller.go +++ b/pkg/controller/direct/securesourcemanager/repository_controller.go @@ -19,6 +19,10 @@ import ( "fmt" "strings" + cloudresourcemanager "cloud.google.com/go/resourcemanager/apiv3" + gcp "cloud.google.com/go/securesourcemanager/apiv1" + securesourcemanagerpb "cloud.google.com/go/securesourcemanager/apiv1/securesourcemanagerpb" + refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/securesourcemanager/v1alpha1" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config" @@ -26,9 +30,6 @@ import ( "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase" "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry" - gcp "cloud.google.com/go/securesourcemanager/apiv1" - - securesourcemanagerpb "cloud.google.com/go/securesourcemanager/apiv1/securesourcemanagerpb" "google.golang.org/api/option" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -70,15 +71,13 @@ func (m *modelSecureSourceManagerRepository) AdapterForObject(ctx context.Contex return nil, fmt.Errorf("error converting to %T: %w", obj, err) } - // Resolve SecureSourceManagerInstanceRef - instanceRef := obj.Spec.InstanceRef - normalizedExternal, err := instanceRef.NormalizedExternal(ctx, reader, obj.Namespace) + id, err := krm.NewSecureSourceManagerRepositoryRef(ctx, reader, obj) if err != nil { return nil, err } - obj.Spec.InstanceRef.External = normalizedExternal - id, err := krm.NewSecureSourceManagerRepositoryRef(ctx, reader, obj) + // Get Project GCP client + projectClient, err := m.projectsClient(ctx) if err != nil { return nil, err } @@ -89,22 +88,39 @@ func (m *modelSecureSourceManagerRepository) AdapterForObject(ctx context.Contex return nil, err } return &SecureSourceManagerRepositoryAdapter{ - id: id, - gcpClient: gcpClient, - desired: obj, + id: id, + projectClient: projectClient, + gcpClient: gcpClient, + reader: reader, + desired: obj, }, nil } +func (m *modelSecureSourceManagerRepository) projectsClient(ctx context.Context) (*cloudresourcemanager.ProjectsClient, error) { + opts, err := m.config.RESTClientOptions() + if err != nil { + return nil, err + } + + crmClient, err := cloudresourcemanager.NewProjectsRESTClient(ctx, opts...) + if err != nil { + return nil, fmt.Errorf("building cloudresourcemanager client: %w", err) + } + return crmClient, err +} + func (m *modelSecureSourceManagerRepository) AdapterForURL(ctx context.Context, url string) (directbase.Adapter, error) { // TODO: Support URLs return nil, nil } type SecureSourceManagerRepositoryAdapter struct { - id *krm.SecureSourceManagerRepositoryRef - gcpClient *gcp.Client - desired *krm.SecureSourceManagerRepository - actual *securesourcemanagerpb.Repository + id *krm.SecureSourceManagerRepositoryRef + projectClient *cloudresourcemanager.ProjectsClient + gcpClient *gcp.Client + reader client.Reader + desired *krm.SecureSourceManagerRepository + actual *securesourcemanagerpb.Repository } var _ directbase.Adapter = &SecureSourceManagerRepositoryAdapter{} @@ -132,6 +148,18 @@ func (a *SecureSourceManagerRepositoryAdapter) Create(ctx context.Context, creat mapCtx := &direct.MapContext{} desired := a.desired.DeepCopy() + + // Resolve SecureSourceManagerInstanceRef + instanceRef := a.desired.Spec.InstanceRef + normalizedExternal, err := instanceRef.NormalizedExternal(ctx, a.reader, desired.Namespace) + if err != nil { + return err + } + desired.Spec.InstanceRef.External = normalizedExternal + if err := desired.Spec.InstanceRef.ConvertToProjectNumber(ctx, a.projectClient); err != nil { + return err + } + resource := SecureSourceManagerRepositorySpec_ToProto(mapCtx, &desired.Spec) if mapCtx.Err() != nil { return mapCtx.Err() diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log index ab9f31955d..86e41bf0ca 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log @@ -118,21 +118,17 @@ X-Xss-Protection: 0 { "error": { "code": 404, - "message": "Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found", + "message": "\"Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found\"", "status": "NOT_FOUND" } } --- -POST https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories?%24alt=json%3Benum-encoding%3Dint&repositoryId=ssmrepository-${uniqueId} +GET https://cloudresourcemanager.googleapis.com/v3/projects/${projectId}?%24alt=json%3Benum-encoding%3Dint Content-Type: application/json User-Agent: kcc/controller-manager -x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 - -{ - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}" -} +x-goog-request-params: name=projects%2F${projectId} 200 OK Cache-Control: private @@ -146,22 +142,23 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "metadata": { - "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", - "apiVersion": "v1", - "createTime": "2024-04-01T12:34:56.123456Z", - "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "verb": "create" - }, - "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" + "createTime": "2024-04-01T12:34:56.123456Z", + "etag": "abcdef0123A=", + "name": "projects/${projectNumber}", + "projectId": "${projectId}", + "state": 1 } --- -GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} +POST https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories?%24alt=json%3Benum-encoding%3Dint&repositoryId=ssmrepository-${uniqueId} Content-Type: application/json User-Agent: kcc/controller-manager -x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} +x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 + +{ + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}" +} 200 OK Cache-Control: private @@ -180,17 +177,15 @@ X-Xss-Protection: 0 "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "create" }, - "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", + "name": "operations/${operationID}", "response": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Repository", "createTime": "2024-04-01T12:34:56.123456Z", - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", @@ -219,9 +214,8 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", @@ -248,6 +242,7 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { + "done": true, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", @@ -255,7 +250,10 @@ X-Xss-Protection: 0 "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "delete" }, - "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" + "name": "operations/${operationID}", + "response": { + "@type": "type.googleapis.com/google.protobuf.Empty" + } } --- diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log index 3955a201fd..049dc58ce6 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log @@ -118,29 +118,17 @@ X-Xss-Protection: 0 { "error": { "code": 404, - "message": "Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found", + "message": "\"Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found\"", "status": "NOT_FOUND" } } --- -POST https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories?%24alt=json%3Benum-encoding%3Dint&repositoryId=ssmrepository-${uniqueId} +GET https://cloudresourcemanager.googleapis.com/v3/projects/${projectId}?%24alt=json%3Benum-encoding%3Dint Content-Type: application/json User-Agent: kcc/controller-manager -x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 - -{ - "initialConfig": { - "defaultBranch": "main", - "gitignores": [ - "python" - ], - "license": "mit", - "readme": "default" - }, - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}" -} +x-goog-request-params: name=projects%2F${projectId} 200 OK Cache-Control: private @@ -154,22 +142,31 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "metadata": { - "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", - "apiVersion": "v1", - "createTime": "2024-04-01T12:34:56.123456Z", - "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "verb": "create" - }, - "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" + "createTime": "2024-04-01T12:34:56.123456Z", + "etag": "abcdef0123A=", + "name": "projects/${projectNumber}", + "projectId": "${projectId}", + "state": 1 } --- -GET https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/operations/${operationID} +POST https://securesourcemanager.googleapis.com/v1/projects/${projectId}/locations/us-central1/repositories?%24alt=json%3Benum-encoding%3Dint&repositoryId=ssmrepository-${uniqueId} Content-Type: application/json User-Agent: kcc/controller-manager -x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} +x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 + +{ + "initialConfig": { + "defaultBranch": "main", + "gitignores": [ + "python" + ], + "license": "mit", + "readme": "default" + }, + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}" +} 200 OK Cache-Control: private @@ -188,25 +185,15 @@ X-Xss-Protection: 0 "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "create" }, - "name": "projects/${projectId}/locations/us-central1/operations/${operationID}", + "name": "operations/${operationID}", "response": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Repository", "createTime": "2024-04-01T12:34:56.123456Z", - "initialConfig": { - "defaultBranch": "main", - "gitignores": [ - "python" - ], - "license": "mit", - "readme": "default" - }, - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", @@ -235,17 +222,8 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", - "initialConfig": { - "defaultBranch": "main", - "gitignores": [ - "python" - ], - "license": "mit", - "readme": "default" - }, - "instance": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", + "instance": "projects/${projectNumber}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "name": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", - "updateTime": "2024-04-01T12:34:56.123456Z", "uris": { "api": "https://ssminstance-dep-${uniqueId}-${projectNumber}-api.us-central1.sourcemanager.dev/v1/projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "gitHttps": "https://ssminstance-dep-${uniqueId}-${projectNumber}-git.us-central1.sourcemanager.dev/${projectId}/ssmrepository-${uniqueId}.git", @@ -272,6 +250,7 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { + "done": true, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", @@ -279,7 +258,10 @@ X-Xss-Protection: 0 "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "delete" }, - "name": "projects/${projectId}/locations/us-central1/operations/${operationID}" + "name": "operations/${operationID}", + "response": { + "@type": "type.googleapis.com/google.protobuf.Empty" + } } --- From 18e044a647a9cdc27d6edb4fcc1d93ba8c6e41f1 Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 18 Dec 2024 17:31:50 +0000 Subject: [PATCH 21/22] Add realgcp log --- .../_http.log | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log index 86e41bf0ca..d3ca06f629 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log @@ -4,7 +4,6 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} 404 Not Found -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -32,7 +31,6 @@ x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 {} 200 OK -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -43,10 +41,12 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { + "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -61,7 +61,6 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} 200 OK -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -78,6 +77,7 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -105,7 +105,6 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} 404 Not Found -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -118,7 +117,7 @@ X-Xss-Protection: 0 { "error": { "code": 404, - "message": "\"Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found\"", + "message": "Resource \"projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}\" was not found", "status": "NOT_FOUND" } } @@ -131,7 +130,6 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId} 200 OK -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -143,10 +141,13 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", + "displayName": "${projectId}", "etag": "abcdef0123A=", "name": "projects/${projectNumber}", + "parent": "folders/${folderID}", "projectId": "${projectId}", - "state": 1 + "state": 1, + "updateTime": "2024-04-01T12:34:56.123456Z" } --- @@ -161,7 +162,6 @@ x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 } 200 OK -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -177,6 +177,7 @@ X-Xss-Protection: 0 "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "create" }, @@ -202,7 +203,6 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} 200 OK -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -231,7 +231,6 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} 200 OK -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -247,6 +246,7 @@ X-Xss-Protection: 0 "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "delete" }, @@ -264,7 +264,6 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} 200 OK -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -295,7 +294,6 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} 200 OK -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -306,10 +304,12 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { + "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, @@ -324,7 +324,6 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} 200 OK -Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -341,6 +340,7 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", + "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, From 518ee2b75c32e9f1d056402c2ad1c13dc61c30bf Mon Sep 17 00:00:00 2001 From: Eric Pang Date: Wed, 18 Dec 2024 17:47:29 +0000 Subject: [PATCH 22/22] add mock log --- mockgcp/mocksecuresourcemanager/repository.go | 12 +++++++--- .../_http.log | 22 ++++++++++--------- .../_http.log | 4 +++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/mockgcp/mocksecuresourcemanager/repository.go b/mockgcp/mocksecuresourcemanager/repository.go index d45d40edff..5974fd4887 100644 --- a/mockgcp/mocksecuresourcemanager/repository.go +++ b/mockgcp/mocksecuresourcemanager/repository.go @@ -42,7 +42,7 @@ func (s *secureSourceManagerServer) GetRepository(ctx context.Context, req *pb.G obj := &pb.Repository{} 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, status.Errorf(codes.NotFound, "Resource \"%s\" was not found", fqn) } return nil, err } @@ -90,7 +90,10 @@ func (s *secureSourceManagerServer) CreateRepository(ctx context.Context, req *p if err := s.storage.Create(ctx, fqn, obj); err != nil { return nil, err } - return s.operations.DoneLRO(ctx, "", op, obj) + return s.operations.DoneLRO(ctx, "", op, func() *pb.Repository { + op.EndTime = timestamppb.New(now) + return obj + }()) } func (s *secureSourceManagerServer) DeleteRepository(ctx context.Context, req *pb.DeleteRepositoryRequest) (*longrunning.Operation, error) { @@ -114,7 +117,10 @@ func (s *secureSourceManagerServer) DeleteRepository(ctx context.Context, req *p ApiVersion: "v1", } - return s.operations.DoneLRO(ctx, "", op, &emptypb.Empty{}) + return s.operations.DoneLRO(ctx, "", op, func() *emptypb.Empty { + op.EndTime = timestamppb.New(now) + return &emptypb.Empty{} + }()) } type RepositoryName struct { diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log index d3ca06f629..3facd2f626 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositorybasic/_http.log @@ -4,6 +4,7 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} 404 Not Found +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -31,6 +32,7 @@ x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 {} 200 OK +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -41,12 +43,10 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -61,6 +61,7 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} 200 OK +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -77,7 +78,6 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "create" }, @@ -105,6 +105,7 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} 404 Not Found +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -130,6 +131,7 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId} 200 OK +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -141,13 +143,10 @@ X-Xss-Protection: 0 { "createTime": "2024-04-01T12:34:56.123456Z", - "displayName": "${projectId}", "etag": "abcdef0123A=", "name": "projects/${projectNumber}", - "parent": "folders/${folderID}", "projectId": "${projectId}", - "state": 1, - "updateTime": "2024-04-01T12:34:56.123456Z" + "state": 1 } --- @@ -162,6 +161,7 @@ x-goog-request-params: parent=projects%2F${projectId}%2Flocations%2Fus-central1 } 200 OK +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -203,6 +203,7 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} 200 OK +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -231,6 +232,7 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Frepositories%2Fssmrepository-${uniqueId} 200 OK +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -264,6 +266,7 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} 200 OK +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -294,6 +297,7 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Finstances%2Fssminstance-dep-${uniqueId} 200 OK +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -304,12 +308,10 @@ X-Frame-Options: SAMEORIGIN X-Xss-Protection: 0 { - "done": false, "metadata": { "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, @@ -324,6 +326,7 @@ User-Agent: kcc/controller-manager x-goog-request-params: name=projects%2F${projectId}%2Flocations%2Fus-central1%2Foperations%2F${operationID} 200 OK +Cache-Control: private Content-Type: application/json; charset=UTF-8 Server: ESF Vary: Origin @@ -340,7 +343,6 @@ X-Xss-Protection: 0 "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", "endTime": "2024-04-01T12:34:56.123456Z", - "requestedCancellation": false, "target": "projects/${projectId}/locations/us-central1/instances/ssminstance-dep-${uniqueId}", "verb": "delete" }, diff --git a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log index 049dc58ce6..2671cf8bdf 100644 --- a/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log +++ b/pkg/test/resourcefixture/testdata/basic/securesourcemanager/securesourcemanagerrepository/securesourcemanagerrepositoryfull/_http.log @@ -118,7 +118,7 @@ X-Xss-Protection: 0 { "error": { "code": 404, - "message": "\"Resource 'projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}' was not found\"", + "message": "Resource \"projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}\" was not found", "status": "NOT_FOUND" } } @@ -185,6 +185,7 @@ X-Xss-Protection: 0 "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "create" }, @@ -255,6 +256,7 @@ X-Xss-Protection: 0 "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata", "apiVersion": "v1", "createTime": "2024-04-01T12:34:56.123456Z", + "endTime": "2024-04-01T12:34:56.123456Z", "target": "projects/${projectId}/locations/us-central1/repositories/ssmrepository-${uniqueId}", "verb": "delete" },