Skip to content

Commit

Permalink
fixed refere header issue, used client-go for chaos infra connection …
Browse files Browse the repository at this point in the history
…instead of kubectl apply (#254)

Signed-off-by: Sarthak Jain <[email protected]>
  • Loading branch information
SarthakJain26 authored Sep 16, 2024
1 parent 5b4f3cd commit 4f49849
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 95 deletions.
3 changes: 0 additions & 3 deletions Usage_0.23.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\*\*
Expand Down
9 changes: 3 additions & 6 deletions Usage_interactive.md
Original file line number Diff line number Diff line change
Expand Up @@ -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\*\*
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/infrastructure/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
infraID
name
token
manifest
}
}
`
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions pkg/apis/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -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
Expand Down
20 changes: 6 additions & 14 deletions pkg/cmd/connect/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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))
},
}

Expand Down
72 changes: 2 additions & 70 deletions pkg/k8s/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ import (
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"

"k8s.io/client-go/discovery/cached/memory"
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down

0 comments on commit 4f49849

Please sign in to comment.