From 2c00a7ee923a173d4f850535842575d274c77d45 Mon Sep 17 00:00:00 2001 From: Stanislav Kosorin Date: Sun, 1 Sep 2024 20:13:05 +0200 Subject: [PATCH] Working port add, daemonset --- bmv2-cni/main.go | 82 ++++++++----------- .../Containerfile | 64 +++------------ .../manifests/kube-router-daemonset.yaml | 22 ++--- .../container_migration_in_kubernetes/rst.sh | 13 +++ 4 files changed, 71 insertions(+), 110 deletions(-) create mode 100755 examples/container_migration_in_kubernetes/rst.sh diff --git a/bmv2-cni/main.go b/bmv2-cni/main.go index 43d6118..984e905 100644 --- a/bmv2-cni/main.go +++ b/bmv2-cni/main.go @@ -4,11 +4,12 @@ import ( "encoding/json" "fmt" "log" + "math/rand" "net" "os" "os/exec" - "strconv" "strings" + "time" "github.com/containernetworking/cni/pkg/skel" "github.com/containernetworking/cni/pkg/types" @@ -166,61 +167,46 @@ func cmdDel(args *skel.CmdArgs) error { func connectToBMv2Switch(ifName, thriftPort string) error { logger.Printf("Connecting veth %s to BMv2 switch on thrift port %s", ifName, thriftPort) + rng := rand.New(rand.NewSource(time.Now().UnixNano())) + randomPortNum := rng.Intn(65535) + 1 - // Run show_ports command to get currently used port numbers cmd := exec.Command("simple_switch_CLI", "--thrift-port", thriftPort) - cmd.Stdin = strings.NewReader("show_ports\n") + cmd.Stdin = strings.NewReader(fmt.Sprintf("port_add %s %d\n", ifName, randomPortNum)) output, err := cmd.CombinedOutput() - if err != nil { - logger.Printf("Error running show_ports: %v. Output: %s", err, output) - return fmt.Errorf("failed to run show_ports: %s", output) - } - - // Parse the output to find the highest port number - nextPortNum, err := getNextPortNumber(string(output)) - if err != nil { - logger.Printf("Error determining next port number: %v", err) - return err - } - - // Use the next available port number - cmd = exec.Command("simple_switch_CLI", "--thrift-port", thriftPort) - cmd.Stdin = strings.NewReader(fmt.Sprintf("port_add %s %d\n", ifName, nextPortNum)) - output, err = cmd.CombinedOutput() if err != nil { logger.Printf("Error adding port to BMv2 switch: %v. Output: %s", err, output) return fmt.Errorf("failed to add port to BMv2 switch: %s", output) } - logger.Printf("Port %d added to BMv2 switch successfully", nextPortNum) + logger.Printf("Port %d added to BMv2 switch successfully", randomPortNum) return nil } // Helper function to parse the show_ports output and determine the next available port number -func getNextPortNumber(output string) (int, error) { - lines := strings.Split(output, "\n") - for i, line := range lines { - logger.Printf("Line %d: %s", i, line) - } - numLines := len(lines) - - if numLines < 3 { - // If there are fewer than 2 lines, default to port 0 - return 0, nil - } - - // Get the second-to-last line - secondToLastLine := lines[numLines-3] - logger.Printf("Second-to-last line: %s", secondToLastLine) - fields := strings.Fields(secondToLastLine) - - // Attempt to parse the first field as the port number - if len(fields) > 0 { - portNum, err := strconv.Atoi(fields[0]) - if err == nil { - return portNum + 1, nil // Return the next available port number - } - } - - // Default to port 0 if parsing fails - return 0, nil -} +// func getNextPortNumber(output string) (int, error) { +// lines := strings.Split(output, "\n") +// for i, line := range lines { +// logger.Printf("Line %d: %s", i, line) +// } +// numLines := len(lines) + +// if numLines < 3 { +// // If there are fewer than 2 lines, default to port 0 +// return 0, nil +// } + +// // Get the second-to-last line +// secondToLastLine := lines[numLines-3] +// logger.Printf("Second-to-last line: %s", secondToLastLine) +// fields := strings.Fields(secondToLastLine) + +// // Attempt to parse the first field as the port number +// if len(fields) > 0 { +// portNum, err := strconv.Atoi(fields[0]) +// if err == nil { +// return portNum + 1, nil // Return the next available port number +// } +// } + +// // Default to port 0 if parsing fails +// return 0, nil +// } diff --git a/examples/container_migration_in_kubernetes/Containerfile b/examples/container_migration_in_kubernetes/Containerfile index 9bfb233..da2d132 100644 --- a/examples/container_migration_in_kubernetes/Containerfile +++ b/examples/container_migration_in_kubernetes/Containerfile @@ -1,58 +1,20 @@ -ARG PARENT_VERSION=latest -FROM p4lang/pi:${PARENT_VERSION} -LABEL maintainer="P4 Developers " +FROM debian:bullseye-slim -ARG CC=gcc -ARG CXX=g++ -ARG GCOV= -ARG sswitch_grpc=yes - -ENV BM_DEPS automake \ - build-essential \ - clang-8 \ - clang-10 \ +RUN apt-get update && apt-get install -y --no-install-recommends \ + ca-certificates \ curl \ - git \ - lcov \ - libgmp-dev \ - libpcap-dev \ - libboost-dev \ - libboost-program-options-dev \ - libboost-system-dev \ - libboost-filesystem-dev \ - libboost-thread-dev \ - libtool \ - pkg-config \ - ca-certificates - -ENV BM_RUNTIME_DEPS libboost-program-options1.71.0 \ - libboost-system1.71.0 \ - libboost-filesystem1.71.0 \ - libboost-thread1.71.0 \ - libgmp10 \ - libpcap0.8 \ - python3 \ - python-is-python3 - -# Install dependencies -RUN apt-get update && \ - apt-get install -y --no-install-recommends $BM_DEPS $BM_RUNTIME_DEPS - -# Clone the repository -RUN git clone https://github.com/p4lang/behavioral-model.git /behavioral-model - -# Set working directory -WORKDIR /behavioral-model/ + gnupg2 \ + && rm -rf /var/lib/apt/lists/* -RUN ./autogen.sh +RUN echo "Acquire::ForceIPv4 \"true\";" > /etc/apt/apt.conf.d/99force-ipv4 -RUN ./configure --with-thrift --with-pdfixed --with-pi +RUN echo 'deb http://download.opensuse.org/repositories/home:/p4lang/Debian_11/ /' > /etc/apt/sources.list.d/home:p4lang.list \ + && curl -fsSL https://download.opensuse.org/repositories/home:p4lang/Debian_11/Release.key | gpg --dearmor -o /etc/apt/trusted.gpg.d/home_p4lang.gpg -RUN make -j2 +RUN apt-get update && apt-get install -y --no-install-recommends \ + p4lang-bmv2 \ + && rm -rf /var/lib/apt/lists/* -RUN make install-strip && ldconfig +ENTRYPOINT ["simple_switch_grpc"] -RUN apt-get purge $BM_DEPS && \ - apt-get autoremove -y --purge && \ - rm -rf /behavioral-model /var/cache/apt/* /var/lib/apt/lists/* && \ - echo 'Image done' +CMD ["--help"] diff --git a/examples/container_migration_in_kubernetes/manifests/kube-router-daemonset.yaml b/examples/container_migration_in_kubernetes/manifests/kube-router-daemonset.yaml index ffb2af5..eb235bb 100644 --- a/examples/container_migration_in_kubernetes/manifests/kube-router-daemonset.yaml +++ b/examples/container_migration_in_kubernetes/manifests/kube-router-daemonset.yaml @@ -33,19 +33,19 @@ spec: - key: "node-role.kubernetes.io/master" effect: "NoSchedule" containers: - - name: s1-container - image: docker.io/p4lang/p4c:1.2.4.13 + - name: bmv2-switch + image: docker.io/skosorin/bmv2 imagePullPolicy: IfNotPresent - command: ["/bin/sh", "-c"] args: - - | - simple_switch_grpc \ - --device-id 0 \ - /load_balance.json \ - --log-console \ - --thrift-port 9090 \ - -- \ - --grpc-server-addr 0.0.0.0:50051 + - "--device-id" + - "0" + - "/load_balance.json" + - "--log-console" + - "--thrift-port" + - "9090" + - "--" + - "--grpc-server-addr" + - "0.0.0.0:50051" securityContext: privileged: true volumeMounts: diff --git a/examples/container_migration_in_kubernetes/rst.sh b/examples/container_migration_in_kubernetes/rst.sh new file mode 100755 index 0000000..ce50ace --- /dev/null +++ b/examples/container_migration_in_kubernetes/rst.sh @@ -0,0 +1,13 @@ +sudo kubeadm reset -f +sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --cri-socket=unix:///var/run/crio/crio.sock + +mkdir -p $HOME/.kube +sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config +sudo chown $(id -u):$(id -g) $HOME/.kube/config + +kubectl taint nodes --all node-role.kubernetes.io/master- +kubectl taint nodes --all node-role.kubernetes.io/control-plane- + +kubectl apply -f manifests/kube-router-daemonset.yaml + +kubectl get pods --all-namespaces