Skip to content

Commit

Permalink
Add a flag to control CRD management
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Oct 19, 2023
1 parent 7e36489 commit 7c1107d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
8 changes: 7 additions & 1 deletion v2/cmd/controller/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ type Flags struct {
WebhookPort int
WebhookCertDir string
EnableLeaderElection bool
EnableCRDManagement bool
CRDPatterns string // This is a ; delimited string containing a collection of patterns
PreUpgradeCheck bool
}

func (f Flags) String() string {
return fmt.Sprintf(
"MetricsAddr: %s, HealthAddr: %s, WebhookServerPort: %d, WebhookServerCertDir: %s, EnableLeaderElection: %t, CRDPatterns: %s, PreUpgradeCheck: %t",
"MetricsAddr: %s, HealthAddr: %s, Webhook=Port: %d, WebhookCertDir: %s, EnableLeaderElection: %t, EnableCRDManagement: %t, CRDPatterns: %s, PreUpgradeCheck: %t",
f.MetricsAddr,
f.HealthAddr,
f.WebhookPort,
f.WebhookCertDir,
f.EnableLeaderElection,
f.EnableCRDManagement,
f.CRDPatterns,
f.PreUpgradeCheck)
}
Expand All @@ -46,6 +48,7 @@ func ParseFlags(args []string) (Flags, error) {
var webhookPort int
var webhookCertDir string
var enableLeaderElection bool
var enableCRDmanagement bool
var crdPatterns string
var preUpgradeCheck bool

Expand All @@ -56,6 +59,8 @@ func ParseFlags(args []string) (Flags, error) {
flagSet.StringVar(&webhookCertDir, "webhook-cert-dir", "", "The directory the webhook server's certs are stored.")
flagSet.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controllers manager. Enabling this will ensure there is only one active controllers manager.")
flagSet.BoolVar(&enableCRDmanagement, "enable-crd-management", true,
"Allows the operator to manage all custom resource definitions, including upgrades.")
flagSet.StringVar(&crdPatterns, "crd-pattern", "", "Install these CRDs. CRDs already in the cluster will also always be upgraded.")
flagSet.BoolVar(&preUpgradeCheck, "pre-upgrade-check", false,
"Enable pre upgrade check to check if existing crds contain helm 'keep' policy.")
Expand All @@ -68,6 +73,7 @@ func ParseFlags(args []string) (Flags, error) {
WebhookPort: webhookPort,
WebhookCertDir: webhookCertDir,
EnableLeaderElection: enableLeaderElection,
EnableCRDManagement: enableCRDmanagement,
CRDPatterns: crdPatterns,
PreUpgradeCheck: preUpgradeCheck,
}, nil
Expand Down
50 changes: 28 additions & 22 deletions v2/cmd/controller/app/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/webhook"

"github.com/Azure/azure-service-operator/v2/api"
"github.com/Azure/azure-service-operator/v2/internal/config"
Expand Down Expand Up @@ -156,39 +157,44 @@ func SetupControllerManager(ctx context.Context, setupLog logr.Logger, flgs Flag
setupLog.Error(err, "failed to initialize CRD client")
os.Exit(1)
}

goalCRDs, err := crdManager.LoadOperatorCRDs(crdmanagement.CRDLocation, cfg.PodNamespace)
if err != nil {
setupLog.Error(err, "failed to load CRDs from disk")
os.Exit(1)
}

existingCRDs, err := crdManager.ListOperatorCRDs(ctx)
if err != nil {
setupLog.Error(err, "failed to list current CRDs")
os.Exit(1)
}

// We only apply CRDs if we're in webhooks mode. No other mode will have CRD CRUD permissions
if cfg.OperatorMode.IncludesWebhooks() {
var installationInstructions []*crdmanagement.CRDInstallationInstruction
installationInstructions, err = crdManager.DetermineCRDsToInstallOrUpgrade(goalCRDs, existingCRDs, flgs.CRDPatterns)
// By default, assume the existing CRDs are the goal CRDs. If CRD management is enabled, we will
// load the goal CRDs from disk and apply them.
goalCRDs := existingCRDs
if flgs.EnableCRDManagement {
var err error
goalCRDs, err = crdManager.LoadOperatorCRDs(crdmanagement.CRDLocation, cfg.PodNamespace)
if err != nil {
setupLog.Error(err, "failed to determine CRDs to apply")
setupLog.Error(err, "failed to load CRDs from disk")
os.Exit(1)
}

included := crdmanagement.IncludedCRDs(installationInstructions)
if len(included) == 0 {
err = errors.New("No existing CRDs in cluster and no --crd-pattern specified")
setupLog.Error(err, "failed to apply CRDs")
os.Exit(1)
}
// We only apply CRDs if we're in webhooks mode. No other mode will have CRD CRUD permissions
if cfg.OperatorMode.IncludesWebhooks() {
var installationInstructions []*crdmanagement.CRDInstallationInstruction
installationInstructions, err = crdManager.DetermineCRDsToInstallOrUpgrade(goalCRDs, existingCRDs, flgs.CRDPatterns)
if err != nil {
setupLog.Error(err, "failed to determine CRDs to apply")
os.Exit(1)
}

err = crdManager.ApplyCRDs(ctx, installationInstructions)
if err != nil {
setupLog.Error(err, "failed to apply CRDs")
os.Exit(1)
included := crdmanagement.IncludedCRDs(installationInstructions)
if len(included) == 0 {
err = errors.New("No existing CRDs in cluster and no --crd-pattern specified")
setupLog.Error(err, "failed to apply CRDs")
os.Exit(1)
}

err = crdManager.ApplyCRDs(ctx, installationInstructions)
if err != nil {
setupLog.Error(err, "failed to apply CRDs")
os.Exit(1)
}
}
}

Expand Down

0 comments on commit 7c1107d

Please sign in to comment.