Skip to content

Commit

Permalink
chore(backport release-1.2): fix(cmd): improve Argo integration checks (
Browse files Browse the repository at this point in the history
#3349)

Co-authored-by: Hidde Beydals <[email protected]>
  • Loading branch information
akuitybot and hiddeco authored Jan 23, 2025
1 parent 8788ec0 commit b934ffa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
33 changes: 22 additions & 11 deletions cmd/controlplane/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,28 @@ func (o *apiOptions) run(ctx context.Context) error {
return fmt.Errorf("error creating Kubernetes client for Kargo API server: %w", err)
}

switch {
case !serverCfg.RolloutsIntegrationEnabled:
o.Logger.Info("Argo Rollouts integration is disabled")
case !argoRolloutsExists(ctx, restCfg):
o.Logger.Info(
"Argo Rollouts integration was enabled, but no Argo Rollouts " +
"CRDs were found. Proceeding without Argo Rollouts integration.",
)
serverCfg.RolloutsIntegrationEnabled = false
default:
o.Logger.Info("Argo Rollouts integration is enabled")
if serverCfg.RolloutsIntegrationEnabled {
var exists bool
if exists, err = argoRolloutsExists(ctx, restCfg); !exists || err != nil {
// If we are unable to determine if Argo Rollouts is installed, we
// will return an error and fail to start the server. Note this
// will only happen if we get an inconclusive response from the API
// server (e.g. due to network issues), and not if Argo Rollouts is
// not installed.
if err != nil {
return fmt.Errorf("unable to determine if Argo Rollouts is installed: %w", err)
}

o.Logger.Info(
"Argo Rollouts integration was enabled, but no Argo Rollouts " +
"CRDs were found. Proceeding without Argo Rollouts integration.",
)
serverCfg.RolloutsIntegrationEnabled = false
} else {
o.Logger.Debug("Argo Rollouts integration is enabled")
}
} else {
o.Logger.Debug("Argo Rollouts integration is disabled")
}

if serverCfg.AdminConfig != nil {
Expand Down
26 changes: 24 additions & 2 deletions cmd/controlplane/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ func (o *controllerOptions) setupKargoManager(
)
}
if stagesReconcilerCfg.RolloutsIntegrationEnabled {
if argoRolloutsExists(ctx, restCfg) {
var exists bool
if exists, err = argoRolloutsExists(ctx, restCfg); exists {
o.Logger.Info("Argo Rollouts integration is enabled")
if err = rollouts.AddToScheme(scheme); err != nil {
return nil, stagesReconcilerCfg, fmt.Errorf(
Expand All @@ -167,6 +168,18 @@ func (o *controllerOptions) setupKargoManager(
)
}
} else {
// If we are unable to determine if Argo Rollouts is installed, we
// will return an error and fail to start the controller. Note this
// will only happen if we get an inconclusive response from the API
// server (e.g. due to network issues), and not if Argo Rollouts is
// not installed.
if err != nil {
return nil, stagesReconcilerCfg, fmt.Errorf(
"unable to determine if Argo Rollouts is installed: %w",
err,
)
}

// Disable Argo Rollouts integration if the CRDs are not found.
stagesReconcilerCfg.RolloutsIntegrationEnabled = false
o.Logger.Info(
Expand Down Expand Up @@ -241,7 +254,16 @@ func (o *controllerOptions) setupArgoCDManager(ctx context.Context) (manager.Man
// There's a chance there is only permission to interact with Argo CD
// Application resources in a single namespace, so we will use that
// namespace when attempting to determine if Argo CD CRDs are installed.
if !argoCDExists(ctx, restCfg, argocdNamespace) {
var exists bool
if exists, err = argoCDExists(ctx, restCfg, argocdNamespace); !exists || err != nil {
// If we are unable to determine if Argo CD is installed, we will
// return an error and fail to start the controller. Note this
// will only happen if we get an inconclusive response from the API
// server (e.g. due to network issues), and not if Argo CD is not
// installed.
if err != nil {
return nil, fmt.Errorf("unable to determine if Argo CD is installed: %w", err)
}
o.Logger.Info(
"Argo CD integration was enabled, but no Argo CD CRDs were found. " +
"Proceeding without Argo CD integration.",
Expand Down
23 changes: 13 additions & 10 deletions cmd/controlplane/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,41 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func argoCDExists(
ctx context.Context,
restCfg *rest.Config,
namespace string,
) bool {
if client, err := dynamic.NewForConfig(restCfg); err == nil {
if _, err = client.Resource(
) (bool, error) {
c, err := dynamic.NewForConfig(restCfg)
if err == nil {
if _, err = c.Resource(
schema.GroupVersionResource{
Group: "argoproj.io",
Version: "v1alpha1",
Resource: "applications",
},
).Namespace(namespace).List(ctx, metav1.ListOptions{Limit: 1}); err == nil {
return true
return true, nil
}
}
return false
return false, client.IgnoreNotFound(err)
}

func argoRolloutsExists(ctx context.Context, restCfg *rest.Config) bool {
if client, err := dynamic.NewForConfig(restCfg); err == nil {
if _, err = client.Resource(
func argoRolloutsExists(ctx context.Context, restCfg *rest.Config) (bool, error) {
c, err := dynamic.NewForConfig(restCfg)
if err == nil {
if _, err = c.Resource(
schema.GroupVersionResource{
Group: "argoproj.io",
Version: "v1alpha1",
Resource: "analysistemplates",
},
).List(ctx, metav1.ListOptions{Limit: 1}); err == nil {
return true
return true, nil
}
}
return false
return false, client.IgnoreNotFound(err)
}

0 comments on commit b934ffa

Please sign in to comment.