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

Feat: support multi cluster for source watcher #28

Merged
merged 1 commit into from
Feb 8, 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
2 changes: 1 addition & 1 deletion controllers/triggerservice/triggerservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (r *Reconciler) createRoles(ctx context.Context, ts *standardv1alpha1.Trigg
return err
}
role := &rbacv1.ClusterRoleBinding{}
if err := r.Get(ctx, types.NamespacedName{Namespace: ts.Namespace, Name: "kube-trigger"}, role); err != nil {
if err := r.Get(ctx, types.NamespacedName{Namespace: "", Name: "kube-trigger"}, role); err != nil {
return err
}
subject := rbacv1.Subject{
Expand Down
1 change: 0 additions & 1 deletion examples/triggerservice-create-event-listener.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ spec:
filter: ""
action:
type: create-event-listener

Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

"github.com/kubevela/kube-trigger/api/v1alpha1"
Expand Down Expand Up @@ -67,9 +67,7 @@ func init() {
}

// Setup prepares controllers
func Setup(ctrlConf types.Config, eh []eventhandler.EventHandler) *Controller {
conf := ctrl.GetConfigOrDie()
ctx := context.Background()
func Setup(ctx context.Context, conf *rest.Config, ctrlConf types.Config, eh []eventhandler.EventHandler) *Controller {
mapper, err := apiutil.NewDiscoveryRESTMapper(conf)
if err != nil {
logrus.WithField("source", v1alpha1.SourceTypeResourceWatcher).Fatal(err)
Expand All @@ -88,7 +86,7 @@ func Setup(ctrlConf types.Config, eh []eventhandler.EventHandler) *Controller {
if err != nil {
logrus.WithField("source", v1alpha1.SourceTypeResourceWatcher).Fatal(err)
}
dynamicClient, err := dynamic.NewForConfig(ctrl.GetConfigOrDie())
dynamicClient, err := dynamic.NewForConfig(conf)
if err != nil {
logrus.WithField("source", v1alpha1.SourceTypeResourceWatcher).Fatal(err)
}
Expand Down
82 changes: 78 additions & 4 deletions pkg/source/builtin/k8sresourcewatcher/k8s_resource_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ import (

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kubevela/kube-trigger/api/v1alpha1"
"github.com/kubevela/kube-trigger/pkg/eventhandler"
Expand All @@ -31,6 +38,15 @@ import (
sourcetypes "github.com/kubevela/kube-trigger/pkg/source/types"
)

const (
clusterLabel string = "cluster.core.oam.dev/cluster-credential-type"
defaultCluster string = "local"
clusterCertKey string = "tls.key"
clusterCertData string = "tls.crt"
clusterCAData string = "ca.crt"
clusterEndpoint string = "endpoint"
)

type K8sResourceWatcher struct {
configs map[string]*types.Config
eventHandlers map[string][]eventhandler.EventHandler
Expand Down Expand Up @@ -87,14 +103,72 @@ func (w *K8sResourceWatcher) Init(properties *runtime.RawExtension, eh eventhand

func (w *K8sResourceWatcher) Run(ctx context.Context) error {
for k, config := range w.configs {
go func(c *types.Config, handlers []eventhandler.EventHandler) {
resourceController := controller.Setup(*c, handlers)
resourceController.Run(ctx.Done())
}(config, w.eventHandlers[k])
configs, err := getConfigFromSecret(ctx, config.Clusters)
if err != nil {
return err
}
for _, kubeConfig := range configs {
go func(kube *rest.Config, c *types.Config, handlers []eventhandler.EventHandler) {
resourceController := controller.Setup(ctx, kube, *c, handlers)
resourceController.Run(ctx.Done())
}(kubeConfig, config, w.eventHandlers[k])
}
}
return nil
}

func getConfigFromSecret(ctx context.Context, clusters []string) ([]*rest.Config, error) {
configs := make([]*rest.Config, 0)
config := ctrl.GetConfigOrDie()
cli, err := client.New(config, client.Options{Scheme: scheme.Scheme})
if err != nil {
return nil, err
}
if len(clusters) == 0 {
req, err := labels.NewRequirement(clusterLabel, selection.Exists, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean it requires cluster-gateway to function?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It requires a cluster-gateway typed X509 secret but does not need a cluster-gateway. This is a temporary implementation.

if err != nil {
return nil, err
}
secrets := &corev1.SecretList{}
if err := cli.List(ctx, secrets, client.MatchingLabelsSelector{labels.NewSelector().Add(*req)}); err != nil {
return nil, err
}
for _, secret := range secrets.Items {
configs = append(configs, generateRestConfig(&secret))
}
configs = append(configs, config)
return configs, nil
}
for _, cluster := range clusters {
if cluster == defaultCluster {
configs = append(configs, config)
continue
}
secret := &corev1.Secret{}
if err := cli.Get(ctx, client.ObjectKey{Name: cluster, Namespace: "vela-system"}, secret); err != nil {
return nil, err
}
configs = append(configs, generateRestConfig(secret))
}
return configs, nil
}

func generateRestConfig(secret *corev1.Secret) *rest.Config {
c := &rest.Config{
Host: string(secret.Data[clusterEndpoint]),
TLSClientConfig: rest.TLSClientConfig{
KeyData: secret.Data[clusterCertKey],
CertData: secret.Data[clusterCertData],
},
}
if ca, ok := secret.Data[clusterCAData]; ok {
c.TLSClientConfig.CAData = ca
} else {
c.Insecure = true
}
return c
}

func (w *K8sResourceWatcher) Type() string {
return v1alpha1.SourceTypeResourceWatcher
}
1 change: 1 addition & 0 deletions pkg/source/builtin/k8sresourcewatcher/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Config struct {
Namespace string `json:"namespace,omitempty"`
Events []EventType `json:"events,omitempty"`
MatchingLabels map[string]string `json:"matchingLabels,omitempty"`
Clusters []string `json:"clusters,omitempty"`
}

func (c *Config) Key() string {
Expand Down
9 changes: 9 additions & 0 deletions pkg/templates/static/action/record-event.cue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import (
"strconv"
)

ns: kube.#List & {
$params: {
resource: {
apiVersion: "v1"
kind: "Namespace"
}
}
}

meta: {
apiVersion: "standard.oam.dev/v1alpha1"
kind: "EventListener"
Expand Down