Skip to content
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

Create namespace/sa/rolebinding #133

Merged
merged 1 commit into from
Feb 29, 2024
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
13 changes: 12 additions & 1 deletion cmd/k8s-netperf/k8s-netperf.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ var rootCmd = &cobra.Command{
log.Warn("😥 Prometheus is not available")
}

// Build the namespace and create the sa account
err = k8s.BuildInfra(client)
if err != nil {
log.Error(err)
os.Exit(1)
}

// Build the SUT (Deployments)
err = k8s.BuildSUT(client, &s)
if err != nil {
Expand Down Expand Up @@ -345,7 +352,11 @@ func cleanup(client *kubernetes.Clientset) {
os.Exit(1)
}
}

err = k8s.DestroyNamespace(client)
if err != nil {
log.Error(err)
os.Exit(1)
}
}

func executeWorkload(nc config.Config, s config.PerfScenarios, hostNet bool, iperf3 bool, uperf bool) result.Data {
Expand Down
64 changes: 64 additions & 0 deletions pkg/k8s/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cloud-bulldozer/k8s-netperf/pkg/metrics"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
v1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/watch"
Expand Down Expand Up @@ -41,6 +42,7 @@ type ServiceParams struct {
}

const sa string = "netperf"
const namespace string = "netperf"

// NetperfServerCtlPort control port for the service
const NetperfServerCtlPort = 12865
Expand All @@ -67,6 +69,56 @@ const clientAcrossRole = "client-across"
const hostNetServerRole = "host-server"
const hostNetClientRole = "host-client"

func BuildInfra(client *kubernetes.Clientset) error {
_, err := client.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
if err == nil {
log.Infof("♻️ Namespace already exists, reusing it")
} else {
log.Infof("🔨 Creating namespace : %s", namespace)
_, err := client.CoreV1().Namespaces().Create(context.TODO(), &apiv1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("😥 Unable to create namespace - %s", err)
}
}
_, err = client.CoreV1().ServiceAccounts(namespace).Get(context.TODO(), sa, metav1.GetOptions{})
if err == nil {
log.Infof("♻️ Service account already exists, reusing it")
} else {
log.Infof("🔨 Creating service account : %s", sa)
_, err = client.CoreV1().ServiceAccounts(namespace).Create(context.TODO(), &apiv1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: sa}}, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("😥 Unable to create service account")
}
}
rBinding := &v1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: sa,
Namespace: namespace,
},
RoleRef: v1.RoleRef{
Kind: "ClusterRole",
Name: "system:openshift:scc:hostnetwork",
jtaleric marked this conversation as resolved.
Show resolved Hide resolved
},
Subjects: []v1.Subject{
{
Namespace: namespace,
Name: sa,
Kind: "ServiceAccount",
},
},
}
_, err = client.RbacV1().RoleBindings(namespace).Get(context.TODO(), sa, metav1.GetOptions{})
if err == nil {
log.Infof("♻️ Role binding already exists, reusing it")
} else {
_, err = client.RbacV1().RoleBindings(namespace).Create(context.TODO(), rBinding, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("😥 Unable to create role-binding")
}
}
return nil
}

// BuildSUT Build the k8s env to run network performance tests
func BuildSUT(client *kubernetes.Clientset, s *config.PerfScenarios) error {
// Check if nodes have the zone label to keep the netperf test
Expand Down Expand Up @@ -654,6 +706,18 @@ func DestroyService(client *kubernetes.Clientset, serv apiv1.Service) error {
})
}

// DestroyNamespace cleans up the namespace k8s-netperf created
func DestroyNamespace(client *kubernetes.Clientset) error {
_, err := client.CoreV1().Namespaces().Get(context.TODO(), namespace, metav1.GetOptions{})
if err == nil {
deletePolicy := metav1.DeletePropagationForeground
return client.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
}
return nil
}

// DestroyDeployment cleans up a specific deployment from a namespace
func DestroyDeployment(client *kubernetes.Clientset, dp appsv1.Deployment) error {
deletePolicy := metav1.DeletePropagationForeground
Expand Down
1 change: 0 additions & 1 deletion testing/kind-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
Loading