|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package inspector |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "time" |
| 26 | + |
| 27 | + core "k8s.io/api/core/v1" |
| 28 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 31 | + "github.com/arangodb/kube-arangodb/pkg/util/globals" |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/definitions" |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle" |
| 34 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/version" |
| 35 | +) |
| 36 | + |
| 37 | +func init() { |
| 38 | + requireRegisterInspectorLoader(configMapsInspectorLoaderObj) |
| 39 | +} |
| 40 | + |
| 41 | +var configMapsInspectorLoaderObj = configMapsInspectorLoader{} |
| 42 | + |
| 43 | +type configMapsInspectorLoader struct { |
| 44 | +} |
| 45 | + |
| 46 | +func (p configMapsInspectorLoader) Component() definitions.Component { |
| 47 | + return definitions.ConfigMap |
| 48 | +} |
| 49 | + |
| 50 | +func (p configMapsInspectorLoader) Load(ctx context.Context, i *inspectorState) { |
| 51 | + var q configMapsInspector |
| 52 | + p.loadV1(ctx, i, &q) |
| 53 | + i.configMaps = &q |
| 54 | + q.state = i |
| 55 | + q.last = time.Now() |
| 56 | +} |
| 57 | + |
| 58 | +func (p configMapsInspectorLoader) loadV1(ctx context.Context, i *inspectorState, q *configMapsInspector) { |
| 59 | + var z configMapsInspectorV1 |
| 60 | + |
| 61 | + z.configMapInspector = q |
| 62 | + |
| 63 | + z.configMaps, z.err = p.getV1ConfigMaps(ctx, i) |
| 64 | + |
| 65 | + q.v1 = &z |
| 66 | +} |
| 67 | + |
| 68 | +func (p configMapsInspectorLoader) getV1ConfigMaps(ctx context.Context, i *inspectorState) (map[string]*core.ConfigMap, error) { |
| 69 | + objs, err := p.getV1ConfigMapsList(ctx, i) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + r := make(map[string]*core.ConfigMap, len(objs)) |
| 75 | + |
| 76 | + for id := range objs { |
| 77 | + r[objs[id].GetName()] = objs[id] |
| 78 | + } |
| 79 | + |
| 80 | + return r, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (p configMapsInspectorLoader) getV1ConfigMapsList(ctx context.Context, i *inspectorState) ([]*core.ConfigMap, error) { |
| 84 | + ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx) |
| 85 | + defer cancel() |
| 86 | + obj, err := i.client.Kubernetes().CoreV1().ConfigMaps(i.namespace).List(ctxChild, meta.ListOptions{ |
| 87 | + Limit: globals.GetGlobals().Kubernetes().RequestBatchSize().Get(), |
| 88 | + }) |
| 89 | + |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + items := obj.Items |
| 95 | + cont := obj.Continue |
| 96 | + var s = int64(len(items)) |
| 97 | + |
| 98 | + if z := obj.RemainingItemCount; z != nil { |
| 99 | + s += *z |
| 100 | + } |
| 101 | + |
| 102 | + ptrs := make([]*core.ConfigMap, 0, s) |
| 103 | + |
| 104 | + for { |
| 105 | + for id := range items { |
| 106 | + ptrs = append(ptrs, &items[id]) |
| 107 | + } |
| 108 | + |
| 109 | + if cont == "" { |
| 110 | + break |
| 111 | + } |
| 112 | + |
| 113 | + items, cont, err = p.getV1ConfigMapsListRequest(ctx, i, cont) |
| 114 | + |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return ptrs, nil |
| 121 | +} |
| 122 | + |
| 123 | +func (p configMapsInspectorLoader) getV1ConfigMapsListRequest(ctx context.Context, i *inspectorState, cont string) ([]core.ConfigMap, string, error) { |
| 124 | + ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx) |
| 125 | + defer cancel() |
| 126 | + obj, err := i.client.Kubernetes().CoreV1().ConfigMaps(i.namespace).List(ctxChild, meta.ListOptions{ |
| 127 | + Limit: globals.GetGlobals().Kubernetes().RequestBatchSize().Get(), |
| 128 | + Continue: cont, |
| 129 | + }) |
| 130 | + |
| 131 | + if err != nil { |
| 132 | + return nil, "", err |
| 133 | + } |
| 134 | + |
| 135 | + return obj.Items, obj.Continue, err |
| 136 | +} |
| 137 | + |
| 138 | +func (p configMapsInspectorLoader) Verify(i *inspectorState) error { |
| 139 | + if err := i.configMaps.v1.err; err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func (p configMapsInspectorLoader) Copy(from, to *inspectorState, override bool) { |
| 147 | + if to.configMaps != nil { |
| 148 | + if !override { |
| 149 | + return |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + to.configMaps = from.configMaps |
| 154 | + to.configMaps.state = to |
| 155 | +} |
| 156 | + |
| 157 | +func (p configMapsInspectorLoader) Name() string { |
| 158 | + return "configMaps" |
| 159 | +} |
| 160 | + |
| 161 | +type configMapsInspector struct { |
| 162 | + state *inspectorState |
| 163 | + |
| 164 | + last time.Time |
| 165 | + |
| 166 | + v1 *configMapsInspectorV1 |
| 167 | +} |
| 168 | + |
| 169 | +func (p *configMapsInspector) LastRefresh() time.Time { |
| 170 | + return p.last |
| 171 | +} |
| 172 | + |
| 173 | +func (p *configMapsInspector) Refresh(ctx context.Context) error { |
| 174 | + p.Throttle(p.state.throttles).Invalidate() |
| 175 | + return p.state.refresh(ctx, configMapsInspectorLoaderObj) |
| 176 | +} |
| 177 | + |
| 178 | +func (p *configMapsInspector) Version() version.Version { |
| 179 | + return version.V1 |
| 180 | +} |
| 181 | + |
| 182 | +func (p *configMapsInspector) Throttle(c throttle.Components) throttle.Throttle { |
| 183 | + return c.ConfigMap() |
| 184 | +} |
| 185 | + |
| 186 | +func (p *configMapsInspector) validate() error { |
| 187 | + if p == nil { |
| 188 | + return errors.Errorf("ConfigMapInspector is nil") |
| 189 | + } |
| 190 | + |
| 191 | + if p.state == nil { |
| 192 | + return errors.Errorf("Parent is nil") |
| 193 | + } |
| 194 | + |
| 195 | + return p.v1.validate() |
| 196 | +} |
0 commit comments