Skip to content

Add common functions for SDK test #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions support/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,23 @@ func CreatePersistentVolumeClaim(t Test, namespace string, storageSize string, a

return pvc
}

func GetNodes(t Test) []corev1.Node {
t.T().Helper()
nodes, err := t.Client().Core().CoreV1().Nodes().List(t.Ctx(), metav1.ListOptions{})
t.Expect(err).NotTo(gomega.HaveOccurred())
return nodes.Items
}

func GetNodeInternalIP(t Test, node corev1.Node) (IP string) {
t.T().Helper()

for _, address := range node.Status.Addresses {
if address.Type == "InternalIP" {
IP = address.Address
}
}
t.Expect(IP).Should(gomega.Not(gomega.BeEmpty()), "Node internal IP address not found")

return
}
4 changes: 2 additions & 2 deletions support/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package support
// ***********************

const (
CodeFlareSDKVersion = "0.9.0"
CodeFlareSDKVersion = "0.12.0"
RayVersion = "2.5.0"
RayImage = "rayproject/ray:2.5.0"
RayImage = "quay.io/project-codeflare/ray:latest-py39-cu118"
)
14 changes: 14 additions & 0 deletions support/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const (

// Type of cluster test is run on
ClusterTypeEnvVar = "CLUSTER_TYPE"

// Hostname of the Kubernetes cluster
ClusterHostname = "CLUSTER_HOSTNAME"
)

type ClusterType string
Expand All @@ -49,6 +52,7 @@ const (
OsdCluster ClusterType = "OSD"
OcpCluster ClusterType = "OCP"
HypershiftCluster ClusterType = "HYPERSHIFT"
KindCluster ClusterType = "KIND"
UndefinedCluster ClusterType = "UNDEFINED"
)

Expand Down Expand Up @@ -90,12 +94,22 @@ func GetClusterType(t Test) ClusterType {
return OcpCluster
case "HYPERSHIFT":
return HypershiftCluster
case "KIND":
return KindCluster
default:
t.T().Logf("Expected environment variable %s contains unexpected value: '%s'", ClusterTypeEnvVar, clusterType)
return UndefinedCluster
}
}

func GetClusterHostname(t Test) string {
hostname, ok := os.LookupEnv(ClusterHostname)
if !ok {
t.T().Fatalf("Expected environment variable %s not found, please define cluster hostname.", ClusterHostname)
}
return hostname
}

func lookupEnvOrDefault(key, value string) string {
if v, ok := os.LookupEnv(key); ok {
return v
Expand Down
60 changes: 60 additions & 0 deletions support/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ func CreateRole(t Test, namespace string, policyRules []rbacv1.PolicyRule) *rbac
return role
}

func CreateClusterRole(t Test, policyRules []rbacv1.PolicyRule) *rbacv1.ClusterRole {
t.T().Helper()

role := &rbacv1.ClusterRole{
TypeMeta: metav1.TypeMeta{
APIVersion: rbacv1.SchemeGroupVersion.String(),
Kind: "ClusterRole",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "clusterrole-",
},
Rules: policyRules,
}
role, err := t.Client().Core().RbacV1().ClusterRoles().Create(t.Ctx(), role, metav1.CreateOptions{})
t.Expect(err).NotTo(gomega.HaveOccurred())
t.T().Logf("Created ClusterRole %s/%s successfully", role.Namespace, role.Name)

t.T().Cleanup(func() {
t.Client().Core().RbacV1().ClusterRoles().Delete(t.Ctx(), role.Name, metav1.DeleteOptions{})
})

return role
}

func CreateRoleBinding(t Test, namespace string, serviceAccount *corev1.ServiceAccount, role *rbacv1.Role) *rbacv1.RoleBinding {
t.T().Helper()

Expand Down Expand Up @@ -76,3 +100,39 @@ func CreateRoleBinding(t Test, namespace string, serviceAccount *corev1.ServiceA

return rb
}

func CreateClusterRoleBinding(t Test, serviceAccount *corev1.ServiceAccount, role *rbacv1.ClusterRole) *rbacv1.ClusterRoleBinding {
t.T().Helper()

roleBinding := &rbacv1.ClusterRoleBinding{
TypeMeta: metav1.TypeMeta{
APIVersion: rbacv1.SchemeGroupVersion.String(),
Kind: "ClusterRoleBinding",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "crb-",
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.SchemeGroupVersion.Group,
Kind: "ClusterRole",
Name: role.Name,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
APIGroup: corev1.SchemeGroupVersion.Group,
Name: serviceAccount.Name,
Namespace: serviceAccount.Namespace,
},
},
}
rb, err := t.Client().Core().RbacV1().ClusterRoleBindings().Create(t.Ctx(), roleBinding, metav1.CreateOptions{})
t.Expect(err).NotTo(gomega.HaveOccurred())
t.T().Logf("Created ClusterRoleBinding %s/%s successfully", role.Namespace, role.Name)

t.T().Cleanup(func() {
t.Client().Core().RbacV1().ClusterRoleBindings().Delete(t.Ctx(), rb.Name, metav1.DeleteOptions{})
})

return rb
}