Skip to content

Commit

Permalink
backup: init backup controller
Browse files Browse the repository at this point in the history
Signed-off-by: Xieql <[email protected]>
  • Loading branch information
Xieql committed Oct 12, 2023
1 parent 1a2db3a commit f05c19b
Show file tree
Hide file tree
Showing 6 changed files with 685 additions and 8 deletions.
41 changes: 41 additions & 0 deletions cmd/fleet-manager/backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright Kurator 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 backup

import (
"context"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"

"kurator.dev/kurator/cmd/fleet-manager/options"
fleet "kurator.dev/kurator/pkg/fleet-manager"
)

var log = ctrl.Log.WithName("backup")

func InitControllers(ctx context.Context, opts *options.Options, mgr ctrl.Manager) error {
if err := (&fleet.BackupManager{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: opts.Concurrency, RecoverPanic: true}); err != nil {
log.Error(err, "unable to create controller", "controller", "Backup")
return err
}

return nil
}
7 changes: 6 additions & 1 deletion cmd/fleet-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"

"kurator.dev/kurator/cmd/fleet-manager/application"
"kurator.dev/kurator/cmd/fleet-manager/backup"
"kurator.dev/kurator/cmd/fleet-manager/options"
"kurator.dev/kurator/cmd/fleet-manager/scheme"
fleet "kurator.dev/kurator/pkg/fleet-manager"
Expand Down Expand Up @@ -138,7 +139,11 @@ func run(ctx context.Context, opts *options.Options) error {
}

if err = application.InitControllers(ctx, opts, mgr); err != nil {
return fmt.Errorf("application init fail, %w", err)
return fmt.Errorf("application init controllers fail, %w", err)
}

if err = backup.InitControllers(ctx, opts, mgr); err != nil {
return fmt.Errorf("backup init controllers fail, %w", err)
}

log.Info("starting manager", "version", version.Get().String())
Expand Down
10 changes: 10 additions & 0 deletions manifests/charts/fleet-manager/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ rules:
- patch
- update
- watch
- apiGroups:
- backup.kurator.dev
resources:
- '*'
verbs:
- get
- list
- patch
- update
- watch
- apiGroups:
- kustomize.toolkit.fluxcd.io
resources:
Expand Down
38 changes: 31 additions & 7 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ import (
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
promclient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
"github.com/sirupsen/logrus"
veleroapi "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
helmclient "helm.sh/helm/v3/pkg/kube"
corev1 "k8s.io/api/core/v1"
crdclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
kubeclient "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
)

Expand All @@ -42,8 +46,9 @@ type Client struct {
crd crdclientset.Interface
helm *helmclient.Client

karmada karmadaclientset.Interface
prom promclient.Interface
karmada karmadaclientset.Interface
prom promclient.Interface
ctrlRuntimeClient client.Client
}

func NewClient(rest genericclioptions.RESTClientGetter) (*Client, error) {
Expand All @@ -58,12 +63,27 @@ func NewClient(rest genericclioptions.RESTClientGetter) (*Client, error) {
karmadaClient := karmadaclientset.NewForConfigOrDie(c)
promClient := promclient.NewForConfigOrDie(c)

// create Scheme to add velero resource
scheme := runtime.NewScheme()
if err := corev1.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf("failed to add corev1 to scheme: %v", err)
}
if err := veleroapi.AddToScheme(scheme); err != nil {
return nil, fmt.Errorf("failed to add veleroapi to scheme: %v", err)
}
// create controller-runtime client with scheme
ctrlRuntimeClient, err := client.New(c, client.Options{Scheme: scheme})
if err != nil {
return nil, err
}

return &Client{
kube: kubeClient,
helm: helmClient,
crd: crdClientSet,
karmada: karmadaClient,
prom: promClient,
kube: kubeClient,
helm: helmClient,
crd: crdClientSet,
karmada: karmadaClient,
prom: promClient,
ctrlRuntimeClient: ctrlRuntimeClient,
}, nil
}

Expand Down Expand Up @@ -179,3 +199,7 @@ func (c *Client) NewClusterHelmClient(clusterName string) (helmclient.Interface,
clusterGetter := NewRESTClientGetter(clusterConfig)
return helmclient.New(clusterGetter), nil
}

func (c *Client) CtrlRuntimeClient() client.Client {
return c.ctrlRuntimeClient
}
Loading

0 comments on commit f05c19b

Please sign in to comment.