Skip to content

Commit 758c162

Browse files
committed
[cluster-autoscaler-1.34] Add peer fields to replace inlined Kubernetes API fields
1 parent 0fc7b8b commit 758c162

File tree

22 files changed

+1144
-397
lines changed

22 files changed

+1144
-397
lines changed

cluster-autoscaler/cloudprovider/externalgrpc/README.md

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,7 @@ The `CloudProvider` interface was designed with the assumption that its implemen
5252

5353
### Code Generation
5454

55-
To regenerate the gRPC code:
56-
57-
1. install `protoc` and `protoc-gen-go-grpc`:
58-
59-
```bash
60-
go install google.golang.org/protobuf/cmd/[email protected]
61-
go install google.golang.org/grpc/cmd/[email protected]
62-
```
63-
64-
2. import proto dependencies using go modules
65-
```bash
66-
go mod vendor
67-
```
68-
69-
3. generate gRPC client and server code:
70-
71-
```bash
72-
protoc \
73-
-I ./cluster-autoscaler \
74-
-I ./cluster-autoscaler/vendor \
75-
--go_out=. \
76-
--go-grpc_out=. \
77-
./cluster-autoscaler/cloudprovider/externalgrpc/protos/externalgrpc.proto
78-
```
55+
To regenerate the gRPC code, run the `cluster-autoscaler/hack/update-proto.sh` script
7956

8057
### General considerations
8158

cluster-autoscaler/cloudprovider/externalgrpc/examples/external-grpc-cloud-provider-service/wrapper/wrapper.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"google.golang.org/grpc/codes"
2525
"google.golang.org/grpc/status"
2626
"google.golang.org/protobuf/types/known/anypb"
27+
2728
apiv1 "k8s.io/api/core/v1"
2829
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2930
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
@@ -123,8 +124,25 @@ func (w *Wrapper) PricingNodePrice(_ context.Context, req *protos.PricingNodePri
123124
return nil, err
124125
}
125126
reqNode := req.GetNode()
126-
reqStartTime := req.GetStartTime()
127-
reqEndTime := req.GetEndTime()
127+
128+
var reqStartTime *metav1.Time
129+
if startTimestamp := req.GetStartTimestamp(); startTimestamp != nil {
130+
// read standard protobuf timestamp if set
131+
reqStartTime = &metav1.Time{Time: startTimestamp.AsTime()}
132+
} else {
133+
// otherwise fallback to reading metav1.Time
134+
reqStartTime = req.GetStartTime()
135+
}
136+
137+
var reqEndTime *metav1.Time
138+
if endTimestamp := req.GetEndTimestamp(); endTimestamp != nil {
139+
// read standard protobuf timestamp if set
140+
reqEndTime = &metav1.Time{Time: endTimestamp.AsTime()}
141+
} else {
142+
// otherwise fallback to reading metav1.Time
143+
reqEndTime = req.GetEndTime()
144+
}
145+
128146
if reqNode == nil || reqStartTime == nil || reqEndTime == nil {
129147
return nil, fmt.Errorf("request fields were nil")
130148
}
@@ -148,9 +166,38 @@ func (w *Wrapper) PricingPodPrice(_ context.Context, req *protos.PricingPodPrice
148166
}
149167
return nil, err
150168
}
151-
reqPod := req.GetPod()
152-
reqStartTime := req.GetStartTime()
153-
reqEndTime := req.GetEndTime()
169+
170+
var reqPod *apiv1.Pod
171+
if podBytes := req.GetPodBytes(); podBytes != nil {
172+
// decode from opaque bytes into pod if set
173+
pod := &apiv1.Pod{}
174+
if err := pod.Unmarshal(podBytes); err != nil {
175+
return nil, err
176+
}
177+
reqPod = pod
178+
} else {
179+
// otherwise fallback to reading inlined pod
180+
reqPod = req.GetPod()
181+
}
182+
183+
var reqStartTime *metav1.Time
184+
if startTimestamp := req.GetStartTimestamp(); startTimestamp != nil {
185+
// read standard protobuf timestamp if set
186+
reqStartTime = &metav1.Time{Time: startTimestamp.AsTime()}
187+
} else {
188+
// otherwise fallback to reading metav1.Time
189+
reqStartTime = req.GetStartTime()
190+
}
191+
192+
var reqEndTime *metav1.Time
193+
if endTimestamp := req.GetEndTimestamp(); endTimestamp != nil {
194+
// read standard protobuf timestamp if set
195+
reqEndTime = &metav1.Time{Time: endTimestamp.AsTime()}
196+
} else {
197+
// otherwise fallback to reading metav1.Time
198+
reqEndTime = req.GetEndTime()
199+
}
200+
154201
if reqPod == nil || reqStartTime == nil || reqEndTime == nil {
155202
return nil, fmt.Errorf("request fields were nil")
156203
}

