Skip to content

Commit

Permalink
Merge pull request #2012 from yuwenma/cbwp-etag
Browse files Browse the repository at this point in the history
feat: CBWP update with ETag
  • Loading branch information
google-oss-prow[bot] authored Jun 18, 2024
2 parents 04db90b + ed008f3 commit 6944199
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apis/cloudbuild/v1alpha1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func Convert_WorkerPool_API_v1_To_KRM_status(in *cloudbuildpb.WorkerPool, out *C
return nil
}
out.ObservedState = &CloudBuildWorkerPoolObservedState{}

out.ObservedState.ETag = &in.Etag
if err := Convert_PrivatePoolV1Config_API_v1_To_KRM(in.GetPrivatePoolV1Config(), out.ObservedState); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions apis/cloudbuild/v1alpha1/workerpool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ type CloudBuildWorkerPoolObservedState struct {
// +optional
WorkerConfig *WorkerConfig `json:"workerConfig,omitempty"`
NetworkConfig *NetworkConfigState `json:"networkConfig,omitempty"`

/* The Checksum computed by the server, using weak indicator.*/
// +optional
ETag *string `json:"etag,omitempty"`
}

type NetworkConfigState struct {
Expand Down
5 changes: 5 additions & 0 deletions apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ spec:
description: The creation timestamp of the workerpool.
format: date-time
type: string
etag:
description: The Checksum computed by the server, using weak indicator.
type: string
networkConfig:
properties:
egressOption:
Expand Down
71 changes: 71 additions & 0 deletions mockgcp/common/fields/etag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 fields

import (
"crypto/sha256"
"encoding/base64"
"fmt"

"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)

var mustFields = []string{
"displayName",
"state",
}

// ComputeEtag computes the etag of the proto object with weak indicator.
func ComputeEtag(obj proto.Message) string {
pb := proto.Clone(obj)

// ignore dynamic fields like timestampe or uniqueId.
descriptor := pb.ProtoReflect().Descriptor()
fieldDescs := descriptor.Fields()
for i := 0; i < fieldDescs.Len(); i++ {
fieldDesc := fieldDescs.Get(i)
must := false
for _, mustField := range mustFields {
if fieldDesc.JSONName() == mustField {
must = true
}
}
if !must {
pb.ProtoReflect().Clear(fieldDesc)
}
}

m, err := prototext.Marshal(pb)
if err != nil {
panic(fmt.Sprintf("converting to prototext: %v", err))
}
h := sha256.Sum256([]byte(m))
str := base64.StdEncoding.EncodeToString(h[:])
strong := fmt.Sprintf(`"%s"`, str) // ETag must be quoted.
return "W/" + strong
}

func ComputeEtagBytes(obj proto.Message) []byte {
return []byte(ComputeEtag(obj))
}

func ComputeEtagPtr(obj proto.Message) *string {
return ptrTo(ComputeEtag(obj))
}

func ptrTo[T any](t T) *T {
return &t
}
5 changes: 4 additions & 1 deletion mockgcp/mockcloudbuild/workerpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

"cloud.google.com/go/longrunning/autogen/longrunningpb"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/fields"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/devtools/cloudbuild/v1"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -77,6 +78,7 @@ func (s *CloudBuildV1) CreateWorkerPool(ctx context.Context, req *pb.CreateWorke
result.CreateTime = now
result.UpdateTime = now
result.State = pb.WorkerPool_RUNNING
result.Etag = fields.ComputeEtag(result)
return result, nil
})
}
Expand All @@ -99,7 +101,7 @@ func (s *CloudBuildV1) UpdateWorkerPool(ctx context.Context, req *pb.UpdateWorke
f := target.FieldByName(path)
if f.IsValid() && f.CanSet() {
switch f.Kind() {
case reflect.Int:
case reflect.Int, reflect.Int64:
intVal := source.FieldByName(path).Int()
f.SetInt(intVal)
case reflect.String:
Expand All @@ -122,6 +124,7 @@ func (s *CloudBuildV1) UpdateWorkerPool(ctx context.Context, req *pb.UpdateWorke
result := proto.Clone(obj).(*pb.WorkerPool)
result.UpdateTime = now
result.State = pb.WorkerPool_RUNNING
result.Etag = fields.ComputeEtag(result)
return result, nil
})
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/controller/direct/cloudbuild/workerpool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ func (a *Adapter) Update(ctx context.Context, u *unstructured.Unstructured) erro

wp := &cloudbuildpb.WorkerPool{
Name: a.fullyQualifiedName(),
Etag: a.actual.Etag,
}
desired := a.desired.DeepCopy()
err := krm.Convert_WorkerPool_KRM_To_API_v1(desired, wp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ status:
observedGeneration: 2
observedState:
createTime: "1970-01-01T00:00:00Z"
etag: W/"pwzCjvRptOAz64ZilGUmgNZjYVh0LzD3Oh+zEbMPULw="
networkConfig:
egressOption: NO_PUBLIC_EGRESS
peeredNetwork: projects/${projectId}/global/networks/computenetwork-${uniqueId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ X-Xss-Protection: 0
"@type": "type.googleapis.com/google.devtools.cloudbuild.v1.WorkerPool",
"createTime": "2024-04-01T12:34:56.123456Z",
"displayName": "New CloudBuild WorkerPool",
"etag": "abcdef0123A=",
"name": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}",
"privatePoolV1Config": {
"networkConfig": {
Expand Down Expand Up @@ -715,6 +716,7 @@ X-Xss-Protection: 0
"response": {
"@type": "type.googleapis.com/google.devtools.cloudbuild.v1.WorkerPool",
"displayName": "New CloudBuild WorkerPool",
"etag": "abcdef0123A=",
"name": "projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}",
"privatePoolV1Config": {
"networkConfig": {
Expand Down

0 comments on commit 6944199

Please sign in to comment.