Merge pull request #72 from chengleqi/issue-1106

feat: Support set image operation for sidecarsets
This commit is contained in:
Jeremy 2023-03-03 15:39:16 +08:00 committed by GitHub
commit debf778f75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 3 deletions

View File

@ -19,6 +19,7 @@ package set
import (
"fmt"
kruiseappsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
"github.com/openkruise/kruise-tools/pkg/internal/polymorphichelpers"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
@ -240,9 +241,15 @@ func (o *SetImageOptions) Run() error {
}
continue
}
initContainerFound := setImage(spec.InitContainers, name, resolvedImageName)
containerFound := setImage(spec.Containers, name, resolvedImageName)
var initContainerFound, containerFound bool
// Check if the type is kruiseappsv1alpha1.SidecarSet, and if the placeholder is nil.
if t, ok := obj.(*kruiseappsv1alpha1.SidecarSet); ok && spec == nil {
initContainerFound = setSideCarImage(t.Spec.InitContainers, name, resolvedImageName)
containerFound = setSideCarImage(t.Spec.Containers, name, resolvedImageName)
} else {
initContainerFound = setImage(spec.InitContainers, name, resolvedImageName)
containerFound = setImage(spec.Containers, name, resolvedImageName)
}
if !containerFound && !initContainerFound {
allErrs = append(allErrs, fmt.Errorf("error: unable to find container named %q", name))
}
@ -316,6 +323,19 @@ func setImage(containers []corev1.Container, containerName string, image string)
return containerFound
}
// setSideCarImage
func setSideCarImage(containers []kruiseappsv1alpha1.SidecarContainer, containerName string, image string) bool {
containerFound := false
// Find the container to update, and update its image
for i, c := range containers {
if c.Name == containerName || containerName == "*" {
containerFound = true
containers[i].Image = image
}
}
return containerFound
}
// getResourcesAndImages retrieves resources and container name:images pair from given args
func getResourcesAndImages(args []string) (resources []string, containerImages map[string]string, err error) {
pairType := "image"

View File

@ -23,6 +23,8 @@ import (
"strings"
"testing"
kruiseappsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
@ -612,6 +614,22 @@ func TestSetImageRemote(t *testing.T) {
path: "/namespaces/test/replicationcontrollers/nginx",
args: []string{"replicationcontroller", "nginx", "*=thingy"},
},
{
name: "set image kruiseappsv1alpha1.SidecarSet",
object: &kruiseappsv1alpha1.SidecarSet{
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
Spec: kruiseappsv1alpha1.SidecarSetSpec{
Containers: []kruiseappsv1alpha1.SidecarContainer{
{
Container: corev1.Container{Name: "nginx", Image: "nginx"},
},
},
},
},
groupVersion: corev1.SchemeGroupVersion,
path: "/namespaces/test/sidecarsets/nginx",
args: []string{"sidecarsets", "nginx", "*=thingy"},
},
}
for _, input := range inputs {
t.Run(input.name, func(t *testing.T) {

View File

@ -40,6 +40,12 @@ func updatePodSpecForObject(obj runtime.Object, fn func(*v1.PodSpec) error) (boo
return true, fn(&t.Spec.Template.Spec)
case *kruiseappsv1alpha1.DaemonSet:
return true, fn(&t.Spec.Template.Spec)
case *kruiseappsv1alpha1.SidecarSet:
// Because the fn function requires passing v1.PodSpec as a parameter,
// but kruiseappsv1alpha1.SidecarSet only has kruiseappsv1alpha1.SidecarContainer,
// and their types do not match.
// Therefore, passing nil as a placeholder here.
return true, fn(nil)
case *v1.Pod:
return true, fn(&t.Spec)
// ReplicationController