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

Admin injection #59

Closed
wants to merge 6 commits into from
Closed
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
17 changes: 16 additions & 1 deletion app/dubbo-cp/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd

import (
"fmt"
"os"
"time"

"github.com/apache/dubbo-kubernetes/pkg/webhook"
Expand All @@ -39,7 +40,11 @@ import (
"github.com/spf13/cobra"
)

const gracefullyShutdownDuration = 3 * time.Second
const (
gracefullyShutdownDuration = 3 * time.Second
AdminRegistryAddress = "ADMIN_REGISTRY_ADDRESS"
AdminPrometheusAddress = "ADMIN_PROMETHEUS_ADDRESS"
)

// This is the open file limit below which the control plane may not
// reasonably have enough descriptors to accept all its clients.
Expand All @@ -56,6 +61,16 @@ func newRunCmdWithOpts(opts cmd.RunCmdOpts) *cobra.Command {
RunE: func(cmd *cobra.Command, _ []string) error {
cfg := dubbo_cp.DefaultConfig()
err := config.Load(args.configPath, &cfg)
registryenv := os.Getenv(AdminRegistryAddress)
promenv := os.Getenv(AdminPrometheusAddress)
if registryenv != "" {
cfg.Admin.Registry.Address = registryenv
cfg.Admin.MetadataReport.Address = registryenv
cfg.Admin.ConfigCenter = registryenv
}
if promenv != "" {
cfg.Admin.Prometheus.Address = promenv
}
if err != nil {
logger.Sugar().Error(err, "could not load the configuration")
return err
Expand Down
16 changes: 13 additions & 3 deletions app/dubboctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,25 @@ type RootCommandConfig struct {
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(args []string) {
rootCmd := getRootCmd(args)
// when flag error occurs, print usage string.
// but if an error occurs when executing command, usage string will not be printed.
rootCmd.SetFlagErrorFunc(func(command *cobra.Command, err error) error {
command.Println(command.UsageString())

return err
})

cobra.CheckErr(rootCmd.Execute())
}

func getRootCmd(args []string) *cobra.Command {
// rootCmd represents the base command when called without any subcommands
rootCmd := &cobra.Command{
Use: "dubboctl",
Short: "dubbo control interface",
Long: ``,
Use: "dubboctl",
Short: "dubbo control interface",
Long: ``,
SilenceErrors: true,
SilenceUsage: true,
}

cfg := RootCommandConfig{
Expand Down
39 changes: 27 additions & 12 deletions app/dubboctl/identifier/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,34 @@
package identifier

import (
"path/filepath"
"runtime"
"net/url"

"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/filesystem"
"github.com/apache/dubbo-kubernetes/deploy"
)

var (
_, b, _, _ = runtime.Caller(0)
// Root folder of dubbo-admin
// This relies on the fact this file is 3 levels up from the root; if this changes, adjust the path below
Root = filepath.Join(filepath.Dir(b), "../../..")
Deploy = filepath.Join(Root, "/deploy")
Charts = filepath.Join(Deploy, "/charts")
Profiles = filepath.Join(Deploy, "profiles")
Addons = filepath.Join(Deploy, "addons")
AddonDashboards = filepath.Join(Addons, "dashboards")
AddonManifests = filepath.Join(Addons, "manifests")
// deploy dir is root in embed.FS
deployUri = &url.URL{
Scheme: filesystem.EmbedSchema,
OmitHost: true,
}

chartsUri = deployUri.JoinPath("charts")
profilesUri = deployUri.JoinPath("profiles")
addonsUri = deployUri.JoinPath("addons")
addonDashboardsUri = addonsUri.JoinPath("dashboards")
addonManifestsUri = addonsUri.JoinPath("manifests")

Charts = chartsUri.String()
Profiles = profilesUri.String()
Addons = addonsUri.String()
AddonDashboards = addonDashboardsUri.String()
AddonManifests = addonManifestsUri.String()
)

var UnionFS filesystem.UnionFS

func init() {
UnionFS = filesystem.NewUnionFS(deploy.EmbedRootFS)
}
4 changes: 3 additions & 1 deletion app/dubboctl/internal/dubbo/deploy.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ spec:
image: {{.Image}}
env:
- name: DUBBO_CTL_VERSION
value: 0.0.1
value: 0.0.1{{if .Registry}}
- name: DUBBO_REGISTRY_ADDRESS
value: {{.Registry}}{{end}}
ports:
- containerPort: {{.Port}}
name: dubbo
Expand Down
2 changes: 2 additions & 0 deletions app/dubboctl/internal/dubbo/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Deployment struct {
TargetPort int
NodePort int
UseNodePort bool
Registry string
UseProm bool
}

Expand Down Expand Up @@ -101,6 +102,7 @@ func (d *DeployApp) Deploy(ctx context.Context, f *Dubbo, option ...DeployOption
TargetPort: targetPort,
NodePort: f.Deploy.NodePort,
UseNodePort: nodePort > 0,
Registry: f.Deploy.Registry,
UseProm: f.Deploy.UseProm,
})
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions app/dubboctl/internal/dubbo/dubbo.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type DeploySpec struct {
ContainerPort int `yaml:"containerPort,omitempty"`
TargetPort int `yaml:"targetPort,omitempty"`
NodePort int `yaml:"nodePort,omitempty"`
Registry string `yaml:"registry,omitempty"`
UseProm bool `yaml:"-"`
}

Expand Down Expand Up @@ -371,6 +372,46 @@ func (f *Dubbo) CheckLabels(ns string, client *Client) error {
}

if namespace != "" {
var name string
var dns string

nacosSelector := client2.MatchingLabels{
"dubbo.apache.org/nacos": "true",
}

nacosList := &corev1.ServiceList{}
if err := client.KubeCtl.List(context.Background(), nacosList, nacosSelector, client2.InNamespace(namespace)); err != nil {
if errors2.IsNotFound(err) {
return nil
} else {
return err
}
}
if len(nacosList.Items) > 0 {
name = nacosList.Items[0].Name
dns = fmt.Sprintf("nacos://%s.%s.svc", name, namespace)
f.Deploy.Registry = dns
}

zkSelector := client2.MatchingLabels{
"dubbo.apache.org/zookeeper": "true",
}

zkList := &corev1.ServiceList{}
if err := client.KubeCtl.List(context.Background(), zkList, zkSelector, client2.InNamespace(namespace)); err != nil {
if errors2.IsNotFound(err) {
return nil
} else {
return err
}
}

if len(zkList.Items) > 0 {
name = zkList.Items[0].Name
dns = fmt.Sprintf("zookeeper://%s.%s.svc", name, namespace)
f.Deploy.Registry = dns
}

promSelector := client2.MatchingLabels{
"dubbo.apache.org/prometheus": "true",
}
Expand Down
67 changes: 67 additions & 0 deletions app/dubboctl/internal/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ package filesystem

import (
"archive/zip"
"embed"
"errors"
"fmt"
"io"
"io/fs"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -267,6 +270,70 @@ func (m maskingFS) Readlink(link string) (string, error) {
return m.fs.Readlink(link)
}

const (
EmbedSchema = "embed"
)

// UnionFS is an os.FS and embed.FS union fs.
// Files in embed.FS has the header "embed://", and files in os.FS don't have this header.
type UnionFS struct {
embedFS embed.FS
}

func NewUnionFS(embedFS embed.FS) UnionFS {
return UnionFS{
embedFS: embedFS,
}
}

func (u UnionFS) ReadFile(name string) ([]byte, error) {
uri, _ := url.Parse(name)
if uri != nil && uri.Scheme == EmbedSchema {
name = strings.TrimLeft(uri.Opaque, "/")
return u.embedFS.ReadFile(name)
}

return os.ReadFile(name)
}

func (u UnionFS) Open(name string) (fs.File, error) {
uri, _ := url.Parse(name)
if uri != nil && uri.Scheme == EmbedSchema {
name = strings.TrimLeft(uri.Opaque, "/")
return u.embedFS.Open(name)
}

return os.Open(name)
}

func (u UnionFS) ReadDir(name string) ([]fs.DirEntry, error) {
uri, _ := url.Parse(name)
if uri != nil && uri.Scheme == EmbedSchema {
name = strings.TrimLeft(uri.Opaque, "/")
return u.embedFS.ReadDir(name)
}

return os.ReadDir(name)
}

func (u UnionFS) Stat(name string) (fs.FileInfo, error) {
f, err := u.Open(name)
if err != nil {
return nil, err
}

return f.Stat()
}

func (u UnionFS) Readlink(link string) (string, error) {
uri, _ := url.Parse(link)
if uri != nil && uri.Scheme == EmbedSchema {
return "", errors.New("embed FS not support read link")
}

return os.Readlink(link)
}

// CopyFromFS copies files from the `src` dir on the accessor Filesystem to local filesystem into `dest` dir.
// The src path uses slashes as their separator.
// The dest path uses OS specific separator.
Expand Down
18 changes: 9 additions & 9 deletions app/dubboctl/internal/kube/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package kube

import (
"os"
"path"
"strings"
"unicode/utf8"
Expand All @@ -26,6 +25,7 @@ import (

"github.com/apache/dubbo-kubernetes/app/dubboctl/identifier"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/apis/dubbo.apache.org/v1alpha1"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/filesystem"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/manifest"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/manifest/render"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/util"
Expand Down Expand Up @@ -133,7 +133,7 @@ func NewAdminComponent(spec *v1alpha1.AdminSpec, opts ...ComponentOption) (Compo
renderer, err := render.NewLocalRenderer(
render.WithName(string(Admin)),
render.WithNamespace(newOpts.Namespace),
render.WithFS(os.DirFS(newOpts.ChartPath)),
render.WithFS(filesystem.NewSubFS(newOpts.ChartPath, identifier.UnionFS)),
render.WithDir("dubbo-admin"))
if err != nil {
return nil, err
Expand Down Expand Up @@ -195,7 +195,7 @@ func NewGrafanaComponent(spec *v1alpha1.GrafanaSpec, opts ...ComponentOption) (C
renderer, err = render.NewLocalRenderer(
render.WithName(string(Grafana)),
render.WithNamespace(newOpts.Namespace),
render.WithFS(os.DirFS(newOpts.ChartPath)),
render.WithFS(filesystem.NewSubFS(newOpts.ChartPath, identifier.UnionFS)),
render.WithDir("grafana"),
)
if err != nil {
Expand Down Expand Up @@ -247,7 +247,7 @@ func NewNacosComponent(spec *v1alpha1.NacosSpec, opts ...ComponentOption) (Compo
renderer, err := render.NewLocalRenderer(
render.WithName(string(Nacos)),
render.WithNamespace(newOpts.Namespace),
render.WithFS(os.DirFS(newOpts.ChartPath)),
render.WithFS(filesystem.NewSubFS(newOpts.ChartPath, identifier.UnionFS)),
render.WithDir("nacos"))
if err != nil {
return nil, err
Expand Down Expand Up @@ -309,7 +309,7 @@ func NewZookeeperComponent(spec *v1alpha1.ZookeeperSpec, opts ...ComponentOption
renderer, err = render.NewLocalRenderer(
render.WithName(string(Zookeeper)),
render.WithNamespace(newOpts.Namespace),
render.WithFS(os.DirFS(newOpts.ChartPath)),
render.WithFS(filesystem.NewSubFS(newOpts.ChartPath, identifier.UnionFS)),
render.WithDir("zookeeper"),
)
if err != nil {
Expand Down Expand Up @@ -374,7 +374,7 @@ func NewPrometheusComponent(spec *v1alpha1.PrometheusSpec, opts ...ComponentOpti
renderer, err = render.NewLocalRenderer(
render.WithName(string(Prometheus)),
render.WithNamespace(newOpts.Namespace),
render.WithFS(os.DirFS(newOpts.ChartPath)),
render.WithFS(filesystem.NewSubFS(newOpts.ChartPath, identifier.UnionFS)),
render.WithDir("prometheus"),
)
if err != nil {
Expand Down Expand Up @@ -439,7 +439,7 @@ func NewSkywalkingComponent(spec *v1alpha1.SkywalkingSpec, opts ...ComponentOpti
renderer, err = render.NewLocalRenderer(
render.WithName(string(Skywalking)),
render.WithNamespace(newOpts.Namespace),
render.WithFS(os.DirFS(newOpts.ChartPath)),
render.WithFS(filesystem.NewSubFS(newOpts.ChartPath, identifier.UnionFS)),
render.WithDir("skywalking"),
)
if err != nil {
Expand Down Expand Up @@ -504,7 +504,7 @@ func NewZipkinComponent(spec *v1alpha1.ZipkinSpec, opts ...ComponentOption) (Com
renderer, err = render.NewLocalRenderer(
render.WithName(string(Zipkin)),
render.WithNamespace(newOpts.Namespace),
render.WithFS(os.DirFS(newOpts.ChartPath)),
render.WithFS(filesystem.NewSubFS(newOpts.ChartPath, identifier.UnionFS)),
render.WithDir("zipkin"),
)
if err != nil {
Expand Down Expand Up @@ -583,7 +583,7 @@ func addDashboards(base string, namespace string) (string, error) {
configMap.Data = make(map[string]string)
dashboardName := "external-dashboard.json"
dashboardPath := path.Join(identifier.AddonDashboards, dashboardName)
content, err := os.ReadFile(dashboardPath)
content, err := identifier.UnionFS.ReadFile(dashboardPath)
if err != nil {
return "", err
}
Expand Down
6 changes: 3 additions & 3 deletions app/dubboctl/internal/manifest/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package manifest

import (
"fmt"
"os"
"path"
"strings"

"github.com/apache/dubbo-kubernetes/app/dubboctl/identifier"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/apis/dubbo.apache.org/v1alpha1"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/util"

Expand Down Expand Up @@ -68,7 +68,7 @@ func ReadYamlAndProfile(filenames []string, setFlags []string) (string, string,
func ReadAndOverlayYamls(filenames []string) (string, error) {
var output string
for _, filename := range filenames {
file, err := os.ReadFile(strings.TrimSpace(filename))
file, err := identifier.UnionFS.ReadFile(strings.TrimSpace(filename))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func OverlaySetFlags(base string, setFlags []string) (string, error) {
// It may add some filters in the future.
func ReadProfilesNames(profilesPath string) ([]string, error) {
var res []string
dir, err := os.ReadDir(profilesPath)
dir, err := identifier.UnionFS.ReadDir(profilesPath)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading