mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-08-06 05:54:28 -04:00
add namespace to kubectl requests (#315)
* add namespace to kubectl requests * Add tests for missing/wrong namespace Co-authored-by: Otto Bittner <cobittner@posteo.net>
This commit is contained in:
parent
c37fab0a4c
commit
e0ce2e8a51
6 changed files with 43 additions and 27 deletions
|
@ -101,14 +101,14 @@ func (c *Client) CreateConfigMap(ctx context.Context, configMap corev1.ConfigMap
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string) error {
|
||||
deployments := c.clientset.AppsV1().Deployments(corev1.NamespaceAll)
|
||||
func (c *Client) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string, namespace string) error {
|
||||
deployments := c.clientset.AppsV1().Deployments(namespace)
|
||||
|
||||
// retry resource update if an error occurs
|
||||
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
||||
result, err := deployments.Get(ctx, name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to get latest version of Deployment: %v", err)
|
||||
return fmt.Errorf("failed to get Deployment to add toleration: %v", err)
|
||||
}
|
||||
|
||||
result.Spec.Template.Spec.Tolerations = append(result.Spec.Template.Spec.Tolerations, tolerations...)
|
||||
|
@ -123,14 +123,14 @@ func (c *Client) AddTolerationsToDeployment(ctx context.Context, tolerations []c
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string) error {
|
||||
deployments := c.clientset.AppsV1().Deployments(corev1.NamespaceAll)
|
||||
func (c *Client) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string, namespace string) error {
|
||||
deployments := c.clientset.AppsV1().Deployments(namespace)
|
||||
|
||||
// retry resource update if an error occurs
|
||||
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
|
||||
result, err := deployments.Get(ctx, name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to get latest version of Deployment: %v", err)
|
||||
return fmt.Errorf("failed to get Deployment to add node selector: %v", err)
|
||||
}
|
||||
|
||||
for k, v := range selectors {
|
||||
|
|
|
@ -77,12 +77,14 @@ var (
|
|||
}
|
||||
tolerationsDeployment = appsv1.Deployment{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-deployment",
|
||||
Namespace: "test-ns",
|
||||
Name: "test-deployment",
|
||||
},
|
||||
}
|
||||
selectorsDeployment = appsv1.Deployment{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-deployment",
|
||||
Namespace: "test-ns",
|
||||
Name: "test-deployment",
|
||||
},
|
||||
Spec: appsv1.DeploymentSpec{
|
||||
Template: k8s.PodTemplateSpec{
|
||||
|
@ -300,15 +302,22 @@ func TestGetObjects(t *testing.T) {
|
|||
|
||||
func TestAddTolerationsToDeployment(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
namespace string
|
||||
name string
|
||||
tolerations []corev1.Toleration
|
||||
wantErr bool
|
||||
}{
|
||||
"Success": {
|
||||
name: "test-deployment",
|
||||
namespace: "test-ns",
|
||||
name: "test-deployment",
|
||||
},
|
||||
"Specifying non-existent deployment fails": {
|
||||
name: "wrong-name",
|
||||
namespace: "test-ns",
|
||||
name: "wrong-name",
|
||||
wantErr: true,
|
||||
},
|
||||
"Wrong namespace": {
|
||||
name: "test-deployment",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
@ -319,7 +328,7 @@ func TestAddTolerationsToDeployment(t *testing.T) {
|
|||
require := require.New(t)
|
||||
|
||||
client := newClientWithFakes(t, map[string]string{}, &tolerationsDeployment)
|
||||
err := client.AddTolerationsToDeployment(context.Background(), tc.tolerations, tc.name)
|
||||
err := client.AddTolerationsToDeployment(context.Background(), tc.tolerations, tc.name, tc.namespace)
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
|
@ -331,16 +340,23 @@ func TestAddTolerationsToDeployment(t *testing.T) {
|
|||
|
||||
func TestAddNodeSelectorsToDeployment(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
namespace string
|
||||
name string
|
||||
selectors map[string]string
|
||||
wantErr bool
|
||||
}{
|
||||
"Success": {
|
||||
namespace: "test-ns",
|
||||
name: "test-deployment",
|
||||
selectors: map[string]string{"some-key": "some-value"},
|
||||
},
|
||||
"Specifying non-existent deployment fails": {
|
||||
name: "wrong-name",
|
||||
namespace: "test-ns",
|
||||
name: "wrong-name",
|
||||
wantErr: true,
|
||||
},
|
||||
"Wrong namespace": {
|
||||
name: "test-deployment",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
@ -351,7 +367,7 @@ func TestAddNodeSelectorsToDeployment(t *testing.T) {
|
|||
require := require.New(t)
|
||||
|
||||
client := newClientWithFakes(t, map[string]string{}, &selectorsDeployment)
|
||||
err := client.AddNodeSelectorsToDeployment(context.Background(), tc.selectors, tc.name)
|
||||
err := client.AddNodeSelectorsToDeployment(context.Background(), tc.selectors, tc.name, tc.namespace)
|
||||
if tc.wantErr {
|
||||
assert.Error(err)
|
||||
return
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue