Skip to content
This repository has been archived by the owner on Apr 7, 2020. It is now read-only.

Commit

Permalink
Ensure worker CRDs conditionally at startup
Browse files Browse the repository at this point in the history
Prevent REST mapping issues

```noteworthy operator
All provider controllers do now have a new flag `--worker-deploy-crds` which defaults to `true`. If enabled then all worker-relevant CRDs like `MachineDeployment`, etc. will be deployed during startup time of the controller.
```
  • Loading branch information
rfranzke committed Jun 13, 2019
1 parent 29f9daa commit f140892
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/gardener/gardener-extensions/pkg/controller"
controllercmd "github.com/gardener/gardener-extensions/pkg/controller/cmd"
"github.com/gardener/gardener-extensions/pkg/controller/infrastructure"
"github.com/gardener/gardener-extensions/pkg/controller/worker"

"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -56,14 +57,18 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
workerCtrlOpts = &controllercmd.ControllerOptions{
MaxConcurrentReconciles: 5,
}
workerReconcileOpts = &worker.Options{
DeployCRDs: true,
}
workerCtrlOptsUnprefixed = controllercmd.NewOptionAggregator(workerCtrlOpts, workerReconcileOpts)

controllerSwitches = alicloudcmd.ControllerSwitchOptions()

aggOption = controllercmd.NewOptionAggregator(
restOpts,
mgrOpts,
controllercmd.PrefixOption("infrastructure-", &unprefixedInfraOpts),
controllercmd.PrefixOption("worker-", workerCtrlOpts),
controllercmd.PrefixOption("worker-", &workerCtrlOptsUnprefixed),
controllerSwitches,
)
)
Expand All @@ -80,6 +85,12 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
controllercmd.LogErrAndExit(err, "Error completing config options")
}

if workerReconcileOpts.Completed().DeployCRDs {
if err := worker.ApplyMachineResourcesForConfig(ctx, restOpts.Completed().Config); err != nil {
controllercmd.LogErrAndExit(err, "Error ensuring the machine CRDs")
}
}

mgr, err := manager.New(restOpts.Completed().Config, mgrOpts.Completed().Options())
if err != nil {
controllercmd.LogErrAndExit(err, "Could not instantiate manager")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/gardener/gardener-extensions/pkg/controller"
controllercmd "github.com/gardener/gardener-extensions/pkg/controller/cmd"
"github.com/gardener/gardener-extensions/pkg/controller/infrastructure"
"github.com/gardener/gardener-extensions/pkg/controller/worker"
webhookcmd "github.com/gardener/gardener-extensions/pkg/webhook/cmd"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -63,6 +64,10 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
workerCtrlOpts = &controllercmd.ControllerOptions{
MaxConcurrentReconciles: 5,
}
workerReconcileOpts = &worker.Options{
DeployCRDs: true,
}
workerCtrlOptsUnprefixed = controllercmd.NewOptionAggregator(workerCtrlOpts, workerReconcileOpts)

controllerSwitches = awscmd.ControllerSwitchOptions()
webhookSwitches = awscmd.WebhookAddToManagerOptions()
Expand All @@ -72,7 +77,7 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
mgrOpts,
controllercmd.PrefixOption("controlplane-", controlPlaneCtrlOpts),
controllercmd.PrefixOption("infrastructure-", &infraCtrlOptsUnprefixed),
controllercmd.PrefixOption("worker-", workerCtrlOpts),
controllercmd.PrefixOption("worker-", &workerCtrlOptsUnprefixed),
configFileOpts,
controllerSwitches,
webhookSwitches,
Expand All @@ -97,6 +102,12 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
controllercmd.LogErrAndExit(err, "Error completing options")
}

if workerReconcileOpts.Completed().DeployCRDs {
if err := worker.ApplyMachineResourcesForConfig(ctx, restOpts.Completed().Config); err != nil {
controllercmd.LogErrAndExit(err, "Error ensuring the machine CRDs")
}
}

mgr, err := manager.New(restOpts.Completed().Config, mgrOpts.Completed().Options())
if err != nil {
controllercmd.LogErrAndExit(err, "Could not instantiate manager")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/gardener/gardener-extensions/pkg/controller"
controllercmd "github.com/gardener/gardener-extensions/pkg/controller/cmd"
"github.com/gardener/gardener-extensions/pkg/controller/infrastructure"
"github.com/gardener/gardener-extensions/pkg/controller/worker"

"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -56,14 +57,18 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
workerCtrlOpts = &controllercmd.ControllerOptions{
MaxConcurrentReconciles: 5,
}
workerReconcileOpts = &worker.Options{
DeployCRDs: true,
}
workerCtrlOptsUnprefixed = controllercmd.NewOptionAggregator(workerCtrlOpts, workerReconcileOpts)

controllerSwitches = azurecmd.ControllerSwitchOptions()

aggOption = controllercmd.NewOptionAggregator(
restOpts,
mgrOpts,
controllercmd.PrefixOption("infrastructure-", &infraCtrlOptsUnprefixed),
controllercmd.PrefixOption("worker-", workerCtrlOpts),
controllercmd.PrefixOption("worker-", &workerCtrlOptsUnprefixed),
controllerSwitches,
)
)
Expand All @@ -80,6 +85,12 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
controllercmd.LogErrAndExit(err, "Error completing config options")
}

