Skip to content

Commit

Permalink
Avoid version coercion in components (#73)
Browse files Browse the repository at this point in the history
Kubernetes will occasionally switch kind versions on apply automatically via mutating webhooks, this is fine, but ruins our ability to detect deprecations in manifests, so revert back to manifest versions where we can find them.
  • Loading branch information
michaeljguarino authored Nov 3, 2023
1 parent 8b956bf commit c119958
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
25 changes: 25 additions & 0 deletions pkg/manifests/versions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package manifests

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type GroupName struct {
Group string
Kind string
Name string
}

func VersionCache(manifests []*unstructured.Unstructured) map[GroupName]string {
res := map[GroupName]string{}
for _, man := range manifests {
gvk := man.GroupVersionKind()
name := GroupName{
Group: gvk.Group,
Kind: gvk.Kind,
Name: man.GetName(),
}
res[name] = gvk.Version
}
return res
}
5 changes: 4 additions & 1 deletion pkg/sync/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime/debug"
"time"

manis "github.com/pluralsh/deployment-operator/pkg/manifests"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/cli-utils/pkg/apply"
Expand Down Expand Up @@ -110,6 +111,8 @@ func (engine *Engine) processItem(item interface{}) error {
})
return engine.UpdatePruneStatus(id, svc.Name, svc.Namespace, ch, len(manifests))
}

vcache := manis.VersionCache(manifests)
log.Info("Apply service", "name", svc.Name, "namespace", svc.Namespace)
if err := engine.CheckNamespace(svc.Namespace); err != nil {
log.Error(err, "failed to check namespace")
Expand Down Expand Up @@ -137,7 +140,7 @@ func (engine *Engine) processItem(item interface{}) error {
InventoryPolicy: inventory.PolicyAdoptAll,
})

return engine.UpdateApplyStatus(id, svc.Name, svc.Namespace, ch, false)
return engine.UpdateApplyStatus(id, svc.Name, svc.Namespace, ch, false, vcache)
}

func (engine *Engine) splitObjects(id string, objs []*unstructured.Unstructured) (*unstructured.Unstructured, []*unstructured.Unstructured, error) {
Expand Down
20 changes: 16 additions & 4 deletions pkg/sync/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

console "github.com/pluralsh/console-client-go"
"github.com/pluralsh/deployment-operator/pkg/manifests"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -200,7 +201,7 @@ func FormatActionGroupEvent(age event.ActionGroupEvent) error {
return nil
}

func (engine *Engine) UpdateApplyStatus(id, name, namespace string, ch <-chan event.Event, printStatus bool) error {
func (engine *Engine) UpdateApplyStatus(id, name, namespace string, ch <-chan event.Event, printStatus bool, vcache map[manifests.GroupName]string) error {
var statsCollector stats.Stats
var err error
components := []*console.ComponentAttributes{}
Expand Down Expand Up @@ -261,7 +262,7 @@ func (engine *Engine) UpdateApplyStatus(id, name, namespace string, ch <-chan ev
}

for _, v := range statusCollector.latestStatus {
consoleAttr := fromSyncResult(v)
consoleAttr := fromSyncResult(v, vcache)
if consoleAttr != nil {
components = append(components, consoleAttr)
}
Expand All @@ -274,17 +275,28 @@ func (engine *Engine) UpdateApplyStatus(id, name, namespace string, ch <-chan ev
return nil
}

func fromSyncResult(e event.StatusEvent) *console.ComponentAttributes {
func fromSyncResult(e event.StatusEvent, vcache map[manifests.GroupName]string) *console.ComponentAttributes {
if e.Resource == nil {
return nil
}
gvk := e.Resource.GroupVersionKind()
gname := manifests.GroupName{
Group: gvk.Group,
Kind: gvk.Kind,
Name: e.Resource.GetName(),
}

version := gvk.Version
if v, ok := vcache[gname]; ok {
version = v
}

return &console.ComponentAttributes{
Group: gvk.Group,
Kind: gvk.Kind,
Namespace: e.Resource.GetNamespace(),
Name: e.Resource.GetName(),
Version: gvk.Version,
Version: version,
Synced: e.PollResourceInfo.Status == status.CurrentStatus,
State: toStatus(e.Resource),
}
Expand Down

0 comments on commit c119958

Please sign in to comment.