github.com/argoproj/argo-cd/v2@v2.10.9/test/e2e/fixture/cluster/actions.go (about)

     1  package cluster
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"log"
     8  	"strings"
     9  
    10  	"github.com/argoproj/argo-cd/v2/common"
    11  	"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
    12  	"github.com/argoproj/argo-cd/v2/util/clusterauth"
    13  	"k8s.io/client-go/kubernetes"
    14  	"k8s.io/client-go/tools/clientcmd"
    15  
    16  	clusterpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
    17  	"github.com/argoproj/argo-cd/v2/test/e2e/fixture"
    18  )
    19  
    20  // this implements the "when" part of given/when/then
    21  //
    22  // none of the func implement error checks, and that is complete intended, you should check for errors
    23  // using the Then()
    24  type Actions struct {
    25  	context      *Context
    26  	lastOutput   string
    27  	lastError    error
    28  	ignoreErrors bool
    29  }
    30  
    31  func (a *Actions) IgnoreErrors() *Actions {
    32  	a.ignoreErrors = true
    33  	return a
    34  }
    35  
    36  func (a *Actions) DoNotIgnoreErrors() *Actions {
    37  	a.ignoreErrors = false
    38  	return a
    39  }
    40  
    41  func (a *Actions) Create(args ...string) *Actions {
    42  	_, clusterClient, _ := fixture.ArgoCDClientset.NewClusterClient()
    43  
    44  	_, err := clusterClient.Create(context.Background(), &clusterpkg.ClusterCreateRequest{
    45  		Cluster: &v1alpha1.Cluster{
    46  			Server:             a.context.server,
    47  			Name:               a.context.name,
    48  			Config:             v1alpha1.ClusterConfig{BearerToken: a.context.bearerToken},
    49  			ConnectionState:    v1alpha1.ConnectionState{},
    50  			ServerVersion:      "",
    51  			Namespaces:         a.context.namespaces,
    52  			RefreshRequestedAt: nil,
    53  			Info:               v1alpha1.ClusterInfo{},
    54  			Shard:              nil,
    55  			ClusterResources:   false,
    56  			Project:            a.context.project,
    57  		},
    58  		Upsert: a.context.upsert,
    59  	})
    60  
    61  	if err != nil {
    62  		if !a.ignoreErrors {
    63  			log.Fatalf(fmt.Sprintf("Failed to upsert cluster %v", err.Error()))
    64  		}
    65  		a.lastError = errors.New(err.Error())
    66  	}
    67  
    68  	return a
    69  }
    70  
    71  func (a *Actions) CreateWithRBAC(args ...string) *Actions {
    72  	pathOpts := clientcmd.NewDefaultPathOptions()
    73  	config, err := pathOpts.GetStartingConfig()
    74  	if err != nil {
    75  		a.lastError = err
    76  		return a
    77  	}
    78  	clientConfig := clientcmd.NewDefaultClientConfig(*config, &clientcmd.ConfigOverrides{})
    79  	conf, err := clientConfig.ClientConfig()
    80  	if err != nil {
    81  		a.lastError = err
    82  		return a
    83  	}
    84  	client := kubernetes.NewForConfigOrDie(conf)
    85  
    86  	_, err = clusterauth.InstallClusterManagerRBAC(client, "kube-system", []string{}, common.BearerTokenTimeout)
    87  	if err != nil {
    88  		a.lastError = err
    89  		return a
    90  	}
    91  
    92  	return a.Create()
    93  }
    94  
    95  func (a *Actions) List() *Actions {
    96  	a.context.t.Helper()
    97  	a.runCli("cluster", "list")
    98  	return a
    99  }
   100  
   101  func (a *Actions) Get() *Actions {
   102  	a.context.t.Helper()
   103  	a.runCli("cluster", "get", a.context.server)
   104  	return a
   105  }
   106  
   107  func (a *Actions) GetByName(name string) *Actions {
   108  	a.context.t.Helper()
   109  	a.runCli("cluster", "get", name)
   110  	return a
   111  }
   112  
   113  func (a *Actions) SetNamespaces() *Actions {
   114  	a.context.t.Helper()
   115  	a.runCli("cluster", "set", a.context.name, "--namespace", strings.Join(a.context.namespaces, ","))
   116  	return a
   117  }
   118  
   119  func (a *Actions) DeleteByName() *Actions {
   120  	a.context.t.Helper()
   121  
   122  	a.runCli("cluster", "rm", a.context.name, "--yes")
   123  	return a
   124  }
   125  
   126  func (a *Actions) DeleteByServer() *Actions {
   127  	a.context.t.Helper()
   128  
   129  	a.runCli("cluster", "rm", a.context.server, "--yes")
   130  	return a
   131  }
   132  
   133  func (a *Actions) Then() *Consequences {
   134  	a.context.t.Helper()
   135  	return &Consequences{a.context, a}
   136  }
   137  
   138  func (a *Actions) runCli(args ...string) {
   139  	a.context.t.Helper()
   140  	a.lastOutput, a.lastError = fixture.RunCli(args...)
   141  }