|
| 1 | +package extended |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + g "github.com/onsi/ginkgo/v2" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/client-go/dynamic" |
| 12 | + "k8s.io/client-go/kubernetes" |
| 13 | +) |
| 14 | + |
| 15 | +// YamlKmsTestCase represents a KMS test case from YAML |
| 16 | +type YamlKmsTestCase struct { |
| 17 | + Name string `yaml:"name"` |
| 18 | + Initial string `yaml:"initial"` |
| 19 | + Expected string `yaml:"expected,omitempty"` |
| 20 | + ExpectedError string `yaml:"expectedError,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +// ComputeNode interface to handle compute nodes across different cloud platforms |
| 24 | +type ComputeNode interface { |
| 25 | + GetName() string |
| 26 | + GetInstanceID() (string, error) |
| 27 | + CreateKMSKey() string |
| 28 | + LoadKMSTestCasesFromYAML() ([]YamlKmsTestCase, error) |
| 29 | + GetIamRoleNameFromId() string |
| 30 | + RenderKmsKeyPolicy() string |
| 31 | + UpdateKmsPolicy(keyID string) |
| 32 | +} |
| 33 | + |
| 34 | +// instance is the base struct for all compute node implementations |
| 35 | +type instance struct { |
| 36 | + nodeName string |
| 37 | + kubeClient *kubernetes.Clientset |
| 38 | + dynamicClient dynamic.Interface |
| 39 | + ctx context.Context |
| 40 | +} |
| 41 | + |
| 42 | +func (i *instance) GetName() string { |
| 43 | + return i.nodeName |
| 44 | +} |
| 45 | + |
| 46 | +// ComputeNodes handles a collection of ComputeNode interfaces |
| 47 | +type ComputeNodes []ComputeNode |
| 48 | + |
| 49 | +// GetNodes gets master nodes according to platform with the specified label |
| 50 | +func GetNodes(ctx context.Context, kubeClient *kubernetes.Clientset, dynamicClient dynamic.Interface, label string) (ComputeNodes, func()) { |
| 51 | + platform := checkPlatform(kubeClient) |
| 52 | + |
| 53 | + switch platform { |
| 54 | + case "aws": |
| 55 | + return GetAwsNodes(ctx, kubeClient, dynamicClient, label) |
| 56 | + case "gcp": |
| 57 | + g.Skip("GCP platform KMS support not yet implemented") |
| 58 | + return nil, nil |
| 59 | + case "azure": |
| 60 | + g.Skip("Azure platform KMS support not yet implemented") |
| 61 | + return nil, nil |
| 62 | + default: |
| 63 | + g.Skip(fmt.Sprintf("Platform %s is not supported for KMS tests. Expected AWS, GCP, or Azure.", platform)) |
| 64 | + return nil, nil |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// checkPlatform determines the cloud platform of the cluster |
| 69 | +func checkPlatform(kubeClient *kubernetes.Clientset) string { |
| 70 | + // Check for AWS-specific labels or annotations |
| 71 | + nodes, err := kubeClient.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{Limit: 1}) |
| 72 | + if err != nil || len(nodes.Items) == 0 { |
| 73 | + return "unknown" |
| 74 | + } |
| 75 | + |
| 76 | + node := nodes.Items[0] |
| 77 | + |
| 78 | + // Check provider ID format |
| 79 | + if providerID := node.Spec.ProviderID; providerID != "" { |
| 80 | + if strings.HasPrefix(providerID, "aws://") { |
| 81 | + return "aws" |
| 82 | + } |
| 83 | + if strings.HasPrefix(providerID, "gce://") { |
| 84 | + return "gcp" |
| 85 | + } |
| 86 | + if strings.HasPrefix(providerID, "azure://") { |
| 87 | + return "azure" |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return "unknown" |
| 92 | +} |
| 93 | + |
| 94 | +// getAWSRegion gets the AWS region from environment or config |
| 95 | +func getAWSRegion() string { |
| 96 | + if region := os.Getenv("AWS_REGION"); region != "" { |
| 97 | + return region |
| 98 | + } |
| 99 | + // Default to us-east-1 if not specified |
| 100 | + return "us-east-1" |
| 101 | +} |
0 commit comments