Skip to content

Commit e7b35ba

Browse files
authored
[Feature] LocalStorage Priority support (#969)
1 parent bfd1a6a commit e7b35ba

File tree

8 files changed

+215
-47
lines changed

8 files changed

+215
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- (Bugfix) Orphan PVC are not removed
55
- (Bugfix) Remove LocalStorage Deadlock
66
- (Bugfix) Skip arangosync members state inspection checks
7+
- (Feature) Add LocalStorage DaemonSet Priority support
78

89
## [1.2.10](https://github.com/arangodb/kube-arangodb/tree/1.2.10) (2022-04-27)
910
- (Feature) Allow configuration for securityContext.runAsUser value
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 v1alpha
22+
23+
type LocalStoragePodCustomization struct {
24+
Priority *int32 `json:"priority,omitempty"`
25+
}
26+
27+
func (l *LocalStoragePodCustomization) GetPriority() *int32 {
28+
if l == nil {
29+
return nil
30+
}
31+
32+
return l.Priority
33+
}

pkg/apis/storage/v1alpha/local_storage_spec.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ import (
2929
// LocalStorageSpec contains the specification part of
3030
// an ArangoLocalStorage.
3131
type LocalStorageSpec struct {
32-
StorageClass StorageClassSpec `json:"storageClass"`
33-
LocalPath []string `json:"localPath,omitempty"`
32+
StorageClass StorageClassSpec `json:"storageClass"`
33+
LocalPath []string `json:"localPath,omitempty"`
34+
3435
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
3536
Privileged *bool `json:"privileged,omitempty"`
37+
38+
PodCustomization *LocalStoragePodCustomization `json:"podCustomization,omitempty"`
3639
}
3740

3841
// Validate the given spec, returning an error on validation

pkg/apis/storage/v1alpha/zz_generated.deepcopy.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/storage/daemon_set.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func (ls *LocalStorage) ensureDaemonSet(apiObject *api.ArangoLocalStorage) error
9595
},
9696
NodeSelector: apiObject.Spec.NodeSelector,
9797
ImagePullSecrets: ls.imagePullSecrets,
98+
Priority: apiObject.Spec.PodCustomization.GetPriority(),
9899
},
99100
},
100101
}

pkg/storage/daemon_set_test.go

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,65 +21,77 @@
2121
package storage
2222

2323
import (
24-
"context"
2524
"testing"
2625

2726
api "github.com/arangodb/kube-arangodb/pkg/apis/storage/v1alpha"
28-
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
29-
3027
"github.com/stretchr/testify/require"
31-
v1 "k8s.io/api/core/v1"
32-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
core "k8s.io/api/core/v1"
3329
)
3430

