Skip to content

Commit 84e16c3

Browse files
authored
Merge pull request #28 from wantedly/onsd/use-updater
2 parents 278d916 + 7234cec commit 84e16c3

4 files changed

+44
-26
lines changed

controllers/.snapshots/TestDeploymentCopyReconciler-copied_deployment_exists.yaml

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ items:
5252
name: some-container
5353
resources: {}
5454
status: {}
55-
- apiVersion: apps/v1
56-
kind: Deployment
57-
metadata:
55+
- metadata:
5856
creationTimestamp: null
5957
labels:
6058
app: some-app

controllers/deploymentcopy_controller.go

+18-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
apierrors "k8s.io/apimachinery/pkg/api/errors"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/types"
28-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2928
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3029

3130
"k8s.io/apimachinery/pkg/runtime"
@@ -34,6 +33,7 @@ import (
3433
logf "sigs.k8s.io/controller-runtime/pkg/log"
3534

3635
duplicationv1beta1 "github.com/wantedly/deployment-duplicator/api/v1beta1"
36+
refresh "github.com/wantedly/resource-refresher"
3737
)
3838

3939
var log = logf.Log.WithName("controller")
@@ -53,10 +53,6 @@ type DeploymentCopyReconciler struct {
5353

5454
// Reconcile is part of the main kubernetes reconciliation loop which aims to
5555
// move the current state of the cluster closer to the desired state.
56-
// TODO(user): Modify the Reconcile function to compare the state specified by
57-
// the DeploymentCopy object against the actual cluster state, and then
58-
// perform operations to make the cluster state reflect the state specified by
59-
// the user.
6056
//
6157
// For more details, check Reconcile and its Result here:
6258
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
@@ -130,23 +126,29 @@ func (r *DeploymentCopyReconciler) Reconcile(ctx context.Context, req ctrl.Reque
130126
)
131127
}
132128
}
129+
133130
copiedDeploy := &appsv1.Deployment{
134131
ObjectMeta: metav1.ObjectMeta{
135-
Name: fmt.Sprintf("%s-%s", copied.ObjectMeta.Name, instance.Spec.NameSuffix),
136-
Namespace: instance.Namespace,
132+
Name: fmt.Sprintf("%s-%s", copied.ObjectMeta.Name, instance.Spec.NameSuffix),
133+
Namespace: instance.Namespace,
134+
Labels: labels,
135+
Annotations: annotations,
136+
},
137+
Spec: spec,
138+
}
139+
140+
copiedDeployList := refresh.ObjectList{
141+
Items: []client.Object{copiedDeploy},
142+
GroupVersionKind: appsv1.SchemeGroupVersion.WithKind("Deployment"),
143+
Identity: func(obj client.Object) (string, error) {
144+
return obj.GetName(), nil
137145
},
138146
}
139147

