generate istio destinationRule subset from pod-template-hash && add fields to allow user set canary/stable subset name

Signed-off-by: yunbo <yunbo10124scut@gmail.com>
This commit is contained in:
yunbo 2024-04-09 19:14:30 +08:00
parent 07c1731e8a
commit 86c32e4941
19 changed files with 375 additions and 98 deletions

View File

@ -126,6 +126,7 @@ func (src *Rollout) ConvertTo(dst conversion.Hub) error {
func ConversionToV1beta1TrafficRoutingRef(src TrafficRoutingRef) (dst v1beta1.TrafficRoutingRef) {
dst.Service = src.Service
dst.GracePeriodSeconds = src.GracePeriodSeconds
dst.AdditionalParams = src.AdditionalParams
if src.Ingress != nil {
dst.Ingress = &v1beta1.IngressTrafficRouting{
ClassType: src.Ingress.ClassType,

View File

@ -23,6 +23,8 @@ import (
const (
ProgressingRolloutFinalizerPrefix = "progressing.rollouts.kruise.io"
IstioStableSubsetName = "istio.destinationRule.stableSubsetName"
IstioCanarySubsetName = "istio.destinationRule.canarySubsetName"
)
// TrafficRoutingRef hosts all the different configuration for supported service meshes to enable more fine-grained traffic routing
@ -38,6 +40,10 @@ type TrafficRoutingRef struct {
Gateway *GatewayTrafficRouting `json:"gateway,omitempty"`
// CustomNetworkRefs hold a list of custom providers to route traffic
CustomNetworkRefs []CustomNetworkRef `json:"customNetworkRefs,omitempty"`
// vaild keys:
// + IstioStableSubsetName
// + IstioCanarySubsetName
AdditionalParams map[string]string `json:"additionalParams,omitempty"`
}
// IngressTrafficRouting configuration for ingress controller to control traffic routing

View File

@ -16,6 +16,11 @@ limitations under the License.
package v1beta1
const (
IstioStableSubsetName = "istio.destinationRule.stableSubsetName"
IstioCanarySubsetName = "istio.destinationRule.canarySubsetName"
)
// TrafficRoutingRef hosts all the different configuration for supported service meshes to enable more fine-grained traffic routing
type TrafficRoutingRef struct {
// Service holds the name of a service which selects pods with stable version and don't select any pods with canary version.
@ -29,6 +34,10 @@ type TrafficRoutingRef struct {
Gateway *GatewayTrafficRouting `json:"gateway,omitempty"`
// CustomNetworkRefs hold a list of custom providers to route traffic
CustomNetworkRefs []ObjectRef `json:"customNetworkRefs,omitempty"`
// vaild keys:
// + IstioStableSubsetName
// + IstioCanarySubsetName
AdditionalParams map[string]string `json:"additionalParams,omitempty"`
}
// IngressTrafficRouting configuration for ingress controller to control traffic routing

View File

@ -344,6 +344,11 @@ spec:
for supported service meshes to enable more fine-grained
traffic routing
properties:
additionalParams:
additionalProperties:
type: string
description: 'vaild keys:'
type: object
customNetworkRefs:
description: CustomNetworkRefs hold a list of custom
providers to route traffic
@ -834,6 +839,11 @@ spec:
for supported service meshes to enable more fine-grained
traffic routing
properties:
additionalParams:
additionalProperties:
type: string
description: 'vaild keys:'
type: object
customNetworkRefs:
description: CustomNetworkRefs hold a list of custom
providers to route traffic

View File

@ -54,6 +54,11 @@ spec:
for supported service meshes to enable more fine-grained traffic
routing
properties:
additionalParams:
additionalProperties:
type: string
description: 'vaild keys:'
type: object
customNetworkRefs:
description: CustomNetworkRefs hold a list of custom providers
to route traffic

View File

@ -100,11 +100,14 @@ func objectToTable(path string) error {
Annotations: testCase.Original.GetAnnotations(),
Spec: testCase.Original.Object["spec"],
},
Matches: step.TrafficRoutingStrategy.Matches,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
CanaryService: canaryService,
StableService: stableService,
Matches: step.TrafficRoutingStrategy.Matches,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
CanaryService: canaryService,
StableService: stableService,
StableRevision: "podtemplatehash-v1",
CanaryRevision: "podtemplatehash-v2",
RevisionLabelKey: "pod-template-hash",
}
uList[fmt.Sprintf("step_%d", i)] = data
}
@ -128,11 +131,14 @@ func objectToTable(path string) error {
Annotations: testCase.Original.GetAnnotations(),
Spec: testCase.Original.Object["spec"],
},
Matches: matches,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
CanaryService: canaryService,
StableService: stableService,
Matches: matches,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
CanaryService: canaryService,
StableService: stableService,
StableRevision: "podtemplatehash-v1",
CanaryRevision: "podtemplatehash-v2",
RevisionLabelKey: "pod-template-hash",
}
uList["steps_0"] = data
} else {

View File

@ -16,6 +16,9 @@ trafficRouting:
- apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
name: ds-demo
additionalParams:
istio.destinationRule.stableSubsetName: "version-base"
istio.destinationRule.canarySubsetName: "canary"
original:
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
@ -27,9 +30,7 @@ original:
loadBalancer:
simple: ROUND_ROBIN
subsets:
- labels:
version: base
name: version-base
- name: version-base
expected:
- apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
@ -42,8 +43,8 @@ expected:
simple: ROUND_ROBIN
subsets:
- labels:
version: base
pod-template-hash: "podtemplatehash-v1"
name: version-base
- labels:
istio.service.tag: gray
pod-template-hash: "podtemplatehash-v2"
name: canary

View File

@ -1,8 +1,51 @@
local spec = obj.data.spec
local podLabelKey = obj.revisionLabelKey
if spec.subsets == nil then
spec.subsets = {}
end
-- selector lables might come from pod-template-hash and patchPodTemplateMetadata
-- now we only support pod-template-hash
local stableLabels = {}
if obj.stableRevision ~= nil and obj.stableRevision ~= "" then
stableLabels[podLabelKey] = obj.stableRevision
end
local canaryLabels = {}
if obj.canaryRevision ~= nil and obj.canaryRevision ~= "" then
canaryLabels[podLabelKey] = obj.canaryRevision
end
local StableNameAlreadyExist = false
-- if stableName already exists, just appened the lables
for _, subset in ipairs(spec.subsets) do
if subset.name == obj.stableName then
StableNameAlreadyExist = true
if next(stableLabels) ~= nil then
subset.labels = subset.labels or {}
for key, value in pairs(stableLabels) do
subset.labels[key] = value
end
end
end
end
-- if stableName doesn't exist, create it and its labels
if not StableNameAlreadyExist then
local stable = {}
stable.name = obj.stableName
if next(stableLabels) ~= nil then
stable.labels = stableLabels
end
table.insert(spec.subsets, stable)
end
-- Aussue the canaryName never exist, create it and its labels
local canary = {}
canary.labels = {}
canary.name = "canary"
local podLabelKey = "istio.service.tag"
canary.labels[podLabelKey] = "gray"
canary.name = obj.canaryName
if next(canaryLabels) ~= nil then
canary.labels = canaryLabels
end
table.insert(spec.subsets, canary)
return obj.data

View File

@ -19,6 +19,9 @@ trafficRouting:
- apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
name: vs-demo
additionalParams:
istio.destinationRule.stableSubsetName: "base"
istio.destinationRule.canarySubsetName: "canary"
original:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService

View File

@ -20,6 +20,9 @@ trafficRouting:
- apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
name: vs-demo
additionalParams:
istio.destinationRule.stableSubsetName: "base"
istio.destinationRule.canarySubsetName: "canary"
original:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService

View File

@ -12,6 +12,9 @@ trafficRouting:
- apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
name: vs-demo
additionalParams:
istio.destinationRule.stableSubsetName: "base"
istio.destinationRule.canarySubsetName: "canary"
original:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService

View File

@ -75,7 +75,7 @@ function GenerateRoutesWithMatches(spec, matches, stableService, canaryService)
-- stableService == canaryService indicates DestinationRule exists and subset is set to be canary by default
if stableService == canaryService then
route.route[1].destination.host = stableService
route.route[1].destination.subset = "canary"
route.route[1].destination.subset = obj.canaryName
else
route.route[1].destination.host = canaryService
end
@ -99,7 +99,7 @@ function GenerateRoutes(spec, stableService, canaryService, stableWeight, canary
canary = {
destination = {
host = stableService,
subset = "canary",
subset = obj.canaryName,
},
weight = canaryWeight,
}

View File

@ -250,14 +250,14 @@ func (m *canaryReleaseManager) doCanaryFinalising(c *RolloutContext) (bool, erro
if err != nil || !done {
return done, err
}
// 3. set workload.pause=false; set workload.partition=0
done, err = m.finalizingBatchRelease(c)
// 3. modify network api(ingress or gateway api) configuration, and route 100% traffic to stable pods.
done, err = m.trafficRoutingManager.FinalisingTrafficRouting(tr, false)
c.NewStatus.CanaryStatus.LastUpdateTime = tr.LastUpdateTime
if err != nil || !done {
return done, err
}
// 4. modify network api(ingress or gateway api) configuration, and route 100% traffic to stable pods.
done, err = m.trafficRoutingManager.FinalisingTrafficRouting(tr, false)
c.NewStatus.CanaryStatus.LastUpdateTime = tr.LastUpdateTime
// 4. set workload.pause=false; set workload.partition=0
done, err = m.finalizingBatchRelease(c)
if err != nil || !done {
return done, err
}

View File

@ -278,14 +278,16 @@ func newNetworkProvider(c client.Client, con *TrafficRoutingContext, sService, c
trafficRouting := con.ObjectRef[0]
if trafficRouting.CustomNetworkRefs != nil {
return custom.NewCustomController(c, custom.Config{
Key: con.Key,
RolloutNs: con.Namespace,
CanaryService: cService,
StableService: sService,
TrafficConf: trafficRouting.CustomNetworkRefs,
OwnerRef: con.OwnerRef,
//only set for CustomController, never work for Ingress and Gateway
DisableGenerateCanaryService: con.DisableGenerateCanaryService,
Key: con.Key,
RolloutNs: con.Namespace,
CanaryService: cService,
StableService: sService,
TrafficConf: trafficRouting.CustomNetworkRefs,
OwnerRef: con.OwnerRef,
AdditionalParams: trafficRouting.AdditionalParams,
RevisionLabelKey: con.RevisionLabelKey,
StableRevision: con.StableRevision,
CanaryRevision: con.CanaryRevision,
})
}
if trafficRouting.Ingress != nil {

View File

@ -319,12 +319,12 @@ var (
Data: map[string]string{
"lua.traffic.routing.VirtualService.networking.istio.io": `
spec = obj.data.spec
if obj.canaryWeight == -1 then
obj.canaryWeight = 100
obj.stableWeight = 0
end
function GetHost(destination)
local host = destination.destination.host
dot_position = string.find(host, ".", 1, true)
@ -333,7 +333,7 @@ var (
end
return host
end
-- find routes of VirtualService with stableService
function GetRulesToPatch(spec, stableService, protocol)
local matchedRoutes = {}
@ -351,7 +351,7 @@ var (
end
return matchedRoutes
end
function CalculateWeight(route, stableWeight, n)
local weight
if (route.weight) then
@ -361,7 +361,7 @@ var (
end
return weight
end
-- generate routes with matches, insert a rule before other rules, only support http headers, cookies etc.
function GenerateRoutesWithMatches(spec, matches, stableService, canaryService)
for _, match in ipairs(matches) do
@ -395,14 +395,14 @@ var (
-- stableService == canaryService indicates DestinationRule exists and subset is set to be canary by default
if stableService == canaryService then
route.route[1].destination.host = stableService
route.route[1].destination.subset = "canary"
route.route[1].destination.subset = obj.canaryName
else
route.route[1].destination.host = canaryService
end
table.insert(spec.http, 1, route)
end
end
-- generate routes without matches, change every rule whose host is stableService
function GenerateRoutes(spec, stableService, canaryService, stableWeight, canaryWeight, protocol)
local matchedRules = GetRulesToPatch(spec, stableService, protocol)
@ -419,12 +419,12 @@ var (
canary = {
destination = {
host = stableService,
subset = "canary",
subset = obj.canaryName,
},
weight = canaryWeight,
}
end
-- incase there are multiple versions traffic already, do a for-loop
for _, route in ipairs(rule.route) do
-- update stable service weight
@ -433,7 +433,7 @@ var (
table.insert(rule.route, canary)
end
end
if (obj.matches and next(obj.matches) ~= nil)
then
GenerateRoutesWithMatches(spec, obj.matches, obj.stableService, obj.canaryService)
@ -443,22 +443,66 @@ var (
GenerateRoutes(spec, obj.stableService, obj.canaryService, obj.stableWeight, obj.canaryWeight, "tls")
end
return obj.data
`,
"lua.traffic.routing.DestinationRule.networking.istio.io": `
local spec = obj.data.spec
local podLabelKey = obj.revisionLabelKey
if spec.subsets == nil then
spec.subsets = {}
end
-- selector lables might come from pod-template-hash and patchPodTemplateMetadata
-- now we only support pod-template-hash
local stableLabels = {}
if obj.stableRevision ~= nil and obj.stableRevision ~= "" then
stableLabels[podLabelKey] = obj.stableRevision
end
local canaryLabels = {}
if obj.canaryRevision ~= nil and obj.canaryRevision ~= "" then
canaryLabels[podLabelKey] = obj.canaryRevision
end
local StableNameAlreadyExist = false
-- if stableName already exists, just appened the lables
for _, subset in ipairs(spec.subsets) do
if subset.name == obj.stableName then
StableNameAlreadyExist = true
if next(stableLabels) ~= nil then
subset.labels = subset.labels or {}
for key, value in pairs(stableLabels) do
subset.labels[key] = value
end
end
end
end
-- if stableName doesn't exist, create it and its labels
if not StableNameAlreadyExist then
local stable = {}
stable.name = obj.stableName
if next(stableLabels) ~= nil then
stable.labels = stableLabels
end
table.insert(spec.subsets, stable)
end
-- Aussue the canaryName never exist, create it and its labels
local canary = {}
canary.labels = {}
canary.name = "canary"
local podLabelKey = "istio.service.tag"
canary.labels[podLabelKey] = "gray"
canary.name = obj.canaryName
if next(canaryLabels) ~= nil then
canary.labels = canaryLabels
end
table.insert(spec.subsets, canary)
return obj.data
`,
},
}
virtualServiceDemo = `
virtualServiceDemo1 = `
{
"apiVersion": "networking.istio.io/v1alpha3",
"kind": "VirtualService",
@ -486,7 +530,54 @@ var (
}
}
`
destinationRuleDemo = `
destinationRuleDemo1 = `
{
"apiVersion": "networking.istio.io/v1alpha3",
"kind": "DestinationRule",
"metadata": {
"name": "dr-demo"
},
"spec": {
"host": "echoserver",
"trafficPolicy": {
"loadBalancer": {
"simple": "ROUND_ROBIN"
}
}
}
}
`
virtualServiceDemo2 = `
{
"apiVersion": "networking.istio.io/v1alpha3",
"kind": "VirtualService",
"metadata": {
"name": "echoserver",
"annotations": {
"virtual": "test"
}
},
"spec": {
"hosts": [
"echoserver.example.com"
],
"http": [
{
"route": [
{
"destination": {
"host": "echoserver",
"subset": "hello"
}
}
]
}
]
}
}
`
destinationRuleDemo2 = `
{
"apiVersion": "networking.istio.io/v1alpha3",
"kind": "DestinationRule",
@ -497,10 +588,7 @@ var (
"host": "echoserver",
"subsets": [
{
"labels": {
"version": "base"
},
"name": "version-base"
"name":"hello"
}
],
"trafficPolicy": {
@ -757,12 +845,12 @@ func TestDoTrafficRoutingWithIstio(t *testing.T) {
getUnstructureds: func() []*unstructured.Unstructured {
objects := make([]*unstructured.Unstructured, 0)
u := &unstructured.Unstructured{}
_ = u.UnmarshalJSON([]byte(virtualServiceDemo))
_ = u.UnmarshalJSON([]byte(virtualServiceDemo1))
u.SetAPIVersion("networking.istio.io/v1alpha3")
objects = append(objects, u)
u = &unstructured.Unstructured{}
_ = u.UnmarshalJSON([]byte(destinationRuleDemo))
_ = u.UnmarshalJSON([]byte(destinationRuleDemo1))
u.SetAPIVersion("networking.istio.io/v1alpha3")
objects = append(objects, u)
return objects
@ -770,12 +858,17 @@ func TestDoTrafficRoutingWithIstio(t *testing.T) {
getRollout: func() (*v1beta1.Rollout, *util.Workload) {
obj := demoIstioRollout.DeepCopy()
obj.Status.CanaryStatus.LastUpdateTime = &metav1.Time{Time: time.Now().Add(-10 * time.Second)}
obj.Spec.Strategy.Canary.TrafficRoutings[0].AdditionalParams = map[string]string{
// map is empty, thus the subset names will be default value: stable and canary
// v1beta1.IstioStableSubsetName: "version-base",
// v1beta1.IstioCanarySubsetName: "canary",
}
return obj, &util.Workload{RevisionLabelKey: apps.DefaultDeploymentUniqueLabelKey}
},
expectUnstructureds: func() []*unstructured.Unstructured {
objects := make([]*unstructured.Unstructured, 0)
u := &unstructured.Unstructured{}
_ = u.UnmarshalJSON([]byte(virtualServiceDemo))
_ = u.UnmarshalJSON([]byte(virtualServiceDemo1))
annotations := map[string]string{
OriginalSpecAnnotation: `{"spec":{"hosts":["echoserver.example.com"],"http":[{"route":[{"destination":{"host":"echoserver"}}]}]},"annotations":{"virtual":"test"}}`,
"virtual": "test",
@ -788,12 +881,12 @@ func TestDoTrafficRoutingWithIstio(t *testing.T) {
objects = append(objects, u)
u = &unstructured.Unstructured{}
_ = u.UnmarshalJSON([]byte(destinationRuleDemo))
_ = u.UnmarshalJSON([]byte(destinationRuleDemo1))
annotations = map[string]string{
OriginalSpecAnnotation: `{"spec":{"host":"echoserver","subsets":[{"labels":{"version":"base"},"name":"version-base"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}}`,
OriginalSpecAnnotation: `{"spec":{"host":"echoserver","trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}}`,
}
u.SetAnnotations(annotations)
specStr = `{"host":"echoserver","subsets":[{"labels":{"version":"base"},"name":"version-base"},{"labels":{"istio.service.tag":"gray"},"name":"canary"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}`
specStr = `{"host":"echoserver","subsets":[{"labels":{"pod-template-hash":"podtemplatehash-v1"},"name":"stable"},{"labels":{"pod-template-hash":"podtemplatehash-v2"},"name":"canary"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}`
_ = json.Unmarshal([]byte(specStr), &spec)
u.Object["spec"] = spec
objects = append(objects, u)
@ -815,12 +908,12 @@ func TestDoTrafficRoutingWithIstio(t *testing.T) {
getUnstructureds: func() []*unstructured.Unstructured {
objects := make([]*unstructured.Unstructured, 0)
u := &unstructured.Unstructured{}
_ = u.UnmarshalJSON([]byte(virtualServiceDemo))
_ = u.UnmarshalJSON([]byte(virtualServiceDemo2))
u.SetAPIVersion("networking.istio.io/v1alpha3")
objects = append(objects, u)
u = &unstructured.Unstructured{}
_ = u.UnmarshalJSON([]byte(destinationRuleDemo))
_ = u.UnmarshalJSON([]byte(destinationRuleDemo2))
u.SetAPIVersion("networking.istio.io/v1alpha3")
objects = append(objects, u)
return objects
@ -830,30 +923,34 @@ func TestDoTrafficRoutingWithIstio(t *testing.T) {
// set DisableGenerateCanaryService as true
obj.Spec.Strategy.Canary.DisableGenerateCanaryService = true
obj.Status.CanaryStatus.LastUpdateTime = &metav1.Time{Time: time.Now().Add(-10 * time.Second)}
obj.Spec.Strategy.Canary.TrafficRoutings[0].AdditionalParams = map[string]string{
v1beta1.IstioStableSubsetName: "hello",
v1beta1.IstioCanarySubsetName: "world",
}
return obj, &util.Workload{RevisionLabelKey: apps.DefaultDeploymentUniqueLabelKey}
},
expectUnstructureds: func() []*unstructured.Unstructured {
objects := make([]*unstructured.Unstructured, 0)
u := &unstructured.Unstructured{}
_ = u.UnmarshalJSON([]byte(virtualServiceDemo))
_ = u.UnmarshalJSON([]byte(virtualServiceDemo2))
annotations := map[string]string{
OriginalSpecAnnotation: `{"spec":{"hosts":["echoserver.example.com"],"http":[{"route":[{"destination":{"host":"echoserver"}}]}]},"annotations":{"virtual":"test"}}`,
OriginalSpecAnnotation: `{"spec":{"hosts":["echoserver.example.com"],"http":[{"route":[{"destination":{"host":"echoserver","subset":"hello"}}]}]},"annotations":{"virtual":"test"}}`,
"virtual": "test",
}
u.SetAnnotations(annotations)
specStr := `{"hosts":["echoserver.example.com"],"http":[{"route":[{"destination":{"host":"echoserver"},"weight":95},{"destination":{"host":"echoserver","subset":"canary"},"weight":5}]}]}`
specStr := `{"hosts":["echoserver.example.com"],"http":[{"route":[{"destination":{"host":"echoserver","subset":"hello"},"weight":95},{"destination":{"host":"echoserver","subset":"world"},"weight":5}]}]}`
var spec interface{}
_ = json.Unmarshal([]byte(specStr), &spec)
u.Object["spec"] = spec
objects = append(objects, u)
u = &unstructured.Unstructured{}
_ = u.UnmarshalJSON([]byte(destinationRuleDemo))
_ = u.UnmarshalJSON([]byte(destinationRuleDemo2))
annotations = map[string]string{
OriginalSpecAnnotation: `{"spec":{"host":"echoserver","subsets":[{"labels":{"version":"base"},"name":"version-base"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}}`,
OriginalSpecAnnotation: `{"spec":{"host":"echoserver","subsets":[{"name":"hello"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}}`,
}
u.SetAnnotations(annotations)
specStr = `{"host":"echoserver","subsets":[{"labels":{"version":"base"},"name":"version-base"},{"labels":{"istio.service.tag":"gray"},"name":"canary"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}`
specStr = `{"host":"echoserver","subsets":[{"labels":{"pod-template-hash":"podtemplatehash-v1"},"name":"hello"},{"labels":{"pod-template-hash":"podtemplatehash-v2"},"name":"world"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}`
_ = json.Unmarshal([]byte(specStr), &spec)
u.Object["spec"] = spec
objects = append(objects, u)

View File

@ -53,6 +53,16 @@ type LuaData struct {
Matches []v1beta1.HttpRouteMatch
CanaryService string
StableService string
// workload.RevisionLabelKey
RevisionLabelKey string
// status.CanaryStatus.StableRevision
StableRevision string
// status.CanaryStatus.PodTemplateHash
CanaryRevision string
// specify the name of subset used as stable subset, default 'stable'; if not found, create a subset named stable
StableName string
// sepcify the name of canary subset that will be created, default 'canary'
CanaryName string
}
type Data struct {
Spec interface{} `json:"spec,omitempty"`
@ -72,9 +82,16 @@ type Config struct {
CanaryService string
StableService string
// network providers need to be created
TrafficConf []v1beta1.ObjectRef
OwnerRef metav1.OwnerReference
DisableGenerateCanaryService bool
TrafficConf []v1beta1.ObjectRef
OwnerRef metav1.OwnerReference
// DisableGenerateCanaryService bool
AdditionalParams map[string]string
// workload.RevisionLabelKey
RevisionLabelKey string
// status.CanaryStatus.StableRevision
StableRevision string
// status.CanaryStatus.PodTemplateHash
CanaryRevision string
}
func NewCustomController(client client.Client, conf Config) (network.NetworkProvider, error) {
@ -268,13 +285,27 @@ func (r *customController) executeLuaForCanary(spec Data, strategy *v1beta1.Traf
// so we need to pass weight=-1 to indicate the case where weight is nil.
weight = utilpointer.Int32(-1)
}
// default value
stableSubsetName, ok := r.conf.AdditionalParams[v1beta1.IstioStableSubsetName]
if !ok {
stableSubsetName = "stable" //default value
}
canarySubsetName, ok := r.conf.AdditionalParams[v1beta1.IstioCanarySubsetName]
if !ok {
canarySubsetName = "canary" //default value
}
data := &LuaData{
Data: spec,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
Matches: matches,
CanaryService: r.conf.CanaryService,
StableService: r.conf.StableService,
Data: spec,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
Matches: matches,
CanaryService: r.conf.CanaryService,
StableService: r.conf.StableService,
RevisionLabelKey: r.conf.RevisionLabelKey,
StableRevision: r.conf.StableRevision,
CanaryRevision: r.conf.CanaryRevision,
StableName: stableSubsetName,
CanaryName: canarySubsetName,
}
unObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(data)

View File

@ -89,9 +89,6 @@ var (
"host": "mockb",
"subsets": [
{
"labels": {
"version": "base"
},
"name": "version-base"
}
],
@ -281,6 +278,13 @@ func TestEnsureRoutes(t *testing.T) {
Name: "dr-demo",
},
},
AdditionalParams: map[string]string{
v1beta1.IstioStableSubsetName: "version-base",
v1beta1.IstioCanarySubsetName: "canary",
},
StableRevision: "podtemplatehash-v1",
CanaryRevision: "podtemplatehash-v2",
RevisionLabelKey: "pod-template-hash",
}
},
expectUnstructureds: func() []*unstructured.Unstructured {
@ -301,10 +305,10 @@ func TestEnsureRoutes(t *testing.T) {
u = &unstructured.Unstructured{}
_ = u.UnmarshalJSON([]byte(destinationRuleDemo))
annotations = map[string]string{
OriginalSpecAnnotation: `{"spec":{"host":"mockb","subsets":[{"labels":{"version":"base"},"name":"version-base"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}}`,
OriginalSpecAnnotation: `{"spec":{"host":"mockb","subsets":[{"name":"version-base"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}}`,
}
u.SetAnnotations(annotations)
specStr = `{"host":"mockb","subsets":[{"labels":{"version":"base"},"name":"version-base"},{"labels":{"istio.service.tag":"gray"},"name":"canary"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}`
specStr = `{"host":"mockb","subsets":[{"labels":{"pod-template-hash":"podtemplatehash-v1"},"name":"version-base"},{"labels":{"pod-template-hash":"podtemplatehash-v2"},"name":"canary"}],"trafficPolicy":{"loadBalancer":{"simple":"ROUND_ROBIN"}}}`
_ = json.Unmarshal([]byte(specStr), &spec)
u.Object["spec"] = spec
objects = append(objects, u)
@ -540,11 +544,16 @@ func TestLuaScript(t *testing.T) {
Annotations: testCase.Original.GetAnnotations(),
Spec: testCase.Original.Object["spec"],
},
Matches: step.TrafficRoutingStrategy.Matches,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
CanaryService: canaryService,
StableService: stableService,
Matches: step.TrafficRoutingStrategy.Matches,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
CanaryService: canaryService,
StableService: stableService,
StableRevision: "podtemplatehash-v1",
CanaryRevision: "podtemplatehash-v2",
RevisionLabelKey: "pod-template-hash",
StableName: rollout.Spec.Strategy.Canary.TrafficRoutings[0].AdditionalParams[v1beta1.IstioStableSubsetName],
CanaryName: rollout.Spec.Strategy.Canary.TrafficRoutings[0].AdditionalParams[v1beta1.IstioCanarySubsetName],
}
nSpec, err := executeLua(data, script)
if err != nil {
@ -580,11 +589,16 @@ func TestLuaScript(t *testing.T) {
Annotations: testCase.Original.GetAnnotations(),
Spec: testCase.Original.Object["spec"],
},
Matches: matches,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
CanaryService: canaryService,
StableService: stableService,
Matches: matches,
CanaryWeight: *weight,
StableWeight: 100 - *weight,
CanaryService: canaryService,
StableService: stableService,
StableRevision: "podtemplatehash-v1",
CanaryRevision: "podtemplatehash-v2",
RevisionLabelKey: "pod-template-hash",
StableName: trafficRouting.Spec.ObjectRef[0].AdditionalParams[v1beta1.IstioStableSubsetName],
CanaryName: trafficRouting.Spec.ObjectRef[0].AdditionalParams[v1beta1.IstioCanarySubsetName],
}
nSpec, err := executeLua(data, script)
if err != nil {

View File

@ -1,8 +1,51 @@
local spec = obj.data.spec
local podLabelKey = obj.revisionLabelKey
if spec.subsets == nil then
spec.subsets = {}
end
-- selector lables might come from pod-template-hash and patchPodTemplateMetadata
-- now we only support pod-template-hash
local stableLabels = {}
if obj.stableRevision ~= nil and obj.stableRevision ~= "" then
stableLabels[podLabelKey] = obj.stableRevision
end
local canaryLabels = {}
if obj.canaryRevision ~= nil and obj.canaryRevision ~= "" then
canaryLabels[podLabelKey] = obj.canaryRevision
end
local StableNameAlreadyExist = false
-- if stableName already exists, just appened the lables
for _, subset in ipairs(spec.subsets) do
if subset.name == obj.stableName then
StableNameAlreadyExist = true
if next(stableLabels) ~= nil then
subset.labels = subset.labels or {}
for key, value in pairs(stableLabels) do
subset.labels[key] = value
end
end
end
end
-- if stableName doesn't exist, create it and its labels
if not StableNameAlreadyExist then
local stable = {}
stable.name = obj.stableName
if next(stableLabels) ~= nil then
stable.labels = stableLabels
end
table.insert(spec.subsets, stable)
end
-- Aussue the canaryName never exist, create it and its labels
local canary = {}
canary.labels = {}
canary.name = "canary"
local podLabelKey = "istio.service.tag"
canary.labels[podLabelKey] = "gray"
canary.name = obj.canaryName
if next(canaryLabels) ~= nil then
canary.labels = canaryLabels
end
table.insert(spec.subsets, canary)
return obj.data

View File

@ -159,7 +159,7 @@ function GenerateMatchedRoutes(spec, matches, stableService, canaryService, stab
-- if stableService == canaryService, then do e2e release
if stableService == canaryService then
route.route[1].destination.host = stableService
route.route[1].destination.subset = "canary"
route.route[1].destination.subset = obj.canaryName
else
route.route[1].destination.host = canaryService
end
@ -189,7 +189,7 @@ function GenerateRoutes(spec, stableService, canaryService, stableWeight, canary
canary = {
destination = {
host = stableService,
subset = "canary",
subset = obj.canaryName,
},
weight = canaryWeight,
}