Skip to content

Commit

Permalink
[IMP]增加对猪齿鱼支持
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeBye committed Jun 4, 2024
1 parent e211f2a commit 546148f
Show file tree
Hide file tree
Showing 18 changed files with 607 additions and 161 deletions.
4 changes: 2 additions & 2 deletions cmd/helm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ charts in a repository, use 'helm search'.
`

func newInstallCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client := action.NewInstall(cfg)
client := action.NewInstall(cfg, &action.Install{})
valueOpts := &values.Options{}
var outfmt output.Format

Expand Down Expand Up @@ -311,7 +311,7 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options
cancel()
}()

return client.RunWithContext(ctx, chartRequested, vals)
return client.RunWithContext(ctx, chartRequested, vals, "")
}

// checkIfInstallable validates if a chart can be installed
Expand Down
49 changes: 41 additions & 8 deletions cmd/helm/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (

"helm.sh/helm/v3/cmd/helm/require"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/getter"
)

const showDesc = `
Expand Down Expand Up @@ -56,9 +58,16 @@ This command inspects a chart (directory, file, or URL) and displays the content
of the CustomResourceDefinition files
`

const hookChartDesc = `
This command inspects a chart (directory, file, or URL) and displays the contents
of hooks
`

func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client := action.NewShowWithConfig(action.ShowAll, cfg)

client.Namespace = settings.Namespace()

showCommand := &cobra.Command{
Use: "show",
Short: "show information of a chart",
Expand Down Expand Up @@ -88,7 +97,7 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if err != nil {
return err
}
output, err := runShow(args, client)
output, err := runShow(args, client, nil)
if err != nil {
return err
}
Expand All @@ -109,7 +118,7 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if err != nil {
return err
}
output, err := runShow(args, client)
output, err := runShow(args, client, nil)
if err != nil {
return err
}
Expand All @@ -130,7 +139,7 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if err != nil {
return err
}
output, err := runShow(args, client)
output, err := runShow(args, client, nil)
if err != nil {
return err
}
Expand All @@ -151,7 +160,7 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if err != nil {
return err
}
output, err := runShow(args, client)
output, err := runShow(args, client, nil)
if err != nil {
return err
}
Expand All @@ -172,7 +181,31 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if err != nil {
return err
}
output, err := runShow(args, client)
output, err := runShow(args, client, nil)
if err != nil {
return err
}
fmt.Fprint(out, output)
return nil
},
}

hookSubCmd := &cobra.Command{
Use: "hooks [CHART]",
Short: "shows the chart's hook",
Long: hookChartDesc,
Args: require.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

valueOpts := &values.Options{}
p := getter.All(settings)
vals, err := valueOpts.MergeValues(p)
if err != nil {
return err
}

client.OutputFormat = action.ShowHook
output, err := runShow(args, client, vals)
if err != nil {
return err
}
Expand All @@ -181,7 +214,7 @@ func newShowCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
},
}

cmds := []*cobra.Command{all, readmeSubCmd, valuesSubCmd, chartSubCmd, crdsSubCmd}
cmds := []*cobra.Command{all, readmeSubCmd, valuesSubCmd, chartSubCmd, hookSubCmd, crdsSubCmd}
for _, subCmd := range cmds {
addShowFlags(subCmd, client)
showCommand.AddCommand(subCmd)
Expand Down Expand Up @@ -211,7 +244,7 @@ func addShowFlags(subCmd *cobra.Command, client *action.Show) {
}
}

func runShow(args []string, client *action.Show) (string, error) {
func runShow(args []string, client *action.Show, vals map[string]interface{}) (string, error) {
debug("Original chart version: %q", client.Version)
if client.Version == "" && client.Devel {
debug("setting version to >0.0.0-0")
Expand All @@ -222,7 +255,7 @@ func runShow(args []string, client *action.Show) (string, error) {
if err != nil {
return "", err
}
return client.Run(cp)
return client.Run(cp, vals)
}

func addRegistryClient(client *action.Show) error {
Expand Down
2 changes: 1 addition & 1 deletion cmd/helm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
var validate bool
var includeCrds bool
var skipTests bool
client := action.NewInstall(cfg)
client := action.NewInstall(cfg, &action.Install{})
valueOpts := &values.Options{}
var kubeVersion string
var extraAPIs []string
Expand Down
6 changes: 3 additions & 3 deletions cmd/helm/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ which can contain sensitive values. To hide Kubernetes Secrets use the
`

