-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hot region label setting for tikv auto-scaling (#1833)
* mutate * fix log * add admission configuration * remove useless log * format by comment * use tikv cli * remove useless code * remove cmlister * fix lint * fix tpl
- Loading branch information
Showing
14 changed files
with
226 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2020 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 pod | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/BurntSushi/toml" | ||
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" | ||
"github.com/pingcap/tidb-operator/pkg/controller" | ||
"github.com/pingcap/tidb-operator/pkg/features" | ||
"github.com/pingcap/tidb-operator/pkg/label" | ||
operatorUtils "github.com/pingcap/tidb-operator/pkg/util" | ||
"github.com/pingcap/tidb-operator/pkg/webhook/util" | ||
admissionv1beta1 "k8s.io/api/admission/v1beta1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/klog" | ||
) | ||
|
||
func (pc *PodAdmissionControl) mutatePod(ar *admissionv1beta1.AdmissionRequest) *admissionv1beta1.AdmissionResponse { | ||
pod := &corev1.Pod{} | ||
if err := json.Unmarshal(ar.Object.Raw, pod); err != nil { | ||
return util.ARFail(err) | ||
} | ||
original := pod.DeepCopy() | ||
l := label.Label(pod.Labels) | ||
if !l.IsManagedByTiDBOperator() { | ||
return util.ARSuccess() | ||
} | ||
if !l.IsTiKV() { | ||
return util.ARSuccess() | ||
} | ||
tcName, exist := pod.Labels[label.InstanceLabelKey] | ||
if !exist { | ||
return util.ARSuccess() | ||
} | ||
namespace := ar.Namespace | ||
|
||
tc, err := pc.operatorCli.PingcapV1alpha1().TidbClusters(namespace).Get(tcName, metav1.GetOptions{}) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
return util.ARSuccess() | ||
} | ||
return util.ARFail(err) | ||
} | ||
|
||
if features.DefaultFeatureGate.Enabled(features.AutoScaling) { | ||
err := pc.tikvHotRegionSchedule(tc, pod) | ||
if err != nil { | ||
return util.ARFail(err) | ||
} | ||
} | ||
|
||
patch, err := util.CreateJsonPatch(original, pod) | ||
if err != nil { | ||
return util.ARFail(err) | ||
} | ||
return util.ARPatch(patch) | ||
} | ||
|
||
func (pc *PodAdmissionControl) tikvHotRegionSchedule(tc *v1alpha1.TidbCluster, pod *corev1.Pod) error { | ||
podName := pod.Name | ||
ordinal, err := operatorUtils.GetOrdinalFromPodName(podName) | ||
if err != nil { | ||
return err | ||
} | ||
sets := operatorUtils.GetAutoScalingOutSlots(tc, v1alpha1.TiKVMemberType) | ||
if !sets.Has(ordinal) { | ||
return nil | ||
} | ||
|
||
cmName := controller.TiKVMemberName(tc.Name) | ||
cm, err := pc.kubeCli.CoreV1().ConfigMaps(tc.Namespace).Get(cmName, metav1.GetOptions{}) | ||
if err != nil { | ||
klog.Infof("cm[%s/%s] found error,err %v", tc.Namespace, cmName, err) | ||
return err | ||
} | ||
v, ok := cm.Data["config-file"] | ||
if !ok { | ||
return fmt.Errorf("tc[%s/%s]'s tikv config[config-file] is missing", tc.Namespace, tc.Name) | ||
} | ||
config := &v1alpha1.TiKVConfig{} | ||
err = toml.Unmarshal([]byte(v), config) | ||
if err != nil { | ||
return err | ||
} | ||
if config.Server == nil { | ||
config.Server = &v1alpha1.TiKVServerConfig{} | ||
} | ||
if config.Server.Labels == nil { | ||
config.Server.Labels = map[string]string{} | ||
} | ||
// TODO: add document to explain the hot region label | ||
config.Server.Labels["specialUse"] = "hotRegion" | ||
for id, c := range pod.Spec.Containers { | ||
if c.Name == "tikv" { | ||
appendExtraLabelsENVForTiKV(config.Server.Labels, &c) | ||
pod.Spec.Containers[id] = c | ||
break | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters