|
| 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 resources |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "time" |
| 27 | + |
| 28 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + |
| 30 | + schedulerApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1beta1" |
| 31 | + schedulerContainerResourcesApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1beta1/container/resources" |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/deployment/patch" |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/integrations/sidecar" |
| 34 | + "github.com/arangodb/kube-arangodb/pkg/metrics" |
| 35 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 36 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" |
| 37 | + inspectorInterface "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector" |
| 38 | + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/patcher" |
| 39 | +) |
| 40 | + |
| 41 | +var ( |
| 42 | + inspectedArangoProfilesCounters = metrics.MustRegisterCounterVec(metricsComponent, "inspected_arango_profiles", "Number of ArangoProfiles inspections per deployment", metrics.DeploymentName) |
| 43 | + inspectArangoProfilesDurationGauges = metrics.MustRegisterGaugeVec(metricsComponent, "inspect_arango_profiles_duration", "Amount of time taken by a single inspection of all ArangoProfiles for a deployment (in sec)", metrics.DeploymentName) |
| 44 | +) |
| 45 | + |
| 46 | +// EnsureArangoProfiles creates all ArangoProfiles needed to run the given deployment |
| 47 | +func (r *Resources) EnsureArangoProfiles(ctx context.Context, cachedStatus inspectorInterface.Inspector) error { |
| 48 | + start := time.Now() |
| 49 | + spec := r.context.GetSpec() |
| 50 | + arangoProfiles := cachedStatus.ArangoProfileModInterface().V1Beta1() |
| 51 | + apiObject := r.context.GetAPIObject() |
| 52 | + deploymentName := apiObject.GetName() |
| 53 | + |
| 54 | + defer metrics.SetDuration(inspectArangoProfilesDurationGauges.WithLabelValues(deploymentName), start) |
| 55 | + counterMetric := inspectedArangoProfilesCounters.WithLabelValues(deploymentName) |
| 56 | + |
| 57 | + reconcileRequired := k8sutil.NewReconcile(cachedStatus) |
| 58 | + |
| 59 | + intName := fmt.Sprintf("%s-int", deploymentName) |
| 60 | + |
| 61 | + integration, err := sidecar.NewIntegration(&schedulerContainerResourcesApi.Image{ |
| 62 | + Image: util.NewType(r.context.GetOperatorImage()), |
| 63 | + }, spec.Integration.GetSidecar()) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + integrationChecksum, err := integration.Checksum() |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + if c, err := cachedStatus.ArangoProfile().V1Beta1(); err == nil { |
| 74 | + counterMetric.Inc() |
| 75 | + if s, ok := c.GetSimple(intName); !ok { |
| 76 | + s = &schedulerApi.ArangoProfile{ |
| 77 | + ObjectMeta: meta.ObjectMeta{ |
| 78 | + Name: intName, |
| 79 | + Namespace: apiObject.GetNamespace(), |
| 80 | + OwnerReferences: []meta.OwnerReference{ |
| 81 | + apiObject.AsOwner(), |
| 82 | + }, |
| 83 | + }, |
| 84 | + Spec: schedulerApi.ProfileSpec{ |
| 85 | + Template: integration, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + if _, err := cachedStatus.ArangoProfileModInterface().V1Beta1().Create(ctx, s, meta.CreateOptions{}); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + reconcileRequired.Required() |
| 94 | + } else { |
| 95 | + currChecksum, err := s.Spec.Template.Checksum() |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + if s.Spec.Selectors != nil { |
| 101 | + if _, changed, err := patcher.Patcher[*schedulerApi.ArangoProfile](ctx, arangoProfiles, s, meta.PatchOptions{}, |
| 102 | + func(in *schedulerApi.ArangoProfile) []patch.Item { |
| 103 | + return []patch.Item{ |
| 104 | + patch.ItemRemove(patch.NewPath("spec", "selectors")), |
| 105 | + } |
| 106 | + }); err != nil { |
| 107 | + return err |
| 108 | + } else if changed { |
| 109 | + reconcileRequired.Required() |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if currChecksum != integrationChecksum { |
| 114 | + if _, changed, err := patcher.Patcher[*schedulerApi.ArangoProfile](ctx, arangoProfiles, s, meta.PatchOptions{}, |
| 115 | + func(in *schedulerApi.ArangoProfile) []patch.Item { |
| 116 | + return []patch.Item{ |
| 117 | + patch.ItemReplace(patch.NewPath("spec", "template"), integration), |
| 118 | + } |
| 119 | + }); err != nil { |
| 120 | + return err |
| 121 | + } else if changed { |
| 122 | + reconcileRequired.Required() |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return reconcileRequired.Reconcile(ctx) |
| 129 | +} |
0 commit comments