3531
// TestEnsureDaemonSet tests ensureDaemonSet() method
3632
func TestEnsureDaemonSet(t *testing.T) {
37-
testNamespace := "testNs"
38-
testLsName := "testDsName"
39-
40-
testPodName := "testPodName"
4133
testImage := "test-image"
4234

43-
testPullSecrets := []v1.LocalObjectReference{
35+
tps := []core.LocalObjectReference{
4436
{
4537
Name: "custom-docker",
4638
},
4739
}
4840

49-
ls := &LocalStorage{
50-
apiObject: &api.ArangoLocalStorage{
51-
ObjectMeta: metav1.ObjectMeta{
52-
Name: testLsName,
53-
Namespace: testNamespace,
41+
ls, ds := generateDaemonSet(t, core.PodSpec{
42+
ImagePullSecrets: tps,
43+
Containers: []core.Container{
44+
{
45+
Name: testImage,
46+
ImagePullPolicy: core.PullAlways,
47+
Image: testImage,
5448
},
55-
Spec: api.LocalStorageSpec{},
56-
},
57-
deps: Dependencies{
58-
Client: kclient.NewFakeClient(),
5949
},
60-
config: Config{
61-
Namespace: testNamespace,
62-
PodName: testPodName,
63-
},
64-
image: testImage,
65-
imagePullSecrets: testPullSecrets,
66-
imagePullPolicy: v1.PullAlways,
67-
}
50+
}, api.LocalStorageSpec{})
51+
52+
require.Equal(t, ds.GetName(), ls.apiObject.GetName())
53+
require.Equal(t, ds.Spec.Template.Spec.ImagePullSecrets, tps)
54+
require.Equal(t, len(ds.Spec.Template.Spec.Containers), 1)
55+
56+
c := ds.Spec.Template.Spec.Containers[0]
57+
require.Equal(t, c.Image, testImage)
58+
require.Equal(t, c.ImagePullPolicy, core.PullAlways)
59+
require.Nil(t, ds.Spec.Template.Spec.Priority)
60+
}
6861

69-
err := ls.ensureDaemonSet(ls.apiObject)
70-
require.NoError(t, err)
62+
// TestEnsureDaemonSet tests ensureDaemonSet() method
63+
func TestEnsureDaemonSet_WithPriority(t *testing.T) {
64+
testImage := "test-image"
65+
var priority int32 = 555
7166

72-
// verify if DaemonSet has been created with correct values
73-
ds, err := ls.deps.Client.Kubernetes().AppsV1().DaemonSets(testNamespace).Get(context.Background(), testLsName, metav1.GetOptions{})
74-
require.NoError(t, err)
67+
tps := []core.LocalObjectReference{
68+
{
69+
Name: "custom-docker",
70+
},
71+
}
7572

76-
pod := ds.Spec.Template.Spec
73+
ls, ds := generateDaemonSet(t, core.PodSpec{
74+
ImagePullSecrets: tps,
75+
Containers: []core.Container{
76+
{
77+
Name: testImage,
78+
ImagePullPolicy: core.PullAlways,
79+
Image: testImage,
80+
},
81+
},
82+
}, api.LocalStorageSpec{
83+
PodCustomization: &api.LocalStoragePodCustomization{
84+
Priority: &priority,
85+
},
86+
})
7787

78-
require.Equal(t, ds.GetName(), testLsName)
79-
require.Equal(t, pod.ImagePullSecrets, testPullSecrets)
80-
require.Equal(t, len(pod.Containers), 1)
88+
require.Equal(t, ds.GetName(), ls.apiObject.GetName())
89+
require.Equal(t, ds.Spec.Template.Spec.ImagePullSecrets, tps)
90+
require.Equal(t, len(ds.Spec.Template.Spec.Containers), 1)
8191

82-
c := pod.Containers[0]
92+
c := ds.Spec.Template.Spec.Containers[0]
8393
require.Equal(t, c.Image, testImage)
84-
require.Equal(t, c.ImagePullPolicy, v1.PullAlways)
94+
require.Equal(t, c.ImagePullPolicy, core.PullAlways)
95+
require.NotNil(t, ds.Spec.Template.Spec.Priority)
96+
require.Equal(t, priority, *ds.Spec.Template.Spec.Priority)
8597
}

pkg/storage/image.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@ import (
2424
"context"
2525

2626
"github.com/arangodb/kube-arangodb/pkg/util/errors"
27-
v1 "k8s.io/api/core/v1"
28-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"github.com/rs/zerolog"
28+
core "k8s.io/api/core/v1"
29+
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
30+
"k8s.io/client-go/kubernetes"
2931
)
3032

3133
// getMyImage fetched the docker image from my own pod
32-
func (l *LocalStorage) getMyImage() (string, v1.PullPolicy, []v1.LocalObjectReference, error) {
33-
log := l.deps.Log
34-
ns := l.config.Namespace
34+
func (l *LocalStorage) getMyImage() (string, core.PullPolicy, []core.LocalObjectReference, error) {
35+
return getImage(l.deps.Log, l.config.Namespace, l.config.PodName, l.deps.Client.Kubernetes())
36+
}
3537

36-
p, err := l.deps.Client.Kubernetes().CoreV1().Pods(ns).Get(context.Background(), l.config.PodName, metav1.GetOptions{})
38+
func getImage(log zerolog.Logger, ns, name string, client kubernetes.Interface) (string, core.PullPolicy, []core.LocalObjectReference, error) {
39+
p, err := client.CoreV1().Pods(ns).Get(context.Background(), name, meta.GetOptions{})
3740
if err != nil {
38-
log.Debug().Err(err).Str("pod-name", l.config.PodName).Msg("Failed to get my own pod")
41+
log.Debug().Err(err).Str("pod-name", name).Msg("Failed to get my own pod")
3942
return "", "", nil, errors.WithStack(err)
4043
}
4144

pkg/storage/utils_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 storage
22+
23+
import (
24+
"context"
25+
"fmt"
26+
"strings"
27+
"testing"
28+
29+
api "github.com/arangodb/kube-arangodb/pkg/apis/storage/v1alpha"
30+
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
31+
"github.com/dchest/uniuri"
32+
"github.com/rs/zerolog/log"
33+
"github.com/stretchr/testify/require"
34+
apps "k8s.io/api/apps/v1"
35+
core "k8s.io/api/core/v1"
36+
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
37+
)
38+
39+
func generateDaemonSet(t *testing.T, podSpec core.PodSpec, lsSpec api.LocalStorageSpec) (*LocalStorage, *apps.DaemonSet) {
40+
client := kclient.NewFakeClient()
41+
42+
name := fmt.Sprintf("pod-%s", strings.ToLower(uniuri.NewLen(6)))
43+
nameLS := fmt.Sprintf("pod-%s", strings.ToLower(uniuri.NewLen(6)))
44+
ns := fmt.Sprintf("ns-%s", strings.ToLower(uniuri.NewLen(6)))
45+
46+
pod := core.Pod{
47+
ObjectMeta: meta.ObjectMeta{
48+
Name: name,
49+
Namespace: ns,
50+
},
51+
Spec: podSpec,
52+
}
53+
54+
if _, err := client.Kubernetes().CoreV1().Pods(ns).Create(context.Background(), &pod, meta.CreateOptions{}); err != nil {
55+
require.NoError(t, err)
56+
}
57+
58+
image, pullPolicy, pullSecrets, err := getImage(log.Logger, ns, name, client.Kubernetes())
59+
require.NoError(t, err)
60+
61+
ls := &LocalStorage{
62+
apiObject: &api.ArangoLocalStorage{
63+
ObjectMeta: meta.ObjectMeta{
64+
Name: nameLS,
65+
Namespace: ns,
66+
},
67+
Spec: lsSpec,
68+
},
69+
deps: Dependencies{
70+
Client: client,
71+
},
72+
config: Config{
73+
Namespace: ns,
74+
PodName: name,
75+
},
76+
image: image,
77+
imagePullSecrets: pullSecrets,
78+
imagePullPolicy: pullPolicy,
79+
}
80+
81+
err = ls.ensureDaemonSet(ls.apiObject)
82+
require.NoError(t, err)
83+
84+
// verify if DaemonSet has been created with correct values
85+
ds, err := ls.deps.Client.Kubernetes().AppsV1().DaemonSets(ns).Get(context.Background(), nameLS, meta.GetOptions{})
86+
require.NoError(t, err)
87+
88+
return ls, ds
89+
}

0 commit comments

Comments
 (0)