Skip to content

Commit c2f2ef3

Browse files
committed
feat: support plugin apply skip no change
Signed-off-by: zwtop <wang.zhan@smartx.com>
1 parent 3e090c1 commit c2f2ef3

File tree

10 files changed

+253
-30
lines changed

10 files changed

+253
-30
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.PHONY: test docker-test
22

33
test:
4-
go test ./... --race --coverprofile coverage.out
4+
go test --gcflags=all=-l ./... --race --coverprofile coverage.out
55

66
docker-test:
77
$(eval WORKDIR := /go/src/github.com/everoute/container)

client/clienttest/runtime.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ func (r *runtime) ListContainers(ctx context.Context) ([]*model.Container, error
118118
return containers, nil
119119
}
120120

121-
func (r *runtime) GetContainerStatus(ctx context.Context, containerID string) (containerd.Status, error) {
121+
func (r *runtime) GetContainerStatus(ctx context.Context, containerID string) (client.ContainerStatus, error) {
122122
_, ok := r.containers[containerID]
123123
if !ok {
124-
return containerd.Status{}, fmt.Errorf("container %s not found", containerID)
124+
return client.ContainerStatus{}, fmt.Errorf("container %s not found", containerID)
125125
}
126-
return containerd.Status{Status: containerd.Running}, nil
126+
return client.ContainerStatus{Status: containerd.Status{Status: containerd.Running}}, nil
127127
}
128128

129129
func (r *runtime) ExecCommand(ctx context.Context, containerID string, commands []string) (*containerd.ExitStatus, error) {

client/interface.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222

2323
"github.com/containerd/containerd"
24+
"github.com/containerd/containerd/containers"
2425
"github.com/containerd/containerd/images"
2526
"github.com/containerd/containerd/platforms"
2627

@@ -69,6 +70,11 @@ type ImageManager interface {
6970
GetImage(ctx context.Context, ref string) (*images.Image, bool, error)
7071
}
7172

73+
type ContainerStatus struct {
74+
containerd.Status
75+
containers.Container
76+
}
77+
7278
// ContainerManager contains methods to manipulate containers managed by a
7379
// container runtime. The methods are thread-safe.
7480
type ContainerManager interface {
@@ -85,7 +91,7 @@ type ContainerManager interface {
8591
ListContainers(ctx context.Context) ([]*model.Container, error)
8692

8793
// GetContainerStatus return the container status
88-
GetContainerStatus(ctx context.Context, containerID string) (containerd.Status, error)
94+
GetContainerStatus(ctx context.Context, containerID string) (ContainerStatus, error)
8995

9096
// ExecCommand exec giving commands and return result
9197
ExecCommand(ctx context.Context, containerID string, commands []string) (*containerd.ExitStatus, error)

client/runtime.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,20 +334,28 @@ func (r *runtime) RemoveNamespace(ctx context.Context) error {
334334
return ignoreNotFoundError(err)
335335
}
336336

337-
func (r *runtime) GetContainerStatus(ctx context.Context, containerID string) (containerd.Status, error) {
337+
func (r *runtime) GetContainerStatus(ctx context.Context, containerID string) (ContainerStatus, error) {
338338
ctx = namespaces.WithNamespace(ctx, r.namespace)
339339

340340
c, err := r.client.LoadContainer(ctx, containerID)
341341
if err != nil {
342-
return containerd.Status{}, fmt.Errorf("load container: %w", err)
342+
return ContainerStatus{}, fmt.Errorf("load container: %w", err)
343343
}
344344

345345
task, err := c.Task(ctx, nil)
346346
if err != nil {
347-
return containerd.Status{}, fmt.Errorf("load task: %w", err)
347+
return ContainerStatus{}, fmt.Errorf("load task: %w", err)
348348
}
349349

350-
return task.Status(ctx)
350+
status, err := task.Status(ctx)
351+
if err != nil {
352+
return ContainerStatus{}, fmt.Errorf("get task status: %w", err)
353+
}
354+
355+
return ContainerStatus{
356+
Status: status,
357+
Container: lo.Must(c.Info(ctx, containerd.WithoutRefreshedMetadata)),
358+
}, nil
351359
}
352360

353361
func (r *runtime) ExecCommand(ctx context.Context, containerID string, commands []string) (*containerd.ExitStatus, error) {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/samber/lo v1.39.0
2222
go.uber.org/goleak v1.1.12
2323
google.golang.org/grpc v1.50.1
24+
gopkg.in/yaml.v3 v3.0.1
2425
k8s.io/apimachinery v0.22.5
2526
k8s.io/klog/v2 v2.30.0
2627
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,7 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
10811081
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10821082
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10831083
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
1084+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10841085
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
10851086
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
10861087
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=

plugin/context.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2025 The Everoute Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package plugin
18+
19+
import (
20+
"context"
21+
22+
"github.com/samber/lo"
23+
)
24+
25+
type ContextKey string
26+
27+
const (
28+
ContextSkipNoChange ContextKey = "skip-no-change"
29+
)
30+
31+
func SetSkipNoChange(ctx context.Context, skip bool) context.Context {
32+
return context.WithValue(ctx, ContextSkipNoChange, skip)
33+
}
34+
35+
func GetSkipNoChange(ctx context.Context) bool {
36+
v := ctx.Value(ContextSkipNoChange)
37+
skip, ok := v.(bool)
38+
return lo.If(ok, skip).Else(false)
39+
}
40+
41+
func WithSkipNoChange(ctx context.Context) context.Context {
42+
return SetSkipNoChange(ctx, true)
43+
}

plugin/context_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2025 The Everoute Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package plugin_test
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
. "github.com/onsi/gomega"
24+
25+
"github.com/everoute/container/plugin"
26+
)
27+
28+
func TestContextSkipNoChange(t *testing.T) {
29+
RegisterTestingT(t)
30+
31+
ctx := context.Background()
32+
Expect(plugin.GetSkipNoChange(ctx)).Should(BeFalse())
33+
ctx = plugin.WithSkipNoChange(ctx)
34+
Expect(plugin.GetSkipNoChange(ctx)).Should(BeTrue())
35+
ctx = plugin.SetSkipNoChange(ctx, false)
36+
Expect(plugin.GetSkipNoChange(ctx)).Should(BeFalse())
37+
}

0 commit comments

Comments
 (0)