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

Detect changes/drift through a filtered diff of the apply #379

Closed
wants to merge 2 commits into from
Closed
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
57 changes: 38 additions & 19 deletions controllers/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,18 +624,23 @@ func (r *KustomizationReconciler) apply(ctx context.Context, kustomization kusto
start := time.Now()
timeout := kustomization.GetTimeout() + (time.Second * 1)
applyCtx, cancel := context.WithTimeout(ctx, timeout)
var resources map[string]string
defer cancel()
fieldManager := "kustomize-controller"

cmd := fmt.Sprintf("cd %s && kubectl apply --field-manager=%s -f %s.yaml --timeout=%s --cache-dir=/tmp --force=%t",
dirPath, fieldManager, kustomization.GetUID(), kustomization.Spec.Interval.Duration.String(), kustomization.Spec.Force)
// kubectl diff command to get the changeset
diffcmd := fmt.Sprintf("cd %s && kubectl diff --field-manager=%s -f %s.yaml --cache-dir=/tmp",
dirPath, fieldManager, kustomization.GetUID())

if kustomization.Spec.KubeConfig != nil {
kubeConfig, err := imp.WriteKubeConfig(ctx)
if err != nil {
return "", err
}
cmd = fmt.Sprintf("%s --kubeconfig=%s", cmd, kubeConfig)
diffcmd = fmt.Sprintf("%s --kubeconfig %s", diffcmd, kubeConfig)
} else {
// impersonate SA
if kustomization.Spec.ServiceAccountName != "" {
Expand All @@ -645,28 +650,42 @@ func (r *KustomizationReconciler) apply(ctx context.Context, kustomization kusto
}

cmd = fmt.Sprintf("%s --token %s", cmd, saToken)
diffcmd = fmt.Sprintf("%s --token %s", diffcmd, saToken)
}
}

// The reason for we parse the output via kubectl diff could be check from : https://github.com/fluxcd/kustomize-controller/issues/305
diffcommand := exec.CommandContext(applyCtx, "/bin/sh", "-c", diffcmd)
diffoutput, err := diffcommand.CombinedOutput()

// The reason that we still parse diff output when exit is for : https://github.com/kubernetes/kubernetes/pull/82336/files
// For the "diff" command Exit status:
// 0
// No differences were found.
// 1
// Differences were found.
// >1
// Kubectl or diff failed with an error.
if differr, ok := err.(*exec.ExitError); ok {
if differr.ExitCode() > 1 {
log.V(1).Info("Kubectl or diff failed with an error, will execute apply soon")
output, applyErr := execApply(ctx, cmd)
if applyErr != nil {
return "", applyErr
}
resources = parseApplyOutput(output)
} else if differr.ExitCode() == 1 {
resources = parseDiffOutput(diffoutput)
// Since we found difference in "diff" command, so we need to execute apply
log.V(1).Info("Differences found, will execute apply soon")
_, applyErr := execApply(ctx, cmd)
if applyErr != nil {
log.Error(applyErr, "unable to apply")
return "", applyErr
}
}
}

command := exec.CommandContext(applyCtx, "/bin/sh", "-c", cmd)
output, err := command.CombinedOutput()
if err != nil {
if errors.Is(applyCtx.Err(), context.DeadlineExceeded) {
return "", fmt.Errorf("apply timeout: %w", applyCtx.Err())
}

if string(output) == "" {
return "", fmt.Errorf("apply failed: %w, kubectl process was killed, probably due to OOM", err)
}

applyErr := parseApplyError(output)
if applyErr == "" {
applyErr = "no error output found, this may happen because of a timeout"
}
return "", fmt.Errorf("apply failed: %s", applyErr)
}

resources := parseApplyOutput(output)
log.Info(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't log that we applied something on the cluster if we didn't, please move the log below inside else if differr.ExitCode() == 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix done

fmt.Sprintf("Kustomization applied in %s",
time.Now().Sub(start).String()),
Expand Down
Loading