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 139f0d2
Show file tree
Hide file tree
Showing 8 changed files with 688 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"
"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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/prometheus-operator/prometheus-operator/pkg/client v0.56.3
github.com/prometheus/common v0.37.0
github.com/prometheus/prometheus v1.8.2-0.20220315145411-881111fec433
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,8 @@ github.com/prometheus/prometheus v1.8.2-0.20220315145411-881111fec433/go.mod h1:
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
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 139f0d2

Please sign in to comment.