Skip to content

Commit d7bbc4f

Browse files
committed
migrate estimator protobuf to use standard protoc-gen-go
Signed-off-by: zhzhuang-zju <m17799853869@163.com>
1 parent c7c212d commit d7bbc4f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2099
-18506
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/evanphx/json-patch/v5 v5.9.11
1010
github.com/go-co-op/gocron v1.30.1
1111
github.com/go-openapi/jsonpointer v0.22.4
12-
github.com/gogo/protobuf v1.3.2
1312
github.com/google/go-cmp v0.7.0
1413
github.com/google/uuid v1.6.0
1514
github.com/kr/pretty v0.3.1
@@ -35,6 +34,7 @@ require (
3534
golang.org/x/tools v0.41.0
3635
gomodules.xyz/jsonpatch/v2 v2.4.0
3736
google.golang.org/grpc v1.78.0
37+
google.golang.org/protobuf v1.36.11
3838
k8s.io/api v0.35.3
3939
k8s.io/apiextensions-apiserver v0.35.3
4040
k8s.io/apimachinery v0.35.3
@@ -108,6 +108,7 @@ require (
108108
github.com/go-openapi/swag/yamlutils v0.25.4 // indirect
109109
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
110110
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
111+
github.com/gogo/protobuf v1.3.2 // indirect
111112
github.com/golang/protobuf v1.5.4 // indirect
112113
github.com/google/btree v1.1.3 // indirect
113114
github.com/google/cel-go v0.26.1 // indirect
@@ -192,7 +193,6 @@ require (
192193
golang.org/x/telemetry v0.0.0-20260109210033-bd525da824e2 // indirect
193194
google.golang.org/genproto/googleapis/api v0.0.0-20260128011058-8636f8732409 // indirect
194195
google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409 // indirect
195-
google.golang.org/protobuf v1.36.11 // indirect
196196
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
197197
gopkg.in/go-jose/go-jose.v2 v2.6.3 // indirect
198198
gopkg.in/inf.v0 v0.9.1 // indirect

hack/tools/tools.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ limitations under the License.
1919
package tools
2020

2121
import (
22-
_ "github.com/gogo/protobuf/protoc-gen-gogo"
2322
_ "github.com/onsi/ginkgo/v2/ginkgo"
2423
_ "github.com/vektra/mockery/v3"
2524
_ "go.uber.org/mock/mockgen"

hack/update-estimator-protobuf.sh

Lines changed: 94 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,123 @@ set -o pipefail
2020

2121
KARMADA_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
2222

23+
#
24+
# Pin protoc to keep generated headers stable across environments.
25+
# This repo's generated estimator protos should be generated with the pinned
26+
# protoc version to avoid environment-dependent `// versions:` headers.
27+
#
28+
PROTOC_VERSION="23.4"
29+
PROTOC_TAG="v${PROTOC_VERSION}"
30+
PROTOC_DIR="${KARMADA_ROOT}/_tmp/protoc"
31+
2332
DEFAULT_GOPATH=$(go env GOPATH | awk -F ':' '{print $1}')
2433
export GOPATH=${DEFAULT_GOPATH}
2534
export PATH=$PATH:$GOPATH/bin
2635

2736
GO111MODULE=on go install golang.org/x/tools/cmd/goimports
28-
GO111MODULE=on go install k8s.io/code-generator/cmd/go-to-protobuf
29-
GO111MODULE=on go install github.com/gogo/protobuf/protoc-gen-gogo
37+
GO111MODULE=on go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.36.11
38+
GO111MODULE=on go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.5.1
3039
GO111MODULE=on go install github.com/vektra/mockery/v3
3140

41+
setup_protoc() {
42+
# If the pinned protoc already exists and matches, reuse it.
43+
if [[ -x "${PROTOC_DIR}/bin/protoc" ]]; then
44+
export PATH="${PROTOC_DIR}/bin:${PATH}"
45+
return 0
46+
fi
47+
48+
# If system protoc already matches, keep using it (no download needed).
49+
if command -v protoc >/dev/null 2>&1; then
50+
if [[ "$(protoc --version 2>/dev/null)" == libprotoc\ ${PROTOC_VERSION}* ]]; then
51+
return 0
52+
fi
53+
fi
54+
55+
rm -rf "${PROTOC_DIR}"
56+
mkdir -p "${PROTOC_DIR}"
57+
58+
local os arch platform zip_name url tmp_zip
59+
os="$(uname | tr '[:upper:]' '[:lower:]')"
60+
arch="$(uname -m)"
61+
62+
case "${os}" in
63+
linux) os="linux" ;;
64+
darwin) os="darwin" ;;
65+
*) echo "Unsupported OS for protoc download: ${os}" >&2; exit 1 ;;
66+
esac
67+
68+
case "${arch}" in
69+
x86_64|amd64) platform="${os}-x86_64" ;;
70+
aarch64|arm64) platform="${os}-aarch64" ;;
71+
*) echo "Unsupported arch for protoc download: ${arch}" >&2; exit 1 ;;
72+
esac
73+
74+
# Protobuf release artifact names differ slightly for macOS.
75+
if [[ "${os}" == "darwin" ]]; then
76+
if [[ "${platform}" == "darwin-x86_64" ]]; then
77+
zip_name="protoc-${PROTOC_VERSION}-osx-x86_64.zip"
78+
elif [[ "${platform}" == "darwin-aarch64" ]]; then
79+
zip_name="protoc-${PROTOC_VERSION}-osx-aarch_64.zip"
80+
else
81+
echo "Unsupported darwin platform: ${platform}" >&2; exit 1
82+
fi
83+
else
84+
if [[ "${platform}" == "linux-x86_64" ]]; then
85+
zip_name="protoc-${PROTOC_VERSION}-linux-x86_64.zip"
86+
elif [[ "${platform}" == "linux-aarch64" ]]; then
87+
zip_name="protoc-${PROTOC_VERSION}-linux-aarch_64.zip"
88+
else
89+
echo "Unsupported linux platform: ${platform}" >&2; exit 1
90+
fi
91+
fi
92+
93+
url="https://github.com/protocolbuffers/protobuf/releases/download/${PROTOC_TAG}/${zip_name}"
94+
tmp_zip="${KARMADA_ROOT}/_tmp/${zip_name}"
95+
96+
if command -v curl >/dev/null 2>&1; then
97+
curl -sSL -o "${tmp_zip}" "${url}"
98+
elif command -v wget >/dev/null 2>&1; then
99+
wget -qO "${tmp_zip}" "${url}"
100+
else
101+
echo "Neither curl nor wget is available to download protoc" >&2
102+
exit 1
103+
fi
104+
105+
if ! command -v unzip >/dev/null 2>&1; then
106+
echo "unzip is required to install protoc ${PROTOC_VERSION}" >&2
107+
exit 1
108+
fi
109+
110+
unzip -q "${tmp_zip}" -d "${PROTOC_DIR}"
111+
rm -f "${tmp_zip}"
112+
113+
export PATH="${PROTOC_DIR}/bin:${PATH}"
114+
}
115+
32116
# Make dummy GOPATH for go-to-protobuf to generate the files to repo root.
33117
# It is useful for case that karmada repo not in the real GOPATH.
34118
go_path="${KARMADA_ROOT}/_go"
35119
cleanup() {
36-
rm -rf "${go_path}"
120+
rm -rf "${go_path}" "${PROTOC_DIR}"
37121
}
38122
trap "cleanup" EXIT SIGINT
39123

