2021-12-21 14:23:26 +00:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-14 07:47:57 +00:00
|
|
|
"errors"
|
2021-05-11 09:28:56 +00:00
|
|
|
"fmt"
|
2020-09-03 09:34:29 +00:00
|
|
|
|
2021-01-14 07:47:57 +00:00
|
|
|
core "k8s.io/api/core/v1"
|
2020-09-03 09:34:29 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2023-02-01 17:06:36 +00:00
|
|
|
e2enode "k8s.io/kubernetes/test/e2e/framework/node"
|
2023-11-02 11:12:41 +00:00
|
|
|
testutils "k8s.io/kubernetes/test/utils"
|
2020-09-03 09:34:29 +00:00
|
|
|
)
|
|
|
|
|
2023-11-02 11:12:41 +00:00
|
|
|
func addLabelsToNodes(f *framework.Framework, labels map[string]string) error {
|
2020-09-03 09:34:29 +00:00
|
|
|
// NOTE: This makes all nodes (in a multi-node setup) in the test take
|
|
|
|
// the same label values, which is fine for the test
|
|
|
|
nodes, err := f.ClientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to list node: %w", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
for i := range nodes.Items {
|
2023-11-02 11:12:41 +00:00
|
|
|
if err := testutils.AddLabelsToNode(f.ClientSet, nodes.Items[i].Name, labels); err != nil {
|
|
|
|
return fmt.Errorf("failed to add labels to node: %w", err)
|
|
|
|
}
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteNodeLabel(c kubernetes.Interface, labelKey string) error {
|
|
|
|
nodes, err := c.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to list node: %w", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
for i := range nodes.Items {
|
2023-02-01 17:06:36 +00:00
|
|
|
e2enode.RemoveLabelOffNode(c, nodes.Items[i].Name, labelKey)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkNodeHasLabel(c kubernetes.Interface, labelKey, labelValue string) error {
|
|
|
|
nodes, err := c.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return fmt.Errorf("failed to list node: %w", err)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
|
|
|
for i := range nodes.Items {
|
2023-06-02 15:25:01 +00:00
|
|
|
e2enode.ExpectNodeHasLabel(context.TODO(), c, nodes.Items[i].Name, labelKey, labelValue)
|
2020-09-03 09:34:29 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-03 09:34:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-14 07:47:57 +00:00
|
|
|
|
|
|
|
// List all nodes in the cluster (we have one), and return the IP-address.
|
|
|
|
// Possibly need to add a selector, pick the node where a Pod is running?
|
|
|
|
func getKubeletIP(c kubernetes.Interface) (string, error) {
|
|
|
|
nodes, err := c.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
|
|
|
|
if err != nil {
|
2021-05-11 09:28:56 +00:00
|
|
|
return "", fmt.Errorf("failed to list node: %w", err)
|
2021-01-14 07:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range nodes.Items[0].Status.Addresses {
|
|
|
|
if address.Type == core.NodeInternalIP {
|
|
|
|
return address.Address, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("could not find internal IP for node")
|
|
|
|
}
|