Skip to content

Commit

Permalink
Merge pull request #1180 from liangyuanpeng/args_unique
Browse files Browse the repository at this point in the history
Add field of override for component extraArgs
  • Loading branch information
k8s-ci-robot authored Aug 16, 2024
2 parents b34ff35 + 43df963 commit e4552e6
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 12 deletions.
2 changes: 2 additions & 0 deletions pkg/apis/config/v1alpha1/kwokctl_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type ExtraArgs struct {
Key string `json:"key"`
// Value is the value of the extra args.
Value string `json:"value"`
// Override is the value of is it override the arg
Override bool `json:"override"`
}

// ComponentPatches holds information about the component patches.
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/internalversion/kwokctl_configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type ExtraArgs struct {
Key string
// Value is the value of the extra args.
Value string
// Override is the value of is it override the arg
Override bool
}

// ComponentPatches holds information about the component patches.
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/internalversion/zz_generated.conversion.go

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

2 changes: 1 addition & 1 deletion pkg/kwokctl/runtime/binary/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ func (c *Cluster) finishInstall(ctx context.Context, env *env) error {
conf := &env.kwokctlConfig.Options

for i := range env.kwokctlConfig.Components {
runtime.ApplyComponentPatches(&env.kwokctlConfig.Components[i], env.kwokctlConfig.ComponentsPatches)
runtime.ApplyComponentPatches(ctx, &env.kwokctlConfig.Components[i], env.kwokctlConfig.ComponentsPatches)
}

// Setup kubeconfig
Expand Down
2 changes: 1 addition & 1 deletion pkg/kwokctl/runtime/compose/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ func (c *Cluster) finishInstall(ctx context.Context, env *env) error {
conf := &env.kwokctlConfig.Options

for i := range env.kwokctlConfig.Components {
runtime.ApplyComponentPatches(&env.kwokctlConfig.Components[i], env.kwokctlConfig.ComponentsPatches)
runtime.ApplyComponentPatches(ctx, &env.kwokctlConfig.Components[i], env.kwokctlConfig.ComponentsPatches)
}

// Setup kubeconfig
Expand Down
10 changes: 5 additions & 5 deletions pkg/kwokctl/runtime/kind/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ func (c *Cluster) addKubectlProxy(ctx context.Context, env *env) (err error) {
return err
}

runtime.ApplyComponentPatches(&kubectlProxyComponent, env.kwokctlConfig.ComponentsPatches)
runtime.ApplyComponentPatches(ctx, &kubectlProxyComponent, env.kwokctlConfig.ComponentsPatches)

dashboardPod, err := yaml.Marshal(components.ConvertToPod(kubectlProxyComponent))
if err != nil {
Expand Down Expand Up @@ -654,7 +654,7 @@ func (c *Cluster) addKwokController(ctx context.Context, env *env) (err error) {
})
kwokControllerComponent.Volumes = append(kwokControllerComponent.Volumes, logVolumes...)

runtime.ApplyComponentPatches(&kwokControllerComponent, env.kwokctlConfig.ComponentsPatches)
runtime.ApplyComponentPatches(ctx, &kwokControllerComponent, env.kwokctlConfig.ComponentsPatches)

pod := components.ConvertToPod(kwokControllerComponent)
pod.Spec.Containers[0].Env = append(pod.Spec.Containers[0].Env, corev1.EnvVar{
Expand Down Expand Up @@ -709,7 +709,7 @@ func (c *Cluster) addDashboard(ctx context.Context, env *env) (err error) {
return fmt.Errorf("failed to build dashboard component: %w", err)
}

runtime.ApplyComponentPatches(&dashboardComponent, env.kwokctlConfig.ComponentsPatches)
runtime.ApplyComponentPatches(ctx, &dashboardComponent, env.kwokctlConfig.ComponentsPatches)

dashboardPod, err := yaml.Marshal(components.ConvertToPod(dashboardComponent))
if err != nil {
Expand Down Expand Up @@ -859,7 +859,7 @@ func (c *Cluster) addPrometheus(ctx context.Context, env *env) (err error) {
},
)

runtime.ApplyComponentPatches(&prometheusComponent, env.kwokctlConfig.ComponentsPatches)
runtime.ApplyComponentPatches(ctx, &prometheusComponent, env.kwokctlConfig.ComponentsPatches)

prometheusPod, err := yaml.Marshal(components.ConvertToPod(prometheusComponent))
if err != nil {
Expand Down Expand Up @@ -901,7 +901,7 @@ func (c *Cluster) addJaeger(ctx context.Context, env *env) (err error) {
return err
}

runtime.ApplyComponentPatches(&jaegerComponent, env.kwokctlConfig.ComponentsPatches)
runtime.ApplyComponentPatches(ctx, &jaegerComponent, env.kwokctlConfig.ComponentsPatches)

jaegerPod, err := yaml.Marshal(components.ConvertToPod(jaegerComponent))
if err != nil {
Expand Down
32 changes: 27 additions & 5 deletions pkg/kwokctl/runtime/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import (
"context"
"fmt"
"sort"
"strings"

"golang.org/x/sync/errgroup"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
"sigs.k8s.io/kwok/pkg/config"
"sigs.k8s.io/kwok/pkg/kwokctl/components"
"sigs.k8s.io/kwok/pkg/log"
"sigs.k8s.io/kwok/pkg/utils/maps"
"sigs.k8s.io/kwok/pkg/utils/path"
"sigs.k8s.io/kwok/pkg/utils/slices"
Expand Down Expand Up @@ -103,23 +105,43 @@ func GetComponentPatches(conf *internalversion.KwokctlConfiguration, componentNa
}

// ApplyComponentPatches applies patches to a component.
func ApplyComponentPatches(component *internalversion.Component, patches []internalversion.ComponentPatches) {
func ApplyComponentPatches(ctx context.Context, component *internalversion.Component, patches []internalversion.ComponentPatches) {
for _, patch := range patches {
applyComponentPatch(component, patch)
applyComponentPatch(ctx, component, patch)
}
}

func applyComponentPatch(component *internalversion.Component, patch internalversion.ComponentPatches) {
func applyComponentPatch(ctx context.Context, component *internalversion.Component, patch internalversion.ComponentPatches) {
if patch.Name != component.Name {
return
}

component.Volumes = append(component.Volumes, patch.ExtraVolumes...)
component.Envs = append(component.Envs, patch.ExtraEnvs...)

for _, a := range patch.ExtraArgs {
component.Args = append(component.Args, fmt.Sprintf("--%s=%s", a.Key, a.Value))
if a.Override {
component.Args = applyComponentArgsOverride(ctx, component.Args, a)
} else {
component.Args = append(component.Args, fmt.Sprintf("--%s=%s", a.Key, a.Value))
}
}
}

func applyComponentArgsOverride(ctx context.Context, args []string, a internalversion.ExtraArgs) []string {
k := fmt.Sprintf("--%s=", a.Key)
overrided := false
for i := len(args) - 1; i >= 0; i-- {
if strings.HasPrefix(args[i], k) {
args[i] = fmt.Sprintf("--%s=%s", a.Key, a.Value)
overrided = true
break
}
}
if !overrided {
logger := log.FromContext(ctx)
logger.Warn("have not match override", "key", a.Key)
}
return args
}

// ExpandVolumesHostPaths expands relative paths specified in volumes to absolute paths
Expand Down
134 changes: 134 additions & 0 deletions pkg/kwokctl/runtime/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
Copyright 2024 The Kubernetes 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 runtime

import (
"context"
"reflect"
"testing"

"sigs.k8s.io/kwok/pkg/apis/internalversion"
)

func TestApplyComponentArgsOverride(t *testing.T) {
tests := []struct {
name string
args []string
patch internalversion.ExtraArgs
wantArgs []string
}{
{
name: "Override the value",
args: []string{
"--foo=1",
"--bar=2",
},
patch: internalversion.ExtraArgs{
Key: "foo",
Value: "10",
Override: true,
},
wantArgs: []string{
"--foo=10",
"--bar=2",
},
},
{
name: "Unmatched flag",
args: []string{
"--foo=1",
},
patch: internalversion.ExtraArgs{
Key: "bar",
Value: "2",
Override: true,
},
wantArgs: []string{
"--foo=1",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
applyComponentArgsOverride(context.TODO(), tt.args, internalversion.ExtraArgs{
Key: tt.patch.Key,
Value: tt.patch.Value,
Override: tt.patch.Override,
})

if !reflect.DeepEqual(tt.wantArgs, tt.args) {
t.Errorf("Exist is not expact! key:%s want args:%s, got args:%s", tt.patch.Key, tt.wantArgs, tt.args)
}
})
}
}

func TestApplyComponentPatch(t *testing.T) {
tests := []struct {
name string
component internalversion.Component
patch internalversion.ComponentPatches
wantArgs []string
}{
{
name: "Override the value",
component: internalversion.Component{
Name: "test",
Args: []string{"--etcd-servers=http://localhost:2379", "--etcd-prefix=/registry"},
},
patch: internalversion.ComponentPatches{
Name: "test",
ExtraArgs: []internalversion.ExtraArgs{
{
Key: "etcd-servers",
Value: "http://127.0.0.1:2379",
Override: true,
},
},
},
wantArgs: []string{"--etcd-servers=http://127.0.0.1:2379", "--etcd-prefix=/registry"},
},
{
name: "Do not override the value",
component: internalversion.Component{
Name: "test",
Args: []string{"--etcd-servers=http://localhost:2379", "--etcd-prefix=/registry"},
},
patch: internalversion.ComponentPatches{
Name: "test",
ExtraArgs: []internalversion.ExtraArgs{
{
Key: "etcd-servers",
Value: "http://127.0.0.1:2379",
Override: false,
},
},
},
wantArgs: []string{"--etcd-servers=http://localhost:2379", "--etcd-prefix=/registry", "--etcd-servers=http://127.0.0.1:2379"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
applyComponentPatch(context.TODO(), &tt.component, tt.patch)
if !reflect.DeepEqual(tt.wantArgs, tt.component.Args) {
t.Errorf("Exist is not expact! key:%s want args:%s, got args:%s", tt.patch.ExtraArgs[0].Key, tt.wantArgs, tt.component.Args)
}
})
}
}
11 changes: 11 additions & 0 deletions site/content/en/docs/generated/apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -2264,6 +2264,17 @@ string
<p>Value is the value of the extra args.</p>
</td>
</tr>
<tr>
<td>
<code>override</code>
<em>
bool
</em>
</td>
<td>
<p>Override is the value of is it override the arg</p>
</td>
</tr>
</tbody>
</table>
<h3 id="config.kwok.x-k8s.io/v1alpha1.HostPathType">
Expand Down

0 comments on commit e4552e6

Please sign in to comment.