Skip to content

Commit

Permalink
Merge pull request #1269 from Icarus9913/cherrypick/pr1260/release-v0.3
Browse files Browse the repository at this point in the history
fix potential panic with SpiderSubnet feature application pod selector
  • Loading branch information
weizhoublue authored Jan 11, 2023
2 parents 20fe719 + 941c067 commit 53363b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
17 changes: 9 additions & 8 deletions pkg/subnetmanager/app_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,8 @@ func (c *appController) syncHandler(appKey appWorkQueueKey, log *zap.Logger) (er

var app metav1.Object
var subnetConfig *controllers.PodSubnetAnnoConfig
var podAnno, podSelector map[string]string
var podAnno map[string]string
var podSelector *metav1.LabelSelector
var appReplicas int

switch appKey.AppKind {
Expand All @@ -622,7 +623,7 @@ func (c *appController) syncHandler(appKey appWorkQueueKey, log *zap.Logger) (er
}

podAnno = deployment.Spec.Template.Annotations
podSelector = deployment.Spec.Selector.MatchLabels
podSelector = deployment.Spec.Selector
appReplicas = controllers.GetAppReplicas(deployment.Spec.Replicas)
app = deployment.DeepCopy()

Expand All @@ -637,7 +638,7 @@ func (c *appController) syncHandler(appKey appWorkQueueKey, log *zap.Logger) (er
}

podAnno = replicaSet.Spec.Template.Annotations
podSelector = replicaSet.Spec.Selector.MatchLabels
podSelector = replicaSet.Spec.Selector
appReplicas = controllers.GetAppReplicas(replicaSet.Spec.Replicas)
app = replicaSet.DeepCopy()

Expand All @@ -652,7 +653,7 @@ func (c *appController) syncHandler(appKey appWorkQueueKey, log *zap.Logger) (er
}

podAnno = daemonSet.Spec.Template.Annotations
podSelector = daemonSet.Spec.Selector.MatchLabels
podSelector = daemonSet.Spec.Selector
appReplicas = int(daemonSet.Status.DesiredNumberScheduled)
app = daemonSet.DeepCopy()

Expand All @@ -667,7 +668,7 @@ func (c *appController) syncHandler(appKey appWorkQueueKey, log *zap.Logger) (er
}

podAnno = statefulSet.Spec.Template.Annotations
podSelector = statefulSet.Spec.Selector.MatchLabels
podSelector = statefulSet.Spec.Selector
appReplicas = controllers.GetAppReplicas(statefulSet.Spec.Replicas)
app = statefulSet.DeepCopy()

Expand All @@ -682,7 +683,7 @@ func (c *appController) syncHandler(appKey appWorkQueueKey, log *zap.Logger) (er
}

podAnno = job.Spec.Template.Annotations
podSelector = job.Spec.Selector.MatchLabels
podSelector = job.Spec.Selector
appReplicas = controllers.CalculateJobPodNum(job.Spec.Parallelism, job.Spec.Completions)
app = job.DeepCopy()

Expand All @@ -697,7 +698,7 @@ func (c *appController) syncHandler(appKey appWorkQueueKey, log *zap.Logger) (er
}

podAnno = cronJob.Spec.JobTemplate.Spec.Template.Annotations
podSelector = cronJob.Spec.JobTemplate.Spec.Selector.MatchLabels
podSelector = cronJob.Spec.JobTemplate.Spec.Selector
appReplicas = controllers.CalculateJobPodNum(cronJob.Spec.JobTemplate.Spec.Parallelism, cronJob.Spec.JobTemplate.Spec.Completions)
app = cronJob.DeepCopy()

Expand All @@ -721,7 +722,7 @@ func (c *appController) syncHandler(appKey appWorkQueueKey, log *zap.Logger) (er

// createOrMarkIPPool try to create an IPPool or mark IPPool desired IP number with the give SpiderSubnet configuration
func (c *appController) createOrMarkIPPool(ctx context.Context, podSubnetConfig controllers.PodSubnetAnnoConfig, appKind string, app metav1.Object,
podSelector map[string]string, appReplicas int) error {
podSelector *metav1.LabelSelector, appReplicas int) error {
if c.subnetMgr.config.EnableIPv4 && len(podSubnetConfig.SubnetName.IPv4) == 0 {
return fmt.Errorf("IPv4 SpiderSubnet not specified when configuration enableIPv4 is on")
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/subnetmanager/subnet_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ func (sm *subnetManager) GenerateIPsFromSubnetWhenScaleUpIP(ctx context.Context,

// AllocateEmptyIPPool will create an empty IPPool and mark the status.AutoDesiredIPCount
// notice: this function only serves for auto-created IPPool
func (sm *subnetManager) AllocateEmptyIPPool(ctx context.Context, subnetName string, appKind string, app metav1.Object, podSelector map[string]string,
ipNum int, ipVersion types.IPVersion, reclaimIPPool bool) error {
func (sm *subnetManager) AllocateEmptyIPPool(ctx context.Context, subnetName string, appKind string, app metav1.Object,
podSelector *metav1.LabelSelector, ipNum int, ipVersion types.IPVersion, reclaimIPPool bool) error {
if len(subnetName) == 0 {
return fmt.Errorf("%w: spider subnet name must be specified", constant.ErrWrongInput)
}
Expand Down Expand Up @@ -249,13 +249,11 @@ func (sm *subnetManager) AllocateEmptyIPPool(ctx context.Context, subnetName str
Labels: poolLabels,
},
Spec: spiderpoolv1.IPPoolSpec{
Subnet: subnet.Spec.Subnet,
Gateway: subnet.Spec.Gateway,
Vlan: subnet.Spec.Vlan,
Routes: subnet.Spec.Routes,
PodAffinity: &metav1.LabelSelector{
MatchLabels: podSelector,
},
Subnet: subnet.Spec.Subnet,
Gateway: subnet.Spec.Gateway,
Vlan: subnet.Spec.Vlan,
Routes: subnet.Spec.Routes,
PodAffinity: podSelector,
},
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/subnetmanager/types/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type SubnetManager interface {
GetSubnetByName(ctx context.Context, subnetName string) (*spiderpoolv1.SpiderSubnet, error)
ListSubnets(ctx context.Context, opts ...client.ListOption) (*spiderpoolv1.SpiderSubnetList, error)
SetupControllers(client kubernetes.Interface) error
AllocateEmptyIPPool(ctx context.Context, subnetMgrName string, appKind string, app metav1.Object, podSelector map[string]string, ipNum int, ipVersion types.IPVersion, reclaimIPPool bool) error
AllocateEmptyIPPool(ctx context.Context, subnetMgrName string, appKind string, app metav1.Object, podSelector *metav1.LabelSelector, ipNum int, ipVersion types.IPVersion, reclaimIPPool bool) error
CheckScaleIPPool(ctx context.Context, pool *spiderpoolv1.SpiderIPPool, subnetManagerName string, ipNum int) error
GenerateIPsFromSubnetWhenScaleUpIP(ctx context.Context, subnetName string, pool *spiderpoolv1.SpiderIPPool, cursor bool) ([]string, error)
}

0 comments on commit 53363b4

Please sign in to comment.