Skip to content

Commit

Permalink
Fixed potential nil pointer issues (#4054) (#4058)
Browse files Browse the repository at this point in the history
* cherry pick #4054 to release-1.1
  • Loading branch information
ti-srebot authored Jul 2, 2021
1 parent 6f4b099 commit 8f4cd5e
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 24 deletions.
135 changes: 129 additions & 6 deletions pkg/apis/pingcap/v1alpha1/tidbcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ var (
defaultHelperSpec = HelperSpec{}
)

// PDImage return the image used by PD.
//
// If PD isn't specified, return empty string.
func (tc *TidbCluster) PDImage() string {
if tc.Spec.PD == nil {
return ""
}

image := tc.Spec.PD.Image
baseImage := tc.Spec.PD.BaseImage
// base image takes higher priority
Expand All @@ -66,7 +73,14 @@ func (tc *TidbCluster) PDImage() string {
return image
}

// PDVersion return the image version used by PD.
//
// If PD isn't specified, return empty string.
func (tc *TidbCluster) PDVersion() string {
if tc.Spec.PD == nil {
return ""
}

image := tc.PDImage()
colonIdx := strings.LastIndexByte(image, ':')
if colonIdx >= 0 {
Expand All @@ -76,7 +90,14 @@ func (tc *TidbCluster) PDVersion() string {
return "latest"
}

// TiKVImage return the image used by TiKV.
//
// If TiKV isn't specified, return empty string.
func (tc *TidbCluster) TiKVImage() string {
if tc.Spec.TiKV == nil {
return ""
}

image := tc.Spec.TiKV.Image
baseImage := tc.Spec.TiKV.BaseImage
// base image takes higher priority
Expand All @@ -95,15 +116,15 @@ func (tc *TidbCluster) TiKVImage() string {
}

func (tc *TidbCluster) TiKVContainerPrivilege() *bool {
if tc.Spec.TiKV.Privileged == nil {
if tc.Spec.TiKV == nil || tc.Spec.TiKV.Privileged == nil {
pri := false
return &pri
}
return tc.Spec.TiKV.Privileged
}

func (tc *TidbCluster) TiKVEvictLeaderTimeout() time.Duration {
if tc.Spec.TiKV.EvictLeaderTimeout != nil {
if tc.Spec.TiKV != nil && tc.Spec.TiKV.EvictLeaderTimeout != nil {
d, err := time.ParseDuration(*tc.Spec.TiKV.EvictLeaderTimeout)
if err == nil {
return d
Expand All @@ -112,7 +133,14 @@ func (tc *TidbCluster) TiKVEvictLeaderTimeout() time.Duration {
return defaultEvictLeaderTimeout
}

// TiFlashImage return the image used by TiFlash.
//
// If TiFlash isn't specified, return empty string.
func (tc *TidbCluster) TiFlashImage() string {
if tc.Spec.TiFlash == nil {
return ""
}

image := tc.Spec.TiFlash.Image
baseImage := tc.Spec.TiFlash.BaseImage
// base image takes higher priority
Expand All @@ -130,7 +158,14 @@ func (tc *TidbCluster) TiFlashImage() string {
return image
}

// TiCDCImage return the image used by TiCDC.
//
// If TiCDC isn't specified, return empty string.
func (tc *TidbCluster) TiCDCImage() string {
if tc.Spec.TiCDC == nil {
return ""
}

image := tc.Spec.TiCDC.Image
baseImage := tc.Spec.TiCDC.BaseImage
// base image takes higher priority
Expand All @@ -149,14 +184,21 @@ func (tc *TidbCluster) TiCDCImage() string {
}

func (tc *TidbCluster) TiFlashContainerPrivilege() *bool {
if tc.Spec.TiFlash.Privileged == nil {
if tc.Spec.TiFlash == nil || tc.Spec.TiFlash.Privileged == nil {
pri := false
return &pri
}
return tc.Spec.TiFlash.Privileged
}

// TiDBImage return the image used by TiDB.
//
// If TiDB isn't specified, return empty string.
func (tc *TidbCluster) TiDBImage() string {
if tc.Spec.TiDB == nil {
return ""
}

image := tc.Spec.TiDB.Image
baseImage := tc.Spec.TiDB.BaseImage
// base image takes higher priority
Expand All @@ -174,10 +216,14 @@ func (tc *TidbCluster) TiDBImage() string {
return image
}

// PumpImage return the image used by Pump.
//
// If Pump isn't specified, return nil.
func (tc *TidbCluster) PumpImage() *string {
if tc.Spec.Pump == nil {
return nil
}

image := tc.Spec.Pump.Image
baseImage := tc.Spec.Pump.BaseImage
// base image takes higher priority
Expand All @@ -197,7 +243,7 @@ func (tc *TidbCluster) PumpImage() *string {

func (tc *TidbCluster) HelperImage() string {
image := tc.GetHelperSpec().Image
if image == nil {
if image == nil && tc.Spec.TiDB != nil {
// for backward compatibility
image = tc.Spec.TiDB.GetSlowLogTailerSpec().Image
}
Expand All @@ -209,7 +255,7 @@ func (tc *TidbCluster) HelperImage() string {

func (tc *TidbCluster) HelperImagePullPolicy() corev1.PullPolicy {
pp := tc.GetHelperSpec().ImagePullPolicy
if pp == nil {
if pp == nil && tc.Spec.TiDB != nil {
// for backward compatibility
pp = tc.Spec.TiDB.GetSlowLogTailerSpec().ImagePullPolicy
}
Expand Down Expand Up @@ -297,11 +343,24 @@ func (tc *TidbCluster) getDeleteSlots(component string) (deleteSlots sets.Int32)
return
}

// PDAllPodsStarted return whether all pods of PD are started.
//
// If PD isn't specified, return false.
func (tc *TidbCluster) PDAllPodsStarted() bool {
if tc.Spec.PD == nil {
return false
}
return tc.PDStsDesiredReplicas() == tc.PDStsActualReplicas()
}

// PDAllMembersReady return whether all members of PD are ready.
//
// If PD isn't specified, return false.
func (tc *TidbCluster) PDAllMembersReady() bool {
if tc.Spec.PD == nil {
return false
}

if int(tc.PDStsDesiredReplicas()) != len(tc.Status.PD.Members) {
return false
}
Expand Down Expand Up @@ -338,6 +397,9 @@ func (tc *TidbCluster) GetPDFailureReplicas() int32 {
}

func (tc *TidbCluster) PDStsDesiredReplicas() int32 {
if tc.Spec.PD == nil {
return 0
}
return tc.Spec.PD.Replicas + tc.GetPDFailureReplicas()
}

Expand All @@ -350,18 +412,34 @@ func (tc *TidbCluster) PDStsActualReplicas() int32 {
}

func (tc *TidbCluster) PDStsDesiredOrdinals(excludeFailover bool) sets.Int32 {
if tc.Spec.PD == nil {
return sets.Int32{}
}
replicas := tc.Spec.PD.Replicas
if !excludeFailover {
replicas = tc.PDStsDesiredReplicas()
}
return helper.GetPodOrdinalsFromReplicasAndDeleteSlots(replicas, tc.getDeleteSlots(label.PDLabelVal))
}

// TiKVAllPodsStarted return whether all pods of TiKV are started.
//
// If TiKV isn't specified, return false.
func (tc *TidbCluster) TiKVAllPodsStarted() bool {
if tc.Spec.TiKV == nil {
return false
}
return tc.TiKVStsDesiredReplicas() == tc.TiKVStsActualReplicas()
}

// TiKVAllStoresReady return whether all stores of TiKV are ready.
//
// If TiKV isn't specified, return false.
func (tc *TidbCluster) TiKVAllStoresReady() bool {
if tc.Spec.TiKV == nil {
return false
}

if int(tc.TiKVStsDesiredReplicas()) != len(tc.Status.TiKV.Stores) {
return false
}
Expand All @@ -376,6 +454,9 @@ func (tc *TidbCluster) TiKVAllStoresReady() bool {
}

func (tc *TidbCluster) TiKVStsDesiredReplicas() int32 {
if tc.Spec.TiKV == nil {
return 0
}
return tc.Spec.TiKV.Replicas + int32(len(tc.Status.TiKV.FailureStores))
}

Expand All @@ -388,18 +469,34 @@ func (tc *TidbCluster) TiKVStsActualReplicas() int32 {
}

func (tc *TidbCluster) TiKVStsDesiredOrdinals(excludeFailover bool) sets.Int32 {
if tc.Spec.TiKV == nil {
return sets.Int32{}
}
replicas := tc.Spec.TiKV.Replicas
if !excludeFailover {
replicas = tc.TiKVStsDesiredReplicas()
}
return helper.GetPodOrdinalsFromReplicasAndDeleteSlots(replicas, tc.getDeleteSlots(label.TiKVLabelVal))
}

// TiFlashAllPodsStarted return whether all pods of TiFlash are started.
//
// If TiFlash isn't specified, return false.
func (tc *TidbCluster) TiFlashAllPodsStarted() bool {
if tc.Spec.TiFlash == nil {
return false
}
return tc.TiFlashStsDesiredReplicas() == tc.TiFlashStsActualReplicas()
}

// TiFlashAllPodsStarted return whether all stores of TiFlash are ready.
//
// If TiFlash isn't specified, return false.
func (tc *TidbCluster) TiFlashAllStoresReady() bool {
if tc.Spec.TiFlash == nil {
return false
}

if int(tc.TiFlashStsDesiredReplicas()) != len(tc.Status.TiFlash.Stores) {
return false
}
Expand Down Expand Up @@ -447,11 +544,24 @@ func (tc *TidbCluster) TiFlashStsDesiredOrdinals(excludeFailover bool) sets.Int3
return helper.GetPodOrdinalsFromReplicasAndDeleteSlots(replicas, tc.getDeleteSlots(label.TiFlashLabelVal))
}

// TiDBAllPodsStarted return whether all pods of TiDB are started.
//
// If TiDB isn't specified, return false.
func (tc *TidbCluster) TiDBAllPodsStarted() bool {
if tc.Spec.TiDB == nil {
return false
}
return tc.TiDBStsDesiredReplicas() == tc.TiDBStsActualReplicas()
}

// TiDBAllMembersReady return whether all members of TiDB are ready.
//
// If TiDB isn't specified, return false.
func (tc *TidbCluster) TiDBAllMembersReady() bool {
if tc.Spec.TiDB == nil {
return false
}

if int(tc.TiDBStsDesiredReplicas()) != len(tc.Status.TiDB.Members) {
return false
}
Expand All @@ -466,6 +576,9 @@ func (tc *TidbCluster) TiDBAllMembersReady() bool {
}

func (tc *TidbCluster) TiDBStsDesiredReplicas() int32 {
if tc.Spec.TiDB == nil {
return 0
}
return tc.Spec.TiDB.Replicas + int32(len(tc.Status.TiDB.FailureMembers))
}

Expand All @@ -478,13 +591,19 @@ func (tc *TidbCluster) TiDBStsActualReplicas() int32 {
}

func (tc *TidbCluster) TiDBStsDesiredOrdinals(excludeFailover bool) sets.Int32 {
if tc.Spec.TiDB == nil {
return sets.Int32{}
}
replicas := tc.Spec.TiDB.Replicas
if !excludeFailover {
replicas = tc.TiDBStsDesiredReplicas()
}
return helper.GetPodOrdinalsFromReplicasAndDeleteSlots(replicas, tc.getDeleteSlots(label.TiDBLabelVal))
}

// PDIsAvailable return whether PD is available.
//
// If PD isn't specified, return true.
func (tc *TidbCluster) PDIsAvailable() bool {
if tc.Spec.PD == nil {
return true
Expand Down Expand Up @@ -577,7 +696,11 @@ func (tc *TidbCluster) IsPVReclaimEnabled() bool {
}

func (tc *TidbCluster) IsTiDBBinlogEnabled() bool {
binlogEnabled := tc.Spec.TiDB.BinlogEnabled
var binlogEnabled *bool
if tc.Spec.TiDB != nil {
binlogEnabled = tc.Spec.TiDB.BinlogEnabled
}

if binlogEnabled == nil {
isPumpCreated := tc.Spec.Pump != nil
return isPumpCreated
Expand Down
Loading

0 comments on commit 8f4cd5e

Please sign in to comment.