Skip to content

Commit

Permalink
optimize workload selector logic (#3360)
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenShen authored Feb 28, 2024
1 parent 240b1d9 commit aa85ec7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/microservice/aslan/core/common/service/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ type Workload struct {
ProductName string `json:"product_name"`
Replicas int32 `json:"-"`
Spec corev1.PodTemplateSpec `json:"-"`
Selector *metav1.LabelSelector `json:"-"`
Images []string `json:"-"`
Ready bool `json:"ready"`
Annotation map[string]string `json:"-"`
Expand Down Expand Up @@ -705,6 +706,7 @@ func ListWorkloads(envName, productName string, perPage, page int, informer info
workLoads = append(workLoads, &Workload{
Name: v.Name,
Spec: v.Spec.Template,
Selector: v.Spec.Selector,
Type: setting.Deployment,
Replicas: *v.Spec.Replicas,
Images: wrapper.Deployment(v).ImageInfos(),
Expand All @@ -720,6 +722,7 @@ func ListWorkloads(envName, productName string, perPage, page int, informer info
workLoads = append(workLoads, &Workload{
Name: v.Name,
Spec: v.Spec.Template,
Selector: v.Spec.Selector,
Type: setting.StatefulSet,
Replicas: *v.Spec.Replicas,
Images: wrapper.StatefulSet(v).ImageInfos(),
Expand Down Expand Up @@ -877,7 +880,7 @@ func ListWorkloadDetails(envName, clusterID, namespace, productName string, perP
}

if workload.Type == setting.Deployment || workload.Type == setting.StatefulSet {
selector := labels.SelectorFromSet(workload.Spec.Labels)
selector := labels.SelectorFromSet(workload.Selector.MatchLabels)
// Note: In some scenarios, such as environment sharing, there may be more containers in Pod than workload.
// We call GetSelectedPodsInfo to get the status and readiness to keep same logic with k8s projects
productRespInfo.Status, productRespInfo.Ready, productRespInfo.Images = kube.GetSelectedPodsInfo(selector, informer, workload.Images, log)
Expand All @@ -901,7 +904,7 @@ func FindServiceFromIngress(hostInfos []resource.HostInfo, currentWorkload *Work
return []resource.HostInfo{}
}
serviceName := ""
podLabels := labels.Set(currentWorkload.Spec.Labels)
podLabels := labels.Set(currentWorkload.Selector.MatchLabels)
for _, svc := range allServices {
if len(svc.Spec.Selector) == 0 {
continue
Expand Down
2 changes: 2 additions & 0 deletions pkg/microservice/aslan/core/common/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,7 @@ func ToDeploymentWorkload(v *appsv1.Deployment) *Workload {
workload := &Workload{
Name: v.Name,
Spec: v.Spec.Template,
Selector: v.Spec.Selector,
Type: setting.Deployment,
Images: wrapper.Deployment(v).ImageInfos(),
Ready: wrapper.Deployment(v).Ready(),
Expand All @@ -1545,6 +1546,7 @@ func toStsWorkload(v *appsv1.StatefulSet) *Workload {
workload := &Workload{
Name: v.Name,
Spec: v.Spec.Template,
Selector: v.Spec.Selector,
Type: setting.StatefulSet,
Images: wrapper.StatefulSet(v).ImageInfos(),
Ready: wrapper.StatefulSet(v).Ready(),
Expand Down
2 changes: 2 additions & 0 deletions pkg/microservice/aslan/core/environment/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ func GetServiceWorkloads(svcTmpl *commonmodels.Service, env *commonmodels.Produc
ret = append(ret, &commonservice.Workload{
Name: wd.Name,
Spec: wd.Spec.Template,
Selector: wd.Spec.Selector,
Type: setting.Deployment,
Images: wd.ImageInfos(),
Ready: wd.Ready(),
Expand All @@ -282,6 +283,7 @@ func GetServiceWorkloads(svcTmpl *commonmodels.Service, env *commonmodels.Produc
ret = append(ret, &commonservice.Workload{
Name: ws.Name,
Spec: ws.Spec.Template,
Selector: ws.Spec.Selector,
Type: setting.StatefulSet,
Images: ws.ImageInfos(),
Ready: ws.Ready(),
Expand Down

0 comments on commit aa85ec7

Please sign in to comment.