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: support plugin options for templete #625

Merged
merged 1 commit into from
Jun 6, 2024
Merged
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
13 changes: 13 additions & 0 deletions deploy/crds/kosmos.io_virtualclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ spec:
description: Kubeconfig is the kubeconfig of the virtual kubernetes's
control plane
type: string
pluginOptions:
description: datasource for plugin yaml
items:
properties:
name:
type: string
value:
type: string
required:
- name
- value
type: object
type: array
promotePolicies:
description: PromotePolicies definites the policies for promote to
the kubernetes's control plane
Expand Down
5 changes: 5 additions & 0 deletions examples/vitual-cluster-demo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ spec:
promoteResources:
nodeInfos:
- nodeName: nodeName1
pluginOptions:
- name: accessKey
value: hello
- name: secretKey
value: world
11 changes: 11 additions & 0 deletions pkg/apis/kosmos/v1alpha1/virtualcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ type VirtualClusterSpec struct {
// the resources can be nodes or just cpu,memory or gpu resources
// +optional
PromoteResources PromoteResources `json:"promoteResources,omitempty"`

// datasource for plugin yaml
// +optional
PluginOptions []PluginOptions `json:"pluginOptions,omitempty"`
}

type PluginOptions struct {
// +required
Name string `json:"name"`
// +required
Value string `json:"value"`
}

type PromotePolicy struct {
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/kosmos/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 43 additions & 1 deletion pkg/generated/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/kubenest/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type initData struct {
hostPort int32
hostPortMap map[string]int32
kubeNestOptions *ko.KubeNestOptions
virtualCluster *v1alpha1.VirtualCluster
}

type InitOptions struct {
Expand Down Expand Up @@ -184,6 +185,7 @@ func newRunData(opt *InitOptions) (*initData, error) {
hostPort: opt.virtualCluster.Status.Port,
hostPortMap: opt.virtualCluster.Status.PortMap,
kubeNestOptions: opt.KubeNestOptions,
virtualCluster: opt.virtualCluster,
}, nil
}

Expand Down Expand Up @@ -258,3 +260,16 @@ func (i initData) DynamicClient() *dynamic.DynamicClient {
func (i initData) KubeNestOpt() *ko.KubeNestOptions {
return i.kubeNestOptions
}

func (i initData) PluginOptions() map[string]string {
if i.virtualCluster.Spec.PluginOptions == nil {
return nil
}

pluginOptoinsMapping := map[string]string{}

for _, option := range i.virtualCluster.Spec.PluginOptions {
pluginOptoinsMapping[option.Name] = option.Value
}
return pluginOptoinsMapping
}
6 changes: 6 additions & 0 deletions pkg/kubenest/tasks/coredns.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func runCoreDnsHostTask(r workflow.RunData) error {
"Name": data.GetName(),
"ImageRepository": imageRepository,
}
for k, v := range data.PluginOptions() {
templatedMapping[k] = v
}
err = applyYMLTemplate(dynamicClient, component.Path, templatedMapping)
if err != nil {
return err
Expand Down Expand Up @@ -243,6 +246,9 @@ func runCoreDnsVirtualTask(r workflow.RunData) error {
"MetricsPort": MetricsPort,
"HostNodeAddress": HostNodeAddress,
}
for k, v := range data.PluginOptions() {
templatedMapping[k] = v
}
err = applyYMLTemplate(dynamicClient, component.Path, templatedMapping)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions pkg/kubenest/tasks/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ type InitData interface {
HostPortMap() map[string]int32
DynamicClient() *dynamic.DynamicClient
KubeNestOpt() *ko.KubeNestOptions
PluginOptions() map[string]string
}
4 changes: 4 additions & 0 deletions pkg/kubenest/tasks/manifests_components.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func applyComponentsManifests(r workflow.RunData) error {
imageRepository, _ := util.GetImageMessage()
templatedMapping["ImageRepository"] = imageRepository

for k, v := range data.PluginOptions() {
templatedMapping[k] = v
}

for _, component := range components {
klog.V(2).Infof("Deploy component %s", component.Name)
err = applyTemplatedManifests(component.Name, dynamicClient, component.Path, templatedMapping)
Expand Down
13 changes: 12 additions & 1 deletion pkg/kubenest/util/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ import (
"text/template"
)

// pluginOptions
func defaultValue(value interface{}, defaultVal string) string {
if str, ok := value.(string); ok && str != "" {
return str
}
return defaultVal
}

// ParseTemplate validates and parses passed as argument template
func ParseTemplate(strtmpl string, obj interface{}) (string, error) {
var buf bytes.Buffer
tmpl, err := template.New("template").Parse(strtmpl)
tmpl := template.New("template").Funcs(template.FuncMap{
"defaultValue": defaultValue,
})
tmpl, err := tmpl.Parse(strtmpl)
if err != nil {
return "", fmt.Errorf("error when parsing template, err: %w", err)
}
Expand Down
Loading