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

k8s: clean up bookkeeping fields #3555

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 22 additions & 6 deletions internal/k8s/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ type k8sMeta interface {
GetUID() types.UID
GetLabels() map[string]string
GetOwnerReferences() []metav1.OwnerReference
GetAnnotations() map[string]string
SetNamespace(ns string)
SetManagedFields(managedFields []metav1.ManagedFieldsEntry)
}

type emptyMeta struct{}

func (emptyMeta) GetName() string { return "" }
func (emptyMeta) GetNamespace() string { return "" }
func (emptyMeta) GetUID() types.UID { return "" }
func (emptyMeta) GetLabels() map[string]string { return make(map[string]string) }
func (emptyMeta) GetOwnerReferences() []metav1.OwnerReference { return nil }
func (emptyMeta) SetNamespace(ns string) {}
func (emptyMeta) GetName() string { return "" }
func (emptyMeta) GetNamespace() string { return "" }
func (emptyMeta) GetUID() types.UID { return "" }
func (emptyMeta) GetAnnotations() map[string]string { return make(map[string]string) }
func (emptyMeta) GetLabels() map[string]string { return make(map[string]string) }
func (emptyMeta) GetOwnerReferences() []metav1.OwnerReference { return nil }
func (emptyMeta) SetNamespace(ns string) {}
func (emptyMeta) SetManagedFields(mf []metav1.ManagedFieldsEntry) {}

var _ k8sMeta = emptyMeta{}
var _ k8sMeta = &metav1.ObjectMeta{}
Expand Down Expand Up @@ -105,6 +109,18 @@ func (e K8sEntity) GVK() schema.GroupVersionKind {
return gvk
}

// Clean up internal bookkeeping fields. See
// https://github.com/kubernetes/kubernetes/issues/90066
func (e K8sEntity) Clean() {
m := e.meta()
m.SetManagedFields(nil)

annotations := m.GetAnnotations()
if len(annotations) != 0 {
delete(annotations, "kubectl.kubernetes.io/last-applied-configuration")
}
}

func (e K8sEntity) meta() k8sMeta {
if unstruct := e.maybeUnstructuredMeta(); unstruct != nil {
return unstruct
Expand Down
61 changes: 61 additions & 0 deletions internal/k8s/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,67 @@ func TestMutableAndImmutableEntities(t *testing.T) {
}
}

func TestClean(t *testing.T) {
yaml := `apiVersion: v1
kind: Namespace
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"labels":{"app.kubernetes.io/managed-by":"tilt"},"name":"elastic-system"},"spec":{}}
creationTimestamp: "2020-07-07T14:50:17Z"
labels:
app.kubernetes.io/managed-by: tilt
managedFields:
- apiVersion: v1
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:kubectl.kubernetes.io/last-applied-configuration: {}
f:labels:
.: {}
f:app.kubernetes.io/managed-by: {}
f:status:
f:phase: {}
manager: tilt
operation: Update
time: "2020-07-07T14:50:17Z"
name: elastic-system
resourceVersion: "617"
selfLink: /api/v1/namespaces/elastic-system
uid: fa9710ff-7b19-499c-b0f9-faedd1c84969
spec:
finalizers:
- kubernetes
`
entities := mustParseYAML(t, yaml)
entities[0].Clean()

result, err := SerializeSpecYAML(entities)
require.NoError(t, err)

expected := `apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: "2020-07-07T14:50:17Z"
labels:
app.kubernetes.io/managed-by: tilt
name: elastic-system
resourceVersion: "617"
selfLink: /api/v1/namespaces/elastic-system
uid: fa9710ff-7b19-499c-b0f9-faedd1c84969
spec:
finalizers:
- kubernetes
`
assert.Equal(t, expected, result)
if err != nil {
t.Fatal(err)
}

}

func entitiesWithKinds(kinds []string) []K8sEntity {
entities := make([]K8sEntity, len(kinds))
for i, k := range kinds {
Expand Down
5 changes: 5 additions & 0 deletions internal/store/build_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ func (r K8sBuildResult) Facets() []model.Facet {

// For kubernetes deploy targets.
func NewK8sDeployResult(id model.TargetID, uids []types.UID, hashes []k8s.PodTemplateSpecHash, appliedEntities []k8s.K8sEntity) BuildResult {
// Remove verbose fields from the YAML.
for _, e := range appliedEntities {
e.Clean()
}

appliedEntitiesText, err := k8s.SerializeSpecYAML(appliedEntities)
if err != nil {
appliedEntitiesText = fmt.Sprintf("unable to serialize entities to yaml: %s", err.Error())
Expand Down