|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + v1 "k8s.io/api/core/v1" |
| 11 | + "k8s.io/apimachinery/pkg/types" |
| 12 | + "k8s.io/apimachinery/pkg/util/sets" |
| 13 | + "k8s.io/apimachinery/pkg/util/wait" |
| 14 | + "k8s.io/client-go/util/retry" |
| 15 | + "k8s.io/klog/v2" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + cmName = "test-resource-recorder" |
| 21 | +) |
| 22 | + |
| 23 | +func initConfigMap(c client.Client) *v1.ConfigMap { |
| 24 | + //podName := os.Getenv("POD_NAME") |
| 25 | + cm := &v1.ConfigMap{} |
| 26 | + cm.Name = cmName |
| 27 | + cm.Namespace = podNamespace |
| 28 | + cm.Data = map[string]string{} |
| 29 | + if err := c.Create(context.TODO(), cm); err != nil { |
| 30 | + klog.Errorf("Failed to create cm %v", err) |
| 31 | + } |
| 32 | + return cm |
| 33 | +} |
| 34 | + |
| 35 | +var Retry = wait.Backoff{ |
| 36 | + Steps: 50, |
| 37 | + Duration: 10 * time.Millisecond, |
| 38 | + Factor: 1.0, |
| 39 | + Jitter: 0.1, |
| 40 | +} |
| 41 | + |
| 42 | +func add(c client.Client, namespace, kind string) error { |
| 43 | + if namespace == "" || kind == "" { |
| 44 | + return nil |
| 45 | + } |
| 46 | + key := podName |
| 47 | + return retry.RetryOnConflict(Retry, func() error { |
| 48 | + cm := &v1.ConfigMap{} |
| 49 | + if err := c.Get(context.TODO(), types.NamespacedName{Name: cmName, Namespace: podNamespace}, cm); err != nil { |
| 50 | + klog.Errorf("Failed to get cm %v, try init", err) |
| 51 | + cm = initConfigMap(c) |
| 52 | + } |
| 53 | + if cm.Data == nil { |
| 54 | + cm.Data = map[string]string{} |
| 55 | + } |
| 56 | + val, _ := cm.Data[key] |
| 57 | + sto := &store{} |
| 58 | + if val == "" { |
| 59 | + sto.Kinds = map[string]string{} |
| 60 | + } else { |
| 61 | + json.Unmarshal([]byte(val), sto) |
| 62 | + } |
| 63 | + names, _ := sto.Kinds[kind] |
| 64 | + nameSet := list(names) |
| 65 | + if nameSet.Has(namespace) { |
| 66 | + return nil |
| 67 | + } |
| 68 | + nameSet.Insert(namespace) |
| 69 | + sto.Kinds[kind] = toString(nameSet) |
| 70 | + bt, _ := json.Marshal(sto) |
| 71 | + cm.Data[key] = string(bt) |
| 72 | + return c.Update(context.TODO(), cm) |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +func Checker(ctx context.Context, c client.Client) { |
| 77 | + cm := &v1.ConfigMap{} |
| 78 | + for { |
| 79 | + resources := map[string]sets.Set[string]{} |
| 80 | + <-time.After(20 * time.Second) |
| 81 | + |
| 82 | + select { |
| 83 | + case <-ctx.Done(): |
| 84 | + return |
| 85 | + default: |
| 86 | + } |
| 87 | + |
| 88 | + if err := c.Get(context.TODO(), types.NamespacedName{Name: cmName, Namespace: podNamespace}, cm); err != nil { |
| 89 | + klog.Errorf("fail to get configMap %s, %v", cmName, err) |
| 90 | + continue |
| 91 | + } |
| 92 | + if cm.Data == nil { |
| 93 | + klog.Infof("nil configmap data") |
| 94 | + continue |
| 95 | + } |
| 96 | + for pod, val := range cm.Data { |
| 97 | + sto := &store{} |
| 98 | + if err := json.Unmarshal([]byte(val), sto); err != nil { |
| 99 | + klog.Errorf("fail to unmarshal store, %v", err) |
| 100 | + } |
| 101 | + if sto.Kinds == nil { |
| 102 | + continue |
| 103 | + } |
| 104 | + for kind, names := range sto.Kinds { |
| 105 | + set, ok := resources[kind] |
| 106 | + if !ok { |
| 107 | + set = list(names) |
| 108 | + resources[kind] = set |
| 109 | + continue |
| 110 | + } |
| 111 | + nameList := list(names).UnsortedList() |
| 112 | + for _, name := range nameList { |
| 113 | + if set.Has(name) { |
| 114 | + msg := fmt.Sprintf("conflict namespace %s in store, pod: %s", name, pod) |
| 115 | + klog.Errorf(msg) |
| 116 | + <-time.After(20 * time.Second) |
| 117 | + panic(msg) |
| 118 | + } |
| 119 | + set.Insert(name) |
| 120 | + } |
| 121 | + resources[kind] = set |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func list(val string) sets.Set[string] { |
| 128 | + res := sets.New[string]() |
| 129 | + if val == "" { |
| 130 | + return res |
| 131 | + } |
| 132 | + |
| 133 | + res.Insert(strings.Split(val, ",")...) |
| 134 | + return res |
| 135 | +} |
| 136 | + |
| 137 | +func toString(names sets.Set[string]) string { |
| 138 | + return strings.Join(sets.List(names), ",") |
| 139 | +} |
| 140 | + |
| 141 | +type store struct { |
| 142 | + Kinds map[string]string |
| 143 | +} |
0 commit comments