Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Optimization for CreateOrUpdateConfigMap #4170

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9ba9166
Optimization for CreateOrUpdateConfigMap
mianhk Aug 31, 2021
c835711
fix: review suggest
mianhk Sep 29, 2021
d3ae640
Optimize: delete origin CreateOrUpdateConfigMap
mianhk Sep 30, 2021
3075100
Merge remote-tracking branch 'upstream/master' into optimize-CreateOr…
mianhk Sep 30, 2021
d3f5476
fix: function name and configMap NotFound check
mianhk Oct 8, 2021
d5f7622
fix: function name and configMap NotFound check
mianhk Oct 8, 2021
96639c0
fix: always create before update
mianhk Oct 25, 2021
5e73f5a
fix: useless code
mianhk Oct 25, 2021
f1b94bb
fix: debug code
mianhk Oct 26, 2021
f6f39ae
fix: update labels and annotations
mianhk Nov 2, 2021
8a013d9
fix: labelFilterKubeInformerFactory of test cases
mianhk Nov 3, 2021
e1de20a
optimized code
mianhk Nov 3, 2021
0f3fa52
fix : logic
mianhk Nov 4, 2021
df12f46
optimize blank line
mianhk Nov 9, 2021
5c00995
Merge branch 'master' into optimize-CreateOrUpdateConfigMap
DanielZhangQD Nov 11, 2021
786ee93
Merge branch 'master' into optimize-CreateOrUpdateConfigMap
ti-chi-bot Nov 12, 2021
8091d54
Merge branch 'master' into optimize-CreateOrUpdateConfigMap
ti-chi-bot Nov 12, 2021
8506b57
Merge branch 'master' into optimize-CreateOrUpdateConfigMap
ti-chi-bot Nov 16, 2021
f5f583b
Merge branch 'master' into optimize-CreateOrUpdateConfigMap
ti-chi-bot Nov 17, 2021
0d4fc84
fix: e2e test error
mianhk Dec 1, 2021
ed1331f
Merge remote-tracking branch 'origin/optimize-CreateOrUpdateConfigMap…
mianhk Dec 1, 2021
5a3e370
Merge remote-tracking branch 'upstream/master' into optimize-CreateOr…
mianhk Dec 1, 2021
1dfae18
Merge branch 'master' into optimize-CreateOrUpdateConfigMap
DanielZhangQD Dec 3, 2021
27a41a9
fix: configmap check dashboards.yaml
mianhk Dec 3, 2021
742d0e1
Merge remote-tracking branch 'origin/optimize-CreateOrUpdateConfigMap…
mianhk Dec 3, 2021
f236c8d
fix: UpdateConfigMap setOwnerFlag
mianhk Dec 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions pkg/controller/generic_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
corelisterv1 "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -39,8 +40,8 @@ import (
type TypedControlInterface interface {
// CreateOrUpdateSecret create the desired secret or update the current one to desired state if already existed
CreateOrUpdateSecret(controller client.Object, secret *corev1.Secret) (*corev1.Secret, error)
// CreateOrUpdateConfigMap create client desired configmap or update the current one to desired state if already existed
CreateOrUpdateConfigMap(controller client.Object, cm *corev1.ConfigMap) (*corev1.ConfigMap, error)
// CheckAndUpdateConfigMap check configmap before create or update the desired configmap
csuzhangxc marked this conversation as resolved.
Show resolved Hide resolved
CheckAndUpdateConfigMap(configMapLister corelisterv1.ConfigMapLister, controller client.Object, cm *corev1.ConfigMap) (*corev1.ConfigMap, error)
// CreateOrUpdateClusterRole the desired clusterRole or update the current one to desired state if already existed
CreateOrUpdateClusterRole(controller client.Object, clusterRole *rbacv1.ClusterRole) (*rbacv1.ClusterRole, error)
// CreateOrUpdateClusterRoleBinding create the desired clusterRoleBinding or update the current one to desired state if already existed
Expand Down Expand Up @@ -242,8 +243,20 @@ func (w *typedWrapper) CreateOrUpdateServiceAccount(controller client.Object, sa
return result.(*corev1.ServiceAccount), err
}

DanielZhangQD marked this conversation as resolved.
Show resolved Hide resolved
func (w *typedWrapper) CreateOrUpdateConfigMap(controller client.Object, cm *corev1.ConfigMap) (*corev1.ConfigMap, error) {
result, err := w.GenericControlInterface.CreateOrUpdate(controller, cm, func(existing, desired client.Object) error {
func NeedCreateOrUpgradeConfigMap(cmLister corelisterv1.ConfigMapLister, newCm *corev1.ConfigMap, mergeFn MergeFn) (*corev1.ConfigMap, bool) {
// check if need CreateOrUpgrade
oldCm, err := cmLister.ConfigMaps(newCm.Namespace).Get(newCm.Name)
if err != nil {
csuzhangxc marked this conversation as resolved.
Show resolved Hide resolved
return oldCm, true
}
if err := mergeFn(newCm, oldCm); err != nil {
return oldCm, true
}
return oldCm, false
}

func (w *typedWrapper) CheckAndUpdateConfigMap(cmLister corelisterv1.ConfigMapLister, controller client.Object, cm *corev1.ConfigMap) (*corev1.ConfigMap, error) {
mergeFn := func(existing, desired client.Object) error {
existingCm := existing.(*corev1.ConfigMap)
desiredCm := desired.(*corev1.ConfigMap)

Expand All @@ -253,7 +266,12 @@ func (w *typedWrapper) CreateOrUpdateConfigMap(controller client.Object, cm *cor
existingCm.Annotations[k] = v
}
return nil
}, true)
}

if cf, ok := NeedCreateOrUpgradeConfigMap(cmLister, cm, mergeFn); !ok {
return cf, nil
}
result, err := w.GenericControlInterface.CreateOrUpdate(controller, cm, mergeFn, true)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/dm_master_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func (m *masterMemberManager) syncMasterConfigMap(dc *v1alpha1.DMCluster, set *a
if err != nil {
return nil, err
}
return m.deps.TypedControl.CreateOrUpdateConfigMap(dc, newCm)
return m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, dc, newCm)
}

func (m *masterMemberManager) getNewMasterServiceForDMCluster(dc *v1alpha1.DMCluster) *corev1.Service {
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/dm_worker_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (m *workerMemberManager) syncWorkerConfigMap(dc *v1alpha1.DMCluster, set *a
if err != nil {
return nil, err
}
return m.deps.TypedControl.CreateOrUpdateConfigMap(dc, newCm)
return m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, dc, newCm)
}

func getNewWorkerSetForDMCluster(dc *v1alpha1.DMCluster, cm *corev1.ConfigMap) (*apps.StatefulSet, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func (m *pdMemberManager) syncPDConfigMap(tc *v1alpha1.TidbCluster, set *apps.St
if err != nil {
return nil, err
}
return m.deps.TypedControl.CreateOrUpdateConfigMap(tc, newCm)
return m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, tc, newCm)
}

func (m *pdMemberManager) getNewPDServiceForTidbCluster(tc *v1alpha1.TidbCluster) *corev1.Service {
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/pump_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (m *pumpMemberManager) syncConfigMap(tc *v1alpha1.TidbCluster, set *appsv1.
if err != nil {
return nil, err
}
return m.deps.TypedControl.CreateOrUpdateConfigMap(tc, newCm)
return m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, tc, newCm)
}

func getNewPumpHeadlessService(tc *v1alpha1.TidbCluster) *corev1.Service {
Expand Down
3 changes: 2 additions & 1 deletion pkg/manager/member/ticdc_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func (m *ticdcMemberManager) syncTiCDCConfigMap(tc *v1alpha1.TidbCluster, set *a
if err != nil {
return nil, err
}
return m.deps.TypedControl.CreateOrUpdateConfigMap(tc, newCm)
return m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, tc, newCm)

}

// Sync fulfills the manager.Manager interface
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func (m *tidbMemberManager) syncTiDBConfigMap(tc *v1alpha1.TidbCluster, set *app
if err != nil {
return nil, err
}
return m.deps.TypedControl.CreateOrUpdateConfigMap(tc, newCm)
return m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, tc, newCm)
}

func getTiDBConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/tiflash_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (m *tiflashMemberManager) syncConfigMap(tc *v1alpha1.TidbCluster, set *apps
if err != nil {
return nil, err
}
return m.deps.TypedControl.CreateOrUpdateConfigMap(tc, newCm)
return m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, tc, newCm)
}