40124
cleanup
41125

126+
setup_protoc
127+
42128
source "${KARMADA_ROOT}"/hack/util.sh
43129
util:create_gopath_tree "${KARMADA_ROOT}" "${go_path}"
44130
export GOPATH="${go_path}"
45131

46132
# https://github.com/kubernetes/kubernetes/blob/release-1.23/hack/update-generated-protobuf-dockerized.sh
47-
if [[ -z "$(which protoc)" || $(protoc --version | sed -r "s/libprotoc ([0-9]+).*/\1/g") -lt 3 ]]; then
48-
echo "Generating protobuf requires protoc 3.0.0-beta1 or newer. Please download and"
49-
echo "install the platform appropriate Protobuf package for your OS: "
50-
echo
51-
echo " https://github.com/protocolbuffers/protobuf/releases"
52-
echo
53-
echo "WARNING: Protobuf changes are not being validated"
133+
if [[ -z "$(command -v protoc)" || "$(protoc --version 2>/dev/null)" != libprotoc\ ${PROTOC_VERSION}* ]]; then
134+
echo "Generating protobuf requires protoc libprotoc ${PROTOC_VERSION}*." >&2
54135
exit 1
55136
fi
56137

57-
PACKAGES=(
58-
github.com/karmada-io/karmada/pkg/estimator/pb
59-
)
60-
61-
APIMACHINERY_PKGS=(
62-
-k8s.io/apimachinery/pkg/util/intstr
63-
-k8s.io/apimachinery/pkg/api/resource
64-
-k8s.io/apimachinery/pkg/runtime/schema
65-
-k8s.io/apimachinery/pkg/runtime
66-
-k8s.io/apimachinery/pkg/apis/meta/v1
67-
-k8s.io/api/core/v1
68-
)
69-
70-
go-to-protobuf \
71-
--go-header-file=./hack/boilerplate/boilerplate.go.txt \
72-
--apimachinery-packages=$(IFS=, ; echo "${APIMACHINERY_PKGS[*]}") \
73-
--packages=$(IFS=, ; echo "${PACKAGES[*]}") \
74-
--proto-import="${KARMADA_ROOT}/vendor" \
75-
--proto-import="${KARMADA_ROOT}/third_party/protobuf" \
76-
--output-dir="${GOPATH}/src"
138+
protoc --go_out=. --go_opt=paths=source_relative \
139+
-I . -I "${KARMADA_ROOT}/vendor" \
140+
pkg/estimator/pb/estimator.proto
77141

