|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2022 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 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 28 | + "github.com/arangodb/kube-arangodb/pkg/util/globals" |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/throttle" |
| 30 | + core "k8s.io/api/core/v1" |
| 31 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | +) |
| 33 | + |
| 34 | +func init() { |
| 35 | + requireRegisterInspectorLoader(endpointsInspectorLoaderObj) |
| 36 | +} |
| 37 | + |
| 38 | +var endpointsInspectorLoaderObj = endpointsInspectorLoader{} |
| 39 | + |
| 40 | +type endpointsInspectorLoader struct { |
| 41 | +} |
| 42 | + |
| 43 | +func (p endpointsInspectorLoader) Component() throttle.Component { |
| 44 | + return throttle.Endpoints |
| 45 | +} |
| 46 | + |
| 47 | +func (p endpointsInspectorLoader) Load(ctx context.Context, i *inspectorState) { |
| 48 | + var q endpointsInspector |
| 49 | + p.loadV1(ctx, i, &q) |
| 50 | + i.endpoints = &q |
| 51 | + q.state = i |
| 52 | + q.last = time.Now() |
| 53 | +} |
| 54 | + |
| 55 | +func (p endpointsInspectorLoader) loadV1(ctx context.Context, i *inspectorState, q *endpointsInspector) { |
| 56 | + var z endpointsInspectorV1 |
| 57 | + |
| 58 | + z.endpointsInspector = q |
| 59 | + |
| 60 | + z.endpoints, z.err = p.getV1Endpoints(ctx, i) |
| 61 | + |
| 62 | + q.v1 = &z |
| 63 | +} |
| 64 | + |
| 65 | +func (p endpointsInspectorLoader) getV1Endpoints(ctx context.Context, i *inspectorState) (map[string]*core.Endpoints, error) { |
| 66 | + objs, err := p.getV1EndpointsList(ctx, i) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + r := make(map[string]*core.Endpoints, len(objs)) |
| 72 | + |
| 73 | + for id := range objs { |
| 74 | + r[objs[id].GetName()] = objs[id] |
| 75 | + } |
| 76 | + |
| 77 | + return r, nil |
| 78 | +} |
| 79 | + |
| 80 | +func (p endpointsInspectorLoader) getV1EndpointsList(ctx context.Context, i *inspectorState) ([]*core.Endpoints, error) { |
| 81 | + ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx) |
| 82 | + defer cancel() |
| 83 | + obj, err := i.client.Kubernetes().CoreV1().Endpoints(i.namespace).List(ctxChild, meta.ListOptions{ |
| 84 | + Limit: globals.GetGlobals().Kubernetes().RequestBatchSize().Get(), |
| 85 | + }) |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + items := obj.Items |
| 92 | + cont := obj.Continue |
| 93 | + var s = int64(len(items)) |
| 94 | + |
| 95 | + if z := obj.RemainingItemCount; z != nil { |
| 96 | + s += *z |
| 97 | + } |
| 98 | + |
| 99 | + ptrs := make([]*core.Endpoints, 0, s) |
| 100 | + |
| 101 | + for { |
| 102 | + for id := range items { |
| 103 | + ptrs = append(ptrs, &items[id]) |
| 104 | + } |
| 105 | + |
| 106 | + if cont == "" { |
| 107 | + break |
| 108 | + } |
| 109 | + |
| 110 | + items, cont, err = p.getV1EndpointsListRequest(ctx, i, cont) |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return ptrs, nil |
| 118 | +} |
| 119 | + |
| 120 | +func (p endpointsInspectorLoader) getV1EndpointsListRequest(ctx context.Context, i *inspectorState, cont string) ([]core.Endpoints, string, error) { |
| 121 | + ctxChild, cancel := globals.GetGlobalTimeouts().Kubernetes().WithTimeout(ctx) |
| 122 | + defer cancel() |
| 123 | + obj, err := i.client.Kubernetes().CoreV1().Endpoints(i.namespace).List(ctxChild, meta.ListOptions{ |
| 124 | + Limit: globals.GetGlobals().Kubernetes().RequestBatchSize().Get(), |
| 125 | + Continue: cont, |
| 126 | + }) |
| 127 | + |
| 128 | + if err != nil { |
| 129 | + return nil, "", err |
| 130 | + } |
| 131 | + |
| 132 | + return obj.Items, obj.Continue, err |
| 133 | +} |
| 134 | + |
| 135 | +func (p endpointsInspectorLoader) Verify(i *inspectorState) error { |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func (p endpointsInspectorLoader) Copy(from, to *inspectorState, override bool) { |
| 140 | + if to.endpoints != nil { |
| 141 | + if !override { |
| 142 | + return |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + to.endpoints = from.endpoints |
| 147 | + to.endpoints.state = to |
| 148 | +} |
| 149 | + |
| 150 | +func (p endpointsInspectorLoader) Name() string { |
| 151 | + return "endpoints" |
| 152 | +} |
| 153 | + |
| 154 | +type endpointsInspector struct { |
| 155 | + state *inspectorState |
| 156 | + |
| 157 | + last time.Time |
| 158 | + |
| 159 | + v1 *endpointsInspectorV1 |
| 160 | +} |
| 161 | + |
| 162 | +func (p *endpointsInspector) LastRefresh() time.Time { |
| 163 | + return p.last |
| 164 | +} |
| 165 | + |
| 166 | +func (p *endpointsInspector) Refresh(ctx context.Context) error { |
| 167 | + p.Throttle(p.state.throttles).Invalidate() |
| 168 | + return p.state.refresh(ctx, endpointsInspectorLoaderObj) |
| 169 | +} |
| 170 | + |
| 171 | +func (p endpointsInspector) Throttle(c throttle.Components) throttle.Throttle { |
| 172 | + return c.Endpoints() |
| 173 | +} |
| 174 | + |
| 175 | +func (p *endpointsInspector) validate() error { |
| 176 | + if p == nil { |
| 177 | + return errors.Newf("EndpointsInspector is nil") |
| 178 | + } |
| 179 | + |
| 180 | + if p.state == nil { |
| 181 | + return errors.Newf("Parent is nil") |
| 182 | + } |
| 183 | + |
| 184 | + return p.v1.validate() |
| 185 | +} |
0 commit comments