junyan-ling commented on code in PR #187: URL: https://github.com/apache/yunikorn-release/pull/187#discussion_r1953651137
########## soak/setup/setup.go: ########## @@ -0,0 +1,171 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package setup + +import ( + "fmt" + "github.com/apache/yunikorn-core/pkg/log" + "github.com/apache/yunikorn-release/soak/constants" + "github.com/apache/yunikorn-release/soak/framework" + "go.uber.org/zap" + "os" + "os/exec" + "path/filepath" + "strings" +) + +var logger *zap.Logger = log.Log(log.Test) + +func SetK8sContext() error { + homeDir, err := os.UserHomeDir() + if err != nil { + return fmt.Errorf("failed to get home directory: %v", err) + } + kubeconfigPath := filepath.Join(homeDir, ".kube", "config") + os.Setenv("KUBECONFIG", kubeconfigPath) + logger.Info("Set KUBECONFIG", zap.String("path", kubeconfigPath)) + + contextCmd := exec.Command("kubectl", "config", "use-context", constants.KindSoakTestCluster) + contextOutput, err := contextCmd.CombinedOutput() + if err != nil { + return fmt.Errorf("failed to switch kubectl context: %v, output: %s", err, string(contextOutput)) + } + logger.Info("Kubectl context switch output", zap.String("output", strings.TrimSpace(string(contextOutput)))) + + currentContextCmd := exec.Command("kubectl", "config", "current-context") + currentContextOutput, err := currentContextCmd.CombinedOutput() + if err != nil { + return fmt.Errorf("failed to get current context: %v, output: %s", err, string(currentContextOutput)) + } + logger.Info("Current kubectl context", zap.String("context", strings.TrimSpace(string(currentContextOutput)))) + + return nil +} + +func UpgradeSchedulerPerConfig(scheduler framework.TemplateFields) error { + if err := SetK8sContext(); err != nil { + logger.Fatal("failed to set kubernetes context", zap.Error(err)) + return err + } + + logger.Info("Scheduler details", + zap.String("VcoreRequests", *scheduler.VcoreRequests), + zap.String("MemoryRequests", *scheduler.MemoryRequests), + zap.String("VcoreLimits", *scheduler.VcoreLimits), + zap.String("MemoryLimits", *scheduler.MemoryLimits), + zap.String("path", *scheduler.Path)) + + if scheduler.VcoreRequests != nil || scheduler.MemoryRequests != nil || scheduler.VcoreLimits != nil || scheduler.MemoryLimits != nil { + args := []string{ + "upgrade", + "yunikorn", + "yunikorn/yunikorn", + "-n", "yunikorn", + } + + if scheduler.VcoreRequests != nil { + args = append(args, "--set", fmt.Sprintf("resources.requests.cpu=%s", *scheduler.VcoreRequests)) + } + if scheduler.MemoryRequests != nil { + args = append(args, "--set", fmt.Sprintf("resources.requests.memory=%s", *scheduler.MemoryRequests)) + } + if scheduler.VcoreLimits != nil { + args = append(args, "--set", fmt.Sprintf("resources.limits.cpu=%s", *scheduler.VcoreLimits)) + } + if scheduler.MemoryLimits != nil { + args = append(args, "--set", fmt.Sprintf("resources.limits.memory=%s", *scheduler.MemoryLimits)) + } + + cmd := exec.Command("helm", args...) + + logger.Info("Helm command to be executed", + zap.String("command", fmt.Sprintf("helm %s", strings.Join(args, " ")))) + + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("helm upgrade failed: %v, output: %s", err, string(output)) + } + + logger.Info("Helm upgrade successful", + zap.String("command", fmt.Sprintf("helm %s", strings.Join(args, " "))), + zap.String("output", string(output))) + } + + if scheduler.Path != nil { + kubectlArgs := []string{"apply"} + kubectlArgs = append(kubectlArgs, "-f", *scheduler.Path, "-n", "yunikorn") + kubectlCmd := exec.Command("kubectl", kubectlArgs...) + logger.Info("Kubectl command to be executed", + zap.String("command", fmt.Sprintf("kubectl %s", strings.Join(kubectlArgs, " ")))) + + kubectlOutput, err := kubectlCmd.CombinedOutput() + if err != nil { + return fmt.Errorf("kubectl apply failed: %v, output: %s", err, string(kubectlOutput)) + } + logger.Info("Kubectl apply successful", zap.String("output", strings.TrimSpace(string(kubectlOutput)))) + } + + return nil +} + +func SetNodeScalePerConfig(node framework.TemplateFields) error { + if err := SetK8sContext(); err != nil { + logger.Fatal("failed to set kubernetes context", zap.Error(err)) + return err + } + + logger.Info("Node details", + zap.String("path", *node.Path), + zap.Int("NodesDesiredCount", *node.DesiredCount), + zap.Int("maxCount", *node.MaxCount)) + + templateContent, err := os.ReadFile("soak/templates/kwok-node-template.yaml") + if err != nil { + return fmt.Errorf("failed to read template file: %v", err) + } + desiredCount := *node.DesiredCount + + for i := 0; i < desiredCount; i++ { + currentNodeName := fmt.Sprintf("kwok-node-%d", i) + nodeContent := strings.ReplaceAll(string(templateContent), "kwok-node-i", currentNodeName) + + tmpfile, err := os.CreateTemp("", "node-*.yaml") + if err != nil { + return fmt.Errorf("failed to create temp file: %v", err) + } + defer os.Remove(tmpfile.Name()) // Clean up + + if _, err := tmpfile.WriteString(nodeContent); err != nil { + return fmt.Errorf("failed to write to temp file: %v", err) + } + if err := tmpfile.Close(); err != nil { + return fmt.Errorf("failed to close temp file: %v", err) + } + + cmd := exec.Command("kubectl", "apply", "-f", tmpfile.Name()) + output, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("failed to apply node configuration: %v, output: %s", err, string(output)) Review Comment: Not 100% sure, so to be safe, I removed the log for `output ` in case of err, and just logged the err message. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@yunikorn.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org