Skip to content

Commit

Permalink
tikv should use requiredDuringSchedulingIgnoredDuringExecution
Browse files Browse the repository at this point in the history
  • Loading branch information
cofyc committed Oct 17, 2019
1 parent 9e779a6 commit 0c36111
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ lint:
tidy:
@echo "go mod tidy"
go mod tidy
git diff --quiet go.mod go.sum
git diff -U --exit-code go.mod go.sum

check-gosec:
@echo "security checking"
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
github.com/golangplus/fmt v0.0.0-20150411045040-2a5d6d7d2995 // indirect
github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e // indirect
github.com/google/btree v0.0.0-20180124185431-e89373fe6b4a // indirect
github.com/google/go-cmp v0.3.1
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/gophercloud/gophercloud v0.3.0 // indirect
github.com/gorilla/context v1.1.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e/go.mod h1:0AA//
github.com/google/btree v0.0.0-20180124185431-e89373fe6b4a h1:ZJu5NB1Bk5ms4vw0Xu4i+jD32SE9jQXyfnOvwhHqlT0=
github.com/google/btree v0.0.0-20180124185431-e89373fe6b4a/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367 h1:ScAXWS+TR6MZKex+7Z8rneuSJH+FSDqd6ocQyl+ZHo4=
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down
54 changes: 37 additions & 17 deletions tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,25 @@ func GetPodsByLabels(kubeCli kubernetes.Interface, node string, lables map[strin
return nil, nil
}

var affinityTemp string = `{{.Kind}}:
var requiredAffinityTemp string = `{{.Kind}}:
config: |
{{range .Config}} {{.}}
{{end}}
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- weight: {{.Weight}}
podAffinityTerm:
labelSelector:
matchLabels:
app.kubernetes.io/instance: {{.ClusterName}}
app.kubernetes.io/component: {{.Kind}}
topologyKey: {{.TopologyKey}}
namespaces:
- {{.Namespace}}
`

var preferredAffinityTemp string = `{{.Kind}}:
config: |
{{range .Config}} {{.}}
{{end}}
Expand Down Expand Up @@ -189,32 +207,34 @@ type BinLogInfo struct {
TopologyKey string
}

func GetSubValuesOrDie(clusterName, namespace, topologyKey string, pdConfig []string, tikvConfig []string, tidbConfig []string, pumpConfig []string, drainerConfig []string) string {
temp, err := template.New("dt-affinity").Parse(affinityTemp)
func genAffinityConfig(tpl string, info *AffinityInfo) (string, error) {
temp, err := template.New("dt-affinity").Parse(tpl)
if err != nil {
slack.NotifyAndPanic(err)
return "", nil
}
buf := new(bytes.Buffer)
err = temp.Execute(buf, info)
if err != nil {
return "", nil
}
return buf.String(), nil
}

pdbuff := new(bytes.Buffer)
err = temp.Execute(pdbuff, &AffinityInfo{ClusterName: clusterName, Kind: "pd", Weight: 50, Namespace: namespace, TopologyKey: topologyKey, Config: pdConfig})
func GetSubValuesOrDie(clusterName, namespace, topologyKey string, pdConfig []string, tikvConfig []string, tidbConfig []string, pumpConfig []string, drainerConfig []string) string {
pdValues, err := genAffinityConfig(preferredAffinityTemp, &AffinityInfo{ClusterName: clusterName, Kind: "pd", Weight: 50, Namespace: namespace, TopologyKey: topologyKey, Config: pdConfig})
if err != nil {
slack.NotifyAndPanic(err)
}
tikvbuff := new(bytes.Buffer)
err = temp.Execute(tikvbuff, &AffinityInfo{ClusterName: clusterName, Kind: "tikv", Weight: 50, Namespace: namespace, TopologyKey: topologyKey, Config: tikvConfig})

tikvValues, err := genAffinityConfig(requiredAffinityTemp, &AffinityInfo{ClusterName: clusterName, Kind: "tikv", Weight: 50, Namespace: namespace, TopologyKey: topologyKey, Config: tikvConfig})
if err != nil {
slack.NotifyAndPanic(err)
}
tidbbuff := new(bytes.Buffer)
err = temp.Execute(tidbbuff, &AffinityInfo{ClusterName: clusterName, Kind: "tidb", Weight: 50, Namespace: namespace, TopologyKey: topologyKey, Config: tidbConfig})

tidbValues, err := genAffinityConfig(preferredAffinityTemp, &AffinityInfo{ClusterName: clusterName, Kind: "tidb", Weight: 50, Namespace: namespace, TopologyKey: topologyKey, Config: tidbConfig})
if err != nil {
slack.NotifyAndPanic(err)
}
subValues := fmt.Sprintf("%s%s%s", pdbuff.String(), tikvbuff.String(), tidbbuff.String())

//if pumpConfig == nil && drainerConfig == nil {
// return subValues
//}

btemp, err := template.New("binlog").Parse(binlogTemp)
if err != nil {
Expand All @@ -225,8 +245,8 @@ func GetSubValuesOrDie(clusterName, namespace, topologyKey string, pdConfig []st
if err != nil {
slack.NotifyAndPanic(err)
}
subValues = fmt.Sprintf("%s%s", subValues, binlogbuff.String())
return subValues

return pdValues + tikvValues + tidbValues + binlogbuff.String()
}

func GetDrainerSubValuesOrDie(info *DrainerConfig) string {
Expand Down
128 changes: 128 additions & 0 deletions tests/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed 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,
// See the License for the specific language governing permissions and
// limitations under the License.package spec

package tests

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestGetSubValuesOrDie(t *testing.T) {
tests := []struct {
name string
clusterName string
namespace string
topologyKey string
pdConfig []string
tidbConfig []string
tikvConfig []string
pumpConfig []string
drainerConfig []string
expectedOutput string
}{
{
name: "without component configs",
clusterName: "cluster1",
namespace: "ns1",
topologyKey: "rack",
expectedOutput: `pd:
config: |
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
podAffinityTerm:
labelSelector:
matchLabels:
app.kubernetes.io/instance: cluster1
app.kubernetes.io/component: pd
topologyKey: rack
namespaces:
- ns1
tikv:
config: |
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- weight: 50
podAffinityTerm:
labelSelector:
matchLabels:
app.kubernetes.io/instance: cluster1
app.kubernetes.io/component: tikv
topologyKey: rack
namespaces:
- ns1
tidb:
config: |
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
podAffinityTerm:
labelSelector:
matchLabels:
app.kubernetes.io/instance: cluster1
app.kubernetes.io/component: tidb
topologyKey: rack
namespaces:
- ns1
binlog:
pump:
tolerations:
- key: node-role
operator: Equal
value: tidb
effect: "NoSchedule"
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
podAffinityTerm:
topologyKey: rack
namespaces:
- ns1
drainer:
tolerations:
- key: node-role
operator: Equal
value: tidb
effect: "NoSchedule"
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
podAffinityTerm:
topologyKey: rack
namespaces:
- ns1
`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOutput := GetSubValuesOrDie(tt.clusterName, tt.namespace, tt.topologyKey, tt.pdConfig, tt.tidbConfig, tt.tikvConfig, tt.pumpConfig, tt.drainerConfig)
if diff := cmp.Diff(tt.expectedOutput, gotOutput); diff != "" {
t.Errorf("unexpected output (-want, +got): %s", diff)
}
})
}
}

0 comments on commit 0c36111

Please sign in to comment.