|
| 1 | +/* |
| 2 | + * Tencent is pleased to support the open source community by making Blueking Container Service available. |
| 3 | + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | + * Licensed under the MIT License (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * http://opensource.org/licenses/MIT |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 9 | + * either express or implied. See the License for the specific language governing permissions and |
| 10 | + * limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +package multicluster |
| 14 | + |
| 15 | +import ( |
| 16 | + "fmt" |
| 17 | + |
| 18 | + "github.com/Tencent/bk-bcs/bcs-common/pkg/odm/operator" |
| 19 | + "github.com/samber/lo" |
| 20 | + |
| 21 | + "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/resource/constants" |
| 22 | + "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/pkg/util/mapx" |
| 23 | + clusterRes "github.com/Tencent/bk-bcs/bcs-services/cluster-resources/proto/cluster-resources" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + // ConName 资源名称 |
| 28 | + ConName string = "data.metadata.name" |
| 29 | + // ConNamespace 资源命名空间 |
| 30 | + ConNamespace string = "data.metadata.namespace" |
| 31 | + // ConAge 资源创建时间 |
| 32 | + ConAge string = "data.metadata.creationTimestamp" |
| 33 | + // ConLabels 资源标签 |
| 34 | + ConLabels string = "data.metadata.labels" |
| 35 | + // ConAnnotations 资源注解 |
| 36 | + ConAnnotations string = "data.metadata.annotations" |
| 37 | +) |
| 38 | + |
| 39 | +// creatorToCondition creator condition |
| 40 | +func (q *StorageQuery) creatorToCondition() []*operator.Condition { |
| 41 | + conditions := []*operator.Condition{} |
| 42 | + if len(q.QueryFilter.Creator) == 0 && len(q.ViewFilter.Creator) == 0 { |
| 43 | + return conditions |
| 44 | + } |
| 45 | + // 无创建者的情况比较特殊 |
| 46 | + viewCreator := q.ViewFilter.Creator |
| 47 | + queryCreator := q.QueryFilter.Creator |
| 48 | + if lo.Contains(viewCreator, EmptyCreator) { |
| 49 | + viewCreator = []string{EmptyCreator} |
| 50 | + } |
| 51 | + if lo.Contains(queryCreator, EmptyCreator) { |
| 52 | + queryCreator = []string{EmptyCreator} |
| 53 | + } |
| 54 | + // creator 过滤条件 |
| 55 | + var creator []string |
| 56 | + switch { |
| 57 | + case len(viewCreator) == 0: |
| 58 | + creator = queryCreator |
| 59 | + case len(queryCreator) == 0: |
| 60 | + creator = viewCreator |
| 61 | + default: |
| 62 | + // 返回交集创建人 |
| 63 | + creator = lo.Intersect(viewCreator, queryCreator) |
| 64 | + } |
| 65 | + |
| 66 | + if lo.Contains(creator, EmptyCreator) { |
| 67 | + // 使用全角符号代替 '.',区分字段分隔,无创建者的资源,creator字段为 null |
| 68 | + conditions = append(conditions, operator.NewLeafCondition( |
| 69 | + operator.Eq, map[string]interface{}{ |
| 70 | + ConAnnotations + "." + mapx.ConvertPath(LabelCreator): nil})) |
| 71 | + } else { |
| 72 | + conditions = append(conditions, operator.NewLeafCondition( |
| 73 | + operator.In, map[string]interface{}{ |
| 74 | + ConAnnotations + "." + mapx.ConvertPath(LabelCreator): creator})) |
| 75 | + } |
| 76 | + return conditions |
| 77 | +} |
| 78 | + |
| 79 | +// nameToCondition name condition |
| 80 | +func (q *StorageQuery) nameToCondition() []*operator.Condition { |
| 81 | + conditions := []*operator.Condition{} |
| 82 | + // creator 过滤条件 |
| 83 | + if len(q.QueryFilter.Name) != 0 && len(q.ViewFilter.Name) != 0 { |
| 84 | + con := operator.NewBranchCondition( |
| 85 | + operator.And, |
| 86 | + operator.NewLeafCondition(operator.Con, map[string]string{ConName: q.QueryFilter.Name}), |
| 87 | + operator.NewLeafCondition(operator.Con, map[string]string{ConName: q.ViewFilter.Name}), |
| 88 | + ) |
| 89 | + conditions = append(conditions, con) |
| 90 | + return conditions |
| 91 | + } |
| 92 | + if len(q.ViewFilter.Name) != 0 { |
| 93 | + conditions = append(conditions, operator.NewLeafCondition( |
| 94 | + operator.Con, map[string]string{ConName: q.ViewFilter.Name})) |
| 95 | + return conditions |
| 96 | + } |
| 97 | + if len(q.QueryFilter.Name) != 0 { |
| 98 | + conditions = append(conditions, operator.NewLeafCondition( |
| 99 | + operator.Con, map[string]string{ConName: q.QueryFilter.Name})) |
| 100 | + return conditions |
| 101 | + } |
| 102 | + return conditions |
| 103 | +} |
| 104 | + |
| 105 | +// labelToCondition label condition |
| 106 | +func (q *StorageQuery) labelToCondition() []*operator.Condition { |
| 107 | + conditions := []*operator.Condition{} |
| 108 | + // nolint:gocritic |
| 109 | + labelSelector := append(q.ViewFilter.LabelSelector, q.QueryFilter.LabelSelector...) |
| 110 | + // labelSelector 过滤条件 |
| 111 | + for _, v := range labelSelector { |
| 112 | + if len(v.Values) == 0 && v.Op != OpExists && v.Op != OpDoesNotExist { |
| 113 | + continue |
| 114 | + } |
| 115 | + switch v.Op { |
| 116 | + case OpEQ: |
| 117 | + conditions = append(conditions, operator.NewLeafCondition( |
| 118 | + operator.Eq, map[string]interface{}{ |
| 119 | + fmt.Sprintf(ConLabels+".%s", mapx.ConvertPath(v.Key)): v.Values[0]})) |
| 120 | + case OpNotEQ: |
| 121 | + conditions = append(conditions, operator.NewLeafCondition( |
| 122 | + operator.Not, map[string]interface{}{ |
| 123 | + fmt.Sprintf(ConLabels+".%s", mapx.ConvertPath(v.Key)): v.Values[0]})) |
| 124 | + case OpIn: |
| 125 | + conditions = append(conditions, operator.NewLeafCondition( |
| 126 | + operator.In, map[string]interface{}{ |
| 127 | + fmt.Sprintf(ConLabels+".%s", mapx.ConvertPath(v.Key)): v.Values})) |
| 128 | + case OpNotIn: |
| 129 | + conditions = append(conditions, operator.NewLeafCondition( |
| 130 | + operator.Nin, map[string]interface{}{ |
| 131 | + fmt.Sprintf(ConLabels+".%s", mapx.ConvertPath(v.Key)): v.Values})) |
| 132 | + case OpExists: |
| 133 | + conditions = append(conditions, operator.NewLeafCondition( |
| 134 | + operator.Ext, map[string]interface{}{ |
| 135 | + fmt.Sprintf(ConLabels+".%s", mapx.ConvertPath(v.Key)): ""})) |
| 136 | + case OpDoesNotExist: |
| 137 | + conditions = append(conditions, operator.NewLeafCondition( |
| 138 | + operator.Eq, map[string]interface{}{ |
| 139 | + fmt.Sprintf(ConLabels+".%s", mapx.ConvertPath(v.Key)): nil})) |
| 140 | + } |
| 141 | + } |
| 142 | + return conditions |
| 143 | +} |
| 144 | + |
| 145 | +// createSourceToCondition create source condition |
| 146 | +func (q *StorageQuery) createSourceToCondition() []*operator.Condition { |
| 147 | + conditions := []*operator.Condition{} |
| 148 | + if q.ViewFilter.CreateSource == nil && q.QueryFilter.CreateSource == nil { |
| 149 | + return conditions |
| 150 | + } |
| 151 | + |
| 152 | + var createSources []*clusterRes.CreateSource |
| 153 | + |
| 154 | + if q.ViewFilter.CreateSource != nil { |
| 155 | + createSources = append(createSources, q.ViewFilter.CreateSource) |
| 156 | + } |
| 157 | + if q.QueryFilter.CreateSource != nil { |
| 158 | + createSources = append(createSources, q.QueryFilter.CreateSource) |
| 159 | + } |
| 160 | + |
| 161 | + // 创建来源source筛选 |
| 162 | + templateCon := operator.NewBranchCondition( |
| 163 | + operator.Or, |
| 164 | + operator.NewLeafCondition( |
| 165 | + operator.Eq, map[string]interface{}{fmt.Sprintf(ConAnnotations+".%s", |
| 166 | + mapx.ConvertPath(constants.TemplateSourceType)): constants.TemplateSourceTypeValue}), |
| 167 | + operator.NewLeafCondition( |
| 168 | + operator.Eq, map[string]interface{}{fmt.Sprintf(ConLabels+".%s", |
| 169 | + mapx.ConvertPath(constants.TemplateSourceType)): constants.TemplateSourceTypeValue}), |
| 170 | + ) |
| 171 | + helmCon := operator.NewLeafCondition( |
| 172 | + operator.Eq, map[string]interface{}{fmt.Sprintf(ConLabels+".%s", |
| 173 | + mapx.ConvertPath(constants.HelmSourceType)): constants.HelmCreateSource}) |
| 174 | + |
| 175 | + // 要排除掉helm和template的情况 |
| 176 | + webCon := operator.NewBranchCondition( |
| 177 | + operator.And, |
| 178 | + operator.NewBranchCondition( |
| 179 | + operator.Nor, |
| 180 | + templateCon, |
| 181 | + helmCon, |
| 182 | + ), |
| 183 | + operator.NewBranchCondition( |
| 184 | + operator.Or, |
| 185 | + operator.NewLeafCondition(operator.Ne, map[string]interface{}{ |
| 186 | + fmt.Sprintf(ConAnnotations+".%s", mapx.ConvertPath(constants.CreatorAnnoKey)): nil}), |
| 187 | + operator.NewLeafCondition(operator.Ne, map[string]interface{}{ |
| 188 | + fmt.Sprintf(ConLabels+".%s", mapx.ConvertPath(constants.UpdaterAnnoKey)): nil})), |
| 189 | + ) |
| 190 | + for _, cs := range createSources { |
| 191 | + switch cs.Source { |
| 192 | + case constants.TemplateCreateSource: |
| 193 | + // 筛选模板名称和模板版本 |
| 194 | + var temNameVersionCon *operator.Condition |
| 195 | + if cs.Template != nil { |
| 196 | + var temNameCon []*operator.Condition |
| 197 | + if cs.Template.TemplateName != "" { |
| 198 | + temNameCon = append(temNameCon, operator.NewLeafCondition(operator.Con, map[string]interface{}{ |
| 199 | + fmt.Sprintf(ConAnnotations+".%s", |
| 200 | + mapx.ConvertPath(constants.TemplateNameAnnoKey)): cs.Template.TemplateName})) |
| 201 | + |
| 202 | + } |
| 203 | + if cs.Template.TemplateVersion != "" { |
| 204 | + temNameCon = append(temNameCon, operator.NewLeafCondition(operator.Con, map[string]interface{}{ |
| 205 | + fmt.Sprintf(ConAnnotations+".%s", |
| 206 | + mapx.ConvertPath(constants.TemplateVersionAnnoKey)): cs.Template.TemplateVersion})) |
| 207 | + } |
| 208 | + temNameCon = append(temNameCon, templateCon) |
| 209 | + temNameVersionCon = operator.NewBranchCondition(operator.And, temNameCon...) |
| 210 | + } else { |
| 211 | + temNameVersionCon = templateCon |
| 212 | + } |
| 213 | + conditions = append(conditions, temNameVersionCon) |
| 214 | + case constants.HelmCreateSource: |
| 215 | + var helmChartCon *operator.Condition |
| 216 | + if cs.Chart != nil && cs.Chart.ChartName != "" { |
| 217 | + chartCon := operator.NewLeafCondition(operator.Con, map[string]interface{}{ |
| 218 | + fmt.Sprintf(ConLabels+".%s", |
| 219 | + mapx.ConvertPath(constants.HelmChartAnnoKey)): cs.Chart.ChartName}) |
| 220 | + helmChartCon = operator.NewBranchCondition(operator.And, helmCon, chartCon) |
| 221 | + } else { |
| 222 | + helmChartCon = helmCon |
| 223 | + } |
| 224 | + conditions = append(conditions, helmChartCon) |
| 225 | + case constants.WebCreateSource: |
| 226 | + conditions = append(conditions, webCon) |
| 227 | + case constants.ClientCreateSource: |
| 228 | + conditions = append(conditions, operator.NewBranchCondition( |
| 229 | + operator.Nor, templateCon, helmCon, webCon, |
| 230 | + )) |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + return conditions |
| 235 | +} |
| 236 | + |
| 237 | +// ipToCondition ip condition |
| 238 | +func (q *StorageQuery) ipToCondition() []*operator.Condition { |
| 239 | + conditions := []*operator.Condition{} |
| 240 | + if q.QueryFilter.IP == "" { |
| 241 | + return conditions |
| 242 | + } |
| 243 | + // ip 过滤条件 |
| 244 | + con := operator.NewBranchCondition( |
| 245 | + operator.Or, |
| 246 | + operator.NewLeafCondition(operator.Con, map[string]string{"data.spec.clusterIP": q.QueryFilter.IP}), |
| 247 | + operator.NewLeafCondition(operator.Eq, |
| 248 | + operator.M{"data.spec.clusterIPs": operator.M{"$elemMatch": operator.M{"$regex": q.QueryFilter.IP}}}), |
| 249 | + operator.NewLeafCondition(operator.Con, map[string]string{"data.status.hostIP": q.QueryFilter.IP}), |
| 250 | + operator.NewLeafCondition(operator.Eq, operator.M{"data.status.podIPs": operator.M{"$elemMatch": operator. |
| 251 | + M{"ip": operator.M{"$regex": q.QueryFilter.IP}}}}), |
| 252 | + ) |
| 253 | + conditions = append(conditions, con) |
| 254 | + return conditions |
| 255 | +} |
0 commit comments