Skip to content

Commit

Permalink
Update attached clusters mockGCP service to more closely match GCP.
Browse files Browse the repository at this point in the history
  • Loading branch information
hankfreund committed Sep 16, 2024
1 parent 0ab6bd2 commit f1e01f7
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 11 deletions.
39 changes: 38 additions & 1 deletion mockgcp/mockgkemulticloud/attachedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package mockgkemulticloud
import (
"context"
"fmt"
"strings"
"time"

"google.golang.org/genproto/googleapis/longrunning"
Expand All @@ -26,6 +27,7 @@ import (
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"

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

Expand All @@ -45,7 +47,7 @@ func (s *GKEMulticloudV1) GetAttachedCluster(ctx context.Context, req *pb.GetAtt
obj := &pb.AttachedCluster{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
if status.Code(err) == codes.NotFound {
return nil, status.Errorf(codes.NotFound, "Resource '%s' was not found", fqn)
return nil, status.Errorf(codes.NotFound, "cluster not found")
}
return nil, err
}
Expand All @@ -66,6 +68,28 @@ func (s *GKEMulticloudV1) CreateAttachedCluster(ctx context.Context, req *pb.Cre
obj := proto.Clone(req.AttachedCluster).(*pb.AttachedCluster)
obj.Name = fqn

if obj.GetBinaryAuthorization() == nil {
obj.BinaryAuthorization = &pb.BinaryAuthorization{
EvaluationMode: pb.BinaryAuthorization_DISABLED,
}
}
if obj.GetMonitoringConfig() == nil {
obj.MonitoringConfig = &pb.MonitoringConfig{
ManagedPrometheusConfig: &pb.ManagedPrometheusConfig{},
}
}
obj.Fleet.Membership = obj.Fleet.Project + "/locations/global/memberships/" + req.AttachedClusterId
obj.CreateTime = timestamppb.New(now)
obj.UpdateTime = timestamppb.New(now)
obj.Etag = fields.ComputeWeakEtag(obj)
ver, err := trimPlatformVersion(req.GetAttachedCluster().GetPlatformVersion())
if err != nil {
return nil, err
}
obj.KubernetesVersion = ver
obj.State = pb.AttachedCluster_RUNNING
obj.Uid = "111111111111111111111"

if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,6 +154,11 @@ func (s *GKEMulticloudV1) UpdateAttachedCluster(ctx context.Context, req *pb.Upd
case "monitoring_config.managed_prometheus_config.enabled":
obj.MonitoringConfig = req.GetAttachedCluster().GetMonitoringConfig()
case "platformVersion":
ver, err := trimPlatformVersion(req.GetAttachedCluster().GetPlatformVersion())
if err != nil {
return nil, err
}
obj.KubernetesVersion = ver
obj.PlatformVersion = req.GetAttachedCluster().GetPlatformVersion()
default:
return nil, status.Errorf(codes.InvalidArgument, "update_mask path %q not valid", path)
Expand Down Expand Up @@ -178,3 +207,11 @@ func (s *GKEMulticloudV1) DeleteAttachedCluster(ctx context.Context, req *pb.Del
return &emptypb.Empty{}, nil
})
}

func trimPlatformVersion(platformVersion string) (string, error) {
tokens := strings.Split(platformVersion, ".")
if len(tokens) < 2 {
return "", status.Errorf(codes.InvalidArgument, "platform_version %q is not valid", platformVersion)
}
return tokens[0] + "." + tokens[1], nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ status:
reason: UpToDate
status: "True"
type: Ready
createTime: "1970-01-01T00:00:00Z"
kubernetesVersion: "1.28"
observedGeneration: 2
state: RUNNING
uid: "12345678"
updateTime: "1970-01-01T00:00:00Z"
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ X-Xss-Protection: 0
{
"error": {
"code": 404,
"message": "Resource 'projects/${projectId}/locations/us-west1/attachedClusters/kcc-attached-cluster-127' was not found",
"message": "cluster not found",
"status": "NOT_FOUND"
}
}
Expand Down Expand Up @@ -322,18 +322,30 @@ X-Xss-Protection: 0
"name": "projects/${projectId}/locations/us-west1/operations/${operationID}",
"response": {
"@type": "type.googleapis.com/google.cloud.gkemulticloud.v1.AttachedCluster",
"binaryAuthorization": {
"evaluationMode": "DISABLED"
},
"createTime": "2024-04-01T12:34:56.123456Z",
"description": "Test attached cluster",
"distribution": "eks",
"etag": "abcdef0123A=",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"kubernetesVersion": "1.27",
"loggingConfig": {},
"monitoringConfig": {
"managedPrometheusConfig": {}
},
"name": "projects/${projectId}/locations/us-west1/attachedClusters/kcc-attached-cluster-127",
"oidcConfig": {
"issuerUrl": "https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F"
},
"platformVersion": "1.27.0-gke.2",
"state": "RUNNING"
"state": "RUNNING",
"uid": "111111111111111111111",
"updateTime": "2024-04-01T12:34:56.123456Z"
}
}

Expand All @@ -355,17 +367,30 @@ X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"binaryAuthorization": {
"evaluationMode": "DISABLED"
},
"createTime": "2024-04-01T12:34:56.123456Z",
"description": "Test attached cluster",
"distribution": "eks",
"etag": "abcdef0123A=",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"kubernetesVersion": "1.27",
"loggingConfig": {},
"monitoringConfig": {
"managedPrometheusConfig": {}
},
"name": "projects/${projectId}/locations/us-west1/attachedClusters/kcc-attached-cluster-127",
"oidcConfig": {
"issuerUrl": "https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F"
},
"platformVersion": "1.27.0-gke.2"
"platformVersion": "1.27.0-gke.2",
"state": "RUNNING",
"uid": "111111111111111111111",
"updateTime": "2024-04-01T12:34:56.123456Z"
}

---
Expand All @@ -375,13 +400,18 @@ Content-Type: application/json
User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 terraform-provider-google-beta/kcc/controller-manager

{
"binaryAuthorization": {
"evaluationMode": "DISABLED"
},
"description": "Test attached cluster update",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"loggingConfig": {
"componentConfig": null
},
"monitoringConfig": {},
"oidcConfig": {
"issuerUrl": "https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F"
},
Expand Down Expand Up @@ -438,17 +468,30 @@ X-Xss-Protection: 0
"name": "projects/${projectId}/locations/us-west1/operations/${operationID}",
"response": {
"@type": "type.googleapis.com/google.cloud.gkemulticloud.v1.AttachedCluster",
"binaryAuthorization": {
"evaluationMode": "DISABLED"
},
"createTime": "2024-04-01T12:34:56.123456Z",
"description": "Test attached cluster update",
"distribution": "eks",
"etag": "abcdef0123A=",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"kubernetesVersion": "1.28",
"loggingConfig": {},
"monitoringConfig": {
"managedPrometheusConfig": {}
},
"name": "projects/${projectId}/locations/us-west1/attachedClusters/kcc-attached-cluster-127",
"oidcConfig": {
"issuerUrl": "https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F"
},
"platformVersion": "1.28.0-gke.2"
"platformVersion": "1.28.0-gke.2",
"state": "RUNNING",
"uid": "111111111111111111111",
"updateTime": "2024-04-01T12:34:56.123456Z"
}
}

Expand All @@ -470,17 +513,30 @@ X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"binaryAuthorization": {
"evaluationMode": "DISABLED"
},
"createTime": "2024-04-01T12:34:56.123456Z",
"description": "Test attached cluster update",
"distribution": "eks",
"etag": "abcdef0123A=",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"kubernetesVersion": "1.28",
"loggingConfig": {},
"monitoringConfig": {
"managedPrometheusConfig": {}
},
"name": "projects/${projectId}/locations/us-west1/attachedClusters/kcc-attached-cluster-127",
"oidcConfig": {
"issuerUrl": "https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F"
},
"platformVersion": "1.28.0-gke.2"
"platformVersion": "1.28.0-gke.2",
"state": "RUNNING",
"uid": "111111111111111111111",
"updateTime": "2024-04-01T12:34:56.123456Z"
}

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ status:
reason: UpToDate
status: "True"
type: Ready
createTime: "1970-01-01T00:00:00Z"
kubernetesVersion: "1.28"
observedGeneration: 2
state: RUNNING
uid: "12345678"
updateTime: "1970-01-01T00:00:00Z"
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ X-Xss-Protection: 0
{
"error": {
"code": 404,
"message": "Resource 'projects/${projectId}/locations/us-west1/attachedClusters/kcc-attached-cluster-127' was not found",
"message": "cluster not found",
"status": "NOT_FOUND"
}
}
Expand Down Expand Up @@ -356,24 +356,33 @@ X-Xss-Protection: 0
"binaryAuthorization": {
"evaluationMode": "PROJECT_SINGLETON_POLICY_ENFORCE"
},
"createTime": "2024-04-01T12:34:56.123456Z",
"description": "Test attached cluster",
"distribution": "eks",
"etag": "abcdef0123A=",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"kubernetesVersion": "1.27",
"loggingConfig": {
"componentConfig": {
"enableComponents": [
"SYSTEM_COMPONENTS"
]
}
},
"monitoringConfig": {
"managedPrometheusConfig": {}
},
"name": "projects/${projectId}/locations/us-west1/attachedClusters/kcc-attached-cluster-127",
"oidcConfig": {
"issuerUrl": "https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F"
},
"platformVersion": "1.27.0-gke.2",
"state": "RUNNING"
"state": "RUNNING",
"uid": "111111111111111111111",
"updateTime": "2024-04-01T12:34:56.123456Z"
}
}

