kezhenxu94 commented on a change in pull request #2: URL: https://github.com/apache/skywalking-infra-e2e/pull/2#discussion_r551000384
########## 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() Review comment: The cleanup step is not supposed to be here (at the end of `setup`), instead, it should be at the end of the `run` command (if the `run` command is executed), otherwise if we only execute `setup`, no `cleanup` should be done because we are doing step by step debugging. ########## File path: internal/components/setup/compose.go ########## @@ -0,0 +1,24 @@ +// 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 + +const ( + COMPOSE = "compose" + COMPOSECOMMAND = "docker-compose" Review comment: In Go, constants are usually camel case, so the preferable names are `Compose` and `ComposeCommand` ########## File path: commands/setup/setup.go ########## @@ -18,15 +18,36 @@ package setup import ( - "fmt" + "github.com/apache/skywalking-infra-e2e/internal/components/setup" + "github.com/apache/skywalking-infra-e2e/internal/logger" "github.com/spf13/cobra" + + "github.com/apache/skywalking-infra-e2e/internal/flags" ) +func init() { + Setup.Flags().StringVar(&flags.Env, "env", "kind", "specify the run environment") + Setup.Flags().StringVar(&flags.File, "file", "kind.yaml", "specify configuration file") +} + var Setup = &cobra.Command{ Use: "setup", Short: "", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("Not implemented.") + // check if env commands in PATH + if flags.Env == setup.COMPOSE { + if setup.Which(setup.COMPOSECOMMAND) != nil { + logger.Log.Errorf("command %s not found, is it in the PATH?", setup.COMPOSECOMMAND) Review comment: In such cases, we should fail fast and return fast to avoid unnecessary operations ########## 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" ) 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. ########## File path: internal/components/setup/which.go ########## @@ -0,0 +1,28 @@ +// 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 "os/exec" + +// check if binary is present in PATH +func Which(binary string) error { + _, err := exec.LookPath(binary) + + return err +} Review comment: This function is generic (not specific to `setup`) so I think it can be placed in a common package such as `util` or similar if it's really needed, so that others can find it easier. ########## File path: commands/setup/setup.go ########## @@ -18,15 +18,36 @@ package setup import ( - "fmt" + "github.com/apache/skywalking-infra-e2e/internal/components/setup" + "github.com/apache/skywalking-infra-e2e/internal/logger" "github.com/spf13/cobra" + + "github.com/apache/skywalking-infra-e2e/internal/flags" ) +func init() { + Setup.Flags().StringVar(&flags.Env, "env", "kind", "specify the run environment") + Setup.Flags().StringVar(&flags.File, "file", "kind.yaml", "specify configuration file") +} + var Setup = &cobra.Command{ Use: "setup", Short: "", Run: func(cmd *cobra.Command, args []string) { Review comment: Change this kind of methods to `RunE: func(cmd *cobra.Command, args []string) error {` so that we can deal the `error` properly, do not `panic` or `logger.Log.Fatal` at a deep level of the codes and give possibilities to the upper functions to deal with the errors ---------------------------------------------------------------- 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]
