kezhenxu94 commented on a change in pull request #2: URL: https://github.com/apache/skywalking-infra-e2e/pull/2#discussion_r551001117
########## File path: internal/components/setup/kind.go ########## @@ -0,0 +1,92 @@ +// Licensed to 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. Apache Software Foundation (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 ( + "bytes" + "os/exec" + "strings" + + "github.com/apache/skywalking-infra-e2e/internal/flags" + "github.com/apache/skywalking-infra-e2e/internal/logger" +) + +type ExecResult struct { + Command []string + Error error + Stdout string + StdErr string +} + +const ( + KIND = "kind" + KINDCOMMAND = "kind" +) + +var ( + // kind cluster create config + kindConfigFile string +) + +// setup for kind, invoke from command line +func KindSetupInCommand() { + kindConfigFile = flags.File + + execResult := createKindCluster() + err := execResult.Error + if err != nil { + cmd := strings.Join(execResult.Command, " ") + logger.Log.Errorf("Kind cluster create exited abnormally whilst running [%s]\n"+ + "err: %s\nstdout: %s\nstderr: %s", cmd, err, execResult.Stdout, execResult.StdErr) + } else { + defer cleanupKindCluster() + } +} + +func kindExec(args []string) ExecResult { + cmd := exec.Command(KINDCOMMAND, args...) + var stdoutBytes, stderrBytes bytes.Buffer + cmd.Stdout = &stdoutBytes + cmd.Stderr = &stderrBytes + + err := cmd.Run() + execCmd := []string{KINDCOMMAND} + execCmd = append(execCmd, args...) + + return ExecResult{ + Command: execCmd, + Error: err, + Stdout: stdoutBytes.String(), + StdErr: stderrBytes.String(), + } +} Review comment: I hope we can eliminate the `exec.Command` as much as possible, because it highly depends on the users OS and environment, in this particular case, as `kind` is also written in Go, we can simply embed it into this via plain Go module reference like this, ```go import ( kind "sigs.k8s.io/kind/cmd/kind/app" kindcmd "sigs.k8s.io/kind/pkg/cmd" ) // ... other codes func createKindCluster() error { args := []string{"create", "cluster", "--config", kindConfigFile} logger.Log.Info("creating kind cluster...") if err := kind.Run(kindcmd.NewLogger(), kindcmd.StandardIOStreams(), args); err != nil { return err } return nil } ``` This increases the usability drastically with very little effort IMO even if we don't install `kind` in our system. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
