37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cluster_scoped_gc.go 2.17 KB
一键复制 编辑 原始数据 按行查看 历史
dax 提交于 2019-04-23 16:10 . Add check for cluster scoped finalizers
package clustergc
import (
"context"
"strings"
"github.com/rancher/norman/lifecycle"
"github.com/rancher/norman/resource"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
)
func Register(ctx context.Context, management *config.ManagementContext) {
gc := &gcLifecycle{
mgmt: management,
}
management.Management.Clusters("").AddLifecycle(ctx, "cluster-scoped-gc", gc)
}
type gcLifecycle struct {
mgmt *config.ManagementContext
}
func (c *gcLifecycle) Create(obj *v3.Cluster) (runtime.Object, error) {
return obj, nil
}
func (c *gcLifecycle) Updated(obj *v3.Cluster) (runtime.Object, error) {
return nil, nil
}
func cleanFinalizers(clusterName string, object *unstructured.Unstructured, dynamicClient dynamic.ResourceInterface) (*unstructured.Unstructured, error) {
object = object.DeepCopy()
modified := false
md, err := meta.Accessor(object)
if err != nil {
return object, err
}
finalizers := md.GetFinalizers()
for i := len(finalizers) - 1; i >= 0; i-- {
f := finalizers[i]
if strings.HasPrefix(f, lifecycle.ScopedFinalizerKey) && strings.HasSuffix(f, "_"+clusterName) {
finalizers = append(finalizers[:i], finalizers[i+1:]...)
modified = true
}
}
if modified {
md.SetFinalizers(finalizers)
obj, e := dynamicClient.Update(object, metav1.UpdateOptions{})
return obj, e
}
return object, nil
}
func (c *gcLifecycle) Remove(cluster *v3.Cluster) (runtime.Object, error) {
for key := range resource.Get() {
objList, err := c.mgmt.DynamicClient.Resource(key).List(metav1.ListOptions{})
if err != nil {
if errors.IsNotFound(err) {
// skip this iteration, no objects were initialized of this type for the cluster
continue
}
return cluster, err
}
for _, obj := range objList.Items {
_, err = cleanFinalizers(cluster.Name, &obj, c.mgmt.DynamicClient.Resource(key).Namespace(obj.GetNamespace()))
if err != nil {
return cluster, err
}
}
}
return nil, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.4-rc13

搜索帮助