File: solver.go

package info (click to toggle)
deck 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,100 kB
  • sloc: makefile: 19; sh: 3
file content (96 lines) | stat: -rw-r--r-- 2,770 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package solver

import (
	"github.com/kong/deck/crud"
	"github.com/kong/deck/diff"
	"github.com/kong/deck/print"
	"github.com/kong/deck/state"
	"github.com/kong/go-kong/kong"
	"github.com/pkg/errors"
)

// Stats holds the stats related to a Solve.
type Stats struct {
	CreateOps int
	UpdateOps int
	DeleteOps int
}

// Solve generates a diff and walks the graph.
func Solve(doneCh chan struct{}, syncer *diff.Syncer,
	client *kong.Client, parallelism int, dry bool) (Stats, []error) {

	r := buildRegistry(client)

	var stats Stats
	recordOp := func(op crud.Op) {
		switch op {
		case crud.Create:
			stats.CreateOps = stats.CreateOps + 1
		case crud.Update:
			stats.UpdateOps = stats.UpdateOps + 1
		case crud.Delete:
			stats.DeleteOps = stats.DeleteOps + 1
		}
	}

	errs := syncer.Run(doneCh, parallelism, func(e diff.Event) (crud.Arg, error) {
		var err error
		var result crud.Arg

		c := e.Obj.(state.ConsoleString)
		switch e.Op {
		case crud.Create:
			print.CreatePrintln("creating", e.Kind, c.Console())
		case crud.Update:
			diffString, err := getDiff(e.OldObj, e.Obj)
			if err != nil {
				return nil, err
			}
			print.UpdatePrintln("updating", e.Kind, c.Console(), diffString)
		case crud.Delete:
			print.DeletePrintln("deleting", e.Kind, c.Console())
		default:
			panic("unknown operation " + e.Op.String())
		}

		if !dry {
			// sync mode
			// fire the request to Kong
			result, err = r.Do(e.Kind, e.Op, e)
			if err != nil {
				return nil, errors.Wrapf(err, "%v %v %v failed", e.Op, e.Kind, c.Console())
			}
		} else {
			// diff mode
			// return the new obj as is
			result = e.Obj
		}
		// record operation in both: diff and sync commands
		recordOp(e.Op)

		return result, nil
	})
	return stats, errs
}

func buildRegistry(client *kong.Client) *crud.Registry {
	var r crud.Registry
	r.MustRegister("service", &serviceCRUD{client: client})
	r.MustRegister("route", &routeCRUD{client: client})
	r.MustRegister("upstream", &upstreamCRUD{client: client})
	r.MustRegister("target", &targetCRUD{client: client})
	r.MustRegister("certificate", &certificateCRUD{client: client})
	r.MustRegister("sni", &sniCRUD{client: client})
	r.MustRegister("ca_certificate", &caCertificateCRUD{client: client})
	r.MustRegister("plugin", &pluginCRUD{client: client})
	r.MustRegister("consumer", &consumerCRUD{client: client})
	r.MustRegister("key-auth", &keyAuthCRUD{client: client})
	r.MustRegister("hmac-auth", &hmacAuthCRUD{client: client})
	r.MustRegister("jwt-auth", &jwtAuthCRUD{client: client})
	r.MustRegister("basic-auth", &basicAuthCRUD{client: client})
	r.MustRegister("acl-group", &aclGroupCRUD{client: client})
	r.MustRegister("oauth2-cred", &oauth2CredCRUD{client: client})
	r.MustRegister("mtls-auth", &mtlsAuthCRUD{client: client})
	return &r
}