Expand Down Expand Up @@ -409,23 +418,33 @@ X-Xss-Protection: 0
"binaryAuthorization": {
"evaluationMode": "PROJECT_SINGLETON_POLICY_ENFORCE"
},
"createTime": "2024-04-01T12:34:56.123456Z",
"description": "Test attached cluster",
"distribution": "eks",
"etag": "abcdef0123A=",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"kubernetesVersion": "1.27",
"loggingConfig": {
"componentConfig": {
"enableComponents": [
"SYSTEM_COMPONENTS"
]
}
},
"monitoringConfig": {
"managedPrometheusConfig": {}
},
"name": "projects/${projectId}/locations/us-west1/attachedClusters/kcc-attached-cluster-127",
"oidcConfig": {
"issuerUrl": "https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F"
},
"platformVersion": "1.27.0-gke.2"
"platformVersion": "1.27.0-gke.2",
"state": "RUNNING",
"uid": "111111111111111111111",
"updateTime": "2024-04-01T12:34:56.123456Z"
}

---
Expand Down Expand Up @@ -455,6 +474,7 @@ User-Agent: Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.10.1 t
},
"description": "Test attached cluster update",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"loggingConfig": {
Expand Down Expand Up @@ -544,11 +564,15 @@ X-Xss-Protection: 0
"binaryAuthorization": {
"evaluationMode": "DISABLED"
},
"createTime": "2024-04-01T12:34:56.123456Z",
"description": "Test attached cluster update",
"distribution": "eks",
"etag": "abcdef0123A=",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"kubernetesVersion": "1.28",
"loggingConfig": {
"componentConfig": {
"enableComponents": [
Expand All @@ -566,7 +590,10 @@ X-Xss-Protection: 0
"oidcConfig": {
"issuerUrl": "https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F"
},
"platformVersion": "1.28.0-gke.2"
"platformVersion": "1.28.0-gke.2",
"state": "RUNNING",
"uid": "111111111111111111111",
"updateTime": "2024-04-01T12:34:56.123456Z"
}
}

Expand Down Expand Up @@ -606,11 +633,15 @@ X-Xss-Protection: 0
"binaryAuthorization": {
"evaluationMode": "DISABLED"
},
"createTime": "2024-04-01T12:34:56.123456Z",
"description": "Test attached cluster update",
"distribution": "eks",
"etag": "abcdef0123A=",
"fleet": {
"membership": "projects/${projectNumber}/locations/global/memberships/kcc-attached-cluster-127",
"project": "projects/${projectNumber}"
},
"kubernetesVersion": "1.28",
"loggingConfig": {
"componentConfig": {
"enableComponents": [
Expand All @@ -628,7 +659,10 @@ X-Xss-Protection: 0
"oidcConfig": {
"issuerUrl": "https://oidc.eks.us-west-2.amazonaws.com/id/A115FE1C770C2452C75219524036FC0F"
},
"platformVersion": "1.28.0-gke.2"
"platformVersion": "1.28.0-gke.2",
"state": "RUNNING",
"uid": "111111111111111111111",
"updateTime": "2024-04-01T12:34:56.123456Z"
}

---
Expand Down

0 comments on commit f1e01f7

Please sign in to comment.