Skip to content

Commit 1b2e875

Browse files
jwierzboajanikow
andauthored
[Fix] Assign imagePullSecrets to LocalStorage (#921)
Co-authored-by: Adam Janikowski <12255597+ajanikow@users.noreply.github.com>
1 parent d873f99 commit 1b2e875

File tree

6 files changed

+182
-9
lines changed

6 files changed

+182
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- (Feature) Improve Kubernetes clientsets management
55
- Migrate storage-operator CustomResourceDefinition apiVersion to apiextensions.k8s.io/v1
66
- (Feature) Add CRD Installer
7+
- (Bugfix) Assign imagePullSecrets to LocalStorage
78

89
## [1.2.8](https://github.com/arangodb/kube-arangodb/tree/1.2.8) (2022-02-24)
910
- Do not check License V2 on Community images

pkg/storage/daemon_set.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func (ls *LocalStorage) ensureDaemonSet(apiObject *api.ArangoLocalStorage) error
9393
Containers: []core.Container{
9494
c,
9595
},
96-
NodeSelector: apiObject.Spec.NodeSelector,
96+
NodeSelector: apiObject.Spec.NodeSelector,
97+
ImagePullSecrets: ls.imagePullSecrets,
9798
},
9899
},
99100
}

pkg/storage/daemon_set_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
"testing"
26+
27+
api "github.com/arangodb/kube-arangodb/pkg/apis/storage/v1alpha"
28+
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
29+
30+
"github.com/stretchr/testify/require"
31+
v1 "k8s.io/api/core/v1"
32+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
)
34+
35+
// TestEnsureDaemonSet tests ensureDaemonSet() method
36+
func TestEnsureDaemonSet(t *testing.T) {
37+
testNamespace := "testNs"
38+
testLsName := "testDsName"
39+
40+
testPodName := "testPodName"
41+
testImage := "test-image"
42+
43+
testPullSecrets := []v1.LocalObjectReference{
44+
{
45+
Name: "custom-docker",
46+
},
47+
}
48+
49+
ls := &LocalStorage{
50+
apiObject: &api.ArangoLocalStorage{
51+
ObjectMeta: metav1.ObjectMeta{
52+
Name: testLsName,
53+
Namespace: testNamespace,
54+
},
55+
Spec: api.LocalStorageSpec{},
56+
},
57+
deps: Dependencies{
58+
Client: kclient.NewFakeClient(),
59+
},
60+
config: Config{
61+
Namespace: testNamespace,
62+
PodName: testPodName,
63+
},
64+
image: testImage,
65+
imagePullSecrets: testPullSecrets,
66+
imagePullPolicy: v1.PullAlways,
67+
}
68+
69+
err := ls.ensureDaemonSet(ls.apiObject)
70+
require.NoError(t, err)
71+
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)
75+
76+
pod := ds.Spec.Template.Spec
77+
78+
require.Equal(t, ds.GetName(), testLsName)
79+
require.Equal(t, pod.ImagePullSecrets, testPullSecrets)
80+
require.Equal(t, len(pod.Containers), 1)
81+
82+
c := pod.Containers[0]
83+
require.Equal(t, c.Image, testImage)
84+
require.Equal(t, c.ImagePullPolicy, v1.PullAlways)
85+
}

pkg/storage/image.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ import (
2929
)
3030

3131
// getMyImage fetched the docker image from my own pod
32-
func (l *LocalStorage) getMyImage() (string, v1.PullPolicy, error) {
32+
func (l *LocalStorage) getMyImage() (string, v1.PullPolicy, []v1.LocalObjectReference, error) {
3333
log := l.deps.Log
3434
ns := l.config.Namespace
3535

3636
p, err := l.deps.Client.Kubernetes().CoreV1().Pods(ns).Get(context.Background(), l.config.PodName, metav1.GetOptions{})
3737
if err != nil {
3838
log.Debug().Err(err).Str("pod-name", l.config.PodName).Msg("Failed to get my own pod")
39-
return "", "", errors.WithStack(err)
39+
return "", "", nil, errors.WithStack(err)
4040
}
4141

4242
c := p.Spec.Containers[0]
43-
return c.Image, c.ImagePullPolicy, nil
43+
44+
return c.Image, c.ImagePullPolicy, p.Spec.ImagePullSecrets, nil
4445
}

pkg/storage/image_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
"testing"
26+
27+
"github.com/stretchr/testify/require"
28+
29+
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
30+
v1 "k8s.io/api/core/v1"
31+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32+
)
33+
34+
// TestGetMyImage tests getMyImage() method
35+
func TestGetMyImage(t *testing.T) {
36+
testNamespace := "testNs"
37+
testPodName := "testPodname"
38+
testImage := "test-image"
39+
testPullSecrets := []v1.LocalObjectReference{
40+
{
41+
Name: "custom-docker",
42+
},
43+
}
44+
45+
pod := v1.Pod{
46+
ObjectMeta: metav1.ObjectMeta{
47+
Name: testPodName,
48+
Namespace: testNamespace,
49+
},
50+
Spec: v1.PodSpec{
51+
Containers: []v1.Container{
52+
{
53+
Name: "test",
54+
Image: testImage,
55+
ImagePullPolicy: v1.PullAlways,
56+
},
57+
},
58+
ImagePullSecrets: testPullSecrets,
59+
},
60+
}
61+
62+
ls := &LocalStorage{
63+
deps: Dependencies{
64+
Client: kclient.NewFakeClient(),
65+
},
66+
config: Config{
67+
Namespace: testNamespace,
68+
PodName: testPodName,
69+
},
70+
}
71+
72+
// prepare mock
73+
if _, err := ls.deps.Client.Kubernetes().CoreV1().Pods(testNamespace).Create(context.Background(), &pod, metav1.CreateOptions{}); err != nil {
74+
require.NoError(t, err)
75+
}
76+
77+
image, pullPolicy, pullSecrets, err := ls.getMyImage()
78+
require.NoError(t, err)
79+
require.Equal(t, image, testImage)
80+
require.Equal(t, pullPolicy, v1.PullAlways)
81+
require.Equal(t, pullSecrets, testPullSecrets)
82+
}

pkg/storage/local_storage.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ type LocalStorage struct {
9090
stopCh chan struct{}
9191
stopped int32
9292

93-
image string
94-
imagePullPolicy v1.PullPolicy
95-
inspectTrigger trigger.Trigger
96-
pvCleaner *pvCleaner
93+
image string
94+
imagePullPolicy v1.PullPolicy
95+
imagePullSecrets []v1.LocalObjectReference
96+
97+
inspectTrigger trigger.Trigger
98+
pvCleaner *pvCleaner
9799
}
98100

99101
// New creates a new LocalStorage from the given API object.
@@ -160,13 +162,14 @@ func (ls *LocalStorage) run() {
160162
//log := ls.deps.Log
161163

162164
// Find out my image
163-
image, pullPolicy, err := ls.getMyImage()
165+
image, pullPolicy, pullSecrets, err := ls.getMyImage()
164166
if err != nil {
165167
ls.failOnError(err, "Failed to get my own image")
166168
return
167169
}
168170
ls.image = image
169171
ls.imagePullPolicy = pullPolicy
172+
ls.imagePullSecrets = pullSecrets
170173

171174
// Set state
172175
if ls.status.State == api.LocalStorageStateNone {

0 commit comments

Comments
 (0)