Skip to content

Commit

Permalink
fix(cli): add support for components with non-default names (argoproj…
Browse files Browse the repository at this point in the history
…#10200) (argoproj#14605)

* fix(cli): add support for components with non-default names (argoproj#10200)

Co-Authored-By: maheshbaliga <[email protected]>
Signed-off-by: Michael Crenshaw <[email protected]>

---------

Signed-off-by: Michael Crenshaw <[email protected]>
Co-authored-by: maheshbaliga <[email protected]>
Signed-off-by: Alexy Mantha <[email protected]>
  • Loading branch information
2 people authored and alexymantha committed Sep 1, 2023
1 parent db0ff89 commit 66ee05b
Show file tree
Hide file tree
Showing 159 changed files with 899 additions and 67 deletions.
7 changes: 4 additions & 3 deletions cmd/argocd/commands/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

cmdutil "github.com/argoproj/argo-cd/v2/cmd/util"
"github.com/argoproj/argo-cd/v2/common"
argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient"
"github.com/argoproj/argo-cd/v2/util/errors"
"github.com/argoproj/argo-cd/v2/util/settings"

Expand All @@ -35,7 +36,7 @@ var (
)

// NewAdminCommand returns a new instance of an argocd command
func NewAdminCommand() *cobra.Command {
func NewAdminCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
pathOpts = clientcmd.NewDefaultPathOptions()
)
Expand All @@ -49,10 +50,10 @@ func NewAdminCommand() *cobra.Command {
},
}

command.AddCommand(NewClusterCommand(pathOpts))
command.AddCommand(NewClusterCommand(clientOpts, pathOpts))
command.AddCommand(NewProjectsCommand())
command.AddCommand(NewSettingsCommand())
command.AddCommand(NewAppCommand())
command.AddCommand(NewAppCommand(clientOpts))
command.AddCommand(NewRepoCommand())
command.AddCommand(NewImportCommand())
command.AddCommand(NewExportCommand())
Expand Down
17 changes: 10 additions & 7 deletions cmd/argocd/commands/admin/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
"sigs.k8s.io/yaml"

cmdutil "github.com/argoproj/argo-cd/v2/cmd/util"
"github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/controller"
"github.com/argoproj/argo-cd/v2/controller/cache"
"github.com/argoproj/argo-cd/v2/controller/metrics"
argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
appinformers "github.com/argoproj/argo-cd/v2/pkg/client/informers/externalversions"
argocdclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient"
reposerverclient "github.com/argoproj/argo-cd/v2/reposerver/apiclient"
"github.com/argoproj/argo-cd/v2/util/argo"
cacheutil "github.com/argoproj/argo-cd/v2/util/cache"
appstatecache "github.com/argoproj/argo-cd/v2/util/cache/appstate"
Expand All @@ -39,7 +41,7 @@ import (
"github.com/argoproj/argo-cd/v2/util/settings"
)

func NewAppCommand() *cobra.Command {
func NewAppCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var command = &cobra.Command{
Use: "app",
Short: "Manage applications configuration",
Expand All @@ -49,7 +51,7 @@ func NewAppCommand() *cobra.Command {
}

command.AddCommand(NewGenAppSpecCommand())
command.AddCommand(NewReconcileCommand())
command.AddCommand(NewReconcileCommand(clientOpts))
command.AddCommand(NewDiffReconcileResults())
return command
}
Expand Down Expand Up @@ -224,7 +226,7 @@ func diffReconcileResults(res1 reconcileResults, res2 reconcileResults) error {
return nil
}

func NewReconcileCommand() *cobra.Command {
func NewReconcileCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
clientConfig clientcmd.ClientConfig
selector string
Expand Down Expand Up @@ -259,11 +261,12 @@ func NewReconcileCommand() *cobra.Command {
if repoServerAddress == "" {
printLine("Repo server is not provided, trying to port-forward to argocd-repo-server pod.")
overrides := clientcmd.ConfigOverrides{}
repoServerPort, err := kubeutil.PortForward(8081, namespace, &overrides, "app.kubernetes.io/name=argocd-repo-server")
repoServerPodLabelSelector := common.LabelKeyAppName + "=" + clientOpts.RepoServerName
repoServerPort, err := kubeutil.PortForward(8081, namespace, &overrides, repoServerPodLabelSelector)
errors.CheckError(err)
repoServerAddress = fmt.Sprintf("localhost:%d", repoServerPort)
}
repoServerClient := argocdclient.NewRepoServerClientset(repoServerAddress, 60, argocdclient.TLSConfiguration{DisableTLS: false, StrictValidation: false})
repoServerClient := reposerverclient.NewRepoServerClientset(repoServerAddress, 60, reposerverclient.TLSConfiguration{DisableTLS: false, StrictValidation: false})

appClientset := appclientset.NewForConfigOrDie(cfg)
kubeClientset := kubernetes.NewForConfigOrDie(cfg)
Expand Down Expand Up @@ -328,7 +331,7 @@ func reconcileApplications(
kubeClientset kubernetes.Interface,
appClientset appclientset.Interface,
namespace string,
repoServerClient argocdclient.Clientset,
repoServerClient reposerverclient.Clientset,
selector string,
createLiveStateCache func(argoDB db.ArgoDB, appInformer kubecache.SharedIndexInformer, settingsMgr *settings.SettingsManager, server *metrics.MetricsServer) cache.LiveStateCache,
) ([]appReconcileResult, error) {
Expand Down
30 changes: 17 additions & 13 deletions cmd/argocd/commands/admin/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
cmdutil "github.com/argoproj/argo-cd/v2/cmd/util"
"github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/controller/sharding"
argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient"
argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
"github.com/argoproj/argo-cd/v2/util/argo"
Expand All @@ -39,7 +40,7 @@ import (
"github.com/argoproj/argo-cd/v2/util/text/label"
)

func NewClusterCommand(pathOpts *clientcmd.PathOptions) *cobra.Command {
func NewClusterCommand(clientOpts *argocdclient.ClientOptions, pathOpts *clientcmd.PathOptions) *cobra.Command {
var command = &cobra.Command{
Use: "cluster",
Short: "Manage clusters configuration",
Expand All @@ -50,8 +51,8 @@ func NewClusterCommand(pathOpts *clientcmd.PathOptions) *cobra.Command {

command.AddCommand(NewClusterConfig())
command.AddCommand(NewGenClusterConfigCommand(pathOpts))
command.AddCommand(NewClusterStatsCommand())
command.AddCommand(NewClusterShardsCommand())
command.AddCommand(NewClusterStatsCommand(clientOpts))
command.AddCommand(NewClusterShardsCommand(clientOpts))
namespacesCommand := NewClusterNamespacesCommand()
namespacesCommand.AddCommand(NewClusterEnableNamespacedMode())
namespacesCommand.AddCommand(NewClusterDisableNamespacedMode())
Expand All @@ -68,7 +69,7 @@ type ClusterWithInfo struct {
Namespaces []string
}

func loadClusters(ctx context.Context, kubeClient *kubernetes.Clientset, appClient *versioned.Clientset, replicas int, namespace string, portForwardRedis bool, cacheSrc func() (*appstatecache.Cache, error), shard int) ([]ClusterWithInfo, error) {
func loadClusters(ctx context.Context, kubeClient *kubernetes.Clientset, appClient *versioned.Clientset, replicas int, namespace string, portForwardRedis bool, cacheSrc func() (*appstatecache.Cache, error), shard int, redisName string, redisHaProxyName string) ([]ClusterWithInfo, error) {
settingsMgr := settings.NewSettingsManager(ctx, kubeClient, namespace)

argoDB := db.NewDB(namespace, settingsMgr, kubeClient)
Expand All @@ -79,8 +80,10 @@ func loadClusters(ctx context.Context, kubeClient *kubernetes.Clientset, appClie
var cache *appstatecache.Cache
if portForwardRedis {
overrides := clientcmd.ConfigOverrides{}
redisHaProxyPodLabelSelector := common.LabelKeyAppName + "=" + redisHaProxyName
redisPodLabelSelector := common.LabelKeyAppName + "=" + redisName
port, err := kubeutil.PortForward(6379, namespace, &overrides,
"app.kubernetes.io/name=argocd-redis-ha-haproxy", "app.kubernetes.io/name=argocd-redis")
redisHaProxyPodLabelSelector, redisPodLabelSelector)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -146,16 +149,17 @@ func loadClusters(ctx context.Context, kubeClient *kubernetes.Clientset, appClie
return clusters, nil
}

func getControllerReplicas(ctx context.Context, kubeClient *kubernetes.Clientset, namespace string) (int, error) {
func getControllerReplicas(ctx context.Context, kubeClient *kubernetes.Clientset, namespace string, appControllerName string) (int, error) {
appControllerPodLabelSelector := common.LabelKeyAppName + "=" + appControllerName
controllerPods, err := kubeClient.CoreV1().Pods(namespace).List(ctx, v1.ListOptions{
LabelSelector: "app.kubernetes.io/name=argocd-application-controller"})
LabelSelector: appControllerPodLabelSelector})
if err != nil {
return 0, err
}
return len(controllerPods.Items), nil
}

func NewClusterShardsCommand() *cobra.Command {
func NewClusterShardsCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
shard int
replicas int
Expand All @@ -179,14 +183,14 @@ func NewClusterShardsCommand() *cobra.Command {
appClient := versioned.NewForConfigOrDie(clientCfg)

if replicas == 0 {
replicas, err = getControllerReplicas(ctx, kubeClient, namespace)
replicas, err = getControllerReplicas(ctx, kubeClient, namespace, clientOpts.AppControllerName)
errors.CheckError(err)
}
if replicas == 0 {
return
}

clusters, err := loadClusters(ctx, kubeClient, appClient, replicas, namespace, portForwardRedis, cacheSrc, shard)
clusters, err := loadClusters(ctx, kubeClient, appClient, replicas, namespace, portForwardRedis, cacheSrc, shard, clientOpts.RedisName, clientOpts.RedisHaProxyName)
errors.CheckError(err)
if len(clusters) == 0 {
return
Expand Down Expand Up @@ -433,7 +437,7 @@ func NewClusterDisableNamespacedMode() *cobra.Command {
return &command
}

func NewClusterStatsCommand() *cobra.Command {
func NewClusterStatsCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
shard int
replicas int
Expand All @@ -457,10 +461,10 @@ func NewClusterStatsCommand() *cobra.Command {
kubeClient := kubernetes.NewForConfigOrDie(clientCfg)
appClient := versioned.NewForConfigOrDie(clientCfg)
if replicas == 0 {
replicas, err = getControllerReplicas(ctx, kubeClient, namespace)
replicas, err = getControllerReplicas(ctx, kubeClient, namespace, clientOpts.AppControllerName)
errors.CheckError(err)
}
clusters, err := loadClusters(ctx, kubeClient, appClient, replicas, namespace, portForwardRedis, cacheSrc, shard)
clusters, err := loadClusters(ctx, kubeClient, appClient, replicas, namespace, portForwardRedis, cacheSrc, shard, clientOpts.RedisName, clientOpts.RedisHaProxyName)
errors.CheckError(err)

w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
Expand Down
37 changes: 22 additions & 15 deletions cmd/argocd/commands/headless/headless.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra"

"github.com/argoproj/argo-cd/v2/cmd/argocd/commands/initialize"
"github.com/argoproj/argo-cd/v2/common"

"github.com/alicebob/miniredis/v2"
"github.com/golang/protobuf/ptypes/empty"
Expand Down Expand Up @@ -38,21 +39,25 @@ import (
)

type forwardCacheClient struct {
namespace string
context string
init sync.Once
client cache.CacheClient
compression cache.RedisCompressionType
err error
namespace string
context string
init sync.Once
client cache.CacheClient
compression cache.RedisCompressionType
err error
redisHaProxyName string
redisName string
}

func (c *forwardCacheClient) doLazy(action func(client cache.CacheClient) error) error {
c.init.Do(func() {
overrides := clientcmd.ConfigOverrides{
CurrentContext: c.context,
}
redisHaProxyPodLabelSelector := common.LabelKeyAppName + "=" + c.redisHaProxyName
redisPodLabelSelector := common.LabelKeyAppName + "=" + c.redisName
redisPort, err := kubeutil.PortForward(6379, c.namespace, &overrides,
"app.kubernetes.io/name=argocd-redis-ha-haproxy", "app.kubernetes.io/name=argocd-redis")
redisHaProxyPodLabelSelector, redisPodLabelSelector)
if err != nil {
c.err = err
return
Expand Down Expand Up @@ -98,19 +103,21 @@ func (c *forwardCacheClient) NotifyUpdated(key string) error {
}

type forwardRepoClientset struct {
namespace string
context string
init sync.Once
repoClientset repoapiclient.Clientset
err error
namespace string
context string
init sync.Once
repoClientset repoapiclient.Clientset
err error
repoServerName string
}

func (c *forwardRepoClientset) NewRepoServerClient() (io.Closer, repoapiclient.RepoServerServiceClient, error) {
c.init.Do(func() {
overrides := clientcmd.ConfigOverrides{
CurrentContext: c.context,
}
repoServerPort, err := kubeutil.PortForward(8081, c.namespace, &overrides, "app.kubernetes.io/name=argocd-repo-server")
repoServerPodLabelSelector := common.LabelKeyAppName + "=" + c.repoServerName
repoServerPort, err := kubeutil.PortForward(8081, c.namespace, &overrides, repoServerPodLabelSelector)
if err != nil {
c.err = err
return
Expand Down Expand Up @@ -201,7 +208,7 @@ func StartLocalServer(ctx context.Context, clientOpts *apiclient.ClientOptions,
if err != nil {
return fmt.Errorf("error running miniredis: %w", err)
}
appstateCache := appstatecache.NewCache(cache.NewCache(&forwardCacheClient{namespace: namespace, context: ctxStr, compression: compression}), time.Hour)
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,
Expand All @@ -213,7 +220,7 @@ func StartLocalServer(ctx context.Context, clientOpts *apiclient.ClientOptions,
KubeClientset: kubeClientset,
Insecure: true,
ListenHost: *address,
RepoClientset: &forwardRepoClientset{namespace: namespace, context: ctxStr},
RepoClientset: &forwardRepoClientset{namespace: namespace, context: ctxStr, repoServerName: clientOpts.RepoServerName},
EnableProxyExtension: false,
})
srv.Init(ctx)
Expand Down
11 changes: 10 additions & 1 deletion cmd/argocd/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package commands

import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"

"github.com/argoproj/argo-cd/v2/cmd/argocd/commands/admin"
"github.com/argoproj/argo-cd/v2/cmd/argocd/commands/initialize"
cmdutil "github.com/argoproj/argo-cd/v2/cmd/util"
"github.com/argoproj/argo-cd/v2/common"
argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient"
"github.com/argoproj/argo-cd/v2/util/cli"
"github.com/argoproj/argo-cd/v2/util/config"
"github.com/argoproj/argo-cd/v2/util/env"
"github.com/argoproj/argo-cd/v2/util/errors"
"github.com/argoproj/argo-cd/v2/util/localconfig"
)
Expand Down Expand Up @@ -55,7 +59,7 @@ func NewCommand() *cobra.Command {
command.AddCommand(NewLogoutCommand(&clientOpts))
command.AddCommand(initialize.InitCommand(NewCertCommand(&clientOpts)))
command.AddCommand(initialize.InitCommand(NewGPGCommand(&clientOpts)))
command.AddCommand(admin.NewAdminCommand())
command.AddCommand(admin.NewAdminCommand(&clientOpts))

defaultLocalConfigPath, err := localconfig.DefaultLocalConfigPath()
errors.CheckError(err)
Expand All @@ -76,6 +80,11 @@ func NewCommand() *cobra.Command {
command.PersistentFlags().StringVar(&clientOpts.PortForwardNamespace, "port-forward-namespace", config.GetFlag("port-forward-namespace", ""), "Namespace name which should be used for port forwarding")
command.PersistentFlags().IntVar(&clientOpts.HttpRetryMax, "http-retry-max", 0, "Maximum number of retries to establish http connection to Argo CD server")
command.PersistentFlags().BoolVar(&clientOpts.Core, "core", false, "If set to true then CLI talks directly to Kubernetes instead of talking to Argo CD API server")
command.PersistentFlags().StringVar(&clientOpts.ServerName, "server-name", env.StringFromEnv(common.EnvServerName, common.DefaultServerName), fmt.Sprintf("Name of the Argo CD API server; set this or the %s environment variable when the server's name label differs from the default, for example when installing via the Helm chart", common.EnvServerName))
command.PersistentFlags().StringVar(&clientOpts.AppControllerName, "controller-name", env.StringFromEnv(common.EnvAppControllerName, common.DefaultApplicationControllerName), fmt.Sprintf("Name of the Argo CD Application controller; set this or the %s environment variable when the controller's name label differs from the default, for example when installing via the Helm chart", common.EnvAppControllerName))
command.PersistentFlags().StringVar(&clientOpts.RedisHaProxyName, "redis-haproxy-name", env.StringFromEnv(common.EnvRedisHaProxyName, common.DefaultRedisHaProxyName), fmt.Sprintf("Name of the Redis HA Proxy; set this or the %s environment variable when the HA Proxy's name label differs from the default, for example when installing via the Helm chart", common.EnvRedisHaProxyName))
command.PersistentFlags().StringVar(&clientOpts.RedisName, "redis-name", env.StringFromEnv(common.EnvRedisName, common.DefaultRedisName), fmt.Sprintf("Name of the Redis deployment; set this or the %s environment variable when the Redis's name label differs from the default, for example when installing via the Helm chart", common.EnvRedisName))
command.PersistentFlags().StringVar(&clientOpts.RepoServerName, "repo-server-name", env.StringFromEnv(common.EnvRepoServerName, common.DefaultRepoServerName), fmt.Sprintf("Name of the Argo CD Repo server; set this or the %s environment variable when the server's name label differs from the default, for example when installing via the Helm chart", common.EnvRepoServerName))

clientOpts.KubeOverrides = &clientcmd.ConfigOverrides{}
command.PersistentFlags().StringVar(&clientOpts.KubeOverrides.CurrentContext, "kube-context", "", "Directs the command to the given kube-context")
Expand Down
22 changes: 22 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ const (
// LabelKeyAppInstance is the label key to use to uniquely identify the instance of an application
// The Argo CD application name is used as the instance name
LabelKeyAppInstance = "app.kubernetes.io/instance"
// LabelKeyAppName is the label key to use to uniquely identify the name of the Kubernetes application
LabelKeyAppName = "app.kubernetes.io/name"
// LabelKeyLegacyApplicationName is the legacy label (v0.10 and below) and is superseded by 'app.kubernetes.io/instance'
LabelKeyLegacyApplicationName = "applications.argoproj.io/app-name"
// LabelKeySecretType contains the type of argocd secret (currently: 'cluster', 'repository', 'repo-config' or 'repo-creds')
Expand Down Expand Up @@ -233,6 +235,16 @@ const (
EnvCMPWorkDir = "ARGOCD_CMP_WORKDIR"
// EnvGPGDataPath overrides the location where GPG keyring for signature verification is stored
EnvGPGDataPath = "ARGOCD_GPG_DATA_PATH"
// EnvServerName is the name of the Argo CD server component, as specified by the value under the LabelKeyAppName label key.
EnvServerName = "ARGOCD_SERVER_NAME"
// EnvRepoServerName is the name of the Argo CD repo server component, as specified by the value under the LabelKeyAppName label key.
EnvRepoServerName = "ARGOCD_REPO_SERVER_NAME"
// EnvAppControllerName is the name of the Argo CD application controller component, as specified by the value under the LabelKeyAppName label key.
EnvAppControllerName = "ARGOCD_APPLICATION_CONTROLLER_NAME"
// EnvRedisName is the name of the Argo CD redis component, as specified by the value under the LabelKeyAppName label key.
EnvRedisName = "ARGOCD_REDIS_NAME"
// EnvRedisHaProxyName is the name of the Argo CD Redis HA proxy component, as specified by the value under the LabelKeyAppName label key.
EnvRedisHaProxyName = "ARGOCD_REDIS_HAPROXY_NAME"
)

// Config Management Plugin related constants
Expand Down Expand Up @@ -268,6 +280,16 @@ const (
DefaultGitRetryFactor = int64(2)
)

// Constants represent the pod selector labels of the Argo CD component names. These values are determined by the
// installation manifests.
const (
DefaultServerName = "argocd-server"
DefaultRepoServerName = "argocd-repo-server"
DefaultApplicationControllerName = "argocd-application-controller"
DefaultRedisName = "argocd-redis"
DefaultRedisHaProxyName = "argocd-redis-ha-haproxy"
)

// GetGnuPGHomePath retrieves the path to use for GnuPG home directory, which is either taken from GNUPGHOME environment or a default value
func GetGnuPGHomePath() string {
if gnuPgHome := os.Getenv(EnvGnuPGHome); gnuPgHome == "" {
Expand Down
Loading

0 comments on commit 66ee05b

Please sign in to comment.