Skip to content

Commit

Permalink
add common computeEtag function
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma committed Jun 18, 2024
1 parent cda258d commit ed008f3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
4 changes: 3 additions & 1 deletion apis/cloudbuild/v1alpha1/workerpool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ type CloudBuildWorkerPoolObservedState struct {
WorkerConfig *WorkerConfig `json:"workerConfig,omitempty"`
NetworkConfig *NetworkConfigState `json:"networkConfig,omitempty"`

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

type NetworkConfigState struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ spec:
description: The creation timestamp of the workerpool.
format: date-time
type: string
eTag:
etag:
description: The Checksum computed by the server, using weak indicator.
type: string
networkConfig:
properties:
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: 3 additions & 2 deletions 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,7 +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 = "W/11111111111111111111111111111111"
result.Etag = fields.ComputeEtag(result)
return result, nil
})
}
Expand Down Expand Up @@ -123,7 +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 = "W/22222222222222222222222222222222"
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.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ status:
observedGeneration: 2
observedState:
createTime: "1970-01-01T00:00:00Z"
eTag: W/22222222222222222222222222222222
etag: W/"pwzCjvRptOAz64ZilGUmgNZjYVh0LzD3Oh+zEbMPULw="
networkConfig:
egressOption: NO_PUBLIC_EGRESS
peeredNetwork: projects/${projectId}/global/networks/computenetwork-${uniqueId}
Expand Down

0 comments on commit ed008f3

Please sign in to comment.