Skip to content

Commit

Permalink
Disable helm CRD installation for disable-helm-controller (#8702)
Browse files Browse the repository at this point in the history
* Disable helm CRD installation for disable-helm-controller
    The NewContext package requires config as input which would
    require all third-party callers to update when the new go module
    is published.

    This change only affects the behaviour of installation of helm
    CRDs. Existing helm crds installed in a cluster would not be removed
    when disable-helm-controller flag is set on the server.

    Addresses #8701
* address review comments
* remove redundant check

Signed-off-by: Harsimran Singh Maan <maan.harry@gmail.com>
(cherry picked from commit abc2efd)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
  • Loading branch information
harsimranmaan authored and brandond committed Nov 16, 2023
1 parent 7d1034a commit dd1787d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/etcdsnapshot/etcd_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func commandSetup(app *cli.Context, cfg *cmds.Server, config *server.Config) (*e
return nil, fmt.Errorf("etcd database not found in %s", config.ControlConfig.DataDir)
}

sc, err := server.NewContext(ctx, config.ControlConfig.Runtime.KubeConfigAdmin, false)
sc, err := server.NewContext(ctx, config, false)
if err != nil {
return nil, err
}
Expand Down
23 changes: 17 additions & 6 deletions pkg/server/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func (c *Context) Start(ctx context.Context) error {
return start.All(ctx, 5, c.K3s, c.Helm, c.Apps, c.Auth, c.Batch, c.Core)
}

func NewContext(ctx context.Context, cfg string, forServer bool) (*Context, error) {
func NewContext(ctx context.Context, config *Config, forServer bool) (*Context, error) {
cfg := config.ControlConfig.Runtime.KubeConfigAdmin
if forServer {
cfg = config.ControlConfig.Runtime.KubeConfigSupervisor
}
restConfig, err := clientcmd.BuildConfigFromFlags("", cfg)
if err != nil {
return nil, err
Expand All @@ -53,7 +57,7 @@ func NewContext(ctx context.Context, cfg string, forServer bool) (*Context, erro
var recorder record.EventRecorder
if forServer {
recorder = util.BuildControllerEventRecorder(k8s, version.Program+"-supervisor", metav1.NamespaceAll)
if err := crds(ctx, restConfig); err != nil {
if err := registerCrds(ctx, config, restConfig); err != nil {
return nil, errors.Wrap(err, "failed to register CRDs")
}
}
Expand All @@ -70,14 +74,21 @@ func NewContext(ctx context.Context, cfg string, forServer bool) (*Context, erro
}, nil
}

func crds(ctx context.Context, config *rest.Config) error {
factory, err := crd.NewFactoryFromClient(config)
func registerCrds(ctx context.Context, config *Config, restConfig *rest.Config) error {
factory, err := crd.NewFactoryFromClient(restConfig)
if err != nil {
return err
}

types := append(helmcrd.List(), addoncrd.List()...)
factory.BatchCreateCRDs(ctx, types...)
factory.BatchCreateCRDs(ctx, crds(config)...)

return factory.BatchWait()
}

func crds(config *Config) []crd.CRD {
defaultCrds := addoncrd.List()
if !config.ControlConfig.DisableHelmController {
defaultCrds = append(defaultCrds, helmcrd.List()...)
}
return defaultCrds
}
2 changes: 1 addition & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func startOnAPIServerReady(ctx context.Context, config *Config) {
func runControllers(ctx context.Context, config *Config) error {
controlConfig := &config.ControlConfig

sc, err := NewContext(ctx, controlConfig.Runtime.KubeConfigSupervisor, true)
sc, err := NewContext(ctx, config, true)
if err != nil {
return errors.Wrap(err, "failed to create new server context")
}
Expand Down

0 comments on commit dd1787d

Please sign in to comment.