78142
go generate ./pkg/estimator/service

pkg/estimator/client/accurate.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (se *SchedulerEstimator) maxAvailableComponentSets(ctx context.Context, clu
9797

9898
pbReq := &pb.MaxAvailableComponentSetsRequest{
9999
Cluster: cluster,
100-
Components: make([]pb.Component, 0, len(components)),
100+
Components: make([]*pb.Component, 0, len(components)),
101101
Namespace: namespace,
102102
}
103103

@@ -108,10 +108,11 @@ func (se *SchedulerEstimator) maxAvailableComponentSets(ctx context.Context, clu
108108
cr = comp.ReplicaRequirements.DeepCopy()
109109
}
110110

111-
pbReq.Components = append(pbReq.Components, pb.Component{
111+
replicaRequirements := toPBReplicaRequirements(cr)
112+
pbReq.Components = append(pbReq.Components, &pb.Component{
112113
Name: comp.Name,
113114
Replicas: comp.Replicas,
114-
ReplicaRequirements: toPBReplicaRequirements(cr),
115+
ReplicaRequirements: replicaRequirements,
115116
})
116117
}
117118

@@ -122,20 +123,20 @@ func (se *SchedulerEstimator) maxAvailableComponentSets(ctx context.Context, clu
122123
return res.MaxSets, nil
123124
}
124125

125-
// toPBReplicaRequirements converts the API ComponentReplicaRequirements to the pb.ComponentReplicaRequirements value.
126-
func toPBReplicaRequirements(cr *workv1alpha2.ComponentReplicaRequirements) pb.ComponentReplicaRequirements {
127-
var out pb.ComponentReplicaRequirements
126+
// toPBReplicaRequirements converts the API ComponentReplicaRequirements to the pb.ComponentReplicaRequirements pointer.
127+
func toPBReplicaRequirements(cr *workv1alpha2.ComponentReplicaRequirements) *pb.ComponentReplicaRequirements {
128128
if cr == nil {
129-
return out
129+
return nil
130130
}
131-
out.ResourceRequest = cr.ResourceRequest
131+
out := &pb.ComponentReplicaRequirements{}
132+
_ = out.SetResourceRequest(cr.ResourceRequest)
132133
out.PriorityClassName = cr.PriorityClassName
133134
if cr.NodeClaim != nil {
134135
out.NodeClaim = &pb.NodeClaim{
135-
NodeAffinity: cr.NodeClaim.HardNodeAffinity,
136136
NodeSelector: cr.NodeClaim.NodeSelector,
137-
Tolerations: cr.NodeClaim.Tolerations,
138137
}
138+
_ = out.NodeClaim.SetNodeAffinity(cr.NodeClaim.HardNodeAffinity)
139+
_ = out.NodeClaim.SetTolerations(cr.NodeClaim.Tolerations)
139140
}
140141
return out
141142
}
@@ -147,19 +148,20 @@ func (se *SchedulerEstimator) maxAvailableReplicas(ctx context.Context, cluster
147148
}
148149

149150
req := &pb.MaxAvailableReplicasRequest{
150-
Cluster: cluster,
151-
ReplicaRequirements: pb.ReplicaRequirements{},
151+
Cluster: cluster,
152152
}
153153
if replicaRequirements != nil {
154-
req.ReplicaRequirements.ResourceRequest = replicaRequirements.ResourceRequest
155-
req.ReplicaRequirements.Namespace = replicaRequirements.Namespace
156-
req.ReplicaRequirements.PriorityClassName = replicaRequirements.PriorityClassName
154+
req.ReplicaRequirements = &pb.ReplicaRequirements{
155+
Namespace: replicaRequirements.Namespace,
156+
PriorityClassName: replicaRequirements.PriorityClassName,
157+
}
158+
_ = req.ReplicaRequirements.SetResourceRequest(replicaRequirements.ResourceRequest)
157159
if replicaRequirements.NodeClaim != nil {
158160
req.ReplicaRequirements.NodeClaim = &pb.NodeClaim{
159-
NodeAffinity: replicaRequirements.NodeClaim.HardNodeAffinity,
160161
NodeSelector: replicaRequirements.NodeClaim.NodeSelector,
161-
Tolerations: replicaRequirements.NodeClaim.Tolerations,
162162
}
163+
_ = req.ReplicaRequirements.NodeClaim.SetNodeAffinity(replicaRequirements.NodeClaim.HardNodeAffinity)
164+
_ = req.ReplicaRequirements.NodeClaim.SetTolerations(replicaRequirements.NodeClaim.Tolerations)
163165
}
164166
}
165167
res, err := client.MaxAvailableReplicas(ctx, req)
@@ -182,13 +184,13 @@ func (se *SchedulerEstimator) maxUnscheduableReplicas(
182184

183185
req := &pb.UnschedulableReplicasRequest{
184186
Cluster: cluster,
185-
Resource: pb.ObjectReference{
186-
APIVersion: reference.APIVersion,
187+
Resource: &pb.ObjectReference{
188+
ApiVersion: reference.APIVersion,
187189
Kind: reference.Kind,
188190
Namespace: reference.Namespace,
189191
Name: reference.Name,
190192
},
191-
UnschedulableThreshold: threshold,
193+
UnschedulableThreshold: int64(threshold),
192194
}
193195
res, err := client.GetUnschedulableReplicas(ctx, req)
194196
if err != nil {

pkg/estimator/client/general.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ func getMaximumSetsBasedOnResourceModels(cluster *clusterv1alpha1.Cluster, compo
179179
return -1, err
180180
}
181181

182-
pbComponents := make([]pb.Component, 0, len(components))
182+
pbComponents := make([]*pb.Component, 0, len(components))
183183
for _, comp := range components {
184-
pbComponents = append(pbComponents, pb.Component{
184+
pbComponents = append(pbComponents, &pb.Component{
185185
Name: comp.Name,
186186
Replicas: comp.Replicas,
187187
ReplicaRequirements: toPBReplicaRequirements(comp.ReplicaRequirements),

0 commit comments

Comments
 (0)