cluster-autoscaler/cloudprovider/externalgrpc/externalgrpc_cloud_provider.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"google.golang.org/grpc/codes"
3131
"google.golang.org/grpc/credentials"
3232
"google.golang.org/grpc/status"
33+
"google.golang.org/protobuf/types/known/timestamppb"
34+
3335
apiv1 "k8s.io/api/core/v1"
3436
"k8s.io/apimachinery/pkg/api/resource"
3537
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -162,6 +164,9 @@ func (m *pricingModel) NodePrice(node *apiv1.Node, startTime time.Time, endTime
162164
Node: externalGrpcNode(node),
163165
StartTime: &start,
164166
EndTime: &end,
167+
168+
StartTimestamp: timestamppb.New(startTime),
169+
EndTimestamp: timestamppb.New(endTime),
165170
})
166171
if err != nil {
167172
st, ok := status.FromError(err)
@@ -182,10 +187,20 @@ func (m *pricingModel) PodPrice(pod *apiv1.Pod, startTime time.Time, endTime tim
182187
klog.V(5).Infof("Performing gRPC call PricingPodPrice for pod %v", pod.Name)
183188
start := metav1.NewTime(startTime)
184189
end := metav1.NewTime(endTime)
190+
191+
podBytes, err := pod.Marshal()
192+
if err != nil {
193+
return 0, err
194+
}
195+
185196
res, err := m.client.PricingPodPrice(ctx, &protos.PricingPodPriceRequest{
186197
Pod: pod,
187198
StartTime: &start,
188199
EndTime: &end,
200+
201+
PodBytes: podBytes,
202+
StartTimestamp: timestamppb.New(startTime),
203+
EndTimestamp: timestamppb.New(endTime),
189204
})
190205
if err != nil {
191206
st, ok := status.FromError(err)

cluster-autoscaler/cloudprovider/externalgrpc/externalgrpc_cloud_provider_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"google.golang.org/grpc/codes"
2727
"google.golang.org/grpc/status"
2828
"google.golang.org/protobuf/types/known/anypb"
29+
2930
apiv1 "k8s.io/api/core/v1"
3031
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
3132
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/externalgrpc/protos"
@@ -309,15 +310,17 @@ func TestCloudProvider_Pricing(t *testing.T) {
309310
// test correct PodPrice call
310311
m.On(
311312
"PricingPodPrice", mock.Anything, mock.MatchedBy(func(req *protos.PricingPodPriceRequest) bool {
312-
return req.Pod.Name == "pod1"
313+
pod := &apiv1.Pod{}
314+
return pod.Unmarshal(req.PodBytes) == nil && pod.Name == "pod1"
313315
}),
314316
).Return(
315317
&protos.PricingPodPriceResponse{Price: 100},
316318
nil,
317319
)
318320
m.On(
319321
"PricingPodPrice", mock.Anything, mock.MatchedBy(func(req *protos.PricingPodPriceRequest) bool {
320-
return req.Pod.Name == "pod2"
322+
pod := &apiv1.Pod{}
323+
return pod.Unmarshal(req.PodBytes) == nil && pod.Name == "pod2"
321324
}),
322325
).Return(
323326
&protos.PricingPodPriceResponse{Price: 200},
@@ -341,7 +344,8 @@ func TestCloudProvider_Pricing(t *testing.T) {
341344
// test grpc error for PodPrice
342345
m.On(
343346
"PricingPodPrice", mock.Anything, mock.MatchedBy(func(req *protos.PricingPodPriceRequest) bool {
344-
return req.Pod.Name == "pod3"
347+
pod := &apiv1.Pod{}
348+
return pod.Unmarshal(req.PodBytes) == nil && pod.Name == "pod3"
345349
}),
346350
).Return(
347351
&protos.PricingPodPriceResponse{},
@@ -357,7 +361,8 @@ func TestCloudProvider_Pricing(t *testing.T) {
357361
// test notImplemented for PodPrice
358362
m.On(
359363
"PricingPodPrice", mock.Anything, mock.MatchedBy(func(req *protos.PricingPodPriceRequest) bool {
360-
return req.Pod.Name == "pod4"
364+
pod := &apiv1.Pod{}
365+
return pod.Unmarshal(req.PodBytes) == nil && pod.Name == "pod4"
361366
}),
362367
).Return(
363368
&protos.PricingPodPriceResponse{},

0 commit comments

Comments
 (0)