Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate applied-checksum after setting needApplied flag #126

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Create a file called `config.yaml` in `/etc/rancher/agent` with the contents lik
workDirectory: /var/lib/rancher/agent/work
localPlanDirectory: /var/lib/rancher/agent/plans
remoteEnabled: true
remoteSkipAlreadyApplied: false
connectionInfoFile: /etc/rancher/agent/conninfo.yaml
preserveWorkDirectory: true
```
Expand Down Expand Up @@ -49,4 +50,4 @@ data:
plan: e2luc3RydWN0aW9uczpbbmFtZTppbnN0YWxsLWszc119IHtpbnN0cnVjdGlvbnM6W2ltYWdlOmRvY2tlci5pby9yYW5jaGVyL3N5c3RlbS1hZ2VudC1pbnN0YWxsZXItazNzOnYxLjIxLjAtazNzMV19Cg==
```

The above secret is going to install K3s.
The above secret is going to install K3s.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func run(c *cli.Context) error {
return fmt.Errorf("unable to parse connection info file: %w", err)
}

k8splan.Watch(topContext, *applyinator, connInfo)
k8splan.Watch(topContext, *applyinator, connInfo, cf.RemoteSkipAlreadyApplied)
}

if cf.LocalEnabled {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type AgentConfig struct {
LocalPlanDir string `json:"localPlanDirectory,omitempty"`
AppliedPlanDir string `json:"appliedPlanDirectory,omitempty"`
RemoteEnabled bool `json:"remoteEnabled,omitempty"`
RemoteSkipAlreadyApplied bool `json:"remoteSkipAlreadyApplied,omitempty"`
ConnectionInfoFile string `json:"connectionInfoFile,omitempty"`
PreserveWorkDir bool `json:"preserveWorkDirectory,omitempty"`
ImagesDir string `json:"imagesDirectory,omitempty"`
Expand Down
15 changes: 12 additions & 3 deletions pkg/k8splan/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ const (
cooldownTimerDuration = "30s"
)

func Watch(ctx context.Context, applyinator applyinator.Applyinator, connInfo config.ConnectionInfo) {
func Watch(ctx context.Context, applyinator applyinator.Applyinator, connInfo config.ConnectionInfo, skipAlreadyApplied bool) {
w := &watcher{
connInfo: connInfo,
applyinator: applyinator,
connInfo: connInfo,
applyinator: applyinator,
skipAlreadyApplied: skipAlreadyApplied,
}

go w.start(ctx)
Expand All @@ -68,6 +69,7 @@ type watcher struct {
applyinator applyinator.Applyinator
lastAppliedResourceVersion string
secretUID string
skipAlreadyApplied bool
}

func toInt(resourceVersion string) int {
Expand Down Expand Up @@ -198,12 +200,14 @@ func (w *watcher) start(ctx context.Context) {
}
logrus.Tracef("[K8s] Calculated checksum to be %s", cp.Checksum)

alreadyApplied := false
if secretChecksumData, ok := secret.Data[appliedChecksumKey]; ok {
secretChecksum := string(secretChecksumData)
logrus.Tracef("[K8s] Remote plan had an applied checksum value of %s", secretChecksum)
if secretChecksum == cp.Checksum {
logrus.Debugf("[K8s] Applied checksum was the same as the plan from remote. Not applying.")
needsApplied = false
alreadyApplied = true
}
}

Expand All @@ -213,6 +217,11 @@ func (w *watcher) start(ctx context.Context) {
hasRunOnce = true
}

if w.skipAlreadyApplied && alreadyApplied {
logrus.Debugf("[K8s] Skipping already applied plan.")
needsApplied = false
}

// Check to see if we've exceeded our failure count threshold
var maxFailureThreshold int
if rawMaxFailureThreshold, ok := secret.Data[maxFailuresKey]; ok && len(rawMaxFailureThreshold) > 0 {
Expand Down