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:
3u13r 2022-07-28 16:07:29 +02:00 committed by GitHub
parent c37fab0a4c
commit e0ce2e8a51
6 changed files with 43 additions and 27 deletions

View File

@ -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 {

View File

@ -77,11 +77,13 @@ var (
}
tolerationsDeployment = appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
Name: "test-deployment",
},
}
selectorsDeployment = appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test-ns",
Name: "test-deployment",
},
Spec: appsv1.DeploymentSpec{
@ -300,17 +302,24 @@ func TestGetObjects(t *testing.T) {
func TestAddTolerationsToDeployment(t *testing.T) {
testCases := map[string]struct {
namespace string
name string
tolerations []corev1.Toleration
wantErr bool
}{
"Success": {
namespace: "test-ns",
name: "test-deployment",
},
"Specifying non-existent deployment fails": {
namespace: "test-ns",
name: "wrong-name",
wantErr: true,
},
"Wrong namespace": {
name: "test-deployment",
wantErr: true,
},
}
for name, tc := range testCases {
@ -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,18 +340,25 @@ 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": {
namespace: "test-ns",
name: "wrong-name",
wantErr: true,
},
"Wrong namespace": {
name: "test-deployment",
wantErr: true,
},
}
for name, tc := range testCases {
@ -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

View File

@ -20,8 +20,8 @@ type Client interface {
// GetObjects converts resources into prepared info fields for use in ApplyOneObject.
GetObjects(resources resources.Marshaler) ([]*resource.Info, error)
CreateConfigMap(ctx context.Context, configMap corev1.ConfigMap) error
AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string) error
AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string) error
AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string, namespace string) error
AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string, namespace string) error
}
// clientGenerator can generate new clients from a kubeconfig.
@ -86,26 +86,26 @@ func (k *Kubectl) CreateConfigMap(ctx context.Context, configMap corev1.ConfigMa
return nil
}
func (k *Kubectl) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string) error {
func (k *Kubectl) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string, namespace string) error {
client, err := k.clientGenerator.NewClient(k.kubeconfig)
if err != nil {
return err
}
if err = client.AddTolerationsToDeployment(ctx, tolerations, name); err != nil {
if err = client.AddTolerationsToDeployment(ctx, tolerations, name, namespace); err != nil {
return err
}
return nil
}
func (k *Kubectl) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string) error {
func (k *Kubectl) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string, namespace string) error {
client, err := k.clientGenerator.NewClient(k.kubeconfig)
if err != nil {
return err
}
if err = client.AddNodeSelectorsToDeployment(ctx, selectors, name); err != nil {
if err = client.AddNodeSelectorsToDeployment(ctx, selectors, name, namespace); err != nil {
return err
}

View File

@ -37,11 +37,11 @@ func (s *stubClient) CreateConfigMap(ctx context.Context, configMap corev1.Confi
return s.createConfigMapErr
}
func (s *stubClient) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string) error {
func (s *stubClient) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string, namespace string) error {
return s.addTolerationsToDeploymentErr
}
func (s *stubClient) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string) error {
func (s *stubClient) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string, namespace string) error {
return s.addNodeSelectorToDeploymentErr
}

View File

@ -45,8 +45,8 @@ type Client interface {
Apply(resources resources.Marshaler, forceConflicts bool) error
SetKubeconfig(kubeconfig []byte)
CreateConfigMap(ctx context.Context, configMap corev1.ConfigMap) error
AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string) error
AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string) error
AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string, namespace string) error
AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string, namespace string) error
}
type installer interface {
@ -235,13 +235,13 @@ func (k *KubernetesUtil) setupGCPPodNetwork(ctx context.Context, nodeName, nodeP
Effect: "NoSchedule",
},
}
if err = kubectl.AddTolerationsToDeployment(ctx, tolerations, "coredns"); err != nil {
if err = kubectl.AddTolerationsToDeployment(ctx, tolerations, "coredns", "kube-system"); err != nil {
return err
}
selectors := map[string]string{
"node-role.kubernetes.io/control-plane": "",
}
if err = kubectl.AddNodeSelectorsToDeployment(ctx, selectors, "coredns"); err != nil {
if err = kubectl.AddNodeSelectorsToDeployment(ctx, selectors, "coredns", "kube-system"); err != nil {
return err
}

View File

@ -625,11 +625,11 @@ func (s *stubKubectl) CreateConfigMap(ctx context.Context, configMap corev1.Conf
return s.createConfigMapErr
}
func (s *stubKubectl) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string) error {
func (s *stubKubectl) AddTolerationsToDeployment(ctx context.Context, tolerations []corev1.Toleration, name string, namespace string) error {
return s.AddTolerationsToDeploymentErr
}
func (s *stubKubectl) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string) error {
func (s *stubKubectl) AddNodeSelectorsToDeployment(ctx context.Context, selectors map[string]string, name string, namespace string) error {
return s.AddTNodeSelectorsToDeploymentErr
}