Skip to content

Commit 367e59f

Browse files
authored
[Feature] [Scheduler] Create Integration Profile (#1727)
1 parent bda9985 commit 367e59f

File tree

6 files changed

+157
-11
lines changed

6 files changed

+157
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- (Feature) (Scheduler) Merge Strategy
3434
- (Feature) (Networking) Endpoints Destination
3535
- (Improvement) Improve Metrics Handling
36+
- (Feature) (Scheduler) Create Integration Profile
3637

3738
## [1.2.42](https://github.com/arangodb/kube-arangodb/tree/1.2.42) (2024-07-23)
3839
- (Maintenance) Go 1.22.4 & Kubernetes 1.29.6 libraries
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}

pkg/deployment/resources/pod_creator_gateway_pod.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ import (
2929

3030
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1"
3131
schedulerApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1beta1"
32-
schedulerContainerResourcesApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1beta1/container/resources"
3332
shared "github.com/arangodb/kube-arangodb/pkg/apis/shared"
3433
"github.com/arangodb/kube-arangodb/pkg/deployment/features"
3534
"github.com/arangodb/kube-arangodb/pkg/deployment/pod"
3635
"github.com/arangodb/kube-arangodb/pkg/integrations/sidecar"
37-
"github.com/arangodb/kube-arangodb/pkg/util"
3836
"github.com/arangodb/kube-arangodb/pkg/util/collection"
37+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
3938
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
4039
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/interfaces"
4140
kresources "github.com/arangodb/kube-arangodb/pkg/util/k8sutil/resources"
@@ -203,6 +202,14 @@ func (m *MemberGatewayPod) Validate(cachedStatus interfaces.Inspector) error {
203202
return err
204203
}
205204

205+
if c, err := cachedStatus.ArangoProfile().V1Beta1(); err != nil {
206+
return err
207+
} else {
208+
if _, ok := c.GetSimple(fmt.Sprintf("%s-int", m.context.GetName())); !ok {
209+
return errors.Errorf("Unable to find deployment integration")
210+
}
211+
}
212+
206213
return nil
207214
}
208215

@@ -236,14 +243,20 @@ func (m *MemberGatewayPod) Labels() map[string]string {
236243
}
237244

238245
func (m *MemberGatewayPod) Profiles() (schedulerApi.ProfileTemplates, error) {
239-
integration, err := sidecar.NewIntegration(&schedulerContainerResourcesApi.Image{
240-
Image: util.NewType(m.resources.context.GetOperatorImage()),
241-
}, m.spec.Integration.GetSidecar())
242-
246+
c, err := m.cachedStatus.ArangoProfile().V1Beta1()
243247
if err != nil {
244248
return nil, err
245249
}
246250

251+
integration, ok := c.GetSimple(fmt.Sprintf("%s-int", m.context.GetName()))
252+
if !ok {
253+
return nil, errors.Errorf("Unable to find deployment integration")
254+
}
255+
256+
if integration.Status.Accepted == nil {
257+
return nil, errors.Errorf("Unable to find accepted integration integration")
258+
}
259+
247260
integrations, err := sidecar.NewIntegrationEnablement(
248261
sidecar.IntegrationEnvoyV3{
249262
Spec: m.spec,
@@ -258,5 +271,5 @@ func (m *MemberGatewayPod) Profiles() (schedulerApi.ProfileTemplates, error) {
258271

259272
shutdownAnnotation := sidecar.NewShutdownAnnotations([]string{shared.ServerContainerName})
260273

261-
return []*schedulerApi.ProfileTemplate{integration, integrations, shutdownAnnotation}, nil
274+
return []*schedulerApi.ProfileTemplate{integration.Status.Accepted.Template, integrations, shutdownAnnotation}, nil
262275
}

pkg/deployment/resources/resources.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func (r *Resources) EnsureCoreResources(ctx context.Context, cachedStatus inspec
5757
errors.Section(r.EnsureArangoMembers(ctx, cachedStatus), "EnsureArangoMembers"),
5858
errors.Section(r.EnsureServices(ctx, cachedStatus), "EnsureServices"),
5959
errors.Section(r.EnsureConfigMaps(ctx, cachedStatus), "EnsureConfigMaps"),
60-
errors.Section(r.EnsureSecrets(ctx, cachedStatus), "EnsureSecrets"))
60+
errors.Section(r.EnsureSecrets(ctx, cachedStatus), "EnsureSecrets"),
61+
errors.Section(r.EnsureArangoProfiles(ctx, cachedStatus), "EnsureArangoProfiles"))
6162
}
6263

6364
func (r *Resources) EnsureResources(ctx context.Context, serviceMonitorEnabled bool, cachedStatus inspectorInterface.Inspector) error {

pkg/util/k8sutil/inspector/arangoprofile/v1beta1/reader.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ import (
3131

3232
// ModInterface has methods to work with ArangoTask resources only for creation
3333
type ModInterface interface {
34-
Create(ctx context.Context, arangotask *schedulerApi.ArangoProfile, opts meta.CreateOptions) (*schedulerApi.ArangoProfile, error)
35-
Update(ctx context.Context, arangotask *schedulerApi.ArangoProfile, opts meta.UpdateOptions) (*schedulerApi.ArangoProfile, error)
36-
UpdateStatus(ctx context.Context, arangotask *schedulerApi.ArangoProfile, opts meta.UpdateOptions) (*schedulerApi.ArangoProfile, error)
34+
Create(ctx context.Context, arangoProfile *schedulerApi.ArangoProfile, opts meta.CreateOptions) (*schedulerApi.ArangoProfile, error)
35+
Update(ctx context.Context, arangoProfile *schedulerApi.ArangoProfile, opts meta.UpdateOptions) (*schedulerApi.ArangoProfile, error)
36+
UpdateStatus(ctx context.Context, arangoProfile *schedulerApi.ArangoProfile, opts meta.UpdateOptions) (*schedulerApi.ArangoProfile, error)
3737
Patch(ctx context.Context, name string, pt types.PatchType, data []byte, opts meta.PatchOptions, subresources ...string) (result *schedulerApi.ArangoProfile, err error)
3838
Delete(ctx context.Context, name string, opts meta.DeleteOptions) error
3939
}

pkg/util/k8sutil/interfaces/pod_creator.go

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
core "k8s.io/api/core/v1"
2727

2828
schedulerApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1beta1"
29+
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/arangoprofile"
2930
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/configmap"
3031
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/secret"
3132
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/inspector/service"
@@ -35,6 +36,7 @@ type Inspector interface {
3536
secret.Inspector
3637
service.Inspector
3738
configmap.Inspector
39+
arangoprofile.Inspector
3840
}
3941

4042
type PodModifier interface {

0 commit comments

Comments
 (0)