Skip to content

Commit

Permalink
[Feature] Scheduler Integration Service
Browse files Browse the repository at this point in the history
  • Loading branch information
ajanikow committed Mar 28, 2024
1 parent 0a37f52 commit 69e7d04
Show file tree
Hide file tree
Showing 20 changed files with 1,471 additions and 315 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- (Feature) Parametrize ForceDelete timeout
- (Feature) Scheduler BatchJob Integration Definition
- (Feature) Scheduler CronJob Integration Definition
- (Feature) Scheduler BatchJob Integration Service

## [1.2.39](https://github.com/arangodb/kube-arangodb/tree/1.2.39) (2024-03-11)
- (Feature) Extract Scheduler API
Expand Down
204 changes: 204 additions & 0 deletions integrations/scheduler/v1/batch_job_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
//
// DISCLAIMER
//
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v1

import (
"context"
"testing"

"github.com/stretchr/testify/require"
batch "k8s.io/api/batch/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"

pbSchedulerV1 "github.com/arangodb/kube-arangodb/integrations/scheduler/v1/definition"
schedulerApi "github.com/arangodb/kube-arangodb/pkg/apis/scheduler/v1alpha1"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
"github.com/arangodb/kube-arangodb/pkg/util/tests"
)

func Test_BatchJob(t *testing.T) {
ctx, c := context.WithCancel(context.Background())
defer c()

client := kclient.NewFakeClientBuilder().Add(
tests.NewMetaObject(t, tests.FakeNamespace, "test", func(t *testing.T, obj *schedulerApi.ArangoProfile) {
obj.Spec = schedulerApi.ProfileSpec{}
}),
tests.NewMetaObject(t, tests.FakeNamespace, "test-select-all", func(t *testing.T, obj *schedulerApi.ArangoProfile) {
obj.Spec = schedulerApi.ProfileSpec{
Selectors: &schedulerApi.ProfileSelectors{
Label: &meta.LabelSelector{},
},
Template: &schedulerApi.ProfileTemplate{},
}
}),
tests.NewMetaObject(t, tests.FakeNamespace, "test-select-specific", func(t *testing.T, obj *schedulerApi.ArangoProfile) {
obj.Spec = schedulerApi.ProfileSpec{
Selectors: &schedulerApi.ProfileSelectors{
Label: &meta.LabelSelector{
MatchLabels: map[string]string{
"A": "B",
},
},
},
Template: &schedulerApi.ProfileTemplate{},
}
}),
).Client()

scheduler := Client(t, ctx, client, func(c Configuration) Configuration {
c.Namespace = tests.FakeNamespace
c.VerifyAccess = false
return c
})

t.Run("Ensure job does not exist - get", func(t *testing.T) {
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
Name: "test",
})
require.NoError(t, err)

require.False(t, resp.GetExists())
})

t.Run("Ensure job does not exist - list", func(t *testing.T) {
resp, err := scheduler.ListBatchJob(context.Background(), &pbSchedulerV1.ListBatchJobRequest{})
require.NoError(t, err)

require.Len(t, resp.GetBatchJobs(), 0)
})

t.Run("Schedule Job", func(t *testing.T) {
resp, err := scheduler.CreateBatchJob(context.Background(), &pbSchedulerV1.CreateBatchJobRequest{
Spec: &pbSchedulerV1.Spec{
Metadata: &pbSchedulerV1.Metadata{
Name: "test",
},
Job: &pbSchedulerV1.JobBase{
Labels: nil,
Profiles: []string{
"test",
},
},
Containers: map[string]*pbSchedulerV1.ContainerBase{
"example": {
Image: util.NewType("ubuntu:20.04"),
Args: []string{
"/bin/bash",
"-c",
"true",
},
},
},
},
BatchJob: &pbSchedulerV1.BatchJobSpec{
Completions: util.NewType[int32](1),
},
})
require.NoError(t, err)

require.EqualValues(t, "test", resp.GetName())
require.Len(t, resp.Profiles, 2)
require.Contains(t, resp.Profiles, "test")
require.Contains(t, resp.Profiles, "test-select-all")
require.NotContains(t, resp.Profiles, "test-select-specific")
})

t.Run("Ensure job exist - get", func(t *testing.T) {
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
Name: "test",
})
require.NoError(t, err)

require.True(t, resp.GetExists())
})

t.Run("Ensure job exist - list", func(t *testing.T) {
resp, err := scheduler.ListBatchJob(context.Background(), &pbSchedulerV1.ListBatchJobRequest{})
require.NoError(t, err)

require.Len(t, resp.GetBatchJobs(), 1)
require.Contains(t, resp.GetBatchJobs(), "test")
})

t.Run("Ensure job details - pre", func(t *testing.T) {
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
Name: "test",
})
require.NoError(t, err)

require.True(t, resp.GetExists())
require.EqualValues(t, 0, resp.GetBatchJob().GetStatus().GetSucceeded())
})

t.Run("Ensure job details - update", func(t *testing.T) {
job := tests.NewMetaObject[*batch.Job](t, tests.FakeNamespace, "test")

tests.RefreshObjectsC(t, client, &job)

job.Status.Succeeded = 1

tests.UpdateObjectsC(t, client, &job)
})

t.Run("Ensure job details - post", func(t *testing.T) {
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
Name: "test",
})
require.NoError(t, err)

require.True(t, resp.GetExists())
require.EqualValues(t, 1, resp.GetBatchJob().GetStatus().GetSucceeded())
})

t.Run("Delete Job", func(t *testing.T) {
resp, err := scheduler.DeleteBatchJob(context.Background(), &pbSchedulerV1.DeleteBatchJobRequest{
Name: "test",
})
require.NoError(t, err)
require.True(t, resp.GetExists())
})

t.Run("Re-Delete Job", func(t *testing.T) {
resp, err := scheduler.DeleteBatchJob(context.Background(), &pbSchedulerV1.DeleteBatchJobRequest{
Name: "test",
})
require.NoError(t, err)
require.False(t, resp.GetExists())
})

t.Run("Ensure job does not exist after deletion - get", func(t *testing.T) {
resp, err := scheduler.GetBatchJob(context.Background(), &pbSchedulerV1.GetBatchJobRequest{
Name: "test",
})
require.NoError(t, err)

require.False(t, resp.GetExists())
})

t.Run("Ensure job does not exist after deletion - list", func(t *testing.T) {
resp, err := scheduler.ListBatchJob(context.Background(), &pbSchedulerV1.ListBatchJobRequest{})
require.NoError(t, err)

require.Len(t, resp.GetBatchJobs(), 0)
})
}
31 changes: 19 additions & 12 deletions cmd/scheduler.go → integrations/scheduler/v1/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,29 @@
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package cmd
package v1

import (
"github.com/spf13/cobra"
type Mod func(c Configuration) Configuration

"github.com/arangodb/kube-arangodb/pkg/scheduler"
)

func init() {
cmd := &cobra.Command{
Use: "scheduler",
func NewConfiguration() Configuration {
return Configuration{
Namespace: "default",
VerifyAccess: true,
}
}

type Configuration struct {
Namespace string

VerifyAccess bool
}

func (c Configuration) With(mods ...Mod) Configuration {
n := c

if err := scheduler.InitCommand(cmd); err != nil {
panic(err.Error())
for _, mod := range mods {
n = mod(n)
}

cmdMain.AddCommand(cmd)
return n
}
Loading

0 comments on commit 69e7d04

Please sign in to comment.