Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- (Bugfix) Orphan PVC are not removed
- (Bugfix) Remove LocalStorage Deadlock
- (Bugfix) Skip arangosync members state inspection checks
- (Feature) Add LocalStorage DaemonSet Priority support

## [1.2.10](https://github.com/arangodb/kube-arangodb/tree/1.2.10) (2022-04-27)
- (Feature) Allow configuration for securityContext.runAsUser value
Expand Down
33 changes: 33 additions & 0 deletions pkg/apis/storage/v1alpha/local_storage_pod_customization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 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 v1alpha

type LocalStoragePodCustomization struct {
Priority *int32 `json:"priority,omitempty"`
}

func (l *LocalStoragePodCustomization) GetPriority() *int32 {
if l == nil {
return nil
}

return l.Priority
}
7 changes: 5 additions & 2 deletions pkg/apis/storage/v1alpha/local_storage_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ import (
// LocalStorageSpec contains the specification part of
// an ArangoLocalStorage.
type LocalStorageSpec struct {
StorageClass StorageClassSpec `json:"storageClass"`
LocalPath []string `json:"localPath,omitempty"`
StorageClass StorageClassSpec `json:"storageClass"`
LocalPath []string `json:"localPath,omitempty"`

NodeSelector map[string]string `json:"nodeSelector,omitempty"`
Privileged *bool `json:"privileged,omitempty"`

PodCustomization *LocalStoragePodCustomization `json:"podCustomization,omitempty"`
}

// Validate the given spec, returning an error on validation
Expand Down
26 changes: 26 additions & 0 deletions pkg/apis/storage/v1alpha/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/storage/daemon_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (ls *LocalStorage) ensureDaemonSet(apiObject *api.ArangoLocalStorage) error
},
NodeSelector: apiObject.Spec.NodeSelector,
ImagePullSecrets: ls.imagePullSecrets,
Priority: apiObject.Spec.PodCustomization.GetPriority(),
},
},
}
Expand Down
88 changes: 50 additions & 38 deletions pkg/storage/daemon_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,65 +21,77 @@
package storage

import (
"context"
"testing"

api "github.com/arangodb/kube-arangodb/pkg/apis/storage/v1alpha"
"github.com/arangodb/kube-arangodb/pkg/util/kclient"

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

// TestEnsureDaemonSet tests ensureDaemonSet() method
func TestEnsureDaemonSet(t *testing.T) {
testNamespace := "testNs"
testLsName := "testDsName"

testPodName := "testPodName"
testImage := "test-image"

testPullSecrets := []v1.LocalObjectReference{
tps := []core.LocalObjectReference{
{
Name: "custom-docker",
},
}

ls := &LocalStorage{
apiObject: &api.ArangoLocalStorage{
ObjectMeta: metav1.ObjectMeta{
Name: testLsName,
Namespace: testNamespace,
ls, ds := generateDaemonSet(t, core.PodSpec{
ImagePullSecrets: tps,
Containers: []core.Container{
{
Name: testImage,
ImagePullPolicy: core.PullAlways,
Image: testImage,
},
Spec: api.LocalStorageSpec{},
},
deps: Dependencies{
Client: kclient.NewFakeClient(),
},
config: Config{
Namespace: testNamespace,
PodName: testPodName,
},
image: testImage,
imagePullSecrets: testPullSecrets,
imagePullPolicy: v1.PullAlways,
}
}, api.LocalStorageSpec{})

require.Equal(t, ds.GetName(), ls.apiObject.GetName())
require.Equal(t, ds.Spec.Template.Spec.ImagePullSecrets, tps)
require.Equal(t, len(ds.Spec.Template.Spec.Containers), 1)

c := ds.Spec.Template.Spec.Containers[0]
require.Equal(t, c.Image, testImage)
require.Equal(t, c.ImagePullPolicy, core.PullAlways)
require.Nil(t, ds.Spec.Template.Spec.Priority)
}

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

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

pod := ds.Spec.Template.Spec
ls, ds := generateDaemonSet(t, core.PodSpec{
ImagePullSecrets: tps,
Containers: []core.Container{
{
Name: testImage,
ImagePullPolicy: core.PullAlways,
Image: testImage,
},
},
}, api.LocalStorageSpec{
PodCustomization: &api.LocalStoragePodCustomization{
Priority: &priority,
},
})

require.Equal(t, ds.GetName(), testLsName)
require.Equal(t, pod.ImagePullSecrets, testPullSecrets)
require.Equal(t, len(pod.Containers), 1)
require.Equal(t, ds.GetName(), ls.apiObject.GetName())
require.Equal(t, ds.Spec.Template.Spec.ImagePullSecrets, tps)
require.Equal(t, len(ds.Spec.Template.Spec.Containers), 1)

c := pod.Containers[0]
c := ds.Spec.Template.Spec.Containers[0]
require.Equal(t, c.Image, testImage)
require.Equal(t, c.ImagePullPolicy, v1.PullAlways)
require.Equal(t, c.ImagePullPolicy, core.PullAlways)
require.NotNil(t, ds.Spec.Template.Spec.Priority)
require.Equal(t, priority, *ds.Spec.Template.Spec.Priority)
}
17 changes: 10 additions & 7 deletions pkg/storage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@ import (
"context"

"github.com/arangodb/kube-arangodb/pkg/util/errors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/rs/zerolog"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

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

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

Expand Down
89 changes: 89 additions & 0 deletions pkg/storage/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// DISCLAIMER
//
// Copyright 2016-2022 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 storage

import (
"context"
"fmt"
"strings"
"testing"

api "github.com/arangodb/kube-arangodb/pkg/apis/storage/v1alpha"
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
"github.com/dchest/uniuri"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/require"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func generateDaemonSet(t *testing.T, podSpec core.PodSpec, lsSpec api.LocalStorageSpec) (*LocalStorage, *apps.DaemonSet) {
client := kclient.NewFakeClient()

name := fmt.Sprintf("pod-%s", strings.ToLower(uniuri.NewLen(6)))
nameLS := fmt.Sprintf("pod-%s", strings.ToLower(uniuri.NewLen(6)))
ns := fmt.Sprintf("ns-%s", strings.ToLower(uniuri.NewLen(6)))

pod := core.Pod{
ObjectMeta: meta.ObjectMeta{
Name: name,
Namespace: ns,
},
Spec: podSpec,
}

if _, err := client.Kubernetes().CoreV1().Pods(ns).Create(context.Background(), &pod, meta.CreateOptions{}); err != nil {
require.NoError(t, err)
}

image, pullPolicy, pullSecrets, err := getImage(log.Logger, ns, name, client.Kubernetes())
require.NoError(t, err)

ls := &LocalStorage{
apiObject: &api.ArangoLocalStorage{
ObjectMeta: meta.ObjectMeta{
Name: nameLS,
Namespace: ns,
},
Spec: lsSpec,
},
deps: Dependencies{
Client: client,
},
config: Config{
Namespace: ns,
PodName: name,
},
image: image,
imagePullSecrets: pullSecrets,
imagePullPolicy: pullPolicy,
}

err = ls.ensureDaemonSet(ls.apiObject)
require.NoError(t, err)

// verify if DaemonSet has been created with correct values
ds, err := ls.deps.Client.Kubernetes().AppsV1().DaemonSets(ns).Get(context.Background(), nameLS, meta.GetOptions{})
require.NoError(t, err)

return ls, ds
}