Skip to content

Commit

Permalink
convert to interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Gaudreault <[email protected]>
  • Loading branch information
agaudreault committed Jan 9, 2024
1 parent 38fc7db commit 3f51c0e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 80 deletions.
2 changes: 1 addition & 1 deletion applicationset/generators/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

func GetGenerators(ctx context.Context, c client.Client, k8sClient kubernetes.Interface, namespace string, argoCDService services.Repos, dynamicClient *dynamic.DynamicClient, scmConfig SCMConfig) map[string]Generator {
func GetGenerators(ctx context.Context, c client.Client, k8sClient kubernetes.Interface, namespace string, argoCDService services.Repos, dynamicClient dynamic.Interface, scmConfig SCMConfig) map[string]Generator {
terminalGenerators := map[string]Generator{
"List": NewListGenerator(),
"Clusters": NewClusterGenerator(c, ctx, k8sClient, namespace),
Expand Down
59 changes: 34 additions & 25 deletions cmd/argocd-server/commands/argocd_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"

cmdutil "github.com/argoproj/argo-cd/v2/cmd/util"
"github.com/argoproj/argo-cd/v2/common"
Expand Down Expand Up @@ -127,6 +129,12 @@ func NewCommand() *cobra.Command {
StrictValidation: repoServerStrictTLS,
}

dynamicClient := dynamic.NewForConfigOrDie(config)

controllerClient, err := client.New(config, client.Options{})
errors.CheckError(err)
controllerClient = client.NewDryRunClient(controllerClient)

// Load CA information to use for validating connections to the
// repository server, if strict TLS validation was requested.
if !repoServerPlaintext && repoServerStrictTLS {
Expand Down Expand Up @@ -171,31 +179,32 @@ func NewCommand() *cobra.Command {
}

argoCDOpts := server.ArgoCDServerOpts{
Insecure: insecure,
ListenPort: listenPort,
ListenHost: listenHost,
MetricsPort: metricsPort,
MetricsHost: metricsHost,
Namespace: namespace,
BaseHRef: baseHRef,
RootPath: rootPath,
Config: config,
KubeClientset: kubeclientset,
AppClientset: appClientSet,
RepoClientset: repoclientset,
DexServerAddr: dexServerAddress,
DexTLSConfig: dexTlsConfig,
DisableAuth: disableAuth,
EnableGZip: enableGZip,
TLSConfigCustomizer: tlsConfigCustomizer,
Cache: cache,
RepoServerCache: repoServerCache,
XFrameOptions: frameOptions,
ContentSecurityPolicy: contentSecurityPolicy,
RedisClient: redisClient,
StaticAssetsDir: staticAssetsDir,
ApplicationNamespaces: applicationNamespaces,
EnableProxyExtension: enableProxyExtension,
Insecure: insecure,
ListenPort: listenPort,
ListenHost: listenHost,
MetricsPort: metricsPort,
MetricsHost: metricsHost,
Namespace: namespace,
BaseHRef: baseHRef,
RootPath: rootPath,
DynamicClientset: dynamicClient,
KubeControllerClientset: controllerClient,
KubeClientset: kubeclientset,
AppClientset: appClientSet,
RepoClientset: repoclientset,
DexServerAddr: dexServerAddress,
DexTLSConfig: dexTlsConfig,
DisableAuth: disableAuth,
EnableGZip: enableGZip,
TLSConfigCustomizer: tlsConfigCustomizer,
Cache: cache,
RepoServerCache: repoServerCache,
XFrameOptions: frameOptions,
ContentSecurityPolicy: contentSecurityPolicy,
RedisClient: redisClient,
StaticAssetsDir: staticAssetsDir,
ApplicationNamespaces: applicationNamespaces,
EnableProxyExtension: enableProxyExtension,
}

stats.RegisterStackDumper()
Expand Down
39 changes: 27 additions & 12 deletions cmd/argocd/commands/headless/headless.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/argoproj/argo-cd/v2/cmd/argocd/commands/initialize"
"github.com/argoproj/argo-cd/v2/common"
Expand All @@ -19,6 +20,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
cache2 "k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -216,6 +218,17 @@ func MaybeStartLocalServer(ctx context.Context, clientOpts *apiclient.ClientOpti
return fmt.Errorf("error creating kubernetes clientset: %w", err)
}

dynamicClientset, err := dynamic.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("error creating kubernetes dynamic clientset: %w", err)
}

controllerClientset, err := client.New(restConfig, client.Options{})
if err != nil {
return fmt.Errorf("error creating kubernetes controller clientset: %w", err)
}
controllerClientset = client.NewDryRunClient(controllerClientset)

namespace, _, err := clientConfig.Namespace()
if err != nil {
return fmt.Errorf("error getting namespace: %w", err)
Expand All @@ -227,18 +240,20 @@ func MaybeStartLocalServer(ctx context.Context, clientOpts *apiclient.ClientOpti
}
appstateCache := appstatecache.NewCache(cache.NewCache(&forwardCacheClient{namespace: namespace, context: ctxStr, compression: compression, redisHaProxyName: clientOpts.RedisHaProxyName, redisName: clientOpts.RedisName}), time.Hour)
srv := server.NewServer(ctx, server.ArgoCDServerOpts{
EnableGZip: false,
Namespace: namespace,
ListenPort: *port,
AppClientset: appClientset,
DisableAuth: true,
RedisClient: redis.NewClient(&redis.Options{Addr: mr.Addr()}),
Cache: servercache.NewCache(appstateCache, 0, 0, 0),
KubeClientset: kubeClientset,
Insecure: true,
ListenHost: *address,
RepoClientset: &forwardRepoClientset{namespace: namespace, context: ctxStr, repoServerName: clientOpts.RepoServerName},
EnableProxyExtension: false,
EnableGZip: false,
Namespace: namespace,
ListenPort: *port,
AppClientset: appClientset,
DisableAuth: true,
RedisClient: redis.NewClient(&redis.Options{Addr: mr.Addr()}),
Cache: servercache.NewCache(appstateCache, 0, 0, 0),
KubeClientset: kubeClientset,
DynamicClientset: dynamicClientset,
KubeControllerClientset: controllerClientset,
Insecure: true,
ListenHost: *address,
RepoClientset: &forwardRepoClientset{namespace: namespace, context: ctxStr, repoServerName: clientOpts.RepoServerName},
EnableProxyExtension: false,
})
srv.Init(ctx)

Expand Down
10 changes: 5 additions & 5 deletions server/applicationset/applicationset.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Server struct {
db db.ArgoDB
enf *rbac.Enforcer
k8sClient kubernetes.Interface
dynamicClient *dynamic.DynamicClient
dynamicClient dynamic.Interface
client client.Client
repoClientSet repoapiclient.Clientset
appclientset appclientset.Interface
Expand All @@ -65,8 +65,8 @@ type Server struct {
func NewServer(
db db.ArgoDB,
kubeclientset kubernetes.Interface,
dynamicClient *dynamic.DynamicClient,
client client.Client,
dynamicClientset dynamic.Interface,
kubeControllerClientset client.Client,
enf *rbac.Enforcer,
repoClientSet repoapiclient.Clientset,
appclientset appclientset.Interface,
Expand All @@ -82,8 +82,8 @@ func NewServer(
ns: namespace,
db: db,
enf: enf,
dynamicClient: dynamicClient,
client: client,
dynamicClient: dynamicClientset,
client: kubeControllerClientset,
k8sClient: kubeclientset,
repoClientSet: repoClientSet,
appclientset: appclientset,
Expand Down
61 changes: 28 additions & 33 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -198,31 +197,32 @@ type ArgoCDServer struct {
}

type ArgoCDServerOpts struct {
DisableAuth bool
EnableGZip bool
Insecure bool
StaticAssetsDir string
ListenPort int
ListenHost string
MetricsPort int
MetricsHost string
Namespace string
Config *rest.Config
DexServerAddr string
DexTLSConfig *dexutil.DexTLSConfig
BaseHRef string
RootPath string
KubeClientset kubernetes.Interface
AppClientset appclientset.Interface
RepoClientset repoapiclient.Clientset
Cache *servercache.Cache
RepoServerCache *repocache.Cache
RedisClient *redis.Client
TLSConfigCustomizer tlsutil.ConfigCustomizer
XFrameOptions string
ContentSecurityPolicy string
ApplicationNamespaces []string
EnableProxyExtension bool
DisableAuth bool
EnableGZip bool
Insecure bool
StaticAssetsDir string
ListenPort int
ListenHost string
MetricsPort int
MetricsHost string
Namespace string
DexServerAddr string
DexTLSConfig *dexutil.DexTLSConfig
BaseHRef string
RootPath string
DynamicClientset dynamic.Interface
KubeControllerClientset client.Client
KubeClientset kubernetes.Interface
AppClientset appclientset.Interface
RepoClientset repoapiclient.Clientset
Cache *servercache.Cache
RepoServerCache *repocache.Cache
RedisClient *redis.Client
TLSConfigCustomizer tlsutil.ConfigCustomizer
XFrameOptions string
ContentSecurityPolicy string
ApplicationNamespaces []string
EnableProxyExtension bool
}

// initializeDefaultProject creates the default project if it does not already exist
Expand Down Expand Up @@ -853,16 +853,11 @@ func newArgoCDServiceSet(a *ArgoCDServer) *ArgoCDServiceSet {
a.projInformer,
a.ApplicationNamespaces)

dynamicClient := dynamic.NewForConfigOrDie(a.Config)
c, err := client.New(a.Config, client.Options{})
errorsutil.CheckError(err)

c = client.NewDryRunClient(c)
applicationSetService := applicationset.NewServer(
a.db,
a.KubeClientset,
dynamicClient,
c,
a.DynamicClientset,
a.KubeControllerClientset,
a.enf,
a.RepoClientset,
a.AppClientset,
Expand Down
15 changes: 11 additions & 4 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/stretchr/testify/require"
"google.golang.org/grpc/metadata"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
"sigs.k8s.io/yaml"

Expand All @@ -39,6 +40,8 @@ import (
"github.com/argoproj/argo-cd/v2/util/rbac"
settings_util "github.com/argoproj/argo-cd/v2/util/settings"
testutil "github.com/argoproj/argo-cd/v2/util/test"
dynfake "k8s.io/client-go/dynamic/fake"
clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
)

type FakeArgoCDServer struct {
Expand All @@ -52,10 +55,12 @@ func fakeServer(t *testing.T) (*FakeArgoCDServer, func()) {
kubeclientset := fake.NewSimpleClientset(cm, secret)
appClientSet := apps.NewSimpleClientset()
redis, closer := test.NewInMemoryRedis()
port, err := test.GetFreePort()
mockRepoClient := &mocks.Clientset{RepoServerServiceClient: &mocks.RepoServerServiceClient{}}
tmpAssetsDir := t.TempDir()
dynamicClient := dynfake.NewSimpleDynamicClient(runtime.NewScheme())
fakeClient := clientfake.NewClientBuilder().Build()

port, err := test.GetFreePort()
if err != nil {
panic(err)
}
Expand All @@ -78,9 +83,11 @@ func fakeServer(t *testing.T) (*FakeArgoCDServer, func()) {
1*time.Minute,
1*time.Minute,
),
RedisClient: redis,
RepoClientset: mockRepoClient,
StaticAssetsDir: tmpAssetsDir,
RedisClient: redis,
RepoClientset: mockRepoClient,
StaticAssetsDir: tmpAssetsDir,
DynamicClientset: dynamicClient,
KubeControllerClientset: fakeClient,
}
srv := NewServer(context.Background(), argoCDOpts)
fakeSrv := &FakeArgoCDServer{srv, tmpAssetsDir}
Expand Down

0 comments on commit 3f51c0e

Please sign in to comment.