Skip to content

Commit ed49413

Browse files
author
Neelanjan Manna
authored
chore: updates chaosresult get client operation (#770)
Signed-off-by: neelanjan00 <neelanjan.manna@harness.io>
1 parent 0fa32c6 commit ed49413

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

pkg/clients/clientset.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"k8s.io/client-go/dynamic"
99
"k8s.io/client-go/kubernetes"
1010
"k8s.io/client-go/rest"
11-
restclient "k8s.io/client-go/rest"
1211
"k8s.io/client-go/tools/clientcmd"
1312
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
1413
"k8s.io/klog"
@@ -77,9 +76,9 @@ func generateLitmusClientSet(config *rest.Config) (*chaosClient.LitmuschaosV1alp
7776

7877
// buildConfigFromFlags is a helper function that builds configs from a master
7978
// url or a kubeconfig filepath, if nothing is provided it falls back to inClusterConfig
80-
func buildConfigFromFlags(masterUrl, kubeconfigPath string) (*restclient.Config, error) {
79+
func buildConfigFromFlags(masterUrl, kubeconfigPath string) (*rest.Config, error) {
8180
if kubeconfigPath == "" && masterUrl == "" {
82-
kubeconfig, err := restclient.InClusterConfig()
81+
kubeconfig, err := rest.InClusterConfig()
8382
if err == nil {
8483
return kubeconfig, nil
8584
}

pkg/clients/litmus.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,26 @@ func (clients *ClientSets) UpdateChaosEngine(chaosDetails *types.ChaosDetails, e
4747
})
4848
}
4949

50-
func (clients *ClientSets) GetChaosResult(chaosDetails *types.ChaosDetails) (*v1alpha1.ChaosResult, error) {
51-
var result v1alpha1.ChaosResult
50+
func (clients *ClientSets) GetChaosResult(chaosDetails *types.ChaosDetails, resultDetails *types.ResultDetails) (*v1alpha1.ChaosResult, error) {
51+
var result *v1alpha1.ChaosResult
5252

5353
if err := retry.
5454
Times(uint(chaosDetails.Timeout / chaosDetails.Delay)).
5555
Wait(time.Duration(chaosDetails.Delay) * time.Second).
5656
Try(func(attempt uint) error {
57-
resultList, err := clients.LitmusClient.ChaosResults(chaosDetails.ChaosNamespace).List(context.Background(),
58-
v1.ListOptions{LabelSelector: fmt.Sprintf("chaosUID=%s", chaosDetails.ChaosUID)})
59-
if err != nil {
60-
return err
61-
}
62-
if len(resultList.Items) == 0 {
63-
return nil
57+
var err error
58+
59+
result, err = clients.LitmusClient.ChaosResults(chaosDetails.ChaosNamespace).Get(context.Background(), resultDetails.Name, v1.GetOptions{})
60+
if err != nil && !k8serrors.IsNotFound(err) {
61+
return cerrors.Error{ErrorCode: cerrors.ErrorTypeChaosResultCRUD, Target: fmt.Sprintf("{name: %s, namespace: %s}", resultDetails.Name, chaosDetails.ChaosNamespace), Reason: err.Error()}
6462
}
65-
result = resultList.Items[0]
63+
6664
return nil
6765
}); err != nil {
6866
return nil, err
6967
}
7068

71-
return &result, nil
69+
return result, nil
7270
}
7371

7472
func (clients *ClientSets) CreateChaosResult(chaosDetails *types.ChaosDetails, result *v1alpha1.ChaosResult) (bool, error) {

pkg/result/chaosresult.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func ChaosResult(chaosDetails *types.ChaosDetails, clients clients.ClientSets, r
3131
// It tries to get the chaosresult, if available
3232
// it will retry until it got chaos result or met the timeout(3 mins)
3333
isResultAvailable := false
34-
result, err := clients.GetChaosResult(chaosDetails)
34+
result, err := clients.GetChaosResult(chaosDetails, resultDetails)
3535
if err != nil {
3636
return cerrors.Error{ErrorCode: cerrors.ErrorTypeChaosResultCRUD, Target: fmt.Sprintf("{name: %s, namespace: %s}", resultDetails.Name, chaosDetails.ChaosNamespace), Reason: fmt.Sprintf("failed to get result: %s", err.Error())}
3737
}
@@ -234,7 +234,7 @@ func PatchChaosResult(clients clients.ClientSets, chaosDetails *types.ChaosDetai
234234

235235
// SetResultUID sets the ResultUID into the ResultDetails structure
236236
func SetResultUID(resultDetails *types.ResultDetails, clients clients.ClientSets, chaosDetails *types.ChaosDetails) error {
237-
result, err := clients.GetChaosResult(chaosDetails)
237+
result, err := clients.GetChaosResult(chaosDetails, resultDetails)
238238
if err != nil {
239239
return cerrors.Error{ErrorCode: cerrors.ErrorTypeChaosResultCRUD, Target: fmt.Sprintf("{name: %s, namespace: %s}", resultDetails.Name, chaosDetails.ChaosNamespace), Reason: err.Error()}
240240
}
@@ -304,7 +304,7 @@ func AnnotateChaosResult(resultName, namespace, status, kind, name string) error
304304
// GetChaosStatus get the chaos status based on annotations in chaosresult
305305
func GetChaosStatus(resultDetails *types.ResultDetails, chaosDetails *types.ChaosDetails, clients clients.ClientSets) (*v1alpha1.ChaosResult, error) {
306306

307-
result, err := clients.GetChaosResult(chaosDetails)
307+
result, err := clients.GetChaosResult(chaosDetails, resultDetails)
308308
if err != nil {
309309
return nil, cerrors.Error{ErrorCode: cerrors.ErrorTypeChaosResultCRUD, Target: fmt.Sprintf("{name: %s, namespace: %s}", resultDetails.Name, chaosDetails.ChaosNamespace), Reason: err.Error()}
310310
}

0 commit comments

Comments
 (0)