-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2012 from yuwenma/cbwp-etag
feat: CBWP update with ETag
- Loading branch information
Showing
11 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
pkg/clients/generated/apis/cloudbuild/v1alpha1/cloudbuildworkerpool_types.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
pkg/clients/generated/apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters