Skip to content

Commit 0418225

Browse files
authored
Misc traffic-splitter updates (#1289)
1 parent 664a82d commit 0418225

File tree

7 files changed

+41
-34
lines changed

7 files changed

+41
-34
lines changed

cli/cmd/lib_traffic_splitters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func trafficSplitterListTable(trafficSplitter []schema.TrafficSplitter, envNames
102102
lastUpdated := time.Unix(splitAPI.Spec.LastUpdated, 0)
103103
var apis []string
104104
for _, api := range splitAPI.Spec.APIs {
105-
apis = append(apis, api.Name+":"+s.Int(api.Weight))
105+
apis = append(apis, api.Name+":"+s.Int32(api.Weight))
106106
}
107107
apisStr := s.TruncateEllipses(strings.Join(apis, " "), 50)
108108
rows = append(rows, []interface{}{

pkg/lib/k8s/virtual_service.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package k8s
1818

1919
import (
20+
"reflect"
21+
2022
"github.com/cortexlabs/cortex/pkg/lib/errors"
2123
"github.com/cortexlabs/cortex/pkg/lib/sets/strset"
2224
"github.com/cortexlabs/cortex/pkg/lib/urls"
@@ -246,3 +248,23 @@ func ExtractVirtualServiceEndpoints(virtualService *istioclientnetworking.Virtua
246248
}
247249
return endpoints
248250
}
251+
252+
func VirtualServicesMatch(vs1, vs2 istionetworking.VirtualService) bool {
253+
if !strset.New(vs1.Hosts...).IsEqual(strset.New(vs2.Hosts...)) {
254+
return false
255+
}
256+
257+
if !strset.New(vs1.Gateways...).IsEqual(strset.New(vs2.Gateways...)) {
258+
return false
259+
}
260+
261+
if !strset.New(vs1.ExportTo...).IsEqual(strset.New(vs2.ExportTo...)) {
262+
return false
263+
}
264+
265+
if !reflect.DeepEqual(vs1.Http, vs2.Http) {
266+
return false
267+
}
268+
269+
return true
270+
}

pkg/operator/resources/trafficsplitter/api.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package trafficsplitter
1919
import (
2020
"fmt"
2121
"path/filepath"
22-
"reflect"
2322

2423
"github.com/cortexlabs/cortex/pkg/lib/errors"
2524
"github.com/cortexlabs/cortex/pkg/lib/k8s"
@@ -33,7 +32,7 @@ import (
3332
)
3433

3534
func UpdateAPI(apiConfig *userconfig.API, projectID string, force bool) (*spec.API, string, error) {
36-
prevVirtualService, err := getK8sResources(apiConfig)
35+
prevVirtualService, err := config.K8s.GetVirtualService(operator.K8sName(apiConfig.Name))
3736
if err != nil {
3837
return nil, "", err
3938
}
@@ -55,7 +54,7 @@ func UpdateAPI(apiConfig *userconfig.API, projectID string, force bool) (*spec.A
5554
return api, fmt.Sprintf("created %s", api.Resource.UserString()), nil
5655
}
5756

58-
if !areVirtualServiceEqual(prevVirtualService, virtualServiceSpec(api)) {
57+
if !areAPIsEqual(prevVirtualService, virtualServiceSpec(api)) {
5958
if err := config.AWS.UploadMsgpackToS3(api, config.Cluster.Bucket, api.Key); err != nil {
6059
return nil, "", errors.Wrap(err, "upload api spec")
6160
}
@@ -67,6 +66,7 @@ func UpdateAPI(apiConfig *userconfig.API, projectID string, force bool) (*spec.A
6766
}
6867
return api, fmt.Sprintf("updated %s", api.Resource.UserString()), nil
6968
}
69+
7070
return api, fmt.Sprintf("%s is up to date", api.Resource.UserString()), nil
7171
}
7272

@@ -105,17 +105,6 @@ func DeleteAPI(apiName string, keepCache bool) error {
105105
return nil
106106
}
107107

108-
func getK8sResources(apiConfig *userconfig.API) (*istioclientnetworking.VirtualService, error) {
109-
var virtualService *istioclientnetworking.VirtualService
110-
111-
virtualService, err := config.K8s.GetVirtualService(operator.K8sName(apiConfig.Name))
112-
if err != nil {
113-
return nil, err
114-
}
115-
116-
return virtualService, err
117-
}
118-
119108
func applyK8sVirtualService(trafficSplitter *spec.API, prevVirtualService *istioclientnetworking.VirtualService) error {
120109
newVirtualService := virtualServiceSpec(trafficSplitter)
121110

@@ -133,7 +122,7 @@ func getTrafficSplitterDestinations(trafficSplitter *spec.API) []k8s.Destination
133122
for i, api := range trafficSplitter.APIs {
134123
destinations[i] = k8s.Destination{
135124
ServiceName: operator.K8sName(api.Name),
136-
Weight: int32(api.Weight),
125+
Weight: api.Weight,
137126
Port: uint32(_defaultPortInt32),
138127
}
139128
}
@@ -201,11 +190,10 @@ func deleteS3Resources(apiName string) error {
201190
return config.AWS.DeleteS3Dir(config.Cluster.Bucket, prefix, true)
202191
}
203192

204-
func areVirtualServiceEqual(vs1, vs2 *istioclientnetworking.VirtualService) bool {
205-
return vs1.ObjectMeta.Name == vs2.ObjectMeta.Name &&
206-
reflect.DeepEqual(vs1.ObjectMeta.Labels, vs2.ObjectMeta.Labels) &&
207-
reflect.DeepEqual(vs1.ObjectMeta.Annotations, vs2.ObjectMeta.Annotations) &&
208-
reflect.DeepEqual(vs1.Spec.Http, vs2.Spec.Http) &&
209-
reflect.DeepEqual(vs1.Spec.Gateways, vs2.Spec.Gateways) &&
210-
reflect.DeepEqual(vs1.Spec.Hosts, vs2.Spec.Hosts)
193+
func areAPIsEqual(vs1, vs2 *istioclientnetworking.VirtualService) bool {
194+
return vs1.Labels["apiName"] == vs2.Labels["apiName"] &&
195+
vs1.Labels["apiKind"] == vs2.Labels["apiKind"] &&
196+
vs1.Labels["apiID"] == vs2.Labels["apiID"] &&
197+
k8s.VirtualServicesMatch(vs1.Spec, vs2.Spec) &&
198+
operator.DoCortexAnnotationsMatch(vs1, vs2)
211199
}

pkg/operator/resources/trafficsplitter/k8s_specs.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/cortexlabs/cortex/pkg/lib/pointer"
2222
"github.com/cortexlabs/cortex/pkg/operator/operator"
2323
"github.com/cortexlabs/cortex/pkg/types/spec"
24-
"github.com/cortexlabs/cortex/pkg/types/userconfig"
2524
istioclientnetworking "istio.io/client-go/pkg/apis/networking/v1alpha3"
2625
)
2726

@@ -36,9 +35,7 @@ func virtualServiceSpec(trafficSplitter *spec.API) *istioclientnetworking.Virtua
3635
Destinations: getTrafficSplitterDestinations(trafficSplitter),
3736
ExactPath: trafficSplitter.Networking.Endpoint,
3837
Rewrite: pointer.String("predict"),
39-
Annotations: map[string]string{
40-
userconfig.EndpointAnnotationKey: *trafficSplitter.Networking.Endpoint,
41-
userconfig.APIGatewayAnnotationKey: trafficSplitter.Networking.APIGateway.String()},
38+
Annotations: trafficSplitter.ToK8sAnnotations(),
4239
Labels: map[string]string{
4340
"apiName": trafficSplitter.Name,
4441
"apiKind": trafficSplitter.Kind.String(),

pkg/types/spec/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func ErrorInsufficientBatchConcurrencyLevelInf(maxBatchSize int32, threadsPerPro
397397
})
398398
}
399399

400-
func ErrorIncorrectTrafficSplitterWeightTotal(totalWeight int) error {
400+
func ErrorIncorrectTrafficSplitterWeightTotal(totalWeight int32) error {
401401
return errors.WithStack(&errors.Error{
402402
Kind: ErrIncorrectTrafficSplitterWeight,
403403
Message: fmt.Sprintf("expected weights to sum to 100 but found %d", totalWeight),

pkg/types/spec/validations.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ func multiAPIsValidation() *cr.StructFieldValidation {
114114
},
115115
{
116116
StructField: "Weight",
117-
IntValidation: &cr.IntValidation{
117+
Int32Validation: &cr.Int32Validation{
118118
Required: true,
119-
GreaterThanOrEqualTo: pointer.Int(0),
120-
LessThanOrEqualTo: pointer.Int(100),
119+
GreaterThanOrEqualTo: pointer.Int32(0),
120+
LessThanOrEqualTo: pointer.Int32(100),
121121
},
122122
},
123123
},
@@ -1267,7 +1267,7 @@ func validateDockerImagePath(image string, providerType types.ProviderType, awsC
12671267
}
12681268

12691269
func verifyTotalWeight(apis []*userconfig.TrafficSplit) error {
1270-
totalWeight := 0
1270+
totalWeight := int32(0)
12711271
for _, api := range apis {
12721272
totalWeight += api.Weight
12731273
}

pkg/types/userconfig/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Predictor struct {
6060

6161
type TrafficSplit struct {
6262
Name string `json:"name" yaml:"name"`
63-
Weight int `json:"weight" yaml:"weight "`
63+
Weight int32 `json:"weight" yaml:"weight"`
6464
}
6565

6666
type ModelResource struct {
@@ -343,7 +343,7 @@ func (api *API) UserStr(provider types.ProviderType) string {
343343
func (trafficSplit *TrafficSplit) UserStr() string {
344344
var sb strings.Builder
345345
sb.WriteString(fmt.Sprintf("%s: %s\n", NameKey, trafficSplit.Name))
346-
sb.WriteString(fmt.Sprintf("%s: %s\n", WeightKey, s.Int(trafficSplit.Weight)))
346+
sb.WriteString(fmt.Sprintf("%s: %s\n", WeightKey, s.Int32(trafficSplit.Weight)))
347347
return sb.String()
348348
}
349349

0 commit comments

Comments
 (0)