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 ( import (
"fmt" "fmt"
kruiseappsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
"github.com/openkruise/kruise-tools/pkg/internal/polymorphichelpers" "github.com/openkruise/kruise-tools/pkg/internal/polymorphichelpers"
"github.com/spf13/cobra" "github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
@ -240,9 +241,15 @@ func (o *SetImageOptions) Run() error {
} }
continue continue
} }
var initContainerFound, containerFound bool
initContainerFound := setImage(spec.InitContainers, name, resolvedImageName) // Check if the type is kruiseappsv1alpha1.SidecarSet, and if the placeholder is nil.
containerFound := setImage(spec.Containers, name, resolvedImageName) 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 { if !containerFound && !initContainerFound {
allErrs = append(allErrs, fmt.Errorf("error: unable to find container named %q", name)) 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 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 // getResourcesAndImages retrieves resources and container name:images pair from given args
func getResourcesAndImages(args []string) (resources []string, containerImages map[string]string, err error) { func getResourcesAndImages(args []string) (resources []string, containerImages map[string]string, err error) {
pairType := "image" pairType := "image"

View File

@ -23,6 +23,8 @@ import (
"strings" "strings"
"testing" "testing"
kruiseappsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
@ -612,6 +614,22 @@ func TestSetImageRemote(t *testing.T) {
path: "/namespaces/test/replicationcontrollers/nginx", path: "/namespaces/test/replicationcontrollers/nginx",
args: []string{"replicationcontroller", "nginx", "*=thingy"}, 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 { for _, input := range inputs {
t.Run(input.name, func(t *testing.T) { 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) return true, fn(&t.Spec.Template.Spec)
case *kruiseappsv1alpha1.DaemonSet: case *kruiseappsv1alpha1.DaemonSet:
return true, fn(&t.Spec.Template.Spec) 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: case *v1.Pod:
return true, fn(&t.Spec) return true, fn(&t.Spec)
// ReplicationController // ReplicationController