Skip to content
Open
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
20 changes: 20 additions & 0 deletions cmd/clustertree/cluster-manager/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/mcs"
podcontrollers "github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/pod"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/pv"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/pvc"
nodeserver "github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/node-server"
Expand Down Expand Up @@ -161,6 +163,12 @@ func run(ctx context.Context, opts *options.Options) error {
return err
}

discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
klog.Errorf("Unable to create discoveryClient: %v", err)
return err
}

// add cluster controller
clusterController := clusterManager.ClusterController{
Root: mgr.GetClient(),
Expand Down Expand Up @@ -262,6 +270,18 @@ func run(ctx context.Context, opts *options.Options) error {
}
}

promotePolicyController := promote.PromotePolicyController{
RootClient: mgr.GetClient(),
RootClientSet: rootClient,
RootDynamicClient: dynamicClient,
RootDiscoveryClient: discoveryClient,
GlobalLeafManager: globalleafManager,
PromotePolicyOptions: opts.PromotePolicyOptions,
}
if err = promotePolicyController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("error starting %s: %v", promote.PromotePolicyControllerName, err)
}

// init commonController
for i, gvr := range controllers.SYNC_GVRS {
commonController := controllers.SyncResourcesReconciler{
Expand Down
8 changes: 8 additions & 0 deletions cmd/clustertree/cluster-manager/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type Options struct {
BackoffOpts flags.BackoffOptions

SyncPeriod time.Duration

PromotePolicyOptions PromotePolicyOptions
}

type KubernetesOptions struct {
Expand All @@ -59,6 +61,11 @@ type KubernetesOptions struct {
Burst int `json:"burst,omitempty" yaml:"burst,omitempty"`
}

type PromotePolicyOptions struct {
// ExcludeNamespaces are the ns name excluded by default when you need to sync leaf cluster resources
ForbidNamespaces []string
}

func NewOptions() (*Options, error) {
var leaderElection componentbaseconfigv1alpha1.LeaderElectionConfiguration
componentbaseconfigv1alpha1.RecommendedDefaultLeaderElectionConfiguration(&leaderElection)
Expand Down Expand Up @@ -93,6 +100,7 @@ func (o *Options) AddFlags(flags *pflag.FlagSet) {
flags.StringSliceVar(&o.AutoCreateMCSPrefix, "auto-mcs-prefix", []string{}, "The prefix of namespace for service to auto create mcs resources")
flags.StringSliceVar(&o.ReservedNamespaces, "reserved-namespaces", []string{"kube-system"}, "The namespaces protected by Kosmos that the controller-manager will skip.")
flags.DurationVar(&o.SyncPeriod, "sync-period", 0, "the sync period for informer to resync.")
flags.StringSliceVar(&o.PromotePolicyOptions.ForbidNamespaces, "forbid-promote-namespace", []string{}, "This is forbidden to promote namespace")
o.RateLimiterOpts.AddFlags(flags)
o.BackoffOpts.AddFlags(flags)
options.BindLeaderElectionFlags(&o.LeaderElection, flags)
Expand Down
131 changes: 131 additions & 0 deletions deploy/crds/kosmos.io_promotepolicies.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.0
creationTimestamp: null
name: promotepolicies.kosmos.io
spec:
group: kosmos.io
names:
kind: PromotePolicy
listKind: PromotePolicyList
plural: promotepolicies
singular: promotepolicy
scope: Namespaced
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
description: PromotePolicy is custom resource that represents the capture
of sync leaf cluster
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: PromotePolicySpec defines the desired state of promotePolicy
properties:
clusterName:
description: Cluster is a cluster that needs to be migrated
type: string
excludedNamespaceScopedResources:
description: ExcludedNamespaceScopedResources is a slice of namespace-scoped
resource type names to exclude from the backup. If set to "*", all
namespace-scoped resource types are excluded. The default value
is empty.
items:
type: string
nullable: true
type: array
excludedNamespaces:
description: ExcludedNamespaces contains a list of namespaces that
are not included in the backup.
items:
type: string
nullable: true
type: array
includedNamespaceScopedResources:
description: IncludedNamespaceScopedResources is a slice of namespace-scoped
resource type names to include in the backup. The default value
is "*".
items:
type: string
nullable: true
type: array
includedNamespaces:
description: IncludedNamespaces is a slice of namespace names to include
objects from. If empty, all namespaces are included.
items:
type: string
nullable: true
type: array
rollback:
description: Rollback set true, then rollback from the backup
nullable: true
type: string
type: object
status:
description: PromotePolicyStatus defines the observed state of promotePolicy
properties:
backedupFile:
type: string
completionTimestamp:
description: CompletionTimestamp records the time a sync was completed.
Completion time is recorded even on failed sync. The server's time
is used for CompletionTimestamps
format: date-time
nullable: true
type: string
failureReason:
description: FailureReason is an error that caused the entire sync
to fail.
type: string
phase:
description: Phase is the current state of the Backup.
type: string
precheckErrors:
description: PrecheckErrors is a slice of all precheck errors (if
applicable).
items:
type: string
nullable: true
type: array
progress:
description: Progress contains information about the sync's execution
progress. Note that this information is best-effort only -- if fails
to update it for any reason, it may be inaccurate/stale.
nullable: true
properties:
itemsBackedUp:
description: ItemsBackedUp is the number of items that have actually
been written to the backup tarball so far.
type: integer
totalItems:
description: TotalItems is the total number of items to be backed
up. This number may change throughout the execution of the backup
due to plugins that return additional related items to back
up, the velero.io/exclude-from-backup label, and various other
filters that happen as items are processed.
type: integer
type: object
startTimestamp:
description: StartTimestamp records the time a sync was started. The
server's time is used for StartTimestamps
format: date-time
nullable: true
type: string
type: object
type: object
served: true
storage: true
21 changes: 21 additions & 0 deletions examples/promote_policy_demo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: kosmos.io/v1alpha1
kind: PromotePolicy
metadata:
name: promote-pilicy-sample
spec:
includedNamespaces:
- test
- kube-system
excludedNamespaces:
- kube-system
includedNamespaceScopedResources:
- pods
- daemonsets.apps
# - serviceexports.multicluster.x-k8s.io
- nodeconfigs.kosmos.io
excludedNamespaceScopedResources:
# - pods
# - nodeconfigs.kosmos.io
clusterName:
cluster7

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ require (
k8s.io/kube-scheduler v0.0.0
k8s.io/kubectl v0.26.3
k8s.io/kubernetes v1.13.0
k8s.io/metrics v0.26.3
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448
sigs.k8s.io/controller-runtime v0.14.5
sigs.k8s.io/mcs-api v0.1.0
Expand Down
Loading