Skip to content

Commit

Permalink
feature: cmp import cluster use single chart (#1516) (#1519)
Browse files Browse the repository at this point in the history
Co-authored-by: Asher Liu <[email protected]>
  • Loading branch information
erda-bot and iutx authored Aug 20, 2021
1 parent 3348d59 commit 3e1a6f0
Show file tree
Hide file tree
Showing 19 changed files with 361 additions and 649 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions cmd/cluster-init/main.go → cmd/cluster-ops/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"github.com/erda-project/erda/pkg/common"

_ "github.com/erda-project/erda-infra/providers"
_ "github.com/erda-project/erda/modules/cluster-init"
_ "github.com/erda-project/erda/modules/cluster-ops"
)

func main() {
common.Run(&servicehub.RunOptions{
ConfigFile: "conf/cluster-init/cluster-init.yaml",
ConfigFile: "conf/cluster-ops/cluster-ops.yaml",
})
}
1 change: 0 additions & 1 deletion conf/cluster-init/cluster-init.yaml

This file was deleted.

1 change: 1 addition & 0 deletions conf/cluster-ops/cluster-ops.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cluster-ops:
32 changes: 0 additions & 32 deletions modules/cluster-init/config/config.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@ import (
"k8s.io/client-go/rest"

"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/cluster-init/config"
"github.com/erda-project/erda/modules/cluster-ops/config"
erdahelm "github.com/erda-project/erda/pkg/helm"
kc "github.com/erda-project/erda/pkg/k8sclient/config"
)

const (
defaultRepoName = "stable"
InstallModeRemote = "REMOTE"
RepoModeRemote = "REMOTE"
RepoModeLocal = "LOCAL"
LocalRepoPath = "/app/charts"
ErdaBaseCharts = "erda-base"
ErdaAddonsCharts = "erda-addons"
ErdaCharts = "erda"
)

