Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: status controller #179

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/controller-manager/app/controllermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ const (
PolicyRCControllerName = "policyrc"
OverrideControllerName = "overridepolicy"
NamespaceAutoPropagationControllerName = "nsautoprop"
StatusControllerName = "status"
)

var knownControllers = map[string]controllermanager.StartControllerFunc{
FederateControllerName: startFederateController,
PolicyRCControllerName: startPolicyRCController,
OverrideControllerName: startOverridePolicyController,
NamespaceAutoPropagationControllerName: startNamespaceAutoPropagationController,
StatusControllerName: startStatusController,
}

var controllersDisabledByDefault = sets.New(MonitorControllerName)
Expand Down
40 changes: 38 additions & 2 deletions cmd/controller-manager/app/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/kubewharf/kubeadmiral/pkg/controllers/nsautoprop"
"github.com/kubewharf/kubeadmiral/pkg/controllers/override"
"github.com/kubewharf/kubeadmiral/pkg/controllers/policyrc"
"github.com/kubewharf/kubeadmiral/pkg/controllers/status"
)

func startFederateController(
Expand Down Expand Up @@ -55,7 +56,10 @@ func startFederateController(
return federateController, nil
}

func startPolicyRCController(ctx context.Context, controllerCtx *controllercontext.Context) (controllermanager.Controller, error) {
func startPolicyRCController(
ctx context.Context,
controllerCtx *controllercontext.Context,
) (controllermanager.Controller, error) {
policyRCController, err := policyrc.NewPolicyRCController(
controllerCtx.RestConfig,
controllerCtx.FedInformerFactory,
Expand All @@ -72,7 +76,10 @@ func startPolicyRCController(ctx context.Context, controllerCtx *controllerconte
return policyRCController, nil
}

func startOverridePolicyController(ctx context.Context, controllerCtx *controllercontext.Context) (controllermanager.Controller, error) {
func startOverridePolicyController(
ctx context.Context,
controllerCtx *controllercontext.Context,
) (controllermanager.Controller, error) {
overrideController, err := override.NewOverridePolicyController(
controllerCtx.KubeClientset,
controllerCtx.FedClientset,
Expand Down Expand Up @@ -116,3 +123,32 @@ func startNamespaceAutoPropagationController(

return nsAutoPropController, nil
}

func startStatusController(
ctx context.Context,
controllerCtx *controllercontext.Context,
) (controllermanager.Controller, error) {
statusController, err := status.NewStatusController(
controllerCtx.KubeClientset,
controllerCtx.FedClientset,
controllerCtx.FedInformerFactory.Core().V1alpha1().FederatedObjects(),
controllerCtx.FedInformerFactory.Core().V1alpha1().ClusterFederatedObjects(),
controllerCtx.FedInformerFactory.Core().V1alpha1().CollectedStatuses(),
controllerCtx.FedInformerFactory.Core().V1alpha1().ClusterCollectedStatuses(),
controllerCtx.InformerManager,
controllerCtx.FederatedInformerManager,
controllerCtx.ClusterAvailableDelay,
controllerCtx.ClusterUnavailableDelay,
controllerCtx.ComponentConfig.MemberObjectEnqueueDelay,
klog.Background(),
controllerCtx.WorkerCount,
controllerCtx.Metrics,
)
if err != nil {
return nil, fmt.Errorf("error creating sync controller: %w", err)
}

go statusController.Run(ctx)

return statusController, nil
}
14 changes: 10 additions & 4 deletions cmd/controller-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ type Options struct {
LogVerbosity int
KlogVerbosity int

NSAutoPropExcludeRegexp string
CreateCRDsForFTCs bool
ClusterJoinTimeout time.Duration
NSAutoPropExcludeRegexp string
ClusterJoinTimeout time.Duration
MemberObjectEnqueueDelay time.Duration

MaxPodListers int64
EnablePodPruning bool
Expand Down Expand Up @@ -104,14 +104,20 @@ func (o *Options) AddFlags(flags *pflag.FlagSet, allControllers []string, disabl
"",
"If non-empty, namespaces that match this go regular expression will be excluded from auto propagation.",
)
flags.BoolVar(&o.CreateCRDsForFTCs, "create-crds-for-ftcs", false, "Generate CRDs for federated types automatically.")
flags.DurationVar(
&o.ClusterJoinTimeout,
"cluster-join-timeout",
time.Minute*10,
"The maximum amount of time to wait for a new cluster to join the federation before timing out.",
)

flags.DurationVar(
&o.MemberObjectEnqueueDelay,
"member-object-enqueue-delay",
time.Second*5,
"The time to wait before enqueuing the object from member cluster.",
)

flags.Int64Var(&o.MaxPodListers, "max-pod-listers", 0, "The maximum number of concurrent pod listing requests to member clusters. "+
"A non-positive number means unlimited, but may increase the instantaneous memory usage.")
flags.BoolVar(&o.EnablePodPruning, "enable-pod-pruning", false, "Enable pod pruning for pod informer. "+
Expand Down
4 changes: 2 additions & 2 deletions cmd/controller-manager/app/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ func createControllerContext(opts *options.Options) (*controllercontext.Context,

func getComponentConfig(opts *options.Options) (*controllercontext.ComponentConfig, error) {
componentConfig := &controllercontext.ComponentConfig{
FederatedTypeConfigCreateCRDsForFTCs: opts.CreateCRDsForFTCs,
ClusterJoinTimeout: opts.ClusterJoinTimeout,
ClusterJoinTimeout: opts.ClusterJoinTimeout,
MemberObjectEnqueueDelay: opts.MemberObjectEnqueueDelay,
}

if opts.NSAutoPropExcludeRegexp != "" {
Expand Down
39 changes: 39 additions & 0 deletions pkg/apis/core/v1alpha1/extensions_collectedstatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2023 The KubeAdmiral Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Implementations for GenericCollectedStatusObject

func (cs *CollectedStatus) GetGenericCollectedStatus() *GenericCollectedStatus {
return &cs.GenericCollectedStatus
}

func (cs *CollectedStatus) GetLastUpdateTime() *metav1.Time {
return &cs.LastUpdateTime
}

func (ccs *ClusterCollectedStatus) GetGenericCollectedStatus() *GenericCollectedStatus {
return &ccs.GenericCollectedStatus
}

func (ccs *ClusterCollectedStatus) GetLastUpdateTime() *metav1.Time {
return &ccs.LastUpdateTime
}
4 changes: 4 additions & 0 deletions pkg/apis/core/v1alpha1/extensions_federatedtypeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (f *FederatedTypeConfig) IsNamespace() bool {
return f.Name == NamespaceName
}

func (f *FederatedTypeConfig) IsStatusCollectionEnabled() bool {
return f.Spec.StatusCollection != nil && f.Spec.StatusCollection.Enabled
}

func (a *APIResource) Namespaced() bool {
return a.Scope == apiextv1beta1.NamespaceScoped
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/core/v1alpha1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ type GenericFederatedObject interface {
GetStatus() *GenericFederatedObjectStatus
DeepCopyGenericFederatedObject() GenericFederatedObject
}

type GenericCollectedStatusObject interface {
metav1.Object
pkgruntime.Object
GetGenericCollectedStatus() *GenericCollectedStatus
GetLastUpdateTime() *metav1.Time
}
2 changes: 2 additions & 0 deletions pkg/apis/core/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&ClusterFederatedObjectList{},
&CollectedStatus{},
&CollectedStatusList{},
&ClusterCollectedStatus{},
&ClusterCollectedStatusList{},
&FederatedCluster{},
&FederatedClusterList{},
&FederatedTypeConfig{},
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ type ComponentConfig struct {
NSAutoPropExcludeRegexp *regexp.Regexp
FederatedTypeConfigCreateCRDsForFTCs bool
ClusterJoinTimeout time.Duration
MemberObjectEnqueueDelay time.Duration
}
Loading
Loading