Skip to content

Commit

Permalink
chore: decouple the shared container from the custom image in the exe…
Browse files Browse the repository at this point in the history
…c Action (#8024)
  • Loading branch information
leon-inf authored Sep 4, 2024
1 parent e3de9b5 commit 53046e3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
26 changes: 8 additions & 18 deletions pkg/controller/component/kbagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,16 @@ func adaptKBAgentIfCustomImageNContainerDefined(synthesizedComp *SynthesizedComp
if err != nil {
return err
}
if len(image) == 0 {
return nil
}

// init-container to copy binaries to the shared mount point /kubeblocks
initContainer := buildKBAgentInitContainer()
synthesizedComp.PodSpec.InitContainers = append(synthesizedComp.PodSpec.InitContainers, *initContainer)
if len(image) > 0 {
// init-container to copy binaries to the shared mount point /kubeblocks
initContainer := buildKBAgentInitContainer()
synthesizedComp.PodSpec.InitContainers = append(synthesizedComp.PodSpec.InitContainers, *initContainer)

container.Image = image
container.Command[0] = kbAgentCommandOnSharedMount
container.VolumeMounts = append(container.VolumeMounts, sharedVolumeMount)
container.Image = image
container.Command[0] = kbAgentCommandOnSharedMount
container.VolumeMounts = append(container.VolumeMounts, sharedVolumeMount)
}

// TODO: share more container resources
if c != nil {
Expand Down Expand Up @@ -343,15 +342,6 @@ func customExecActionImageNContainer(synthesizedComp *SynthesizedComponent) (str
return "", nil, fmt.Errorf("exec container %s not found", container)
}
}
if len(image) > 0 && len(container) > 0 {
if c.Image == image {
return image, c, nil
}
return "", nil, fmt.Errorf("exec image and container must be the same")
}
if len(image) == 0 && len(container) > 0 {
image = c.Image
}
return image, c, nil
}

Expand Down
35 changes: 25 additions & 10 deletions pkg/controller/component/kbagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (
corev1 "k8s.io/api/core/v1"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
kbagent "github.com/apecloud/kubeblocks/pkg/kbagent"
"github.com/apecloud/kubeblocks/pkg/viperx"
)

var _ = Describe("kb-agent", func() {
Expand Down Expand Up @@ -226,14 +228,13 @@ var _ = Describe("kb-agent", func() {
Expect(err).Should(BeNil())

ic := kbAgentInitContainer()
Expect(ic).ShouldNot(BeNil())
Expect(ic).Should(BeNil())

c := kbAgentContainer()
Expect(c).ShouldNot(BeNil())
Expect(c.Image).Should(Equal(container.Image))
Expect(c.Command[0]).Should(Equal(kbAgentCommandOnSharedMount))
Expect(c.VolumeMounts).Should(HaveLen(1))
Expect(c.VolumeMounts[0]).Should(Equal(sharedVolumeMount))
Expect(c.Image).Should(Equal(viperx.GetString(constant.KBToolsImage)))
Expect(c.Command[0]).Should(Equal(kbAgentCommand))
Expect(c.VolumeMounts).Should(HaveLen(0))
})

It("custom container - volume mounts", func() {
Expand All @@ -251,9 +252,8 @@ var _ = Describe("kb-agent", func() {

c := kbAgentContainer()
Expect(c).ShouldNot(BeNil())
Expect(c.VolumeMounts).Should(HaveLen(2))
Expect(c.VolumeMounts[0]).Should(Equal(sharedVolumeMount))
Expect(c.VolumeMounts[1]).Should(Equal(container.VolumeMounts[0]))
Expect(c.VolumeMounts).Should(HaveLen(1))
Expect(c.VolumeMounts[0]).Should(Equal(container.VolumeMounts[0]))
})

It("custom container - two same containers", func() {
Expand Down Expand Up @@ -317,14 +317,29 @@ var _ = Describe("kb-agent", func() {

It("custom image & container - different images", func() {
image := "custom-image"
synthesizedComp.PodSpec.Containers[0].VolumeMounts = []corev1.VolumeMount{
{
Name: "test-volume",
MountPath: "/test",
},
}
container := synthesizedComp.PodSpec.Containers[0]
Expect(image).ShouldNot(Equal(container.Image))
synthesizedComp.LifecycleActions.PostProvision.Exec.Image = image
synthesizedComp.LifecycleActions.PostProvision.Exec.Container = container.Name

err := buildKBAgentContainer(synthesizedComp)
Expect(err).ShouldNot(BeNil())
Expect(err.Error()).Should(ContainSubstring("exec image and container must be the same"))
Expect(err).Should(BeNil())

ic := kbAgentInitContainer()
Expect(ic).ShouldNot(BeNil())

c := kbAgentContainer()
Expect(c.Image).Should(Equal(image))
Expect(c.Command[0]).Should(Equal(kbAgentCommandOnSharedMount))
Expect(c.VolumeMounts).Should(HaveLen(2))
Expect(c.VolumeMounts[0]).Should(Equal(sharedVolumeMount))
Expect(c.VolumeMounts[1]).Should(Equal(container.VolumeMounts[0]))
})

// TODO: host-network
Expand Down

0 comments on commit 53046e3

Please sign in to comment.