Skip to content

Commit 3ef5ab1

Browse files
fix: New kube applier for server side diff dry run with refactoring (#662)
* fix: New kube applier for server side diff dry run with refactoring Part of a fix for: argoproj/argo-cd#21488 Separate logic to handle server side diff dry run applies. Signed-off-by: Andrii Korotkov <[email protected]> * Break backwards compatibility for a better code Signed-off-by: Andrii Korotkov <[email protected]> * Don't put applier constructor in the interface Signed-off-by: Andrii Korotkov <[email protected]> * Address comments Signed-off-by: Andrii Korotkov <[email protected]> * Address more comments Signed-off-by: Andrii Korotkov <[email protected]> * chore: enable testifylint linter (#657) Signed-off-by: Matthieu MOREL <[email protected]> Signed-off-by: Andrii Korotkov <[email protected]> * chore: enable gofumpt, gosimple and whitespace linters (#666) Signed-off-by: Matthieu MOREL <[email protected]> Signed-off-by: Andrii Korotkov <[email protected]> * chore: enable use-any from revive (#667) Signed-off-by: Matthieu MOREL <[email protected]> Signed-off-by: Andrii Korotkov <[email protected]> * chore: bump golangci-lint to v1.63.4 and list argo-cd linters (#670) Signed-off-by: Matthieu MOREL <[email protected]> Signed-off-by: Andrii Korotkov <[email protected]> * chore: enable unused-parameter and var-declaration from revive (#668) Signed-off-by: Matthieu MOREL <[email protected]> Signed-off-by: Andrii Korotkov <[email protected]> * chore: remove actions/cache duplicated behavior with actions/setup-go (#658) Signed-off-by: Matthieu MOREL <[email protected]> Signed-off-by: Andrii Korotkov <[email protected]> * Add Leo's code Signed-off-by: Andrii Korotkov <[email protected]> * Make linter happy Signed-off-by: Andrii Korotkov <[email protected]> --------- Signed-off-by: Andrii Korotkov <[email protected]> Signed-off-by: Matthieu MOREL <[email protected]> Co-authored-by: Matthieu MOREL <[email protected]>
1 parent 30f4acc commit 3ef5ab1

File tree

5 files changed

+210
-76
lines changed

5 files changed

+210
-76
lines changed

pkg/diff/diff_options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func applyOptions(opts []Option) options {
4040
}
4141

4242
type KubeApplier interface {
43-
ApplyResource(ctx context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy, force, validate, serverSideApply bool, manager string, serverSideDiff bool) (string, error)
43+
ApplyResource(ctx context.Context, obj *unstructured.Unstructured, dryRunStrategy cmdutil.DryRunStrategy, force, validate, serverSideApply bool, manager string) (string, error)
4444
}
4545

4646
// ServerSideDryRunner defines the contract to run a server-side apply in
@@ -66,7 +66,7 @@ func NewK8sServerSideDryRunner(kubeApplier KubeApplier) *K8sServerSideDryRunner
6666
// obj and the given manager in dryrun mode. Will return the predicted live state
6767
// json as string.
6868
func (kdr *K8sServerSideDryRunner) Run(ctx context.Context, obj *unstructured.Unstructured, manager string) (string, error) {
69-
return kdr.dryrunApplier.ApplyResource(ctx, obj, cmdutil.DryRunServer, false, false, true, manager, true)
69+
return kdr.dryrunApplier.ApplyResource(ctx, obj, cmdutil.DryRunServer, false, false, true, manager)
7070
}
7171

7272
func IgnoreAggregatedRoles(ignore bool) Option {

pkg/sync/sync_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R
10011001
message, err = sc.resourceOps.CreateResource(context.TODO(), t.targetObj, dryRunStrategy, validate)
10021002
}
10031003
} else {
1004-
message, err = sc.resourceOps.ApplyResource(context.TODO(), t.targetObj, dryRunStrategy, force, validate, serverSideApply, sc.serverSideApplyManager, false)
1004+
message, err = sc.resourceOps.ApplyResource(context.TODO(), t.targetObj, dryRunStrategy, force, validate, serverSideApply, sc.serverSideApplyManager)
10051005
}
10061006
if err != nil {
10071007
return common.ResultCodeSyncFailed, err.Error()

pkg/utils/kube/ctl.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"k8s.io/kube-openapi/pkg/util/proto"
2020
"k8s.io/kubectl/pkg/util/openapi"
2121

22+
"github.com/argoproj/gitops-engine/pkg/diff"
2223
utils "github.com/argoproj/gitops-engine/pkg/utils/io"
2324
"github.com/argoproj/gitops-engine/pkg/utils/tracing"
2425
)
@@ -295,6 +296,31 @@ func (k *KubectlCmd) ManageResources(config *rest.Config, openAPISchema openapi.
295296
}, cleanup, nil
296297
}
297298

299+
func ManageServerSideDiffDryRuns(config *rest.Config, openAPISchema openapi.Resources, tracer tracing.Tracer, log logr.Logger, onKubectlRun OnKubectlRunFunc) (diff.KubeApplier, func(), error) {
300+
f, err := os.CreateTemp(utils.TempDir, "")
301+
if err != nil {
302+
return nil, nil, fmt.Errorf("failed to generate temp file for kubeconfig: %v", err)
303+
}
304+
_ = f.Close()
305+
err = WriteKubeConfig(config, "", f.Name())
306+
if err != nil {
307+
utils.DeleteFile(f.Name())
308+
return nil, nil, fmt.Errorf("failed to write kubeconfig: %v", err)
309+
}
310+
fact := kubeCmdFactory(f.Name(), "", config)
311+
cleanup := func() {
312+
utils.DeleteFile(f.Name())
313+
}
314+
return &kubectlServerSideDiffDryRunApplier{
315+
config: config,
316+
fact: fact,
317+
openAPISchema: openAPISchema,
318+
tracer: tracer,
319+
log: log,
320+
onKubectlRun: onKubectlRun,
321+
}, cleanup, nil
322+
}
323+
298324
// ConvertToVersion converts an unstructured object into the specified group/version
299325
func (k *KubectlCmd) ConvertToVersion(obj *unstructured.Unstructured, group string, version string) (*unstructured.Unstructured, error) {
300326
span := k.Tracer.StartSpan("ConvertToVersion")

pkg/utils/kube/kubetest/mock_resource_operations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (r *MockResourceOps) GetLastResourceCommand(key kube.ResourceKey) string {
106106
return r.lastCommandPerResource[key]
107107
}
108108

109-
func (r *MockResourceOps) ApplyResource(_ context.Context, obj *unstructured.Unstructured, _ cmdutil.DryRunStrategy, force, validate, serverSideApply bool, manager string, _ bool) (string, error) {
109+
func (r *MockResourceOps) ApplyResource(_ context.Context, obj *unstructured.Unstructured, _ cmdutil.DryRunStrategy, force, validate, serverSideApply bool, manager string) (string, error) {
110110
r.SetLastValidate(validate)
111111
r.SetLastServerSideApply(serverSideApply)
112112
r.SetLastServerSideApplyManager(manager)

0 commit comments

Comments
 (0)