Skip to content

Commit

Permalink
Add additional checks
Browse files Browse the repository at this point in the history
Signed-off-by: Tomáš Smädo <[email protected]>
  • Loading branch information
tsmado committed Nov 5, 2024
1 parent cd72350 commit 8a88ddc
Showing 1 changed file with 72 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"fmt"
"log"
"os"
"strings"
"testing"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

// list of common Harbor components.
// list of common harbor components.
var HarborComponentNames = []string{
"harbor-core",
"harbor-db",
Expand All @@ -21,8 +23,20 @@ var HarborComponentNames = []string{
"nginx",
}

// other registries
var OtherRegistries = []string{
"docker-registry",
"quay",
"jfrog",
"artifacthub",
"dragonfly",
"keppel",
"nexus",
"kraken",
}

func Test_scs_0212_registry_standard_test(t *testing.T) {
// Set up the Kubernetes client
// set up the kubernetes client
config, err := rest.InClusterConfig()
if err != nil {
log.Fatalf("Failed to create rest config: %v", err)
Expand All @@ -33,68 +47,88 @@ func Test_scs_0212_registry_standard_test(t *testing.T) {
log.Fatalf("Failed to create Kubernetes client: %v", err)
}

harborComponentsFound := make(map[string]bool)
otherRegistryFound := false

fmt.Println("Checking for Harbor components in Deployments...")
if err := checkDeployments(clientset); err != nil {
log.Fatalf("Error checking deployments: %v", err)
}
otherRegistryFound = checkHarborDeployments(clientset, harborComponentsFound) || otherRegistryFound

fmt.Println("Checking for Harbor components in Services...")
if err := checkServices(clientset); err != nil {
log.Fatalf("Error checking services: %v", err)
otherRegistryFound = checkHarborServices(clientset, harborComponentsFound) || otherRegistryFound

if otherRegistryFound {
log.Fatalf("Non-Harbor registry detected in the cluster. Failing test.")
}

fmt.Println("Harbor check completed.")
// ensure all harbor components are found
for _, component := range HarborComponentNames {
if !harborComponentsFound[component] {
fmt.Printf("Harbor component missing: %s\n", component)
log.Fatalf("Harbor registry not fully deployed. Failing test.")
}
}

fmt.Println("All Harbor components are deployed, and no other registries are found. Test passed.")
os.Exit(0)
}

// check deployments for the registry
func checkDeployments(clientset *kubernetes.Clientset) error {

// checkHarborDeployments checks if required harbor components are present in deployments
// and ensures no other registries are found.
func checkHarborDeployments(clientset *kubernetes.Clientset, harborComponentsFound map[string]bool) bool {
otherRegistryFound := false
deployments, err := clientset.AppsV1().Deployments("").List(context.TODO(), v1.ListOptions{})
harborDeployments := 0
if err != nil {
return fmt.Errorf("failed to list deployments: %v", err)
log.Fatalf("Failed to list deployments: %v", err)
}

for _, deployment := range deployments.Items {
for _, componentName := range HarborComponentNames {
if containsString(deployment.Name, componentName) {
// check for Harbor components
for _, component := range HarborComponentNames {
if strings.Contains(deployment.Name, component) {
harborComponentsFound[component] = true
fmt.Printf("Found Harbor deployment: %s in namespace: %s\n", deployment.Name, deployment.Namespace)
harborDeployments++
}
}

// check for other registries
for _, registry := range OtherRegistries {
if strings.Contains(deployment.Name, registry) {
otherRegistryFound = true
fmt.Printf("Found non-Harbor registry deployment: %s in namespace: %s\n", deployment.Name, deployment.Namespace)
}
}
}
if harborDeployments > 0 {
fmt.Printf("Harbor deployments found\n")
} else {
fmt.Printf("Harbor was not found in deployments\n")
}
return nil

return otherRegistryFound
}

// check services for the registry components
func checkServices(clientset *kubernetes.Clientset) error {
// checks if required harbor components are present in services
// and ensures no other registries are found.
func checkHarborServices(clientset *kubernetes.Clientset, harborComponentsFound map[string]bool) bool {
otherRegistryFound := false
services, err := clientset.CoreV1().Services("").List(context.TODO(), v1.ListOptions{})
harborServices := 0
if err != nil {
return fmt.Errorf("failed to list services: %v", err)
log.Fatalf("Failed to list services: %v", err)
}

for _, service := range services.Items {
for _, componentName := range HarborComponentNames {
if containsString(service.Name, componentName) {
// check for harbor components
for _, component := range HarborComponentNames {
if strings.Contains(service.Name, component) {
harborComponentsFound[component] = true
fmt.Printf("Found Harbor service: %s in namespace: %s\n", service.Name, service.Namespace)
harborServices++
}
}

// check for other registries
for _, registry := range OtherRegistries {
if strings.Contains(service.Name, registry) {
otherRegistryFound = true
fmt.Printf("Found non-Harbor registry service: %s in namespace: %s\n", service.Name, service.Namespace)
}
}
}
if harborServices > 0 {
fmt.Printf("Harbor services found\n")
} else {
fmt.Printf("Harbor was not found services\n")
}
return nil
}

// containsString checks if a string is contained in another string (case-insensitive).
func containsString(str, substr string) bool {
return len(str) > 0 && len(substr) > 0 && (str == substr || (len(str) > len(substr) && str[:len(substr)] == substr))
return otherRegistryFound
}

0 comments on commit 8a88ddc

Please sign in to comment.