func getNewHeadlessService(tc *v1alpha1.TidbCluster) *corev1.Service {
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/tikv_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (m *tikvMemberManager) syncTiKVConfigMap(tc *v1alpha1.TidbCluster, set *app
if err != nil {
return nil, err
}
return m.deps.TypedControl.CreateOrUpdateConfigMap(tc, newCm)
july2993 marked this conversation as resolved.
Show resolved Hide resolved
return m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, tc, newCm)
}

func getNewServiceForTidbCluster(tc *v1alpha1.TidbCluster, svcConfig SvcConfig) *corev1.Service {
Expand Down
9 changes: 5 additions & 4 deletions pkg/monitor/monitor/monitor_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,17 @@ func (m *MonitorManager) syncTidbMonitorConfig(monitor *v1alpha1.TidbMonitor) er
promCM.Data["prometheus.yml"] = externalContent
}
}
_, err = m.deps.TypedControl.CreateOrUpdateConfigMap(monitor, promCM)
_, err = m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, monitor, promCM)
if err != nil {
klog.Errorf("Fail to CreateOrUpdateConfigMap %s for tm[%s/%s]'s, err: %v", promCM.Name, monitor.Namespace, monitor.Name, err)
klog.Errorf("Fail to CheckAndUpdateConfigMap %s for tm[%s/%s]'s, err: %v", promCM.Name, monitor.Namespace, monitor.Name, err)
return err
}

if monitor.Spec.Grafana != nil {
grafanaCM := getGrafanaConfigMap(monitor)
_, err = m.deps.TypedControl.CreateOrUpdateConfigMap(monitor, grafanaCM)
_, err = m.deps.TypedControl.CheckAndUpdateConfigMap(m.deps.ConfigMapLister, monitor, grafanaCM)
if err != nil {
klog.Errorf("Fail to CreateOrUpdateConfigMap %s for tm[%s/%s]'s, err: %v", grafanaCM.Name, monitor.Namespace, monitor.Name, err)
klog.Errorf("Fail to CheckAndUpdateConfigMap %s for tm[%s/%s]'s, err: %v", grafanaCM.Name, monitor.Namespace, monitor.Name, err)
return err
}
}
Expand Down