Skip to content

Commit

Permalink
use lister
Browse files Browse the repository at this point in the history
  • Loading branch information
Song Gao committed Jun 22, 2020
1 parent cc3d6a7 commit 37b2939
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/tidbcluster/tidb_cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func NewController(
podInformer := kubeInformerFactory.Core().V1().Pods()
nodeInformer := kubeInformerFactory.Core().V1().Nodes()
secretInformer := kubeInformerFactory.Core().V1().Secrets()
scalerInformer := informerFactory.Pingcap().V1alpha1().TidbClusterAutoScalers()

tcControl := controller.NewRealTidbClusterControl(cli, tcInformer.Lister(), recorder)
pdControl := pdapi.NewDefaultPDControl(kubeCli)
Expand Down Expand Up @@ -227,7 +228,7 @@ func NewController(
setControl,
),
mm.NewTidbDiscoveryManager(typedControl),
mm.NewTidbClusterStatusManager(kubeCli, cli),
mm.NewTidbClusterStatusManager(kubeCli, cli, scalerInformer.Lister()),
podRestarter,
&tidbClusterConditionUpdater{},
recorder,
Expand Down
26 changes: 20 additions & 6 deletions pkg/manager/member/tidbcluster_status_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned"
listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/pdapi"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -35,14 +36,16 @@ const (
)

type TidbClusterStatusManager struct {
cli versioned.Interface
pdControl pdapi.PDControlInterface
cli versioned.Interface
pdControl pdapi.PDControlInterface
scalerLister listers.TidbClusterAutoScalerLister
}

func NewTidbClusterStatusManager(kubeCli kubernetes.Interface, cli versioned.Interface) *TidbClusterStatusManager {
func NewTidbClusterStatusManager(kubeCli kubernetes.Interface, cli versioned.Interface, scalerLister listers.TidbClusterAutoScalerLister) *TidbClusterStatusManager {
return &TidbClusterStatusManager{
cli: cli,
pdControl: pdapi.NewDefaultPDControl(kubeCli),
cli: cli,
pdControl: pdapi.NewDefaultPDControl(kubeCli),
scalerLister: scalerLister,
}
}

Expand Down Expand Up @@ -130,14 +133,25 @@ func (tcsm *TidbClusterStatusManager) syncAutoScalerRef(tc *v1alpha1.TidbCluster
}
tacNamespace := tc.Status.AutoScaler.Namespace
tacName := tc.Status.AutoScaler.Name
_, err := tcsm.cli.PingcapV1alpha1().TidbClusterAutoScalers(tacNamespace).Get(tacName, metav1.GetOptions{})
tac, err := tcsm.scalerLister.TidbClusterAutoScalers(tacNamespace).Get(tacName)
if err != nil {
if errors.IsNotFound(err) {
tc.Status.AutoScaler = nil
err = nil
}
return err
}
if tac.Spec.Cluster.Name != tc.Name {
tc.Status.AutoScaler = nil
return nil
}
if len(tac.Spec.Cluster.Namespace) < 1 {
return nil
}
if tac.Spec.Cluster.Namespace != tc.Namespace {
tc.Status.AutoScaler = nil
return nil
}
return nil
}

Expand Down
124 changes: 124 additions & 0 deletions pkg/manager/member/tidbcluster_status_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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 member

import (
"testing"

. "github.com/onsi/gomega"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/client/clientset/versioned/fake"
informers "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions"
"k8s.io/client-go/kubernetes"
kubefake "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
)

func TestSyncAutoScalerRef(t *testing.T) {
g := NewGomegaWithT(t)
testcases := []struct {
name string
haveRef bool
autoScalerExisted bool
correctRef bool
expectedStatusRef *v1alpha1.TidbClusterAutoScalerRef
}{
{
name: "empty Reference",
haveRef: false,
autoScalerExisted: false,
correctRef: false,
expectedStatusRef: nil,
},
{
name: "normal",
haveRef: true,
autoScalerExisted: true,
correctRef: true,
expectedStatusRef: &v1alpha1.TidbClusterAutoScalerRef{
Name: "auto-scaler",
Namespace: "default",
},
},
{
name: "target auto-scaler not existed",
haveRef: true,
autoScalerExisted: false,
correctRef: false,
expectedStatusRef: nil,
},
{
name: "target auto-scaler have changed the cluster target",
haveRef: true,
autoScalerExisted: true,
correctRef: false,
expectedStatusRef: nil,
},
}
for _, testcase := range testcases {
t.Run(testcase.name, func(t *testing.T) {
tsm, _, _, scalerInder := newFakeTidbClusterStatusManager()
tc := newTidbCluster()
tc.Namespace = "default"
tac := newTidbClusterAutoScaler(tc)
if !testcase.haveRef {
tc.Status.AutoScaler = nil
}
tc.Status.AutoScaler = &v1alpha1.TidbClusterAutoScalerRef{
Name: tac.Name,
Namespace: tac.Namespace,
}
if !testcase.correctRef {
tac.Spec.Cluster.Name = "1234"
}

if testcase.autoScalerExisted {
scalerInder.Add(tac)
} else {
}
err := tsm.syncAutoScalerRef(tc)
g.Expect(err).ShouldNot(HaveOccurred())
if testcase.expectedStatusRef == nil {
g.Expect(tc.Status.AutoScaler).Should(BeNil())
} else {
g.Expect(tc.Status.AutoScaler).ShouldNot(BeNil())
g.Expect(tc.Status.AutoScaler.Name).Should(Equal(testcase.expectedStatusRef.Name))
g.Expect(tc.Status.AutoScaler.Namespace).Should(Equal(testcase.expectedStatusRef.Namespace))
}
})
}
}

func newFakeTidbClusterStatusManager() (*TidbClusterStatusManager, kubernetes.Interface, *fake.Clientset, cache.Indexer) {
cli := fake.NewSimpleClientset()
kubeCli := kubefake.NewSimpleClientset()
informerFactory := informers.NewSharedInformerFactoryWithOptions(cli, 0)
scalerFactory := informerFactory.Pingcap().V1alpha1().TidbClusterAutoScalers()
scalerInder := scalerFactory.Informer().GetIndexer()
return NewTidbClusterStatusManager(kubeCli, cli, scalerFactory.Lister()), kubeCli, cli, scalerInder
}

func newTidbClusterAutoScaler(tc *v1alpha1.TidbCluster) *v1alpha1.TidbClusterAutoScaler {
tac := &v1alpha1.TidbClusterAutoScaler{
Spec: v1alpha1.TidbClusterAutoScalerSpec{
Cluster: v1alpha1.TidbClusterRef{
Namespace: tc.Namespace,
Name: tc.Name,
},
},
}
tac.Name = "auto-scaler"
tac.Namespace = "default"
return tac
}
1 change: 1 addition & 0 deletions pkg/manager/member/tiflash_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ func newTidbCluster() *v1alpha1.TidbCluster {
},
TiFlash: &v1alpha1.TiFlashSpec{},
},
Status: v1alpha1.TidbClusterStatus{},
}
}

Expand Down

0 comments on commit 37b2939

Please sign in to comment.