Skip to content

Commit e10ac26

Browse files
authored
[Feature] Scheduler BatchJob Integration Service (#1633)
1 parent 0a37f52 commit e10ac26

20 files changed

+1471
-315
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- (Feature) Parametrize ForceDelete timeout
1717
- (Feature) Scheduler BatchJob Integration Definition
1818
- (Feature) Scheduler CronJob Integration Definition
19+
- (Feature) Scheduler BatchJob Integration Service
1920

2021
## [1.2.39](https://github.com/arangodb/kube-arangodb/tree/1.2.39) (2024-03-11)
2122
- (Feature) Extract Scheduler API
+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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 v1
22+
23+
import (
24+
"context"
25+
"testing"
26+
27+
"github.com/stretchr/testify/require"
28+
batch "k8s.io/api/batch/v1"
29+
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
30+
31+
pbSchedulerV1 "github.com/arangodb/kube-arangodb/integrations/scheduler/v1/definition"
32+
schedulerApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1alpha1"
33+
"github.com/arangodb/kube-arangodb/pkg/util"
34+
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
35+
"github.com/arangodb/kube-arangodb/pkg/util/tests"
36+
)
37+
38+
func Test_BatchJob(t *testing.T) {
39+
ctx, c := context.WithCancel(context.Background())
40+
defer c()
41+
42+
client := kclient.NewFakeClientBuilder().Add(
43+
tests.NewMetaObject(t, tests.FakeNamespace, "test", func(t *testing.T, obj *schedulerApi.ArangoProfile) {
44+
obj.Spec = schedulerApi.ProfileSpec{}
45+
}),
46+
tests.NewMetaObject(t, tests.FakeNamespace, "test-select-all", func(t *testing.T, obj *schedulerApi.ArangoProfile) {
47+
obj.Spec = schedulerApi.ProfileSpec{
48+
Selectors: &schedulerApi.ProfileSelectors{
49+
Label: &meta.LabelSelector{},
50+
},
51+
Template: &schedulerApi.ProfileTemplate{},
52+
}
53+
}),
54+
tests.NewMetaObject(t, tests.FakeNamespace, "test-select-specific", func(t *testing.T, obj *schedulerApi.ArangoProfile) {
55+
obj.Spec = schedulerApi.ProfileSpec{
56+
Selectors: &schedulerApi.ProfileSelectors{
57+
Label: &meta.LabelSelector{
58+
MatchLabels: map[string]string{
59+
"A": "B",
60+
},
61+
},
62+
},
63+
Template: &schedulerApi.ProfileTemplate{},
64+
}
65+
}),
66+
).Client()
67+
68+
scheduler := Client(t, ctx, client, func(c Configuration) Configuration {
69+
c.Namespace = tests.FakeNamespace
70+
c.VerifyAccess = false
71+
return c
72+
})
73+
74+
t.Run("Ensure job does not exist - get", func(t *testing.T) {
75+
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
76+
Name: "test",
77+
})
78+
require.NoError(t, err)
79+
80+
require.False(t, resp.GetExists())
81+
})
82+
83+
t.Run("Ensure job does not exist - list", func(t *testing.T) {
84+
resp, err := scheduler.ListBatchJob(context.Background(), &pbSchedulerV1.ListBatchJobRequest{})
85+
require.NoError(t, err)
86+
87+
require.Len(t, resp.GetBatchJobs(), 0)
88+
})
89+
90+
t.Run("Schedule Job", func(t *testing.T) {
91+
resp, err := scheduler.CreateBatchJob(context.Background(), &pbSchedulerV1.CreateBatchJobRequest{
92+
Spec: &pbSchedulerV1.Spec{
93+
Metadata: &pbSchedulerV1.Metadata{
94+
Name: "test",
95+
},
96+
Job: &pbSchedulerV1.JobBase{
97+
Labels: nil,
98+
Profiles: []string{
99+
"test",
100+
},
101+
},
102+
Containers: map[string]*pbSchedulerV1.ContainerBase{
103+
"example": {
104+
Image: util.NewType("ubuntu:20.04"),
105+
Args: []string{
106+
"/bin/bash",
107+
"-c",
108+
"true",
109+
},
110+
},
111+
},
112+
},
113+
BatchJob: &pbSchedulerV1.BatchJobSpec{
114+
Completions: util.NewType[int32](1),
115+
},
116+
})
117+
require.NoError(t, err)
118+
119+
require.EqualValues(t, "test", resp.GetName())
120+
require.Len(t, resp.Profiles, 2)
121+
require.Contains(t, resp.Profiles, "test")
122+
require.Contains(t, resp.Profiles, "test-select-all")
123+
require.NotContains(t, resp.Profiles, "test-select-specific")
124+
})
125+
126+
t.Run("Ensure job exist - get", func(t *testing.T) {
127+
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
128+
Name: "test",
129+
})
130+
require.NoError(t, err)
131+
132+
require.True(t, resp.GetExists())
133+
})
134+
135+
t.Run("Ensure job exist - list", func(t *testing.T) {
136+
resp, err := scheduler.ListBatchJob(context.Background(), &pbSchedulerV1.ListBatchJobRequest{})
137+
require.NoError(t, err)
138+
139+
require.Len(t, resp.GetBatchJobs(), 1)
140+
require.Contains(t, resp.GetBatchJobs(), "test")
141+
})
142+
143+
t.Run("Ensure job details - pre", func(t *testing.T) {
144+
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
145+
Name: "test",
146+
})
147+
require.NoError(t, err)
148+
149+
require.True(t, resp.GetExists())
150+
require.EqualValues(t, 0, resp.GetBatchJob().GetStatus().GetSucceeded())
151+
})
152+
153+
t.Run("Ensure job details - update", func(t *testing.T) {
154+
job := tests.NewMetaObject[*batch.Job](t, tests.FakeNamespace, "test")
155+
156+
tests.RefreshObjectsC(t, client, &job)
157+
158+
job.Status.Succeeded = 1
159+
160+
tests.UpdateObjectsC(t, client, &job)
161+
})
162+
163+
t.Run("Ensure job details - post", func(t *testing.T) {
164+
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
165+
Name: "test",
166+
})
167+
require.NoError(t, err)
168+
169+
require.True(t, resp.GetExists())
170+
require.EqualValues(t, 1, resp.GetBatchJob().GetStatus().GetSucceeded())
171+
})
172+
173+
t.Run("Delete Job", func(t *testing.T) {
174+
resp, err := scheduler.DeleteBatchJob(context.Background(), &pbSchedulerV1.DeleteBatchJobRequest{
175+
Name: "test",
176+
})
177+
require.NoError(t, err)
178+
require.True(t, resp.GetExists())
179+
})
180+
181+
t.Run("Re-Delete Job", func(t *testing.T) {
182+
resp, err := scheduler.DeleteBatchJob(context.Background(), &pbSchedulerV1.DeleteBatchJobRequest{
183+
Name: "test",
184+
})
185+
require.NoError(t, err)
186+
require.False(t, resp.GetExists())
187+
})
188+
189+
t.Run("Ensure job does not exist after deletion - get", func(t *testing.T) {
190+
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
191+
Name: "test",
192+
})
193+
require.NoError(t, err)
194+
195+
require.False(t, resp.GetExists())
196+
})
197+
198+
t.Run("Ensure job does not exist after deletion - list", func(t *testing.T) {
199+
resp, err := scheduler.ListBatchJob(context.Background(), &pbSchedulerV1.ListBatchJobRequest{})
200+
require.NoError(t, err)
201+
202+
require.Len(t, resp.GetBatchJobs(), 0)
203+
})
204+
}

cmd/scheduler.go renamed to integrations/scheduler/v1/configuration.go

+19-12
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,29 @@
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
2020

21-
package cmd
21+
package v1
2222

23-
import (
24-
"github.com/spf13/cobra"
23+
type Mod func(c Configuration) Configuration
2524

26-
"github.com/arangodb/kube-arangodb/pkg/scheduler"
27-
)
28-
29-
func init() {
30-
cmd := &cobra.Command{
31-
Use: "scheduler",
25+
func NewConfiguration() Configuration {
26+
return Configuration{
27+
Namespace: "default",
28+
VerifyAccess: true,
3229
}
30+
}
31+
32+
type Configuration struct {
33+
Namespace string
34+
35+
VerifyAccess bool
36+
}
37+
38+
func (c Configuration) With(mods ...Mod) Configuration {
39+
n := c
3340

34-
if err := scheduler.InitCommand(cmd); err != nil {
35-
panic(err.Error())
41+
for _, mod := range mods {
42+
n = mod(n)
3643
}
3744

38-
cmdMain.AddCommand(cmd)
45+
return n
3946
}

0 commit comments

Comments
 (0)