Skip to content

Commit

Permalink
install
Browse files Browse the repository at this point in the history
  • Loading branch information
sjmshsh committed Sep 15, 2023
1 parent 97b1444 commit 29aa5e9
Show file tree
Hide file tree
Showing 25 changed files with 332 additions and 217 deletions.
12 changes: 9 additions & 3 deletions app/dubboctl/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"fmt"
"os"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -78,7 +79,7 @@ func runBuildCmd(cmd *cobra.Command, newClient ClientFactory) error {
}
cfg.Configure(f)

clientOptions, err := cfg.clientOptions()
clientOptions, err := cfg.buildclientOptions()
if err != nil {
return err
}
Expand Down Expand Up @@ -131,14 +132,15 @@ func (c *buildConfig) Prompt(d *dubbo.Dubbo) (*buildConfig, error) {
return c, err
}

func (c buildConfig) clientOptions() ([]dubbo.Option, error) {
func (c buildConfig) buildclientOptions() ([]dubbo.Option, error) {
var o []dubbo.Option

if c.UseDockerfile {
o = append(o, dubbo.WithBuilder(dockerfile.NewBuilder()))
} else {
o = append(o, dubbo.WithBuilder(pack.NewBuilder()))
}

return o, nil
}

Expand Down Expand Up @@ -178,7 +180,11 @@ func newBuildConfig(cmd *cobra.Command) *buildConfig {

func (c *buildConfig) Configure(f *dubbo.Dubbo) {
if c.Path == "" {
f.Root = "."
root, err := os.Getwd()
if err != nil {
return
}
f.Root = root
} else {
f.Root = c.Path
}
Expand Down
45 changes: 22 additions & 23 deletions app/dubboctl/cmd/dashboard_all_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

"github.com/apache/dubbo-kubernetes/app/dubboctl/identifier"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/kube"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/operator"
"github.com/apache/dubbo-kubernetes/pkg/core/logger"
"github.com/spf13/cobra"
"go.uber.org/zap/zapcore"
Expand All @@ -41,22 +40,22 @@ import (
var (
// TODO: think about a efficient way to change selectors and ports when yaml files change
// ports are coming from /deploy/charts and /deploy/kubernetes
ComponentPortMap = map[operator.ComponentName]int{
operator.Admin: 8080,
operator.Grafana: 3000,
operator.Nacos: 8848,
operator.Prometheus: 9090,
operator.Skywalking: 8080,
operator.Zipkin: 9411,
ComponentPortMap = map[kube.ComponentName]int{
kube.Admin: 8080,
kube.Grafana: 3000,
kube.Nacos: 8848,
kube.Prometheus: 9090,
kube.Skywalking: 8080,
kube.Zipkin: 9411,
}
// ComponentSelectorMap selectors are coming from /deploy/charts and /deploy/kubernetes
ComponentSelectorMap = map[operator.ComponentName]string{
operator.Admin: "app.kubernetes.io/name=dubbo-admin",
operator.Grafana: "app.kubernetes.io/name=grafana",
operator.Nacos: "app.kubernetes.io/name=nacos",
operator.Prometheus: "app=prometheus",
operator.Skywalking: "app=skywalking, component=ui",
operator.Zipkin: "app.kubernetes.io/name=zipkin",
ComponentSelectorMap = map[kube.ComponentName]string{
kube.Admin: "app.kubernetes.io/name=dubbo-admin",
kube.Grafana: "app.kubernetes.io/name=grafana",
kube.Nacos: "app.kubernetes.io/name=nacos",
kube.Prometheus: "app=prometheus",
kube.Skywalking: "app=skywalking, component=ui",
kube.Zipkin: "app.kubernetes.io/name=zipkin",
}
)

Expand All @@ -82,7 +81,7 @@ func (dca *DashboardCommonArgs) setDefault() {
}
}

func commonDashboardCmd(baseCmd *cobra.Command, compName operator.ComponentName) {
func commonDashboardCmd(baseCmd *cobra.Command, compName kube.ComponentName) {
nameStr := string(compName)
lowerNameStr := strings.ToLower(nameStr)
dcArgs := &DashboardCommonArgs{}
Expand Down Expand Up @@ -125,30 +124,30 @@ func commonDashboardCmd(baseCmd *cobra.Command, compName operator.ComponentName)
}

func ConfigDashboardAdminCmd(baseCmd *cobra.Command) {
commonDashboardCmd(baseCmd, operator.Admin)
commonDashboardCmd(baseCmd, kube.Admin)
}

func ConfigDashboardGrafanaCmd(baseCmd *cobra.Command) {
commonDashboardCmd(baseCmd, operator.Grafana)
commonDashboardCmd(baseCmd, kube.Grafana)
}

func ConfigDashboardNacosCmd(baseCmd *cobra.Command) {
commonDashboardCmd(baseCmd, operator.Nacos)
commonDashboardCmd(baseCmd, kube.Nacos)
}

func ConfigDashboardPrometheusCmd(baseCmd *cobra.Command) {
commonDashboardCmd(baseCmd, operator.Prometheus)
commonDashboardCmd(baseCmd, kube.Prometheus)
}

func ConfigDashboardSkywalkingCmd(baseCmd *cobra.Command) {
commonDashboardCmd(baseCmd, operator.Skywalking)
commonDashboardCmd(baseCmd, kube.Skywalking)
}

func ConfigDashboardZipkinCmd(baseCmd *cobra.Command) {
commonDashboardCmd(baseCmd, operator.Zipkin)
commonDashboardCmd(baseCmd, kube.Zipkin)
}

func portForward(args *DashboardCommonArgs, compName operator.ComponentName, writer io.Writer) error {
func portForward(args *DashboardCommonArgs, compName kube.ComponentName, writer io.Writer) error {
// process args
var podPort int
podPort = ComponentPortMap[compName]
Expand Down
125 changes: 122 additions & 3 deletions app/dubboctl/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@
package cmd

import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/kube"
corev1 "k8s.io/api/core/v1"
errors2 "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

client2 "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/dubbo"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/util"

Expand Down Expand Up @@ -50,7 +58,7 @@ SYNOPSIS
PreRunE: bindEnv("path", "output", "namespace", "image", "envs", "name", "secret", "replicas",
"revisions", "containerPort", "targetPort", "nodePort", "requestCpu", "limitMem",
"minReplicas", "serviceAccount", "serviceAccount", "imagePullPolicy", "maxReplicas", "limitCpu", "requestMem",
"apply", "useDockerfile", "push", "force", "builder-image", "nobuild"),
"apply", "useDockerfile", "force", "builder-image", "nobuild", "context", "kubeConfig", "push"),
RunE: func(cmd *cobra.Command, args []string) error {
return runDeploy(cmd, newClient)
},
Expand Down Expand Up @@ -103,10 +111,14 @@ SYNOPSIS
"Use the dockerfile with the specified path to build")
cmd.Flags().StringP("image", "i", "",
"Container image( [registry]/[namespace]/[name]:[tag] )")
cmd.Flags().BoolP("push", "", false,
cmd.Flags().BoolP("push", "", true,
"Whether to push the image to the registry center by the way")
cmd.Flags().BoolP("force", "f", false,
"Whether to force push")
cmd.Flags().StringP("context", "", "",
"Context in kubeconfig to use")
cmd.Flags().StringP("kubeConfig", "k", "",
"Path to kubeconfig")

addPathFlag(cmd)
baseCmd.AddCommand(cmd)
Expand Down Expand Up @@ -138,12 +150,97 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) error {

cfg.Configure(f)

clientOptions, err := cfg.clientOptions()
clientOptions, err := cfg.deployclientOptions()
if err != nil {
return err
}
client, done := newClient(clientOptions...)
defer done()

key := client2.ObjectKey{
Namespace: metav1.NamespaceSystem,
Name: cfg.Namespace,
}

err = client.KubeCtl.Get(context.Background(), key, &corev1.Namespace{})
if err != nil {
if errors2.IsNotFound(err) {
nsObj := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceSystem,
Name: cfg.Namespace,
},
}
if err := client.KubeCtl.Create(context.Background(), nsObj); err != nil {
return err
}
return nil
} else {
return fmt.Errorf("failed to check if namespace %v exists: %v", cfg.Namespace, err)
}
}

namespaceSelector := client2.MatchingLabels{
"dubbo-deploy": "enabled",
}
nsList := &corev1.NamespaceList{}
if err = client.KubeCtl.List(context.Background(), nsList, namespaceSelector); err != nil {
return err
}
var namespace string
if len(nsList.Items) > 0 {
namespace = nsList.Items[0].Name
}

env := os.Getenv("DUBBO_DEPLOY_NS")
if env != "" {
namespace = env
}

if namespace != "" {
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 {
return err
}
var name string
var dns string
if len(zkList.Items) > 0 {
name = zkList.Items[0].Name
dns = fmt.Sprintf("%s.%s.svc", name, namespace)
f.Deploy.ZookeeperAddress = dns
}

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 {
return err
}
if len(nacosList.Items) > 0 {
name = nacosList.Items[0].Name
dns = fmt.Sprintf("%s.%s.svc", name, namespace)
f.Deploy.NacosAddress = dns
}

promSelector := client2.MatchingLabels{
"dubbo.apache.org/prometheus": "true",
}

promList := &corev1.ServiceList{}
if err := client.KubeCtl.List(context.Background(), promList, promSelector, client2.InNamespace(namespace)); err != nil {
return err
}
if len(promList.Items) > 0 {
f.Deploy.UseProm = true
}
}

if !cfg.NoBuild {
if f.Built() && !cfg.Force {
fmt.Fprintf(cmd.OutOrStdout(), "The Application is up to date, If you still want to build, use `--force true`\n")
Expand Down Expand Up @@ -177,6 +274,24 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) error {
return f.Stamp()
}

func (d DeployConfig) deployclientOptions() ([]dubbo.Option, error) {
o, err := d.buildclientOptions()
if err != nil {
return o, err
}
var cliOpts []kube.CtlClientOption
cliOpts = []kube.CtlClientOption{
kube.WithKubeConfigPath(d.KubeConfig),
kube.WithContext(d.Context),
}
cli, err := kube.NewCtlClient(cliOpts...)
if err != nil {
return o, err
}
o = append(o, dubbo.WithKubeClient(cli))
return o, nil
}

func applyTok8s(cmd *cobra.Command, d *dubbo.Dubbo) error {
file := filepath.Join(d.Root, d.Deploy.Output)
c := exec.CommandContext(cmd.Context(), "kubectl", "apply", "-f", file)
Expand Down Expand Up @@ -276,6 +391,8 @@ func (c DeployConfig) Configure(f *dubbo.Dubbo) {

type DeployConfig struct {
*buildConfig
KubeConfig string
Context string
NoBuild bool
Apply bool
Namespace string
Expand All @@ -300,6 +417,8 @@ type DeployConfig struct {
func newDeployConfig(cmd *cobra.Command) (c *DeployConfig) {
c = &DeployConfig{
buildConfig: newBuildConfig(cmd),
KubeConfig: viper.GetString("kubeConfig"),
Context: viper.GetString("context"),
NoBuild: viper.GetBool("nobuild"),
Apply: viper.GetBool("apply"),
Output: viper.GetString("output"),
Expand Down
11 changes: 6 additions & 5 deletions app/dubboctl/cmd/manifest_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"sort"
"strings"

"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/kube"

"github.com/apache/dubbo-kubernetes/pkg/core/logger"

"go.uber.org/zap/zapcore"
Expand All @@ -31,7 +33,6 @@ import (
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/apis/dubbo.apache.org/v1alpha1"
"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/operator"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/util"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -165,7 +166,7 @@ func generateValues(mgArgs *ManifestGenerateArgs) (*v1alpha1.DubboConfig, string
}

func generateManifests(mgArgs *ManifestGenerateArgs, cfg *v1alpha1.DubboConfig) error {
op, err := operator.NewDubboOperator(cfg.Spec, nil)
op, err := kube.NewDubboOperator(cfg.Spec, nil)
if err != nil {
return err
}
Expand All @@ -191,15 +192,15 @@ func generateManifests(mgArgs *ManifestGenerateArgs, cfg *v1alpha1.DubboConfig)
return nil
}

func sortManifests(manifestMap map[operator.ComponentName]string) (string, error) {
func sortManifests(manifestMap map[kube.ComponentName]string) (string, error) {
var names []string
var resBuilder strings.Builder
for name := range manifestMap {
names = append(names, string(name))
}
sort.Strings(names)
for _, name := range names {
file := manifestMap[operator.ComponentName(name)]
file := manifestMap[kube.ComponentName(name)]
if !strings.HasSuffix(file, render.YAMLSeparator) {
resBuilder.WriteString(file + render.YAMLSeparator)
} else {
Expand All @@ -209,7 +210,7 @@ func sortManifests(manifestMap map[operator.ComponentName]string) (string, error
return resBuilder.String(), nil
}

func writeManifests(manifestMap map[operator.ComponentName]string, outputPath string) error {
func writeManifests(manifestMap map[kube.ComponentName]string, outputPath string) error {
if err := os.MkdirAll(outputPath, os.ModePerm); err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions app/dubboctl/cmd/manifest_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package cmd
import (
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/apis/dubbo.apache.org/v1alpha1"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/kube"
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/operator"
"github.com/apache/dubbo-kubernetes/pkg/core/logger"
"github.com/spf13/cobra"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -80,7 +79,7 @@ func installManifests(miArgs *ManifestInstallArgs, cfg *v1alpha1.DubboConfig) er
if err != nil {
return err
}
op, err := operator.NewDubboOperator(cfg.Spec, cli)
op, err := kube.NewDubboOperator(cfg.Spec, cli)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 29aa5e9

Please sign in to comment.