func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
client := action.NewUpgrade(cfg)
client := action.NewUpgrade(cfg, &action.Upgrade{})
valueOpts := &values.Options{}
var outfmt output.Format
var createNamespace bool
Expand Down Expand Up @@ -127,7 +127,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
if outfmt == output.Table {
fmt.Fprintf(out, "Release %q does not exist. Installing it now.\n", args[0])
}
instClient := action.NewInstall(cfg)
instClient := action.NewInstall(cfg, &action.Install{})
instClient.CreateNamespace = createNamespace
instClient.ChartPathOptions = client.ChartPathOptions
instClient.Force = client.Force
Expand Down Expand Up @@ -235,7 +235,7 @@ func newUpgradeCmd(cfg *action.Configuration, out io.Writer) *cobra.Command {
cancel()
}()

rel, err := client.RunWithContext(ctx, args[0], ch, vals)
rel, err := client.RunWithContext(ctx, args[0], ch, vals, "")
if err != nil {
return errors.Wrap(err, "UPGRADE FAILED")
}
Expand Down
25 changes: 25 additions & 0 deletions pkg/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"

"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/discovery"
Expand Down Expand Up @@ -88,6 +89,8 @@ type Configuration struct {
// KubeClient is a Kubernetes API client.
KubeClient kube.Interface

ClientSet *kubernetes.Clientset

// RegistryClient is a client for working with registries
RegistryClient *registry.Client

Expand All @@ -97,6 +100,22 @@ type Configuration struct {
Log func(string, ...interface{})
}

type C7NOptions struct {
AgentVersion string
AppServiceId int64
ChartName string
ChartVersion string
Command int64
Commit string
ImagePullSecret []v1.LocalObjectReference
IsTest bool
ReleaseName string
ReplicasStrategy string
TestLabel string
V1AppServiceId string
V1Command string
}

// renderResources renders the templates in a chart
//
// TODO: This function is badly in need of a refactor.
Expand Down Expand Up @@ -379,6 +398,11 @@ func (cfg *Configuration) Init(getter genericclioptions.RESTClientGetter, namesp
clientFn: kc.Factory.KubernetesClientSet,
}

clientset, err := kc.Factory.KubernetesClientSet()
if err != nil {
return err
}

var store *storage.Storage
switch helmDriver {
case "secret", "secrets", "":
Expand Down Expand Up @@ -423,6 +447,7 @@ func (cfg *Configuration) Init(getter genericclioptions.RESTClientGetter, namesp
cfg.KubeClient = kc
cfg.Releases = store
cfg.Log = log
cfg.ClientSet = clientset

return nil
}
15 changes: 13 additions & 2 deletions pkg/action/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ import (
"time"

"github.com/pkg/errors"

"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/release"
helmtime "helm.sh/helm/v3/pkg/time"
)

// execHook executes all of the hooks for the given hook event.
func (cfg *Configuration) execHook(rl *release.Release, hook release.HookEvent, timeout time.Duration) error {
func (cfg *Configuration) execHook(rl *release.Release, hook release.HookEvent, timeout time.Duration, c7nOptions *Install) error {
executingHooks := []*release.Hook{}

for _, h := range rl.Hooks {
Expand Down Expand Up @@ -57,6 +56,18 @@ func (cfg *Configuration) execHook(rl *release.Release, hook release.HookEvent,
}

resources, err := cfg.KubeClient.Build(bytes.NewBufferString(h.Manifest), true)

// 如果是agent升级,则跳过添加标签这一步,因为agent原本是直接在集群中安装的没有对应标签,如果在这里加标签k8s会报错
if c7nOptions.C7NOptions.ChartName != "choerodon-cluster-agent" {
// 在这里对要新chart包中的对象添加标签
for _, r := range resources {
err = AddLabel(r, nil, c7nOptions)
if err != nil {
return err
}
}
}

if err != nil {
return errors.Wrapf(err, "unable to build kubernetes object for %s hook %s", hook, h.Path)
}
Expand Down
Loading

0 comments on commit 546148f

Please sign in to comment.