140148
log.Info("try to create or update copied Deployment", "namespace", copiedDeploy.Namespace, "name", copiedDeploy.Name)
141-
if _, err := controllerutil.CreateOrUpdate(ctx, r.Client, copiedDeploy, func() error {
142-
copiedDeploy.Labels = labels
143-
copiedDeploy.Annotations = annotations
144-
copiedDeploy.Spec = spec
145-
146-
// In order to support Update, set controller reference here
147-
return controllerutil.SetControllerReference(instance, copiedDeploy, r.Scheme)
148-
}); err != nil {
149-
return ctrl.Result{}, errors.WithStack(err)
149+
ref := refresh.New(r.Client, r.Scheme)
150+
if err := ref.Refresh(ctx, instance, copiedDeployList); err != nil {
151+
return reconcile.Result{}, errors.WithStack(err)
150152
}
151153

152154
return reconcile.Result{}, nil

go.mod

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ go 1.17
55
require (
66
github.com/onsi/ginkgo v1.16.5
77
github.com/onsi/gomega v1.17.0
8-
k8s.io/apimachinery v0.23.0
9-
k8s.io/client-go v0.23.0
8+
k8s.io/apimachinery v0.23.3
9+
k8s.io/client-go v0.23.1
1010
sigs.k8s.io/controller-runtime v0.11.0
1111
)
1212

1313
require (
1414
github.com/bradleyjkemp/cupaloy/v2 v2.7.0 // indirect
1515
github.com/pmezard/go-difflib v1.0.0 // indirect
1616
github.com/stuart-warren/yamlfmt v0.1.2 // indirect
17+
github.com/wantedly/resource-refresher v0.0.0-20220215090553-7732e7b9abfc // indirect
1718
)
1819

1920
require (
@@ -55,9 +56,9 @@ require (
5556
go.uber.org/multierr v1.6.0 // indirect
5657
go.uber.org/zap v1.19.1 // indirect
5758
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
58-
golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
59+
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
5960
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
60-
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 // indirect
61+
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
6162
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
6263
golang.org/x/text v0.3.7 // indirect
6364
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
@@ -68,13 +69,13 @@ require (
6869
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
6970
gopkg.in/yaml.v2 v2.4.0 // indirect
7071
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
71-
k8s.io/api v0.23.0
72+
k8s.io/api v0.23.1
7273
k8s.io/apiextensions-apiserver v0.23.0 // indirect
7374
k8s.io/component-base v0.23.0 // indirect
7475
k8s.io/klog/v2 v2.30.0 // indirect
7576
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
76-
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b // indirect
77+
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
7778
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
78-
sigs.k8s.io/structured-merge-diff/v4 v4.2.0 // indirect
79+
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
7980
sigs.k8s.io/yaml v1.3.0 // indirect
8081
)

go.sum

+17
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ github.com/stuart-warren/yamlfmt v0.1.2/go.mod h1:X5TuPH+hf4O0U1KBvNqygvHbvAnoi9
453453
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
454454
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
455455
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
456+
github.com/wantedly/resource-refresher v0.0.0-20220215090553-7732e7b9abfc h1:ndYwkIq9zplsocoYqR2qfOZ581lHPAqBX20AeU8L9GE=
457+
github.com/wantedly/resource-refresher v0.0.0-20220215090553-7732e7b9abfc/go.mod h1:UO2n4p1BtT0iENoj6pm1l0Loe5pN6WzFzvK4GA3nK+8=
456458
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
457459
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
458460
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -597,6 +599,8 @@ golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qx
597599
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
598600
golang.org/x/net v0.0.0-20210825183410-e898025ed96a h1:bRuuGXV8wwSdGTB+CtJf+FjgO1APK1CoO39T4BN/XBw=
599601
golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
602+
golang.org/x/net v0.0.0-20211209124913-491a49abca63 h1:iocB37TsdFuN6IBRZ+ry36wrkoV51/tl5vOWqkcPGvY=
603+
golang.org/x/net v0.0.0-20211209124913-491a49abca63/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
600604
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
601605
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
602606
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -689,6 +693,8 @@ golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBc
689693
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
690694
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 h1:M69LAlWZCshgp0QSzyDcSsSIejIEeuaCVpmwcKwyLMk=
691695
golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
696+
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5IqFdQieMLwwCJokV2pc=
697+
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
692698
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
693699
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=
694700
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
@@ -928,13 +934,20 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
928934
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
929935
k8s.io/api v0.23.0 h1:WrL1gb73VSC8obi8cuYETJGXEoFNEh3LU0Pt+Sokgro=
930936
k8s.io/api v0.23.0/go.mod h1:8wmDdLBHBNxtOIytwLstXt5E9PddnZb0GaMcqsvDBpg=
937+
k8s.io/api v0.23.1 h1:ncu/qfBfUoClqwkTGbeRqqOqBCRoUAflMuOaOD7J0c8=
938+
k8s.io/api v0.23.1/go.mod h1:WfXnOnwSqNtG62Y1CdjoMxh7r7u9QXGCkA1u0na2jgo=
931939
k8s.io/apiextensions-apiserver v0.23.0 h1:uii8BYmHYiT2ZTAJxmvc3X8UhNYMxl2A0z0Xq3Pm+WY=
932940
k8s.io/apiextensions-apiserver v0.23.0/go.mod h1:xIFAEEDlAZgpVBl/1VSjGDmLoXAWRG40+GsWhKhAxY4=
933941
k8s.io/apimachinery v0.23.0 h1:mIfWRMjBuMdolAWJ3Fd+aPTMv3X9z+waiARMpvvb0HQ=
934942
k8s.io/apimachinery v0.23.0/go.mod h1:fFCTTBKvKcwTPFzjlcxp91uPFZr+JA0FubU4fLzzFYc=
943+
k8s.io/apimachinery v0.23.1/go.mod h1:SADt2Kl8/sttJ62RRsi9MIV4o8f5S3coArm0Iu3fBno=
944+
k8s.io/apimachinery v0.23.3 h1:7IW6jxNzrXTsP0c8yXz2E5Yx/WTzVPTsHIx/2Vm0cIk=
945+
k8s.io/apimachinery v0.23.3/go.mod h1:BEuFMMBaIbcOqVIJqNZJXGFTP4W6AycEpb5+m/97hrM=
935946
k8s.io/apiserver v0.23.0/go.mod h1:Cec35u/9zAepDPPFyT+UMrgqOCjgJ5qtfVJDxjZYmt4=
936947
k8s.io/client-go v0.23.0 h1:vcsOqyPq7XV3QmQRCBH/t9BICJM9Q1M18qahjv+rebY=
937948
k8s.io/client-go v0.23.0/go.mod h1:hrDnpnK1mSr65lHHcUuIZIXDgEbzc7/683c6hyG4jTA=
949+
k8s.io/client-go v0.23.1 h1:Ma4Fhf/p07Nmj9yAB1H7UwbFHEBrSPg8lviR24U2GiQ=
950+
k8s.io/client-go v0.23.1/go.mod h1:6QSI8fEuqD4zgFK0xbdwfB/PthBsIxCJMa3s17WlcO0=
938951
k8s.io/code-generator v0.23.0/go.mod h1:vQvOhDXhuzqiVfM/YHp+dmg10WDZCchJVObc9MvowsE=
939952
k8s.io/component-base v0.23.0 h1:UAnyzjvVZ2ZR1lF35YwtNY6VMN94WtOnArcXBu34es8=
940953
k8s.io/component-base v0.23.0/go.mod h1:DHH5uiFvLC1edCpvcTDV++NKULdYYU6pR9Tt3HIKMKI=
@@ -948,6 +961,8 @@ k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65/go.mod h1:sX9MT8g7NVZM5lV
948961
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
949962
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b h1:wxEMGetGMur3J1xuGLQY7GEQYg9bZxKn3tKo5k/eYcs=
950963
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
964+
k8s.io/utils v0.0.0-20211116205334-6203023598ed h1:ck1fRPWPJWsMd8ZRFsWc6mh/zHp5fZ/shhbrgPUxDAE=
965+
k8s.io/utils v0.0.0-20211116205334-6203023598ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
951966
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
952967
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
953968
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
@@ -960,6 +975,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
960975
sigs.k8s.io/structured-merge-diff/v4 v4.1.2/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4=
961976
sigs.k8s.io/structured-merge-diff/v4 v4.2.0 h1:kDvPBbnPk+qYmkHmSo8vKGp438IASWofnbbUKDE/bv0=
962977
sigs.k8s.io/structured-merge-diff/v4 v4.2.0/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4=
978+
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 h1:bKCqE9GvQ5tiVHn5rfn1r+yao3aLQEaLzkkmAkf+A6Y=
979+
sigs.k8s.io/structured-merge-diff/v4 v4.2.1/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4=
963980
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
964981
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
965982
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=

0 commit comments

Comments
 (0)