Skip to content

Commit d2f2008

Browse files
committed
Added litmusctl changes
Signed-off-by: Jonsy13 <[email protected]>
1 parent 631646f commit d2f2008

File tree

2 files changed

+22
-34
lines changed

2 files changed

+22
-34
lines changed

custom/litmus-helm-agent/pkg/k8s/client.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"flag"
66
"fmt"
77
"os"
8+
"path/filepath"
89

910
corev1r "k8s.io/api/core/v1"
1011

1112
metav1r "k8s.io/apimachinery/pkg/apis/meta/v1"
1213
"k8s.io/client-go/kubernetes"
1314
"k8s.io/client-go/rest"
1415
"k8s.io/client-go/tools/clientcmd"
16+
"k8s.io/client-go/util/homedir"
1517
)
1618

1719
func ConnectKubeApi() *kubernetes.Clientset {
@@ -72,12 +74,14 @@ func CreateSecret(secretName string, secretData map[string][]byte, NAMESPACE str
7274
}
7375

7476
func getKubeConfig() (*rest.Config, error) {
75-
kubeconfig := flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
76-
flag.Parse()
77-
// Use in-cluster config if kubeconfig path is not specified
78-
if *kubeconfig == "" {
79-
return rest.InClusterConfig()
77+
var kubeconfig *string
78+
if home := homedir.HomeDir(); home != "" {
79+
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
80+
} else {
81+
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
8082
}
83+
flag.Parse()
8184

85+
// use the current context in kubeconfig
8286
return clientcmd.BuildConfigFromFlags("", *kubeconfig)
8387
}

custom/litmus-helm-agent/pkg/litmus/client.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package client
22

33
import (
44
"fmt"
5-
"io/ioutil"
65
kubernetes "litmus-helm-agent/pkg/k8s"
7-
"net/http"
86
"os"
97
"reflect"
108
"strconv"
@@ -92,7 +90,7 @@ func GetProjectID(credentials types.Credentials) string {
9290
}
9391
}
9492
if result == "" {
95-
utils.Red.Println("\n❌ No project found with owner or editor access to current user" + "\n")
93+
utils.Red.Println("\n❌ No project found with owner or editor access to current user")
9694
os.Exit(1)
9795
}
9896
return result
@@ -137,11 +135,11 @@ func CreateInfra(credentials types.Credentials) {
137135
}
138136

139137
if connectionData.Data.RegisterInfraDetails.Token == "" {
140-
utils.Red.Println("\n❌ failed to get the infrastructure registration token: " + "\n")
138+
utils.Red.Println("\n❌ failed to get the infrastructure registration token: ")
141139
os.Exit(1)
142140
}
143141

144-
accessKey, err := validateInfra(connectionData.Data.RegisterInfraDetails.Token, credentials.Endpoint)
142+
accessKey, err := validateInfra(connectionData.Data.RegisterInfraDetails.Manifest)
145143

146144
if err != nil {
147145
utils.Red.Println("❌ Error validate infrastructure: ", err)
@@ -160,52 +158,38 @@ func CreateInfra(credentials types.Credentials) {
160158

161159
fmt.Printf("Infra Successfully declared, starting...\n")
162160
} else {
163-
fmt.Printf("Infra already exist, starting...\n")
161+
fmt.Printf("Infra already exist\n")
164162
}
165163
}
166164

167-
func validateInfra(token string, endpoint string) (string, error) {
168-
var accessKey string
165+
func validateInfra(manifest string) (string, error) {
169166

170-
path := fmt.Sprintf("%s/%s/%s.yaml", endpoint, utils.ChaosYamlPath, token)
171-
req, err := http.NewRequest("GET", path, nil)
172-
if err != nil {
173-
return accessKey, err
174-
}
175-
resp, err := http.DefaultClient.Do(req)
176-
if err != nil {
177-
return accessKey, err
178-
}
179-
defer resp.Body.Close()
180-
resp_body, err := ioutil.ReadAll(resp.Body)
181-
if err != nil {
182-
return accessKey, err
183-
}
184-
manifests := strings.Split(string(resp_body), "---")
167+
manifests := strings.Split(manifest, "---")
185168
for _, manifest := range manifests {
186169
if len(strings.TrimSpace(manifest)) > 0 {
187170
jsonValue, err := ymlToJson.YAMLToJSON([]byte(manifest))
188171
if err != nil {
189-
return accessKey, err
172+
return "", err
190173
}
191174
fieldName, _, _, err := jsonparser.Get([]byte(jsonValue), "metadata", "name")
192175
if err != nil {
193-
return accessKey, err
176+
return "", err
194177
}
195178
fieldKind, _, _, err := jsonparser.Get([]byte(jsonValue), "kind")
196179
if err != nil {
197-
return accessKey, err
180+
return "", err
198181
}
199182
if string(fieldName) == "subscriber-secret" && string(fieldKind) == "Secret" {
200183
if fieldData, _, _, err := jsonparser.Get([]byte(jsonValue), "stringData", "ACCESS_KEY"); err != nil {
201-
return accessKey, err
184+
return "", err
202185
} else {
203-
accessKey = string(fieldData)
186+
return string(fieldData), nil
204187
}
205188
}
206189
}
207190
}
208-
return accessKey, err
191+
192+
return "", fmt.Errorf("Subscriber Secret not found")
209193
}
210194

211195
func Login(LITMUS_FRONTEND_URL string, LITMUS_USERNAME string, LITMUS_PASSWORD string) types.Credentials {

0 commit comments

Comments
 (0)