2019-05-09 22:05:51 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-04-18 20:52:24 +00:00
|
|
|
helmcrd "github.com/k3s-io/helm-controller/pkg/crd"
|
2020-12-01 15:59:10 +00:00
|
|
|
"github.com/k3s-io/helm-controller/pkg/generated/controllers/helm.cattle.io"
|
2023-04-18 20:52:24 +00:00
|
|
|
addoncrd "github.com/k3s-io/k3s/pkg/crd"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/generated/controllers/k3s.cattle.io"
|
2023-05-26 01:35:28 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/util"
|
2022-03-02 23:47:27 +00:00
|
|
|
"github.com/k3s-io/k3s/pkg/version"
|
2021-07-03 00:09:02 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-07-03 11:24:58 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/crd"
|
2021-07-03 08:37:19 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/generated/controllers/apps"
|
|
|
|
"github.com/rancher/wrangler/pkg/generated/controllers/batch"
|
|
|
|
"github.com/rancher/wrangler/pkg/generated/controllers/core"
|
|
|
|
"github.com/rancher/wrangler/pkg/generated/controllers/rbac"
|
2019-05-09 22:05:51 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/start"
|
2023-05-31 18:54:03 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2019-08-27 04:41:20 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
2019-05-09 22:05:51 +00:00
|
|
|
"k8s.io/client-go/rest"
|
2019-08-27 04:41:20 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2023-05-31 18:54:03 +00:00
|
|
|
"k8s.io/client-go/tools/record"
|
2019-05-09 22:05:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Context struct {
|
|
|
|
K3s *k3s.Factory
|
2019-05-26 06:42:09 +00:00
|
|
|
Helm *helm.Factory
|
|
|
|
Batch *batch.Factory
|
2019-05-09 22:05:51 +00:00
|
|
|
Apps *apps.Factory
|
2019-05-26 06:42:09 +00:00
|
|
|
Auth *rbac.Factory
|
2019-05-09 22:05:51 +00:00
|
|
|
Core *core.Factory
|
|
|
|
K8s kubernetes.Interface
|
2023-05-31 18:54:03 +00:00
|
|
|
Event record.EventRecorder
|
2019-05-09 22:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Start(ctx context.Context) error {
|
2019-05-26 06:42:09 +00:00
|
|
|
return start.All(ctx, 5, c.K3s, c.Helm, c.Apps, c.Auth, c.Batch, c.Core)
|
2019-05-09 22:05:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 01:26:39 +00:00
|
|
|
func NewContext(ctx context.Context, cfg string) (*Context, error) {
|
2019-05-09 22:05:51 +00:00
|
|
|
restConfig, err := clientcmd.BuildConfigFromFlags("", cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-26 01:35:28 +00:00
|
|
|
restConfig.UserAgent = util.GetUserAgent(version.Program + "-supervisor")
|
2021-06-10 18:27:36 +00:00
|
|
|
|
2019-05-09 22:05:51 +00:00
|
|
|
if err := crds(ctx, restConfig); err != nil {
|
2021-07-03 00:09:02 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to register CRDs")
|
2019-05-09 22:05:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 18:39:27 +00:00
|
|
|
k8s, err := kubernetes.NewForConfig(restConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-09 22:05:51 +00:00
|
|
|
return &Context{
|
|
|
|
K3s: k3s.NewFactoryFromConfigOrDie(restConfig),
|
2019-05-26 06:42:09 +00:00
|
|
|
Helm: helm.NewFactoryFromConfigOrDie(restConfig),
|
2019-05-09 22:05:51 +00:00
|
|
|
K8s: k8s,
|
2019-05-26 06:42:09 +00:00
|
|
|
Auth: rbac.NewFactoryFromConfigOrDie(restConfig),
|
2019-05-09 22:05:51 +00:00
|
|
|
Apps: apps.NewFactoryFromConfigOrDie(restConfig),
|
2019-05-26 06:42:09 +00:00
|
|
|
Batch: batch.NewFactoryFromConfigOrDie(restConfig),
|
2019-05-09 22:05:51 +00:00
|
|
|
Core: core.NewFactoryFromConfigOrDie(restConfig),
|
2023-05-31 18:54:03 +00:00
|
|
|
Event: util.BuildControllerEventRecorder(k8s, version.Program+"-supervisor", metav1.NamespaceAll),
|
2019-05-09 22:05:51 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func crds(ctx context.Context, config *rest.Config) error {
|
|
|
|
factory, err := crd.NewFactoryFromClient(config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-18 20:52:24 +00:00
|
|
|
types := append(helmcrd.List(), addoncrd.List()...)
|
|
|
|
factory.BatchCreateCRDs(ctx, types...)
|
2019-05-09 22:05:51 +00:00
|
|
|
|
|
|
|
return factory.BatchWait()
|
|
|
|
}
|