1
0
Fork 0
mirror of https://github.com/arangodb/kube-arangodb.git synced 2024-12-15 17:51:03 +00:00

[Fix] Assign imagePullSecrets to LocalStorage (#921)

Co-authored-by: Adam Janikowski <12255597+ajanikow@users.noreply.github.com>
This commit is contained in:
jwierzbo 2022-03-04 14:27:50 +01:00 committed by GitHub
parent d873f99068
commit 1b2e8759f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 182 additions and 9 deletions

View file

@ -4,6 +4,7 @@
- (Feature) Improve Kubernetes clientsets management - (Feature) Improve Kubernetes clientsets management
- Migrate storage-operator CustomResourceDefinition apiVersion to apiextensions.k8s.io/v1 - Migrate storage-operator CustomResourceDefinition apiVersion to apiextensions.k8s.io/v1
- (Feature) Add CRD Installer - (Feature) Add CRD Installer
- (Bugfix) Assign imagePullSecrets to LocalStorage
## [1.2.8](https://github.com/arangodb/kube-arangodb/tree/1.2.8) (2022-02-24) ## [1.2.8](https://github.com/arangodb/kube-arangodb/tree/1.2.8) (2022-02-24)
- Do not check License V2 on Community images - Do not check License V2 on Community images

View file

@ -93,7 +93,8 @@ func (ls *LocalStorage) ensureDaemonSet(apiObject *api.ArangoLocalStorage) error
Containers: []core.Container{ Containers: []core.Container{
c, c,
}, },
NodeSelector: apiObject.Spec.NodeSelector, NodeSelector: apiObject.Spec.NodeSelector,
ImagePullSecrets: ls.imagePullSecrets,
}, },
}, },
} }

View file

@ -0,0 +1,85 @@
//
// 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"
"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"
)
// TestEnsureDaemonSet tests ensureDaemonSet() method
func TestEnsureDaemonSet(t *testing.T) {
testNamespace := "testNs"
testLsName := "testDsName"
testPodName := "testPodName"
testImage := "test-image"
testPullSecrets := []v1.LocalObjectReference{
{
Name: "custom-docker",
},
}
ls := &LocalStorage{
apiObject: &api.ArangoLocalStorage{
ObjectMeta: metav1.ObjectMeta{
Name: testLsName,
Namespace: testNamespace,
},
Spec: api.LocalStorageSpec{},
},
deps: Dependencies{
Client: kclient.NewFakeClient(),
},
config: Config{
Namespace: testNamespace,
PodName: testPodName,
},
image: testImage,
imagePullSecrets: testPullSecrets,
imagePullPolicy: v1.PullAlways,
}
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(testNamespace).Get(context.Background(), testLsName, metav1.GetOptions{})
require.NoError(t, err)
pod := ds.Spec.Template.Spec
require.Equal(t, ds.GetName(), testLsName)
require.Equal(t, pod.ImagePullSecrets, testPullSecrets)
require.Equal(t, len(pod.Containers), 1)
c := pod.Containers[0]
require.Equal(t, c.Image, testImage)
require.Equal(t, c.ImagePullPolicy, v1.PullAlways)
}

View file

@ -29,16 +29,17 @@ import (
) )
// getMyImage fetched the docker image from my own pod // getMyImage fetched the docker image from my own pod
func (l *LocalStorage) getMyImage() (string, v1.PullPolicy, error) { func (l *LocalStorage) getMyImage() (string, v1.PullPolicy, []v1.LocalObjectReference, error) {
log := l.deps.Log log := l.deps.Log
ns := l.config.Namespace ns := l.config.Namespace
p, err := l.deps.Client.Kubernetes().CoreV1().Pods(ns).Get(context.Background(), l.config.PodName, metav1.GetOptions{}) p, err := l.deps.Client.Kubernetes().CoreV1().Pods(ns).Get(context.Background(), l.config.PodName, metav1.GetOptions{})
if err != nil { 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", l.config.PodName).Msg("Failed to get my own pod")
return "", "", errors.WithStack(err) return "", "", nil, errors.WithStack(err)
} }
c := p.Spec.Containers[0] c := p.Spec.Containers[0]
return c.Image, c.ImagePullPolicy, nil
return c.Image, c.ImagePullPolicy, p.Spec.ImagePullSecrets, nil
} }

82
pkg/storage/image_test.go Normal file
View file

@ -0,0 +1,82 @@
//
// 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"
"testing"
"github.com/stretchr/testify/require"
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// TestGetMyImage tests getMyImage() method
func TestGetMyImage(t *testing.T) {
testNamespace := "testNs"
testPodName := "testPodname"
testImage := "test-image"
testPullSecrets := []v1.LocalObjectReference{
{
Name: "custom-docker",
},
}
pod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: testPodName,
Namespace: testNamespace,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "test",
Image: testImage,
ImagePullPolicy: v1.PullAlways,
},
},
ImagePullSecrets: testPullSecrets,
},
}
ls := &LocalStorage{
deps: Dependencies{
Client: kclient.NewFakeClient(),
},
config: Config{
Namespace: testNamespace,
PodName: testPodName,
},
}
// prepare mock
if _, err := ls.deps.Client.Kubernetes().CoreV1().Pods(testNamespace).Create(context.Background(), &pod, metav1.CreateOptions{}); err != nil {
require.NoError(t, err)
}
image, pullPolicy, pullSecrets, err := ls.getMyImage()
require.NoError(t, err)
require.Equal(t, image, testImage)
require.Equal(t, pullPolicy, v1.PullAlways)
require.Equal(t, pullSecrets, testPullSecrets)
}

View file

@ -90,10 +90,12 @@ type LocalStorage struct {
stopCh chan struct{} stopCh chan struct{}
stopped int32 stopped int32
image string image string
imagePullPolicy v1.PullPolicy imagePullPolicy v1.PullPolicy
inspectTrigger trigger.Trigger imagePullSecrets []v1.LocalObjectReference
pvCleaner *pvCleaner
inspectTrigger trigger.Trigger
pvCleaner *pvCleaner
} }
// New creates a new LocalStorage from the given API object. // New creates a new LocalStorage from the given API object.
@ -160,13 +162,14 @@ func (ls *LocalStorage) run() {
//log := ls.deps.Log //log := ls.deps.Log
// Find out my image // Find out my image
image, pullPolicy, err := ls.getMyImage() image, pullPolicy, pullSecrets, err := ls.getMyImage()
if err != nil { if err != nil {
ls.failOnError(err, "Failed to get my own image") ls.failOnError(err, "Failed to get my own image")
return return
} }
ls.image = image ls.image = image
ls.imagePullPolicy = pullPolicy ls.imagePullPolicy = pullPolicy
ls.imagePullSecrets = pullSecrets
// Set state // Set state
if ls.status.State == api.LocalStorageStateNone { if ls.status.State == api.LocalStorageStateNone {