Skip to content

Commit

Permalink
removed the kubectl dependency completely from applyYaml func and rev…
Browse files Browse the repository at this point in the history
…erted the local setup changes

Signed-off-by: deep-poharkar <[email protected]>
  • Loading branch information
deep-poharkar committed Nov 21, 2023
1 parent 066895b commit 8e60e21
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 69 deletions.
83 changes: 37 additions & 46 deletions pkg/apis/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package apis

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/litmuschaos/litmusctl/pkg/k8s"
"github.com/litmuschaos/litmusctl/pkg/types"
"github.com/litmuschaos/litmusctl/pkg/utils"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
)

type manifestData struct {
Expand Down Expand Up @@ -46,7 +49,7 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i

// Query to fetch Infra details from server
query := `{"query":"query {\n getInfraDetails(infraID : \"` + infraID + `\", \n projectID : \"` + projectID + `\"){\n infraNamespace infraID \n}}"}`
resp, err := SendRequest(SendRequestParams{Endpoint: "http://localhost:8080/query", Token: cred.Token}, []byte(query), string(types.Post))
resp, err := SendRequest(SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(query), string(types.Post))
if err != nil {
return "", err
}
Expand All @@ -62,6 +65,7 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i
if resp.StatusCode == http.StatusOK {
err = json.Unmarshal(bodyBytes, &infra)
if err != nil {
fmt.Println("Error in unmarshalling infra")
return "", err
}
if len(infra.Errors) > 0 {
Expand All @@ -73,8 +77,9 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i

// Query to fetch upgraded manifest from the server
query = `{"query":"query {\n getInfraManifest(projectID : \"` + projectID + `\",\n infraID : \"` + infra.Data.GetInfraDetails.InfraID + `\", \n upgrade: true)}"}`
resp, err = SendRequest(SendRequestParams{Endpoint: "http://localhost:8080/query", Token: cred.Token}, []byte(query), string(types.Post))
resp, err = SendRequest(SendRequestParams{Endpoint: cred.Endpoint + utils.GQLAPIPath, Token: cred.Token}, []byte(query), string(types.Post))
if err != nil {

return "", err
}

Expand All @@ -90,69 +95,55 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i
var manifest manifestData
err = json.Unmarshal(bodyBytes, &manifest)
if err != nil {
fmt.Println("Error in unmarshalling manifest")
return "", err
}

if len(manifest.Errors) > 0 {
return "", errors.New(manifest.Errors[0].Message)
}

// To write the manifest data into a temporary file
err = ioutil.WriteFile("chaos-infra-manifest.yaml", []byte(manifest.Data.GetManifest), 0644)
// Fetching subscriber-config from the subscriber
configData, err := k8s.GetConfigMap(c, "subscriber-config", *infra.Data.GetInfraDetails.InfraNamespace)
if err != nil {
return "", err
}
var configMapString string

metadata := new(bytes.Buffer)
fmt.Fprintf(metadata, "\n%s: %s\n%s: %s\n%s: \n %s: %s\n %s: %s\n%s:\n", "apiVersion", "v1",
"kind", "ConfigMap", "metadata", "name", "subscriber-config", "namespace", *infra.Data.GetInfraDetails.InfraNamespace, "data")

for k, v := range configData {
b := new(bytes.Buffer)
if k == "COMPONENTS" {
fmt.Fprintf(b, " %s: |\n %s", k, v)
} else if k == "START_TIME" || k == "IS_INFRA_CONFIRMED" {
fmt.Fprintf(b, " %s: \"%s\"\n", k, v)
} else {
fmt.Fprintf(b, " %s: %s\n", k, v)
}
configMapString = configMapString + b.String()

// Fetching subscriber-config from the subscriber
// configData, err := k8s.GetConfigMap(c, "subscriber-config", *infra.Data.GetInfraDetails.InfraNamespace)
// if err != nil {
// return "", err
// }
// var configMapString string

// metadata := new(bytes.Buffer)
// fmt.Fprintf(metadata, "\n%s: %s\n%s: %s\n%s: \n %s: %s\n %s: %s\n%s:\n", "apiVersion", "v1",
// "kind", "ConfigMap", "metadata", "name", "subscriber-config", "namespace", *infra.Data.GetInfraDetails.InfraNamespace, "data")

// for k, v := range configData {
// b := new(bytes.Buffer)
// if k == "COMPONENTS" {
// fmt.Fprintf(b, " %s: |\n %s", k, v)
// } else if k == "START_TIME" || k == "IS_INFRA_CONFIRMED" {
// fmt.Fprintf(b, " %s: \"%s\"\n", k, v)
// } else {
// fmt.Fprintf(b, " %s: %s\n", k, v)
// }
// configMapString = configMapString + b.String()

// }

yamlOutput, err := k8s.ApplyYaml(k8s.ApplyYamlParams{
Token: cred.Token,
Endpoint: cred.Endpoint,
YamlPath: "chaos-infra-manifest.yaml",
}, kubeconfig, true)
}

yamlOutput, err := k8s.UpgradeInfra([]byte(manifest.Data.GetManifest), kubeconfig)

if err != nil {
return "", err
}
utils.White.Print("\n", yamlOutput)

err = os.Remove("chaos-infra-manifest.yaml")
// Creating a backup for current subscriber-config in the SUBSCRIBER
home, err := homedir.Dir()
cobra.CheckErr(err)

configMapString = metadata.String() + configMapString
err = ioutil.WriteFile(home+"/backupSubscriberConfig.yaml", []byte(configMapString), 0644)
if err != nil {
return "Error removing Chaos Infrastructure manifest: ", err
return "Error creating backup for subscriber config: ", err
}

// Creating a backup for current subscriber-config in the SUBSCRIBER
// home, err := homedir.Dir()
// cobra.CheckErr(err)

// configMapString = metadata.String() + configMapString
// err = ioutil.WriteFile(home+"/backupSubscriberConfig.yaml", []byte(configMapString), 0644)
// if err != nil {
// return "Error creating backup for subscriber config: ", err
// }

utils.White_B.Print("\n ** A backup of subscriber-config configmap has been saved in your system's home directory as backupSubscriberConfig.yaml **\n")

return "Manifest applied successfully", nil
Expand Down
78 changes: 55 additions & 23 deletions pkg/k8s/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"log"
"net/http"
"os"
"os/exec"
"path/filepath"

"k8s.io/client-go/discovery/cached/memory"
Expand Down Expand Up @@ -291,12 +292,61 @@ type ApplyYamlParams struct {
}

func ApplyYaml(params ApplyYamlParams, kubeconfig string, isLocal bool) (output string, err error) {
_, kubeClient, dynamicClient, err := getClientAndConfig(kubeconfig)
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 := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
err = ioutil.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
}

manifest, err := getManifest(isLocal, params, kubeClient)
// If no error found, return standard output
return outStr, nil
}

func UpgradeInfra(manifest []byte, kubeconfig string) (string, error) {

_, kubeClient, dynamicClient, err := getClientAndConfig(kubeconfig)
if err != nil {
return "", err
}
Expand All @@ -314,6 +364,7 @@ func ApplyYaml(params ApplyYamlParams, kubeconfig string, isLocal bool) (output
logrus.Println("🚀 Successfully Upgraded Chaos Infra")

return "Success", nil

}

func getClientAndConfig(kubeconfig string) (*rest.Config, *kubernetes.Clientset, dynamic.Interface, error) {
Expand Down Expand Up @@ -358,29 +409,9 @@ func getClientAndConfig(kubeconfig string) (*rest.Config, *kubernetes.Clientset,
return config, kubeClient, dynamicClient, nil
}

func getManifest(isLocal bool, params ApplyYamlParams, kubeClient *kubernetes.Clientset) ([]byte, error) {
if isLocal {
home := homedir.HomeDir()
chaosInfraManifest := filepath.Join(home, "chaos-infra-manifest.yaml")
return ioutil.ReadFile(chaosInfraManifest)
}

path := fmt.Sprintf("%s%s/%s.yaml", params.Endpoint, params.YamlPath, params.Token)
req, err := http.NewRequest("GET", path, nil)
if err != nil {
return nil, fmt.Errorf("error in creating new request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("error in fetching chaos infra manifest: %w", err)
}
defer resp.Body.Close()

return ioutil.ReadAll(resp.Body)
}

func decodeManifest(manifest []byte) ([]*unstructured.Unstructured, error) {
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(manifest), len(manifest))
// fmt.Println(string(manifest))

// Split the resource file into separate YAML documents.
resources := []*unstructured.Unstructured{}
Expand All @@ -392,6 +423,7 @@ func decodeManifest(manifest []byte) ([]*unstructured.Unstructured, error) {
}
return nil, fmt.Errorf("error in decoding resource: %w", err)
}
fmt.Println("resource", resourcestr)
resources = append(resources, resourcestr)
}

Expand Down

0 comments on commit 8e60e21

Please sign in to comment.