Skip to content

Add server-side apply mode #651

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

Merged
merged 10 commits into from
Apr 27, 2022
Prev Previous commit
Next Next commit
Include server-side created objects when scanning for orphans
  • Loading branch information
smuth4 committed Apr 26, 2022
commit f2de2b108537fddc1fa20d99ffc85c995487d516
19 changes: 17 additions & 2 deletions pkg/kubernetes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ See https://tanka.dev/garbage-collection for more details.`)
continue
}

// skip objects not created explicitely
if _, ok := m.Metadata().Annotations()[AnnotationLastApplied]; !ok {
// skip objects not created explicitly (e.g. pods created from deployments)
if !k.isKubectlCreated(m) {
continue
}

Expand All @@ -88,6 +88,21 @@ See https://tanka.dev/garbage-collection for more details.`)
return orphaned, nil
}

func (k *Kubernetes) isKubectlCreated(manifest manifest.Manifest) bool {
// Check if created by client-side apply
if _, ok := manifest.Metadata().Annotations()[AnnotationLastApplied]; ok {
return true
}
// Check if created by server-side apply
for _, manager := range manifest.Metadata().ManagedFields() {
manager_name := manager.(map[string]interface{})["manager"]
if manager_name == "tanka" || manager_name == "kubectl-client-side-apply" {
return true
}
}
return false
}

func (k *Kubernetes) uids(state manifest.List) (map[string]bool, error) {
uids := make(map[string]bool)

Expand Down
7 changes: 6 additions & 1 deletion pkg/kubernetes/client/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"strings"

"github.com/Masterminds/semver"

"github.com/grafana/tanka/pkg/kubernetes/manifest"
)

Expand All @@ -22,7 +24,10 @@ func (k Kubectl) GetByLabels(namespace, kind string, labels map[string]string) (
for k, v := range labels {
lArgs = append(lArgs, fmt.Sprintf("-l=%s=%s", k, v))
}

// Needed to properly filter for resources that should be pruned
if k.info.ClientVersion.GreaterThan(semver.MustParse("1.21.0")) {
lArgs = append(lArgs, "--show-managed-fields")
}
var opts getOpts
if namespace == "" {
opts.allNamespaces = true
Expand Down
13 changes: 13 additions & 0 deletions pkg/kubernetes/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,19 @@ func (m Metadata) Annotations() map[string]interface{} {
return safeMSI(m, "annotations")
}

// Managed fields of the manifest
func (m Metadata) ManagedFields() []interface{} {
items, ok := m["managedFields"]
if !ok {
return make([]interface{}, 0)
}
list, ok := items.([]interface{})
if !ok {
return make([]interface{}, 0)
}
return list
}

func safeMSI(m map[string]interface{}, key string) map[string]interface{} {
switch t := m[key].(type) {
case map[string]interface{}:
Expand Down