1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 13:29:41 +02:00

fix(kubectl): rollout restart [r8s-322] (#729)

This commit is contained in:
Steven Kang 2025-05-13 11:35:44 +12:00 committed by GitHub
parent 1abdf42f99
commit 3b313b9308
8 changed files with 85 additions and 68 deletions

View file

@ -76,16 +76,16 @@ func (deployer *KubernetesDeployer) getToken(userID portainer.UserID, endpoint *
} }
// Deploy upserts Kubernetes resources defined in manifest(s) // Deploy upserts Kubernetes resources defined in manifest(s)
func (deployer *KubernetesDeployer) Deploy(userID portainer.UserID, endpoint *portainer.Endpoint, manifestFiles []string, namespace string) (string, error) { func (deployer *KubernetesDeployer) Deploy(userID portainer.UserID, endpoint *portainer.Endpoint, resources []string, namespace string) (string, error) {
return deployer.command("apply", userID, endpoint, manifestFiles, namespace) return deployer.command("apply", userID, endpoint, resources, namespace)
} }
// Remove deletes Kubernetes resources defined in manifest(s) // Remove deletes Kubernetes resources defined in manifest(s)
func (deployer *KubernetesDeployer) Remove(userID portainer.UserID, endpoint *portainer.Endpoint, manifestFiles []string, namespace string) (string, error) { func (deployer *KubernetesDeployer) Remove(userID portainer.UserID, endpoint *portainer.Endpoint, resources []string, namespace string) (string, error) {
return deployer.command("delete", userID, endpoint, manifestFiles, namespace) return deployer.command("delete", userID, endpoint, resources, namespace)
} }
func (deployer *KubernetesDeployer) command(operation string, userID portainer.UserID, endpoint *portainer.Endpoint, manifestFiles []string, namespace string) (string, error) { func (deployer *KubernetesDeployer) command(operation string, userID portainer.UserID, endpoint *portainer.Endpoint, resources []string, namespace string) (string, error) {
token, err := deployer.getToken(userID, endpoint, endpoint.Type == portainer.KubernetesLocalEnvironment) token, err := deployer.getToken(userID, endpoint, endpoint.Type == portainer.KubernetesLocalEnvironment)
if err != nil { if err != nil {
return "", errors.Wrap(err, "failed generating a user token") return "", errors.Wrap(err, "failed generating a user token")
@ -120,7 +120,7 @@ func (deployer *KubernetesDeployer) command(operation string, userID portainer.U
return "", errors.Errorf("unsupported operation: %s", operation) return "", errors.Errorf("unsupported operation: %s", operation)
} }
output, err := operationFunc(context.Background(), manifestFiles) output, err := operationFunc(context.Background(), resources)
if err != nil { if err != nil {
return "", errors.Wrapf(err, "failed to execute kubectl %s command", operation) return "", errors.Wrapf(err, "failed to execute kubectl %s command", operation)
} }

View file

@ -12,7 +12,7 @@ func (c *Client) Apply(ctx context.Context, manifests []string) (string, error)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := apply.NewCmdApply("kubectl", c.factory, c.streams) cmd := apply.NewCmdApply("kubectl", c.factory, c.streams)
cmd.SetArgs(manifestFilesToArgs(manifests)) cmd.SetArgs(resourcesToArgs(manifests))
cmd.SetOut(buf) cmd.SetOut(buf)
if err := cmd.ExecuteContext(ctx); err != nil { if err := cmd.ExecuteContext(ctx); err != nil {

View file

@ -12,7 +12,7 @@ func (c *Client) Delete(ctx context.Context, manifests []string) (string, error)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := delete.NewCmdDelete(c.factory, c.streams) cmd := delete.NewCmdDelete(c.factory, c.streams)
cmd.SetArgs(manifestFilesToArgs(manifests)) cmd.SetArgs(resourcesToArgs(manifests))
cmd.Flags().Set("ignore-not-found", "true") cmd.Flags().Set("ignore-not-found", "true")
cmd.SetOut(buf) cmd.SetOut(buf)

View file

@ -1,11 +0,0 @@
package libkubectl
import "strings"
func manifestFilesToArgs(manifestFiles []string) []string {
args := []string{}
for _, path := range manifestFiles {
args = append(args, "-f", strings.TrimSpace(path))
}
return args
}

View file

@ -1,48 +0,0 @@
package libkubectl
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestManifestFilesToArgsHelper(t *testing.T) {
tests := []struct {
name string
manifestFiles []string
expectedArgs []string
}{
{
name: "empty list",
manifestFiles: []string{},
expectedArgs: []string{},
},
{
name: "single manifest",
manifestFiles: []string{"manifest.yaml"},
expectedArgs: []string{"-f", "manifest.yaml"},
},
{
name: "multiple manifests",
manifestFiles: []string{"manifest1.yaml", "manifest2.yaml"},
expectedArgs: []string{"-f", "manifest1.yaml", "-f", "manifest2.yaml"},
},
{
name: "manifests with whitespace",
manifestFiles: []string{" manifest1.yaml ", " manifest2.yaml"},
expectedArgs: []string{"-f", "manifest1.yaml", "-f", "manifest2.yaml"},
},
{
name: "kubernetes resource definitions",
manifestFiles: []string{"deployment/nginx", "service/web"},
expectedArgs: []string{"-f", "deployment/nginx", "-f", "service/web"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := manifestFilesToArgs(tt.manifestFiles)
assert.Equal(t, tt.expectedArgs, args)
})
}
}

View file

@ -0,0 +1,20 @@
package libkubectl
import "strings"
func isManifestFile(resource string) bool {
trimmedResource := strings.TrimSpace(resource)
return strings.HasSuffix(trimmedResource, ".yaml") || strings.HasSuffix(trimmedResource, ".yml")
}
func resourcesToArgs(resources []string) []string {
args := []string{}
for _, resource := range resources {
if isManifestFile(resource) {
args = append(args, "-f", strings.TrimSpace(resource))
} else {
args = append(args, resource)
}
}
return args
}

View file

@ -0,0 +1,53 @@
package libkubectl
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestResourcesToArgsHelper(t *testing.T) {
tests := []struct {
name string
resources []string
expectedArgs []string
}{
{
name: "empty list",
resources: []string{},
expectedArgs: []string{},
},
{
name: "single manifest file",
resources: []string{"manifest.yaml"},
expectedArgs: []string{"-f", "manifest.yaml"},
},
{
name: "multiple manifest files",
resources: []string{"manifest1.yaml", "manifest2.yaml"},
expectedArgs: []string{"-f", "manifest1.yaml", "-f", "manifest2.yaml"},
},
{
name: "manifests with whitespace",
resources: []string{" manifest1.yaml ", " manifest2.yaml"},
expectedArgs: []string{"-f", "manifest1.yaml", "-f", "manifest2.yaml"},
},
{
name: "kubernetes resource definitions",
resources: []string{"deployment/nginx", "service/web"},
expectedArgs: []string{"deployment/nginx", "service/web"},
},
{
name: "rollout restart",
resources: []string{"deployment/nginx", "service/web"},
expectedArgs: []string{"deployment/nginx", "service/web"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := resourcesToArgs(tt.resources)
assert.Equal(t, tt.expectedArgs, args)
})
}
}

View file

@ -12,7 +12,10 @@ func (c *Client) RolloutRestart(ctx context.Context, manifests []string) (string
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cmd := rollout.NewCmdRollout(c.factory, c.streams) cmd := rollout.NewCmdRollout(c.factory, c.streams)
cmd.SetArgs(manifestFilesToArgs(manifests)) args := []string{"restart"}
args = append(args, resourcesToArgs(manifests)...)
cmd.SetArgs(args)
cmd.SetOut(buf) cmd.SetOut(buf)
if err := cmd.ExecuteContext(ctx); err != nil { if err := cmd.ExecuteContext(ctx); err != nil {