Skip to content

Commit

Permalink
feat: add v1beta1 flag to support v1beta1 controllers (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiying-lin authored Aug 16, 2023
1 parent bf0b0df commit fde9de9
Show file tree
Hide file tree
Showing 15 changed files with 335 additions and 178 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ test: manifests generate fmt vet local-unit-test integration-test## Run tests.

.PHONY: local-unit-test
local-unit-test: $(ENVTEST) ## Run tests.
CGO_ENABLED=1 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./pkg/... -race -coverprofile=ut-coverage.xml -covermode=atomic -v
CGO_ENABLED=1 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./pkg/... ./cmd/... -race -coverprofile=ut-coverage.xml -covermode=atomic -v

.PHONY: integration-test
integration-test: $(ENVTEST) ## Run tests.
Expand Down
33 changes: 24 additions & 9 deletions cmd/hubagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/metrics"
workv1alpha1 "sigs.k8s.io/work-api/pkg/apis/v1alpha1"

fleetv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
clusterv1beta1 "go.goms.io/fleet/apis/cluster/v1beta1"
placementv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
fleetv1alpha1 "go.goms.io/fleet/apis/v1alpha1"
"go.goms.io/fleet/cmd/hubagent/options"
"go.goms.io/fleet/cmd/hubagent/workload"
mcv1alpha1 "go.goms.io/fleet/pkg/controllers/membercluster/v1alpha1"
mcv1beta1 "go.goms.io/fleet/pkg/controllers/membercluster/v1beta1"
fleetmetrics "go.goms.io/fleet/pkg/metrics"
"go.goms.io/fleet/pkg/webhook"
// +kubebuilder:scaffold:imports
Expand All @@ -51,7 +53,8 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(fleetv1alpha1.AddToScheme(scheme))
utilruntime.Must(workv1alpha1.AddToScheme(scheme))
utilruntime.Must(fleetv1beta1.AddToScheme(scheme))
utilruntime.Must(placementv1beta1.AddToScheme(scheme))
utilruntime.Must(clusterv1beta1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
klog.InitFlags(nil)

Expand Down Expand Up @@ -93,13 +96,25 @@ func main() {
}

klog.V(2).InfoS("starting hubagent")

if err = (&mcv1alpha1.Reconciler{
Client: mgr.GetClient(),
NetworkingAgentsEnabled: opts.NetworkingAgentsEnabled,
}).SetupWithManager(mgr); err != nil {
klog.ErrorS(err, "unable to create v1alpha1 controller", "controller", "MemberCluster")
exitWithErrorFunc()
if opts.EnableV1Alpha1APIs {
klog.Info("Setting up memberCluster v1alpha1 controller")
if err = (&mcv1alpha1.Reconciler{
Client: mgr.GetClient(),
NetworkingAgentsEnabled: opts.NetworkingAgentsEnabled,
}).SetupWithManager(mgr); err != nil {
klog.ErrorS(err, "unable to create v1alpha1 controller", "controller", "MemberCluster")
exitWithErrorFunc()
}
}
if opts.EnableV1Beta1APIs {
klog.Info("Setting up memberCluster v1beta1 controller")
if err = (&mcv1beta1.Reconciler{
Client: mgr.GetClient(),
NetworkingAgentsEnabled: opts.NetworkingAgentsEnabled,
}).SetupWithManager(mgr); err != nil {
klog.ErrorS(err, "unable to create v1beta1 controller", "controller", "MemberCluster")
exitWithErrorFunc()
}
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions cmd/hubagent/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ type Options struct {
ConcurrentMemberClusterSyncs int
// RateLimiterOpts is the ratelimit parameters for the work queue
RateLimiterOpts RateLimitOptions
// EnableV1Alpha1APIs enables the agents to watch the v1alpha1 CRs.
EnableV1Alpha1APIs bool
// EnableV1Beta1APIs enables the agents to watch the v1beta1 CRs.
EnableV1Beta1APIs bool
}

// NewOptions builds an empty options.
Expand All @@ -83,6 +87,7 @@ func NewOptions() *Options {
ConcurrentClusterPlacementSyncs: 1,
ConcurrentResourceChangeSyncs: 1,
ConcurrentMemberClusterSyncs: 1,
EnableV1Alpha1APIs: true,
}
}

Expand Down Expand Up @@ -114,6 +119,8 @@ func (o *Options) AddFlags(flags *flag.FlagSet) {
flags.IntVar(&o.ConcurrentClusterPlacementSyncs, "concurrent-cluster-placement-syncs", 1, "The number of cluster placement reconcilers to run concurrently.")
flags.IntVar(&o.ConcurrentResourceChangeSyncs, "concurrent-resource-change-syncs", 20, "The number of resourceChange reconcilers that are allowed to run concurrently.")
flags.IntVar(&o.ConcurrentMemberClusterSyncs, "concurrent-member-cluster-syncs", 1, "The number of member cluster reconcilers that are allowed to run concurrently.")
flags.BoolVar(&o.EnableV1Alpha1APIs, "enable-v1alpha1-apis", true, "If set, the agents will watch for the v1alpha1 APIs.")
flags.BoolVar(&o.EnableV1Beta1APIs, "enable-v1beta1-apis", false, "If set, the agents will watch for the v1beta1 APIs.")

o.RateLimiterOpts.AddFlags(flags)
}
8 changes: 6 additions & 2 deletions cmd/hubagent/options/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ func (o *Options) Validate() field.ErrorList {
errs = append(errs, field.Invalid(newPath.Child("SkippedPropagatingAPIs"), o.SkippedPropagatingAPIs, "Invalid API string"))
}
if o.ClusterUnhealthyThreshold.Duration <= 0 {
errs = append(errs, field.Invalid(newPath.Child("ClusterUnhealthyThreshold"), o.ClusterUnhealthyThreshold, "must be greater than 0"))
errs = append(errs, field.Invalid(newPath.Child("ClusterUnhealthyThreshold"), o.ClusterUnhealthyThreshold, "Must be greater than 0"))
}
if o.WorkPendingGracePeriod.Duration <= 0 {
errs = append(errs, field.Invalid(newPath.Child("WorkPendingGracePeriod"), o.WorkPendingGracePeriod, "must be greater than 0"))
errs = append(errs, field.Invalid(newPath.Child("WorkPendingGracePeriod"), o.WorkPendingGracePeriod, "Must be greater than 0"))
}

connectionType := o.WebhookClientConnectionType
if _, err := parseWebhookClientConnectionString(connectionType); err != nil {
errs = append(errs, field.Invalid(newPath.Child("WebhookClientConnectionType"), o.EnableWebhook, err.Error()))
}

if !o.EnableV1Alpha1APIs && !o.EnableV1Beta1APIs {
errs = append(errs, field.Required(newPath.Child("EnableV1Alpha1APIs"), "Either EnableV1Alpha1APIs or EnableV1Beta1APIs is required"))
}

return errs
}
65 changes: 36 additions & 29 deletions cmd/hubagent/options/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
)

// a callback function to modify options
type ModifyOptions func(option *Options)

// New an Options with default parameters
func New(modifyOptions ModifyOptions) Options {
// newTestOptions creates an Options with default parameters.
func newTestOptions(modifyOptions ModifyOptions) Options {
option := Options{
SkippedPropagatingAPIs: "fleet.azure.com;multicluster.x-k8s.io",
WorkPendingGracePeriod: metav1.Duration{Duration: 10 * time.Second},
SkippedPropagatingAPIs: "fleet.azure.com;multicluster.x-k8s.io",
WorkPendingGracePeriod: metav1.Duration{Duration: 10 * time.Second},
ClusterUnhealthyThreshold: metav1.Duration{Duration: 1 * time.Second},
WebhookClientConnectionType: "url",
EnableV1Alpha1APIs: true,
}

if modifyOptions != nil {
Expand All @@ -30,44 +34,47 @@ func New(modifyOptions ModifyOptions) Options {
}

func TestValidateControllerManagerConfiguration(t *testing.T) {
successCases := []Options{
New(nil),
}

for _, successCase := range successCases {
if errs := successCase.Validate(); len(errs) != 0 {
t.Errorf("expected success: %v", errs)
}
}

newPath := field.NewPath("Options")
testCases := map[string]struct {
opt Options
expectedErrs field.ErrorList
opt Options
want field.ErrorList
}{
"valid Options": {
opt: newTestOptions(nil),
want: field.ErrorList{},
},
"invalid SkippedPropagatingAPIs": {
opt: New(func(options *Options) {
opt: newTestOptions(func(options *Options) {
options.SkippedPropagatingAPIs = "a/b/c/d?"
}),
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("SkippedPropagatingAPIs"), "a/b/c/d?", "Invalid API string")},
want: field.ErrorList{field.Invalid(newPath.Child("SkippedPropagatingAPIs"), "a/b/c/d?", "Invalid API string")},
},
"invalid ClusterUnhealthyThreshold": {
opt: newTestOptions(func(options *Options) {
options.ClusterUnhealthyThreshold.Duration = -40 * time.Second
}),
want: field.ErrorList{field.Invalid(newPath.Child("ClusterUnhealthyThreshold"), metav1.Duration{Duration: -40 * time.Second}, "Must be greater than 0")},
},
"invalid WorkPendingGracePeriod": {
opt: New(func(options *Options) {
opt: newTestOptions(func(options *Options) {
options.WorkPendingGracePeriod.Duration = -40 * time.Second
}),
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("WorkPendingGracePeriod"), metav1.Duration{Duration: -40 * time.Second}, "must be greater than 0")},
want: field.ErrorList{field.Invalid(newPath.Child("WorkPendingGracePeriod"), metav1.Duration{Duration: -40 * time.Second}, "Must be greater than 0")},
},
"invalid EnableV1Alpha1APIs": {
opt: newTestOptions(func(option *Options) {
option.EnableV1Alpha1APIs = false
}),
want: field.ErrorList{field.Required(newPath.Child("EnableV1Alpha1APIs"), "Either EnableV1Alpha1APIs or EnableV1Beta1APIs is required")},
},
}

for _, testCase := range testCases {
errs := testCase.opt.Validate()
if len(testCase.expectedErrs) != len(errs) {
t.Fatalf("Expected %d errors, got %d errors: %v", len(testCase.expectedErrs), len(errs), errs)
}
for i, err := range errs {
if err.Error() != testCase.expectedErrs[i].Error() {
t.Fatalf("Expected error: %s, got %s", testCase.expectedErrs[i], err.Error())
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
got := tc.opt.Validate()
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("Validate() errs mismatch (-want, +got):\n%s", diff)
}
}
})
}
}
Loading

0 comments on commit fde9de9

Please sign in to comment.