Skip to content

Commit

Permalink
feat: add essential data structures for service subset selector (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
tokers authored May 25, 2021
1 parent a16e980 commit 5af1fb4
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
12 changes: 12 additions & 0 deletions pkg/kube/apisix/apis/config/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ type ApisixUpstreamConfig struct {
// The health check configurations for the upstream.
// +optional
HealthCheck *HealthCheck `json:"healthCheck,omitempty" yaml:"healthCheck,omitempty"`
// Subsets groups the service endpoints by their labels. Usually used to differentiate
// service versions.
// +optional
Subsets []ApisixUpstreamSubset `json:"subsets,omitempty" yaml:"subsets,omitempty"`
}

// ApisixUpstreamSubset defines a single endpoints group of one Service.
type ApisixUpstreamSubset struct {
// Name is the name of subset.
Name string `json:"name" yaml:"name"`
// Labels is the label set of this subset.
Labels map[string]string `json:"labels" yaml:"labels"`
}

// UpstreamTimeout is settings for the read, send and connect to the upstream.
Expand Down
30 changes: 30 additions & 0 deletions pkg/kube/apisix/apis/config/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/kube/apisix/apis/config/v2alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ type ApisixRouteHTTPMatch struct {
NginxVars []ApisixRouteHTTPMatchExpr `json:"exprs,omitempty" yaml:"exprs,omitempty"`
}

// ApisixRouteHTTPMatchExpre represents a binary route match expression .
// ApisixRouteHTTPMatchExpr represents a binary route match expression .
type ApisixRouteHTTPMatchExpr struct {
// Subject is the expression subject, it can
// be any string composed by literals and nginx
Expand Down
33 changes: 33 additions & 0 deletions pkg/types/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 types

// Labels contains a series of labels.
type Labels map[string]string

// IsSubsetOf checks whether the current Labels is the subset of
// the passed Labels.
func (s Labels) IsSubsetOf(f Labels) bool {
if len(s) == 0 {
// Empty labels matches nothing not everything.
return false
}
for k, v := range s {
if vv, ok := f[k]; !ok || vv != v {
return false
}
}
return true
}
34 changes: 34 additions & 0 deletions pkg/types/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 types

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestLabelsIsSubsetOf(t *testing.T) {
l := Labels{}
f := Labels{
"version": "v1",
"env": "prod",
}
assert.Equal(t, l.IsSubsetOf(f), false)
l["env"] = "prod"
assert.Equal(t, l.IsSubsetOf(f), true)
l["env"] = "qa"
assert.Equal(t, l.IsSubsetOf(f), false)
}
11 changes: 11 additions & 0 deletions samples/deploy/crd/v1beta1/ApisixUpstream.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ spec:
spec:
type: object
properties:
subsets:
type: array
items:
type: object
properties:
name:
type: string
minLength: 1
labels:
type: object
required: ["name", "labels"]
loadbalancer:
type: object
properties:
Expand Down

0 comments on commit 5af1fb4

Please sign in to comment.