if workerReconcileOpts.Completed().DeployCRDs {
if err := worker.ApplyMachineResourcesForConfig(ctx, restOpts.Completed().Config); err != nil {
controllercmd.LogErrAndExit(err, "Error ensuring the machine CRDs")
}
}

mgr, err := manager.New(restOpts.Completed().Config, mgrOpts.Completed().Options())
if err != nil {
controllercmd.LogErrAndExit(err, "Could not instantiate manager")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/gardener/gardener-extensions/pkg/controller"
controllercmd "github.com/gardener/gardener-extensions/pkg/controller/cmd"
"github.com/gardener/gardener-extensions/pkg/controller/infrastructure"
"github.com/gardener/gardener-extensions/pkg/controller/worker"
webhookcmd "github.com/gardener/gardener-extensions/pkg/webhook/cmd"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -60,6 +61,10 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
workerCtrlOpts = &controllercmd.ControllerOptions{
MaxConcurrentReconciles: 5,
}
workerReconcileOpts = &worker.Options{
DeployCRDs: true,
}
workerCtrlOptsUnprefixed = controllercmd.NewOptionAggregator(workerCtrlOpts, workerReconcileOpts)

controllerSwitches = gcpcmd.ControllerSwitchOptions()
webhookSwitches = gcpcmd.WebhookAddToManagerOptions()
Expand All @@ -69,7 +74,7 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
mgrOpts,
controllercmd.PrefixOption("infrastructure-", &unprefixedInfraOpts),
controllercmd.PrefixOption("controlplane-", controlPlaneCtrlOpts),
controllercmd.PrefixOption("worker-", workerCtrlOpts),
controllercmd.PrefixOption("worker-", &workerCtrlOptsUnprefixed),
controllerSwitches,
webhookSwitches,
)
Expand All @@ -96,6 +101,12 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
controllercmd.LogErrAndExit(err, "Error completing config options")
}

if workerReconcileOpts.Completed().DeployCRDs {
if err := worker.ApplyMachineResourcesForConfig(ctx, restOpts.Completed().Config); err != nil {
controllercmd.LogErrAndExit(err, "Error ensuring the machine CRDs")
}
}

mgr, err := manager.New(restOpts.Completed().Config, mgrOpts.Completed().Options())
if err != nil {
controllercmd.LogErrAndExit(err, "Could not instantiate manager")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/gardener/gardener-extensions/pkg/controller"
controllercmd "github.com/gardener/gardener-extensions/pkg/controller/cmd"
"github.com/gardener/gardener-extensions/pkg/controller/infrastructure"
"github.com/gardener/gardener-extensions/pkg/controller/worker"

"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand Down Expand Up @@ -56,14 +57,18 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
workerCtrlOpts = &controllercmd.ControllerOptions{
MaxConcurrentReconciles: 5,
}
workerReconcileOpts = &worker.Options{
DeployCRDs: true,
}
workerCtrlOptsUnprefixed = controllercmd.NewOptionAggregator(workerCtrlOpts, workerReconcileOpts)

controllerSwitches = openstackcmd.ControllerSwitchOptions()

aggOption = controllercmd.NewOptionAggregator(
restOpts,
mgrOpts,
controllercmd.PrefixOption("infrastructure-", &infraCtrlOptsUnprefixed),
controllercmd.PrefixOption("worker-", workerCtrlOpts),
controllercmd.PrefixOption("worker-", &workerCtrlOptsUnprefixed),
controllerSwitches,
)
)
Expand All @@ -80,6 +85,12 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
controllercmd.LogErrAndExit(err, "Error completing config options")
}

if workerReconcileOpts.Completed().DeployCRDs {
if err := worker.ApplyMachineResourcesForConfig(ctx, restOpts.Completed().Config); err != nil {
controllercmd.LogErrAndExit(err, "Error ensuring the machine CRDs")
}
}

