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

backup: init backup controller #394

Merged
merged 3 commits into from
Oct 18, 2023
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
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
2 changes: 2 additions & 0 deletions cmd/fleet-manager/scheme/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
kubescheme "k8s.io/client-go/kubernetes/scheme"

applicationapi "kurator.dev/kurator/pkg/apis/apps/v1alpha1"
backupapi "kurator.dev/kurator/pkg/apis/backups/v1alpha1"
clusterv1alpha1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1"
fleetapi "kurator.dev/kurator/pkg/apis/fleet/v1alpha1"
)
Expand All @@ -36,4 +37,5 @@ func init() {
_ = clusterv1alpha1.AddToScheme(Scheme)
_ = hrapiv2b1.AddToScheme(Scheme)
_ = applicationapi.AddToScheme(Scheme)
_ = backupapi.AddToScheme(Scheme)
}
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
Xieql marked this conversation as resolved.
Show resolved Hide resolved
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
35 changes: 30 additions & 5 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 @@ -44,6 +48,8 @@ type Client struct {

karmada karmadaclientset.Interface
prom promclient.Interface
// it currently only support k8s core API and velero API, because only these schemes are registered
ctrlRuntimeClient client.Client
Xieql marked this conversation as resolved.
Show resolved Hide resolved
}

func NewClient(rest genericclioptions.RESTClientGetter) (*Client, error) {
Expand All @@ -58,12 +64,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 +200,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
Loading