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]Implement the functionality to generate k8s configuration files and Dockerfile #228

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ output/*
/platform/manifest/config/config-dev.yaml
/platform/manifest/config/config-pro.yaml
/platform/dist/


31 changes: 31 additions & 0 deletions cmd/static/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
"github.com/cloudwego/cwgo/pkg/client"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/cloudwego/cwgo/pkg/curd/doc"
"github.com/cloudwego/cwgo/pkg/docker"
"github.com/cloudwego/cwgo/pkg/fallback"
"github.com/cloudwego/cwgo/pkg/job"
"github.com/cloudwego/cwgo/pkg/kube"
"github.com/cloudwego/cwgo/pkg/model"
"github.com/cloudwego/cwgo/pkg/server"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -118,9 +120,32 @@ func Init() *cli.App {
return api_list.Api(globalArgs.ApiArgument)
},
},
{
Name: DockerName,
Usage: DockerUsage,
Flags: dockerFlags(),
Action: func(c *cli.Context) error {
if err := globalArgs.DockerArgument.ParseCli(c); err != nil {
return err
}
return docker.Docker(globalArgs.DockerArgument)
},
},
{
Name: KubeName,
Usage: KubeUsage,
Flags: kubeFlags(),
Action: func(c *cli.Context) error {
if err := globalArgs.DockerArgument.ParseCli(c); err != nil {
return err
}
return kube.Kube(globalArgs.KubeArgument)
},
},
{
Name: FallbackName,
Usage: FallbackUsage,

Action: func(c *cli.Context) error {
if err := globalArgs.FallbackArgument.ParseCli(c); err != nil {
return err
Expand Down Expand Up @@ -229,4 +254,10 @@ Examples:

CompletionPowershellName = "powershell"
CompletionPowershellUsage = "Generate the autocompletion script for powershell"

DockerName = "docker"
DockerUsage = "Generate Dockerfile"

KubeName = "kube"
KubeUsage = "Generate kubernetes files"
)
89 changes: 89 additions & 0 deletions cmd/static/docker_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package static

import (
"github.com/cloudwego/cwgo/config"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

func dockerFlags() []cli.Flag {
globalArgs := config.GetGlobalArgs()
return []cli.Flag{
// Required Flags with Default Values
&cli.StringFlag{
Name: consts.Template,
Usage: "The home path of the template",
Value: consts.Docker, // 使用默认值 consts.Docker
Destination: &globalArgs.DockerArgument.Template,
},
&cli.StringFlag{
Name: consts.Exe,
Usage: "The executable name in the built image",
Destination: &globalArgs.DockerArgument.ExeName,
},
&cli.StringFlag{
Name: consts.Branch,
Value: consts.MainBranch, // 默认值使用 consts.MainBranch
Usage: "The branch of the remote repo",
Destination: &globalArgs.DockerArgument.Branch,
},
&cli.StringFlag{
Name: consts.Base,
Value: consts.Scratch, // 默认值 consts.Scratch
Usage: "The base image to build the docker image, default scratch",
Destination: &globalArgs.DockerArgument.BaseImage,
},
&cli.StringFlag{
Name: consts.Go,
Usage: "The file that contains main function",
Value: consts.Main, // 默认值 consts.Main
Destination: &globalArgs.DockerArgument.Main,
},
&cli.UintFlag{
Name: consts.Port,
Usage: "The port to expose, default 0 will not expose any port",
Destination: &globalArgs.DockerArgument.Port,
},
&cli.StringFlag{
Name: consts.TZ,
Value: consts.AsizShangHai, // 默认值 consts.AsizShangHai
Usage: "The timezone of the container",
Destination: &globalArgs.DockerArgument.TZ,
},
&cli.StringFlag{
Name: consts.Version,
Value: consts.Alpine, // 默认值 consts.Alpine
Usage: "The builder golang image version",
Destination: &globalArgs.DockerArgument.Version,
},
&cli.StringSliceFlag{
Name: consts.Mirror,
Usage: "The mirror site to use in go update",
},
&cli.StringSliceFlag{
Name: consts.Arguments,
Usage: "Arguments will used in go run",
},
&cli.StringSliceFlag{
Name: consts.Etc,
Usage: "The etc file dirs path of the project",
Value: cli.NewStringSlice(consts.Etc), // 默认值 consts.Etc
},
}
}
132 changes: 132 additions & 0 deletions cmd/static/kube_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package static

import (
"github.com/cloudwego/cwgo/config"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

func kubeFlags() []cli.Flag {
globalArgs := config.GetGlobalArgs()
return []cli.Flag{
// Required Flags
&cli.StringFlag{
Name: consts.Name, // Use consts.Name constant
Required: true,
Usage: "The name of the deployment",
Destination: &globalArgs.KubeArgument.Name,
},
&cli.StringFlag{
Name: consts.Namespace, // Use consts.Namespace constant
Required: true,
Usage: "The namespace of the deployment",
Destination: &globalArgs.KubeArgument.Namespace,
},
&cli.StringFlag{
Name: consts.Output, // Use consts.Output constant
Aliases: []string{"o"}, // Alias "o" for "output"
Required: true,
Usage: "The output yaml file",
Destination: &globalArgs.KubeArgument.Output,
},
&cli.IntFlag{
Name: consts.Port, // Use consts.Port constant
Required: true,
Usage: "The port of the deployment to listen on pod",
Destination: &globalArgs.KubeArgument.Port,
},

// Optional Flags with Default Values
&cli.StringFlag{
Name: consts.Branch, // Use consts.Branch constant
Usage: "The branch of the remote repo, works with --remote",
Destination: &globalArgs.KubeArgument.Branch,
},
&cli.StringFlag{
Name: consts.Template, // Use consts.Template constant
Value: consts.Kube, // Use consts.Kube constant
Usage: "The home path of the template",
Destination: &globalArgs.KubeArgument.Template,
},
&cli.StringFlag{
Name: consts.Image, // Use consts.Image constant
Required: true,
Usage: "The docker image for the deployment (required)",
Destination: &globalArgs.KubeArgument.Image,
},
&cli.StringFlag{
Name: consts.ImagePullPolicy, // Use consts.ImagePullPolicy constant
Usage: "Image pull policy. One of Always, Never, IfNotPresent",
Destination: &globalArgs.KubeArgument.ImagePullPolicy,
},
&cli.IntFlag{
Name: consts.LimitCpu, // Use consts.LimitCpu constant
Usage: "The CPU limit for deployment (default 1000)",
Value: consts.DefaultLimitCpu, // Use consts.DefaultLimitCpu constant
Destination: &globalArgs.KubeArgument.LimitCpu,
},
&cli.IntFlag{
Name: consts.LimitMem, // Use consts.LimitMem constant
Usage: "The memory limit for deployment (default 1024)",
Value: consts.DefaultLimitMem, // Use consts.DefaultLimitCpu constant
Destination: &globalArgs.KubeArgument.LimitMem,
},
&cli.IntFlag{
Name: consts.MaxReplicas, // Use consts.MaxReplicas constant
Usage: "The maximum number of replicas for deployment (default 10)",
Value: consts.DefaultMaxReplicas, // Use consts.DefaultLimitCpu constant
Destination: &globalArgs.KubeArgument.MaxReplicas,
},
&cli.IntFlag{
Name: consts.MinReplicas, // Use consts.MinReplicas constant
Usage: "The minimum number of replicas for deployment (default 3)",
Value: consts.DefaultMinReplicas, // Use consts.DefaultLimitCpu constant
Destination: &globalArgs.KubeArgument.MinReplicas,
},

&cli.IntFlag{
Name: consts.Replicas, // Use consts.Replicas constant
Usage: "The number of replicas for deployment (default 3)",
Value: consts.DefaultReplicas, // Use consts.DefaultLimitCpu constant
Destination: &globalArgs.KubeArgument.Replicas,
},
&cli.IntFlag{
Name: consts.RequestCpu, // Use consts.RequestCpu constant
Usage: "The requested CPU for deployment (default 500)",
Value: consts.DefaultRequestCpu, // Use consts.DefaultLimitCpu constant
Destination: &globalArgs.KubeArgument.RequestCpu,
},
&cli.IntFlag{
Name: consts.RequestMem, // Use consts.RequestMem constant
Usage: "The requested memory for deployment (default 512)",
Value: consts.DefaultRequestMem, // Use consts.DefaultLimitCpu constant
Destination: &globalArgs.KubeArgument.RequestMem,
},
&cli.IntFlag{
Name: consts.Revisions, // Use consts.Revisions constant
Usage: "The number of revision histories to limit (default 5)",
Value: consts.DefaultRevisions, // Use consts.DefaultLimitCpu constant
Destination: &globalArgs.KubeArgument.Revisions,
},
&cli.StringFlag{
Name: consts.Secret, // Use consts.Secret constant
Usage: "The secret to pull the image from the registry",
Destination: &globalArgs.KubeArgument.Secret,
},
&cli.StringFlag{
Name: consts.ServiceAccount, // Use consts.ServiceAccount constant
Usage: "The ServiceAccount for the deployment",
Destination: &globalArgs.KubeArgument.ServiceAccount,
},
&cli.IntFlag{
Name: consts.NodePort, // Use consts.NodePort constant
Usage: "The nodePort for the deployment to expose",
Destination: &globalArgs.KubeArgument.NodePort,
},
&cli.IntFlag{
Name: consts.TargetPort, // Use consts.TargetPort constant
Usage: "The targetPort for the deployment, default to port",
Destination: &globalArgs.KubeArgument.TargetPort,
},
}
}
4 changes: 4 additions & 0 deletions config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Argument struct {
*JobArgument
*ApiArgument
*FallbackArgument
*DockerArgument
*KubeArgument
}

func NewArgument() *Argument {
Expand All @@ -50,8 +52,10 @@ func NewArgument() *Argument {
ModelArgument: NewModelArgument(),
DocArgument: NewDocArgument(),
JobArgument: NewJobArgument(),
DockerArgument: NewDockerArgument(),
ApiArgument: NewApiArgument(),
FallbackArgument: NewFallbackArgument(),
KubeArgument: NewKubeArgument(),
}
}

Expand Down
39 changes: 39 additions & 0 deletions config/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package config

import (
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

type DockerArgument struct {
BaseImage string
Branch string
ExeName string
Main string
Template string
Port uint
TZ string
Version string
Mirrors []string
Arguments []string
EtcDirs []string
}

func NewDockerArgument() *DockerArgument {
return &DockerArgument{}
}

func (s *DockerArgument) ParseCli(ctx *cli.Context) error {
s.Template = ctx.String(consts.Template)
s.BaseImage = ctx.String(consts.Base)
s.ExeName = ctx.String(consts.Exe)
s.Branch = ctx.String(consts.Branch)
s.Main = ctx.String(consts.Go)
s.Port = ctx.Uint(consts.Port)
s.TZ = ctx.String(consts.TZ)
s.Version = ctx.String(consts.Version)
s.Mirrors = ctx.StringSlice(consts.Mirror)
s.Arguments = ctx.StringSlice(consts.Arguments)
s.EtcDirs = ctx.StringSlice(consts.Etc)
return nil
}
Loading