Skip to content

Commit

Permalink
optimize imagepulljob code (openkruise#1514)
Browse files Browse the repository at this point in the history
Signed-off-by: liheng.zms <liheng.zms@alibaba-inc.com>
  • Loading branch information
zmberg authored Mar 1, 2024
1 parent 3b7c731 commit 63bc96e
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 9 deletions.
18 changes: 11 additions & 7 deletions pkg/controller/imagepulljob/imagepulljob_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,18 @@ func targetFromSource(source *v1.Secret, keySet referenceSet) *v1.Secret {
target.ObjectMeta = metav1.ObjectMeta{
Namespace: util.GetKruiseDaemonConfigNamespace(),
GenerateName: fmt.Sprintf("%s-", source.Name),
Labels: map[string]string{
SourceSecretUIDLabelKey: string(source.UID),
},
Annotations: map[string]string{
SourceSecretKeyAnno: keyFromObject(source).String(),
TargetOwnerReferencesAnno: keySet.String(),
},
Labels: source.Labels,
Annotations: source.Annotations,
}
if target.Labels == nil {
target.Labels = map[string]string{}
}
target.Labels[SourceSecretUIDLabelKey] = string(source.UID)
if target.Annotations == nil {
target.Annotations = map[string]string{}
}
target.Annotations[SourceSecretKeyAnno] = keyFromObject(source).String()
target.Annotations[TargetOwnerReferencesAnno] = keySet.String()
return target
}

Expand Down
125 changes: 125 additions & 0 deletions pkg/controller/imagepulljob/imagepulljob_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright 2024 The Kruise Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package imagepulljob

import (
"reflect"
"testing"

"github.com/openkruise/kruise/pkg/util"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestTargetFromSource(t *testing.T) {
cases := []struct {
name string
getPara func() (*v1.Secret, referenceSet)
expect *v1.Secret
}{
{
name: "test1, normal1",
getPara: func() (*v1.Secret, referenceSet) {
s1 := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns-foo",
Name: "foo",
Annotations: map[string]string{
"anno1": "value1",
},
Labels: map[string]string{
"labels1": "value2",
},
UID: types.UID("db8acf1c-be68-46a2-9a40-a36c65eedd84"),
},
Type: v1.SecretTypeOpaque,
Data: map[string][]byte{
"data": []byte("foo"),
},
}
ref := map[types.NamespacedName]struct{}{
{Namespace: "ns-foo", Name: "name1"}: {},
}
return s1, ref
},
expect: &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"anno1": "value1",
SourceSecretKeyAnno: "ns-foo/foo",
TargetOwnerReferencesAnno: "ns-foo/name1",
},
Labels: map[string]string{
"labels1": "value2",
SourceSecretUIDLabelKey: "db8acf1c-be68-46a2-9a40-a36c65eedd84",
},
},
Type: v1.SecretTypeOpaque,
Data: map[string][]byte{
"data": []byte("foo"),
},
},
},
{
name: "test1, normal2",
getPara: func() (*v1.Secret, referenceSet) {
s1 := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns-foo",
Name: "foo",
UID: types.UID("db8acf1c-be68-46a2-9a40-a36c65eedd84"),
},
Type: v1.SecretTypeOpaque,
Data: map[string][]byte{
"data": []byte("foo"),
},
}
ref := map[types.NamespacedName]struct{}{
{Namespace: "ns-foo", Name: "name1"}: {},
}
return s1, ref
},
expect: &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
SourceSecretKeyAnno: "ns-foo/foo",
TargetOwnerReferencesAnno: "ns-foo/name1",
},
Labels: map[string]string{
SourceSecretUIDLabelKey: "db8acf1c-be68-46a2-9a40-a36c65eedd84",
},
},
Type: v1.SecretTypeOpaque,
Data: map[string][]byte{
"data": []byte("foo"),
},
},
},
}

for _, cs := range cases {
t.Run(cs.name, func(t *testing.T) {
obj := targetFromSource(cs.getPara())
obj.Namespace = ""
obj.GenerateName = ""
if !reflect.DeepEqual(obj, cs.expect) {
t.Fatalf("expect(%s), but get(%s)", util.DumpJSON(cs.expect), util.DumpJSON(obj))
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func NewDaemon(cfg *rest.Config, bindAddress string) (Daemon, error) {
Healthz: healthz,
}

puller, err := imagepuller.NewController(opts, secretManager)
puller, err := imagepuller.NewController(opts, secretManager, cfg)
if err != nil {
return nil, fmt.Errorf("failed to new image puller controller: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/daemon/imagepuller/imagepuller_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/tools/reference"
Expand All @@ -56,7 +57,7 @@ type Controller struct {
}

// NewController returns the controller for image pulling
func NewController(opts daemonoptions.Options, secretManager daemonutil.SecretManager) (*Controller, error) {
func NewController(opts daemonoptions.Options, secretManager daemonutil.SecretManager, cfg *rest.Config) (*Controller, error) {
genericClient := client.GetGenericClientWithName("kruise-daemon-imagepuller")
informer := newNodeImageInformer(genericClient.KruiseClient, opts.NodeName)

Expand Down

0 comments on commit 63bc96e

Please sign in to comment.