Expand Down Expand Up @@ -65,7 +60,7 @@ func WithConfig(cfg *config.Config) Option {
func (c *Client) Execute() error {
logrus.Debugf("load config: %+v", c.config)

opts, err := c.newHelmClientOptions()
opts, err := c.genHelmClientOptions()
if err != nil {
return fmt.Errorf("get helm client error: %v", err)
}
Expand All @@ -75,14 +70,16 @@ func (c *Client) Execute() error {
return err
}

switch strings.ToUpper(c.config.RepoMode) {
case RepoModeRemote:
// TODO: support repo auth info.
e := &repo.Entry{Name: defaultRepoName, URL: c.config.RepoURL}
// TODO: support repo auth info.
e := &repo.Entry{
Name: defaultRepoName,
URL: c.config.RepoURL,
Username: c.config.RepoUsername,
Password: c.config.RepoPassword,
}

if err = hc.AddOrUpdateRepo(e); err != nil {
return err
}
if err = hc.AddOrUpdateRepo(e); err != nil {
return err
}

if c.config.Reinstall {
Expand All @@ -96,7 +93,8 @@ func (c *Client) Execute() error {
LocalRepoName: defaultRepoName,
}

if err := m.Execute(); err != nil {
if err = m.Execute(); err != nil {
logrus.Errorf("execute uninstall error: %v", err)
return err
}
}
Expand All @@ -107,13 +105,14 @@ func (c *Client) Execute() error {
LocalRepoName: defaultRepoName,
}

if err := m.Execute(); err != nil {
if err = m.Execute(); err != nil {
logrus.Errorf("execute error: %v", err)
return err
}

// Label node only local mode
// TODO: support label remote with rest.config
if strings.ToUpper(c.config.RepoMode) != InstallModeRemote {
if strings.ToUpper(c.config.InstallMode) != InstallModeRemote {
rc, err := rest.InClusterConfig()
if err != nil {
logrus.Errorf("get incluster rest config error: %v", err)
Expand Down Expand Up @@ -153,8 +152,8 @@ func (c *Client) Execute() error {
return nil
}

// newHelmClientOptions create helm client options
func (c *Client) newHelmClientOptions() ([]erdahelm.Option, error) {
// genHelmClientOptions create helm client options
func (c *Client) genHelmClientOptions() ([]erdahelm.Option, error) {
opts := make([]erdahelm.Option, 0)

switch strings.ToUpper(c.config.InstallMode) {
Expand All @@ -173,36 +172,17 @@ func (c *Client) newHelmClientOptions() ([]erdahelm.Option, error) {
opts = append(opts, erdahelm.WithRESTClientGetter(erdahelm.NewRESTClientGetterImpl(rc)))
}

switch strings.ToUpper(c.config.RepoMode) {
case RepoModeLocal:
opts = append(opts, erdahelm.WithLocalChartDiscoverDir(LocalRepoPath))
}

return opts, nil
}

func (c *Client) getInitCharts() []*erdahelm.ChartSpec {
return []*erdahelm.ChartSpec{
{
ReleaseName: ErdaBaseCharts,
ChartName: ErdaBaseCharts,
Version: c.config.Version,
Action: erdahelm.ActionInstall,
Values: c.config.ChartErdaBaseValues,
},
{
ReleaseName: ErdaAddonsCharts,
ChartName: ErdaAddonsCharts,
Version: c.config.Version,
Action: erdahelm.ActionInstall,
Values: c.config.ChartErdaAddonsValues,
},
{
ReleaseName: ErdaCharts,
ChartName: ErdaCharts,
Version: c.config.Version,
Action: erdahelm.ActionInstall,
Values: c.config.ChartErdaValues,
Values: c.config.SetValues,
},
}
}
Expand Down
31 changes: 31 additions & 0 deletions modules/cluster-ops/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package config

type Config struct {
Debug bool `env:"DEBUG" default:"false" desc:"enable debug logging"`
RepoURL string `env:"HELM_REPO_URL" desc:"helm repo url"`
RepoUsername string `env:"HELM_REPO_USERNAME" desc:"helm repo url"`
RepoPassword string `env:"HELM_REPO_PASSWORD" desc:"helm repo url"`
Reinstall bool `env:"REINSTALL" default:"false" desc:"reinstall erda comp"`
Version string `env:"ERDA_CHART_VERSION" desc:"erda chart version"`
SetValues string `env:"ERDA_CHART_VALUES" desc:"provide erda values"`
InstallMode string `env:"INSTALL_MODE" default:"local" desc:"install mode, remote or local"`
TargetCluster string `env:"TARGET_CLUSTER" desc:"special when CREDENTIAL_FROM=CLUSTER_MANAGER"`
NodeLabels string `env:"NODE_LABELS" desc:"node labels after install erda"`
// HELM_NAMESPACE: helm deploy namespace
// HELM_REPO_URL: helm repo address
// HELM_REPOSITORY_CONFIG: helm repository store path
// HELM_REPOSITORY_CACHE: helm charts cache path
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/sirupsen/logrus"

"github.com/erda-project/erda-infra/base/servicehub"
"github.com/erda-project/erda/modules/cluster-init/client"
"github.com/erda-project/erda/modules/cluster-init/config"
"github.com/erda-project/erda/modules/cluster-ops/client"
"github.com/erda-project/erda/modules/cluster-ops/config"
)

type provider struct {
Expand All @@ -43,9 +43,9 @@ func (p *provider) Run(ctx context.Context) error {
}

func init() {
servicehub.Register("cluster-init", &servicehub.Spec{
Services: []string{"cluster-init"},
Description: "cluster init",
servicehub.Register("cluster-ops", &servicehub.Spec{
Services: []string{"cluster-ops"},
Description: "cluster ops",
ConfigFunc: func() interface{} {
return &config.Config{}
},
Expand Down
62 changes: 42 additions & 20 deletions modules/cmp/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ type Conf struct {
OryKratosAddr string `default:"kratos:4433" env:"KRATOS_ADDR"`
OryKratosPrivateAddr string `default:"kratos:4434" env:"KRATOS_PRIVATE_ADDR"`

ErdaNamespace string `default:"erda-system" env:"ERDA_NAMESPACE"`
ErdaHelmChartVersion string `default:"0.1.0" env:"ERDA_HELM_CHART_VERSION"`
ReleaseRepo string `default:"registry.erda.cloud/erda" env:"RELEASE_REPO"`
DialerPublicAddr string `env:"CLUSTER_DIALER_PUBLIC_ADDR"`
ReleaseRegistry string `env:"RELEASE_REGISTRY" default:"registry.erda.cloud/erda"`
ClusterInitVersion string `env:"CLUSTER_INIT_VERSION" default:"0.1"`

HelmChartRepoURL string `env:"HELM_REPO_URL"`
HelmChartRepoUserName string `env:"HELM_REPO_USERNAME"`
HelmChartRepoPassword string `env:"HELM_REPO_PASSWORD"`

ErdaNamespace string `env:"DICE_NAMESPACE" default:"erda-system"`
ErdaVersion string `env:"DICE_VERSION"`
ErdaProtocol string `env:"DICE_PROTOCOL"`
ErdaClusterName string `env:"DICE_CLUSTER_NAME"`
ErdaDomain string `env:"DICE_ROOT_DOMAIN"`
}

var cfg Conf
Expand All @@ -59,16 +67,6 @@ func ListenAddr() string {
return cfg.ListenAddr
}

// SoldierAddr return the address of soldier.
func SoldierAddr() string {
return cfg.SoldierAddr
}

// SchedulerAddr Return the address of scheduler.
func SchedulerAddr() string {
return cfg.SchedulerAddr
}

// Debug Return the switch of debug.
func Debug() bool {
return cfg.Debug
Expand Down Expand Up @@ -136,18 +134,42 @@ func OryCompatibleClientSecret() string {
return ""
}

func ReleaseRegistry() string {
return cfg.ReleaseRegistry
}

func ClusterInitVersion() string {
return cfg.ClusterInitVersion
}

func HelmRepoURL() string {
return cfg.HelmChartRepoURL
}

func HelmRepoUsername() string {
return cfg.HelmChartRepoUserName
}

func HelmRepoPassword() string {
return cfg.HelmChartRepoPassword
}

func ErdaNamespace() string {
return cfg.ErdaNamespace
}

func ErdaHelmChartVersion() string {
return cfg.ErdaHelmChartVersion
func ErdaVersion() string {
return cfg.ErdaVersion
}

func ErdaProtocol() string {
return cfg.ErdaProtocol
}

func ReleaseRepo() string {
return cfg.ReleaseRepo
func ErdaClusterName() string {
return cfg.ErdaClusterName
}

func DialerPublicAddr() string {
return cfg.DialerPublicAddr
func ErdaDomain() string {
return cfg.ErdaDomain
}
15 changes: 0 additions & 15 deletions modules/cmp/endpoints/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,18 +535,3 @@ func (e *Endpoints) InitCluster(ctx context.Context, w http.ResponseWriter, r *h

return nil
}

func (e *Endpoints) ClusterHook(ctx context.Context, r *http.Request, vars map[string]string) (resp httpserver.Responser, err error) {
req := apistructs.ClusterEvent{}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
errstr := fmt.Sprintf("decode clusterhook request fail: %v", err)
logrus.Error(errstr)
return httpserver.HTTPResponse{Status: http.StatusBadRequest, Content: errstr}, nil
}
if err := e.clusters.Hook(&req); err != nil {
errstr := fmt.Sprintf("failed to handle cluster event: %v", err)
logrus.Error(errstr)
return httpserver.HTTPResponse{Status: http.StatusInternalServerError, Content: errstr}, nil
}
return httpserver.HTTPResponse{Status: http.StatusOK}, nil
}
1 change: 0 additions & 1 deletion modules/cmp/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func (e *Endpoints) Routes() []httpserver.Endpoint {
{Path: "/api/cluster", Method: http.MethodGet, Handler: auth(i18nPrinter(e.ClusterInfo))},
{Path: "/api/cluster/init-command", Method: http.MethodGet, WriterHandler: e.InitCluster},
{Path: "/api/org-cluster-info", Method: http.MethodGet, Handler: auth(i18nPrinter(e.OrgClusterInfo))},
{Path: "/api/clusterhook", Method: http.MethodPost, Handler: auth(i18nPrinter(e.ClusterHook))},

// officer apis
{Path: "/api/clusters/{clusterName}/registry/readonly", Method: http.MethodGet, Handler: e.RegistryReadonly},
Expand Down
Loading

0 comments on commit 3e1a6f0

Please sign in to comment.