Skip to content

Commit

Permalink
Add alpha flag for Usages
Browse files Browse the repository at this point in the history
Signed-off-by: Hasan Turken <turkenh@gmail.com>
  • Loading branch information
turkenh committed Sep 11, 2023
1 parent 0e1c4d2 commit 7e4112a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cluster/webhookconfigurations/usage.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
# Note(turkenh): It is not possible to get this generated by kubebuilder at the moment due to
# lack of support for objectSelector in controller-tools.
# See: https://github.com/kubernetes-sigs/controller-tools/blob/master/pkg/webhook/parser.go#L202-L212
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
Expand Down
5 changes: 5 additions & 0 deletions cmd/crossplane/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type startCommand struct {
EnableExternalSecretStores bool `group:"Alpha Features:" help:"Enable support for External Secret Stores."`
EnableCompositionFunctions bool `group:"Alpha Features:" help:"Enable support for Composition Functions."`
EnableCompositionWebhookSchemaValidation bool `group:"Alpha Features:" help:"Enable support for Composition validation using schemas."`
EnableUsages bool `group:"Alpha Features:" help:"Enable support for deletion ordering and resource protection with Usages."`

// These are GA features that previously had alpha or beta feature flags.
// You can't turn off a GA feature. We maintain the flags to avoid breaking
Expand Down Expand Up @@ -196,6 +197,10 @@ func (c *startCommand) Run(s *runtime.Scheme, log logging.Logger) error { //noli
feats.Enable(features.EnableAlphaCompositionWebhookSchemaValidation)
log.Info("Alpha feature enabled", "flag", features.EnableAlphaCompositionWebhookSchemaValidation)
}
if c.EnableUsages {
feats.Enable(features.EnableAlphaUsages)
log.Info("Alpha feature enabled", "flag", features.EnableAlphaUsages)
}
if !c.EnableCompositionRevisions {
log.Info("CompositionRevisions feature is GA and cannot be disabled. The --enable-composition-revisions flag will be removed in a future release.")
}
Expand Down
7 changes: 5 additions & 2 deletions internal/controller/apiextensions/apiextensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package apiextensions

import (
"github.com/crossplane/crossplane/internal/features"
ctrl "sigs.k8s.io/controller-runtime"

"github.com/crossplane/crossplane/internal/controller/apiextensions/composition"
Expand All @@ -37,8 +38,10 @@ func Setup(mgr ctrl.Manager, o controller.Options) error {
return err
}

if err := usage.Setup(mgr, o); err != nil {
return err
if o.Features.Enabled(features.EnableAlphaUsages) {
if err := usage.Setup(mgr, o); err != nil {
return err
}
}

return offered.Setup(mgr, o)
Expand Down
5 changes: 5 additions & 0 deletions internal/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ const (
// details.
// https://github.com/crossplane/crossplane/blob/f32496bed53a393c8239376fd8266ddf2ef84d61/design/design-doc-composition-validating-webhook.md
EnableAlphaCompositionWebhookSchemaValidation feature.Flag = "EnableAlphaCompositionWebhookSchemaValidation"

// EnableAlphaUsages enables alpha support for deletion ordering and
// protection with Usage resource. See the below design for more details.
// https://github.com/crossplane/crossplane/blob/19ea23e7c1fc16b20581755540f9f45afdf89338/design/one-pager-generic-usage-type.md
EnableAlphaUsages feature.Flag = "EnableAlphaUsages"
)
12 changes: 10 additions & 2 deletions internal/initializer/webhook_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ func (c *WebhookConfigurations) Run(ctx context.Context, kube client.Client) err
conf.Webhooks[i].ClientConfig.Service.Namespace = c.ServiceReference.Namespace
conf.Webhooks[i].ClientConfig.Service.Port = c.ServiceReference.Port
}
// See https://github.com/kubernetes-sigs/controller-tools/issues/658
// Note(turkenh): We have webhook configurations other than the
// ones defined with kubebuilder/controller-tools, and we
// name them as we want. So, we need to apply workaround for the
// linked issue below only for the one generated by controller-tools.
if conf.GetName() == "validating-webhook-configuration" {
// See https://github.com/kubernetes-sigs/controller-tools/issues/658
conf.SetName("crossplane")
}
case *admv1.MutatingWebhookConfiguration:
Expand All @@ -122,8 +126,12 @@ func (c *WebhookConfigurations) Run(ctx context.Context, kube client.Client) err
conf.Webhooks[i].ClientConfig.Service.Namespace = c.ServiceReference.Namespace
conf.Webhooks[i].ClientConfig.Service.Port = c.ServiceReference.Port
}
// See https://github.com/kubernetes-sigs/controller-tools/issues/658
// Note(turkenh): We have webhook configurations other than the
// ones defined with kubebuilder/controller-tools, and we
// name them as we want. So, we need to apply workaround for the
// linked issue below only for the one generated by controller-tools.
if conf.GetName() == "mutating-webhook-configuration" {
// See https://github.com/kubernetes-sigs/controller-tools/issues/658
conf.SetName("crossplane")
}
default:
Expand Down
4 changes: 4 additions & 0 deletions internal/usage/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"
"errors"
"fmt"
"github.com/crossplane/crossplane/internal/features"
"net/http"

admissionv1 "k8s.io/api/admission/v1"
Expand Down Expand Up @@ -51,6 +52,9 @@ func IndexValueForObject(u *unstructured.Unstructured) string {

// SetupWebhookWithManager sets up the webhook with the manager.
func SetupWebhookWithManager(mgr ctrl.Manager, options controller.Options) error {
if !options.Features.Enabled(features.EnableAlphaUsages) {
return nil
}
indexer := mgr.GetFieldIndexer()
if err := indexer.IndexField(context.Background(), &v1alpha1.Usage{}, InUseIndexKey, func(obj client.Object) []string {
u := obj.(*v1alpha1.Usage)
Expand Down

0 comments on commit 7e4112a

Please sign in to comment.