|
| 1 | +package scs_k8s_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "testing" |
| 8 | + |
| 9 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/client-go/kubernetes" |
| 11 | + "k8s.io/client-go/rest" |
| 12 | +) |
| 13 | + |
| 14 | +// list of common Harbor components. |
| 15 | +var HarborComponentNames = []string{ |
| 16 | + "harbor-core", |
| 17 | + "harbor-db", |
| 18 | + "harbor-jobservice", |
| 19 | + "harbor-portal", |
| 20 | + "harbor-registry", |
| 21 | + "nginx", |
| 22 | +} |
| 23 | + |
| 24 | +func Test_scs_0212_registry_standard_test(t *testing.T) { |
| 25 | + // Set up the Kubernetes client |
| 26 | + config, err := rest.InClusterConfig() |
| 27 | + if err != nil { |
| 28 | + log.Fatalf("Failed to create rest config: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + clientset, err := kubernetes.NewForConfig(config) |
| 32 | + if err != nil { |
| 33 | + log.Fatalf("Failed to create Kubernetes client: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + fmt.Println("Checking for Harbor components in Deployments...") |
| 37 | + if err := checkDeployments(clientset); err != nil { |
| 38 | + log.Fatalf("Error checking deployments: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + fmt.Println("Checking for Harbor components in Services...") |
| 42 | + if err := checkServices(clientset); err != nil { |
| 43 | + log.Fatalf("Error checking services: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + fmt.Println("Harbor check completed.") |
| 47 | +} |
| 48 | + |
| 49 | +// check deployments for the registry |
| 50 | +func checkDeployments(clientset *kubernetes.Clientset) error { |
| 51 | + deployments, err := clientset.AppsV1().Deployments("").List(context.TODO(), v1.ListOptions{}) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to list deployments: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + for _, deployment := range deployments.Items { |
| 57 | + for _, componentName := range HarborComponentNames { |
| 58 | + if containsString(deployment.Name, componentName) { |
| 59 | + fmt.Printf("Found Harbor deployment: %s in namespace: %s\n", deployment.Name, deployment.Namespace) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +// check services for the registry components |
| 67 | +func checkServices(clientset *kubernetes.Clientset) error { |
| 68 | + services, err := clientset.CoreV1().Services("").List(context.TODO(), v1.ListOptions{}) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("failed to list services: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + for _, service := range services.Items { |
| 74 | + for _, componentName := range HarborComponentNames { |
| 75 | + if containsString(service.Name, componentName) { |
| 76 | + fmt.Printf("Found Harbor service: %s in namespace: %s\n", service.Name, service.Namespace) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +// containsString checks if a string is contained in another string (case-insensitive). |
| 84 | +func containsString(str, substr string) bool { |
| 85 | + return len(str) > 0 && len(substr) > 0 && (str == substr || (len(str) > len(substr) && str[:len(substr)] == substr)) |
| 86 | +} |
0 commit comments