type DeploymentMap struct { Data *sync.Map // key:namespace value:[]*v1.Deployments }
// 添加 func(this *DeploymentMap) Add(deployment *v1.Deployment) { if depList, ok := this.Data.Load(deployment.Namespace); ok { depList = append(depList.([]*v1.Deployment), deployment) this.Data.Store(deployment.Namespace, depList) } else { this.Data.Store(deployment.Namespace, []*v1.Deployment{deployment}) } }
// 获取列表 func(this *DeploymentMap) ListByNs(namespace string) ([]*v1.Deployment, error) { if depList, ok := this.Data.Load(namespace); ok { return depList.([]*v1.Deployment), nil }
returnnil, fmt.Errorf("record not found") }
// 更新 func(this *DeploymentMap) Update(deployment *v1.Deployment) error { if depList, ok := this.Data.Load(deployment.Namespace); ok { depList := depList.([]*v1.Deployment) for i, dep := range depList { if dep.Name == deployment.Name { depList[i] = deployment break } } returnnil }
return fmt.Errorf("deployment [%s] not found", deployment.Name) }
// 删除 func(this *DeploymentMap) Delete(deployment *v1.Deployment) { if depList, ok := this.Data.Load(deployment.Namespace); ok { depList := depList.([]*v1.Deployment) for i, dep := range depList { if dep.Name == deployment.Name { newDepList := append(depList[:i], depList[i+1:]...) this.Data.Store(deployment.Namespace, newDepList) break } } } }