mgr, err := manager.New(restOpts.Completed().Config, mgrOpts.Completed().Options())
if err != nil {
controllercmd.LogErrAndExit(err, "Could not instantiate manager")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/gardener/gardener-extensions/pkg/controller"
controllercmd "github.com/gardener/gardener-extensions/pkg/controller/cmd"
"github.com/gardener/gardener-extensions/pkg/controller/infrastructure"
"github.com/gardener/gardener-extensions/pkg/controller/worker"
webhookcmd "github.com/gardener/gardener-extensions/pkg/webhook/cmd"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -63,18 +64,23 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
workerCtrlOpts = &controllercmd.ControllerOptions{
MaxConcurrentReconciles: 5,
}
workerReconcileOpts = &worker.Options{
DeployCRDs: true,
}
workerCtrlOptsUnprefixed = controllercmd.NewOptionAggregator(workerCtrlOpts, workerReconcileOpts)

controllerSwitches = packetcmd.ControllerSwitchOptions()
webhookSwitches = packetcmd.WebhookAddToManagerOptions()

aggOption = controllercmd.NewOptionAggregator(
restOpts,
mgrOpts,
controllercmd.PrefixOption("infrastructure-", &unprefixedInfraOpts),
controllercmd.PrefixOption("controlplane-", controlPlaneCtrlOpts),
controllercmd.PrefixOption("worker-", workerCtrlOpts),
controllercmd.PrefixOption("infrastructure-", &unprefixedInfraOpts),
controllercmd.PrefixOption("worker-", &workerCtrlOptsUnprefixed),
controllerSwitches,
webhookSwitches,
configFileOpts,
webhookOptions,
)
)

Expand All @@ -96,8 +102,10 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
controllercmd.LogErrAndExit(err, "Error completing options")
}

if err := configFileOpts.Complete(); err != nil {
controllercmd.LogErrAndExit(err, "Error completing config options")
if workerReconcileOpts.Completed().DeployCRDs {
if err := worker.ApplyMachineResourcesForConfig(ctx, restOpts.Completed().Config); err != nil {
controllercmd.LogErrAndExit(err, "Error ensuring the machine CRDs")
}
}

mgr, err := manager.New(restOpts.Completed().Config, mgrOpts.Completed().Options())
Expand Down Expand Up @@ -134,7 +142,6 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
}

aggOption.AddFlags(cmd.Flags())
configFileOpts.AddFlags(cmd.Flags())

return cmd
}
4 changes: 0 additions & 4 deletions pkg/controller/worker/genericactuator/actuator_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ func (a *genericActuator) Reconcile(ctx context.Context, worker *extensionsv1alp
return errors.Wrapf(err, "could not instantiate actuator context")
}

if err := ensureMachineResources(ctx, a.client); err != nil {
return err
}

// Deploy the machine-controller-manager into the cluster.
a.logger.Info("Deploying the machine-controller-manager", "worker", fmt.Sprintf("%s/%s", worker.Namespace, worker.Name))
if err := a.deployMachineControllerManager(ctx, worker, cluster, workerDelegate); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package genericactuator
package worker

import (
"context"

apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsscheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand All @@ -30,7 +32,10 @@ const (
machineVersion = "v1alpha1"
)

var machineCRDs []*apiextensionsv1beta1.CustomResourceDefinition
var (
machineCRDs []*apiextensionsv1beta1.CustomResourceDefinition
apiextensionsScheme = runtime.NewScheme()
)

func init() {
agePrinterColumn := apiextensionsv1beta1.CustomResourceColumnDefinition{
Expand Down Expand Up @@ -295,22 +300,35 @@ func init() {
},
})
}

utilruntime.Must(apiextensionsscheme.AddToScheme(apiextensionsScheme))
}

// ApplyMachineResourcesForConfig ensures that all well-known machine CRDs are created or updated.
func ApplyMachineResourcesForConfig(ctx context.Context, config *rest.Config) error {
c, err := client.New(config, client.Options{Scheme: apiextensionsScheme})
if err != nil {
return err
}

return ApplyMachineResources(ctx, c)
}

// ApplyMachineResources ensures that all well-known machine CRDs are created or updated.
// TODO: Use github.com/gardener/gardener/pkg/utils/flow.Parallel as soon as we can vendor a new Gardener version again.
func ensureMachineResources(ctx context.Context, c client.Client) error {
func ApplyMachineResources(ctx context.Context, c client.Client) error {
for _, crd := range machineCRDs {
obj := &apiextensionsv1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: crd.Name,
},
}
_, err := controllerutil.CreateOrUpdate(ctx, c, obj, func(existing runtime.Object) error {

if _, err := controllerutil.CreateOrUpdate(ctx, c, obj, func(existing runtime.Object) error {
existingCRD := existing.(*apiextensionsv1beta1.CustomResourceDefinition)
existingCRD.Spec = crd.Spec
return nil
})
if err != nil {
}); err != nil {
return err
}
}
Expand Down
Loading

0 comments on commit f140892

Please sign in to comment.