From 02f38790ddf1938e3927a61dedca6f7e6bbe01e4 Mon Sep 17 00:00:00 2001 From: Sarthak Jain Date: Mon, 16 Sep 2024 17:16:19 +0530 Subject: [PATCH] fixed refere header issue, used client-go for chaos infra connection instead of kubectl apply Signed-off-by: Sarthak Jain --- Usage_0.23.0.md | 3 -- Usage_interactive.md | 9 ++-- pkg/apis/infrastructure/query.go | 1 + pkg/apis/request.go | 1 + pkg/apis/upgrade.go | 6 ++- pkg/cmd/connect/infra.go | 20 +++------ pkg/k8s/operations.go | 72 +------------------------------- 7 files changed, 17 insertions(+), 95 deletions(-) diff --git a/Usage_0.23.0.md b/Usage_0.23.0.md index b5156ff7..186550e3 100644 --- a/Usage_0.23.0.md +++ b/Usage_0.23.0.md @@ -174,14 +174,11 @@ Installation Mode: cluster šŸ¤· Do you want to continue with the above details? [Y/N]: Y šŸ‘ Continuing Chaos Infrastructure connection!! -Applying YAML: -https://preview.litmuschaos.io/api/file/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbHVzdGVyX2lkIjoiMDUyZmFlN2UtZGM0MS00YmU4LWJiYTgtMmM4ZTYyNDFkN2I0In0.i31QQDG92X5nD6P_-7TfeAAarZqLvUTFfnAghJYXPiM.yaml šŸ’” Connecting Chaos Infrastructure to ChaosCenter. šŸƒ Chaos Infrastructure is running!! šŸš€ Chaos Infrastructure Connection Successful!! šŸŽ‰ -šŸ‘‰ Litmus Chaos Infrastructure can be accessed here: https://preview.litmuschaos.io/targets ``` #### Verify the new Chaos Infrastructure Connection\*\* diff --git a/Usage_interactive.md b/Usage_interactive.md index 6f281300..31306867 100644 --- a/Usage_interactive.md +++ b/Usage_interactive.md @@ -167,15 +167,12 @@ Service Account: litmus (new) Installation Mode: cluster šŸ¤· Do you want to continue with the above details? [Y/N]: Y -šŸ‘ Continuing Chaos Delegate connection!! -Applying YAML: -https://preview.litmuschaos.io/api/file/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbHVzdGVyX2lkIjoiMDUyZmFlN2UtZGM0MS00YmU4LWJiYTgtMmM4ZTYyNDFkN2I0In0.i31QQDG92X5nD6P_-7TfeAAarZqLvUTFfnAghJYXPiM.yaml +šŸ‘ Continuing Chaos Infrastructure connection!! -šŸ’” Connecting Chaos Delegate to ChaosCenter. +šŸ’” Connecting Chaos Infrastructure to ChaosCenter. šŸƒ Chaos Delegate is running!! -šŸš€ Chaos Delegate Connection Successful!! šŸŽ‰ -šŸ‘‰ Litmus Chaos Delegates can be accessed here: https://preview.litmuschaos.io/targets +šŸš€ Chaos Infrastructure Connection Successful!! šŸŽ‰ ``` #### Verify the new Chaos Delegate Connection\*\* diff --git a/pkg/apis/infrastructure/query.go b/pkg/apis/infrastructure/query.go index 970eaa4d..bc872479 100644 --- a/pkg/apis/infrastructure/query.go +++ b/pkg/apis/infrastructure/query.go @@ -16,6 +16,7 @@ const ( infraID name token + manifest } } ` diff --git a/pkg/apis/request.go b/pkg/apis/request.go index fc94806b..7a5b12f3 100644 --- a/pkg/apis/request.go +++ b/pkg/apis/request.go @@ -32,6 +32,7 @@ func SendRequest(params SendRequestParams, payload []byte, method string) (*http } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", params.Token) + req.Header.Set("Referer", params.Endpoint) resp, err := http.DefaultClient.Do(req) if err != nil { diff --git a/pkg/apis/upgrade.go b/pkg/apis/upgrade.go index 46c590cc..048543ae 100644 --- a/pkg/apis/upgrade.go +++ b/pkg/apis/upgrade.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/sirupsen/logrus" "io" "net/http" "os" @@ -125,11 +126,12 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i } - yamlOutput, err := k8s.UpgradeInfra([]byte(manifest.Data.GetManifest), kubeconfig) - + yamlOutput, err := k8s.ApplyManifest([]byte(manifest.Data.GetManifest), kubeconfig) if err != nil { return "", err } + logrus.Println("šŸš€ Successfully Upgraded Chaos Infrastructure") + utils.White.Print("\n", yamlOutput) // Creating a backup for current subscriber-config in the SUBSCRIBER diff --git a/pkg/cmd/connect/infra.go b/pkg/cmd/connect/infra.go index c0b8188b..47f33bb0 100644 --- a/pkg/cmd/connect/infra.go +++ b/pkg/cmd/connect/infra.go @@ -16,9 +16,10 @@ limitations under the License. package connect import ( - "fmt" "os" + "github.com/sirupsen/logrus" + "github.com/litmuschaos/litmusctl/pkg/apis/environment" "github.com/litmuschaos/litmusctl/pkg/apis/infrastructure" @@ -235,34 +236,25 @@ var infraCmd = &cobra.Command{ os.Exit(1) } - path := fmt.Sprintf("%s%s/%s.yaml", credentials.Endpoint, utils.ChaosYamlPath, infra.Data.RegisterInfraDetails.Token) - utils.White_B.Print("Applying YAML:\n", path) - // Print error message in case Data field is null in response if (infra.Data == infrastructure.RegisterInfra{}) { utils.White_B.Print("\nšŸš« Chaos new infrastructure connection failed: " + infra.Errors[0].Message + "\n") os.Exit(1) } - //Apply infra connection yaml - yamlOutput, err := k8s.ApplyYaml(k8s.ApplyYamlParams{ - Token: infra.Data.RegisterInfraDetails.Token, - Endpoint: credentials.Endpoint, - YamlPath: utils.ChaosYamlPath, - }, kubeconfig, false) + yamlOutput, err := k8s.ApplyManifest([]byte(infra.Data.RegisterInfraDetails.Manifest), kubeconfig) if err != nil { - utils.Red.Print("\nāŒ Failed to apply connection yaml: \n" + err.Error() + "\n") - utils.White_B.Print("\n Error: \n" + err.Error()) + utils.Red.Println("\nāŒ failed to apply infra manifest, error: " + err.Error()) os.Exit(1) } + logrus.Println("šŸš€ Successfully Connected Chaos Infrastructure") - utils.White_B.Print("\n", yamlOutput) + utils.White.Print("\n", yamlOutput) // Watch subscriber pod status k8s.WatchPod(k8s.WatchPodParams{Namespace: newInfra.Namespace, Label: utils.ChaosInfraLabel}, &kubeconfig) utils.White_B.Println("\nšŸš€ Chaos new infrastructure connection successful!! šŸŽ‰") - utils.White_B.Println("šŸ‘‰ Litmus Chaos Infrastructure can be accessed here: " + fmt.Sprintf("%s/%s", credentials.Endpoint, utils.ChaosInfraPath)) }, } diff --git a/pkg/k8s/operations.go b/pkg/k8s/operations.go index ae163fdb..06165d74 100644 --- a/pkg/k8s/operations.go +++ b/pkg/k8s/operations.go @@ -21,11 +21,8 @@ import ( "errors" "flag" "fmt" - "io" "log" - "net/http" "os" - "os/exec" "path/filepath" "k8s.io/client-go/discovery/cached/memory" @@ -282,70 +279,8 @@ func ValidSA(namespace string, kubeconfig *string) (string, bool) { return sa, false } -// Token: Authorization token -// EndPoint: Endpoint in .litmusconfig -// YamlPath: Path of yaml file -type ApplyYamlParams struct { - Token string - Endpoint string - YamlPath string -} - -func ApplyYaml(params ApplyYamlParams, kubeconfig string, isLocal bool) (output string, err error) { - path := params.YamlPath - if !isLocal { - path = fmt.Sprintf("%s%s/%s.yaml", params.Endpoint, params.YamlPath, params.Token) - req, err := http.NewRequest("GET", path, nil) - if err != nil { - return "", err - } - resp, err := http.DefaultClient.Do(req) - if err != nil { - return "", err - } - defer resp.Body.Close() - resp_body, err := io.ReadAll(resp.Body) - if err != nil { - return "", err - } - err = os.WriteFile("chaos-infra-manifest.yaml", resp_body, 0644) - if err != nil { - return "", err - } - path = "chaos-infra-manifest.yaml" - } - - args := []string{"kubectl", "apply", "-f", path} - if kubeconfig != "" { - args = append(args, []string{"--kubeconfig", kubeconfig}...) - } else { - args = []string{"kubectl", "apply", "-f", path} - } - - cmd := exec.Command(args[0], args[1:]...) - var stdout, stderr bytes.Buffer - cmd.Stdout = &stdout - cmd.Stderr = &stderr - err = cmd.Run() - outStr, errStr := stdout.String(), stderr.String() - - // err, can have exit status 1 - if err != nil { - // if we get standard error then, return the same - if errStr != "" { - return "", fmt.Errorf(errStr) - } - - // if not standard error found, return error - return "", err - } - - // If no error found, return standard output - return outStr, nil -} - -// UpgradeInfra upgrades the Chaos Infrastructure using the provided manifest and kubeconfig with the help of client-go library. -func UpgradeInfra(manifest []byte, kubeconfig string) (string, error) { +// ApplyManifest applies the provided manifest and kubeconfig with the help of client-go library. +func ApplyManifest(manifest []byte, kubeconfig string) (string, error) { // Get Kubernetes and dynamic clients along with the configuration. _, kubeClient, dynamicClient, err := getClientAndConfig(kubeconfig) @@ -365,10 +300,7 @@ func UpgradeInfra(manifest []byte, kubeconfig string) (string, error) { return "", err } - logrus.Println("šŸš€ Successfully Upgraded Chaos Infra") - return "Success", nil - } // retrieves the Kubernetes and dynamic clients along with the configuration.