1
1
package apis
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/json"
6
7
"errors"
8
+ "fmt"
7
9
"io/ioutil"
8
10
"net/http"
9
- "os"
10
11
11
12
"github.com/litmuschaos/litmusctl/pkg/k8s"
12
13
"github.com/litmuschaos/litmusctl/pkg/types"
13
14
"github.com/litmuschaos/litmusctl/pkg/utils"
15
+ "github.com/mitchellh/go-homedir"
16
+ "github.com/spf13/cobra"
14
17
)
15
18
16
19
type manifestData struct {
@@ -46,7 +49,7 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i
46
49
47
50
// Query to fetch Infra details from server
48
51
query := `{"query":"query {\n getInfraDetails(infraID : \"` + infraID + `\", \n projectID : \"` + projectID + `\"){\n infraNamespace infraID \n}}"}`
49
- resp , err := SendRequest (SendRequestParams {Endpoint : "http://localhost:8080/query" , Token : cred .Token }, []byte (query ), string (types .Post ))
52
+ resp , err := SendRequest (SendRequestParams {Endpoint : cred . Endpoint + utils . GQLAPIPath , Token : cred .Token }, []byte (query ), string (types .Post ))
50
53
if err != nil {
51
54
return "" , err
52
55
}
@@ -62,6 +65,7 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i
62
65
if resp .StatusCode == http .StatusOK {
63
66
err = json .Unmarshal (bodyBytes , & infra )
64
67
if err != nil {
68
+ fmt .Println ("Error in unmarshalling infra" )
65
69
return "" , err
66
70
}
67
71
if len (infra .Errors ) > 0 {
@@ -73,8 +77,9 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i
73
77
74
78
// Query to fetch upgraded manifest from the server
75
79
query = `{"query":"query {\n getInfraManifest(projectID : \"` + projectID + `\",\n infraID : \"` + infra .Data .GetInfraDetails .InfraID + `\", \n upgrade: true)}"}`
76
- resp , err = SendRequest (SendRequestParams {Endpoint : "http://localhost:8080/query" , Token : cred .Token }, []byte (query ), string (types .Post ))
80
+ resp , err = SendRequest (SendRequestParams {Endpoint : cred . Endpoint + utils . GQLAPIPath , Token : cred .Token }, []byte (query ), string (types .Post ))
77
81
if err != nil {
82
+
78
83
return "" , err
79
84
}
80
85
@@ -90,69 +95,55 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i
90
95
var manifest manifestData
91
96
err = json .Unmarshal (bodyBytes , & manifest )
92
97
if err != nil {
98
+ fmt .Println ("Error in unmarshalling manifest" )
93
99
return "" , err
94
100
}
95
101
96
102
if len (manifest .Errors ) > 0 {
97
103
return "" , errors .New (manifest .Errors [0 ].Message )
98
104
}
99
105
100
- // To write the manifest data into a temporary file
101
- err = ioutil . WriteFile ( "chaos-infra-manifest.yaml " , [] byte ( manifest .Data .GetManifest ), 0644 )
106
+ // Fetching subscriber-config from the subscriber
107
+ configData , err := k8s . GetConfigMap ( c , "subscriber-config " , * infra .Data .GetInfraDetails . InfraNamespace )
102
108
if err != nil {
103
109
return "" , err
104
110
}
111
+ var configMapString string
112
+
113
+ metadata := new (bytes.Buffer )
114
+ fmt .Fprintf (metadata , "\n %s: %s\n %s: %s\n %s: \n %s: %s\n %s: %s\n %s:\n " , "apiVersion" , "v1" ,
115
+ "kind" , "ConfigMap" , "metadata" , "name" , "subscriber-config" , "namespace" , * infra .Data .GetInfraDetails .InfraNamespace , "data" )
116
+
117
+ for k , v := range configData {
118
+ b := new (bytes.Buffer )
119
+ if k == "COMPONENTS" {
120
+ fmt .Fprintf (b , " %s: |\n %s" , k , v )
121
+ } else if k == "START_TIME" || k == "IS_INFRA_CONFIRMED" {
122
+ fmt .Fprintf (b , " %s: \" %s\" \n " , k , v )
123
+ } else {
124
+ fmt .Fprintf (b , " %s: %s\n " , k , v )
125
+ }
126
+ configMapString = configMapString + b .String ()
105
127
106
- // Fetching subscriber-config from the subscriber
107
- // configData, err := k8s.GetConfigMap(c, "subscriber-config", *infra.Data.GetInfraDetails.InfraNamespace)
108
- // if err != nil {
109
- // return "", err
110
- // }
111
- // var configMapString string
112
-
113
- // metadata := new(bytes.Buffer)
114
- // fmt.Fprintf(metadata, "\n%s: %s\n%s: %s\n%s: \n %s: %s\n %s: %s\n%s:\n", "apiVersion", "v1",
115
- // "kind", "ConfigMap", "metadata", "name", "subscriber-config", "namespace", *infra.Data.GetInfraDetails.InfraNamespace, "data")
116
-
117
- // for k, v := range configData {
118
- // b := new(bytes.Buffer)
119
- // if k == "COMPONENTS" {
120
- // fmt.Fprintf(b, " %s: |\n %s", k, v)
121
- // } else if k == "START_TIME" || k == "IS_INFRA_CONFIRMED" {
122
- // fmt.Fprintf(b, " %s: \"%s\"\n", k, v)
123
- // } else {
124
- // fmt.Fprintf(b, " %s: %s\n", k, v)
125
- // }
126
- // configMapString = configMapString + b.String()
127
-
128
- // }
129
-
130
- yamlOutput , err := k8s .ApplyYaml (k8s.ApplyYamlParams {
131
- Token : cred .Token ,
132
- Endpoint : cred .Endpoint ,
133
- YamlPath : "chaos-infra-manifest.yaml" ,
134
- }, kubeconfig , true )
128
+ }
129
+
130
+ yamlOutput , err := k8s .UpgradeInfra ([]byte (manifest .Data .GetManifest ), kubeconfig )
135
131
136
132
if err != nil {
137
133
return "" , err
138
134
}
139
135
utils .White .Print ("\n " , yamlOutput )
140
136
141
- err = os .Remove ("chaos-infra-manifest.yaml" )
137
+ // Creating a backup for current subscriber-config in the SUBSCRIBER
138
+ home , err := homedir .Dir ()
139
+ cobra .CheckErr (err )
140
+
141
+ configMapString = metadata .String () + configMapString
142
+ err = ioutil .WriteFile (home + "/backupSubscriberConfig.yaml" , []byte (configMapString ), 0644 )
142
143
if err != nil {
143
- return "Error removing Chaos Infrastructure manifest : " , err
144
+ return "Error creating backup for subscriber config : " , err
144
145
}
145
146
146
- // Creating a backup for current subscriber-config in the SUBSCRIBER
147
- // home, err := homedir.Dir()
148
- // cobra.CheckErr(err)
149
-
150
- // configMapString = metadata.String() + configMapString
151
- // err = ioutil.WriteFile(home+"/backupSubscriberConfig.yaml", []byte(configMapString), 0644)
152
- // if err != nil {
153
- // return "Error creating backup for subscriber config: ", err
154
- // }
155
-
156
147
utils .White_B .Print ("\n ** A backup of subscriber-config configmap has been saved in your system's home directory as backupSubscriberConfig.yaml **\n " )
157
148
158
149
return "Manifest applied successfully" , nil
0 commit comments