Skip to content

Commit

Permalink
fix circular dependency issue
Browse files Browse the repository at this point in the history
  • Loading branch information
floreks committed Sep 13, 2024
1 parent a7027cc commit 8e5a334
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 33 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ require (
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20240314144324-c7f7c6466f7f // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,6 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpR
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
github.com/tetafro/godot v1.4.16 h1:4ChfhveiNLk4NveAZ9Pu2AN8QZ2nkUGFuadM9lrr5D0=
github.com/tetafro/godot v1.4.16/go.mod h1:2oVxTBSftRTh4+MVfUaUXR6bn2GDXCaMcOG4Dk3rfio=
github.com/thejerf/suture/v4 v4.0.5 h1:F1E/4FZwXWqvlWDKEUo6/ndLtxGAUzMmNqkrMknZbAA=
github.com/thejerf/suture/v4 v4.0.5/go.mod h1:gu9Y4dXNUWFrByqRt30Rm9/UZ0wzRSt9AJS6xu/ZGxU=
github.com/timakin/bodyclose v0.0.0-20230421092635-574207250966 h1:quvGphlmUVU+nhpFa4gg4yJyTRJ13reZMDHrKwYw53M=
github.com/timakin/bodyclose v0.0.0-20230421092635-574207250966/go.mod h1:27bSVNWSBOHm+qRp1T9qzaIpsWEP6TbUnei/43HK+PQ=
github.com/timonwong/loggercheck v0.9.4 h1:HKKhqrjcVj8sxL7K77beXh0adEm6DLjV/QOGeMXEVi4=
Expand Down
7 changes: 7 additions & 0 deletions internal/kubernetes/schema/group_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package schema

type GroupName struct {
Group string
Kind string
Name string
}
1 change: 1 addition & 0 deletions pkg/cache/discovery_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func DiscoveryCache() cmap.ConcurrentMap[string, bool] {
}

func RunDiscoveryCacheInBackgroundOrDie(ctx context.Context, discoveryClient *discovery.DiscoveryClient) {
log.Logger.Info("starting discovery cache")
err := helpers.BackgroundPollUntilContextCancel(ctx, 5*time.Minute, true, true, func(_ context.Context) (done bool, err error) {
if err = updateDiscoveryCache(discoveryClient); err != nil {
log.Logger.Error(err, "can't fetch API versions")
Expand Down
7 changes: 3 additions & 4 deletions pkg/cache/resource_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/clusterreader"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/statusreaders"

"github.com/pluralsh/deployment-operator/pkg/manifests"

"github.com/pluralsh/polly/containers"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -27,6 +25,7 @@ import (
"sigs.k8s.io/cli-utils/pkg/object"

"github.com/pluralsh/deployment-operator/internal/kstatus/watcher"
"github.com/pluralsh/deployment-operator/internal/kubernetes/schema"
"github.com/pluralsh/deployment-operator/internal/utils"
"github.com/pluralsh/deployment-operator/pkg/common"
"github.com/pluralsh/deployment-operator/pkg/log"
Expand Down Expand Up @@ -101,7 +100,7 @@ func Init(ctx context.Context, config *rest.Config, ttl time.Duration) {
w := watcher.NewDynamicStatusWatcher(dynamicClient, discoveryClient, mapper, watcher.Options{
UseCustomObjectFilter: true,
ObjectFilter: nil,
RESTScopeStrategy: lo.ToPtr(kwatcher.RESTScopeRoot),
RESTScopeStrategy: lo.ToPtr(kwatcher.RESTScopeRoot),
Filters: &kwatcher.Filters{
Labels: common.ManagedByAgentLabelSelector(),
Fields: nil,
Expand Down Expand Up @@ -225,7 +224,7 @@ func (in *ResourceCache) GetCacheStatus(key object.ObjMetadata) (*console.Compon
return nil, err
}
in.saveResourceStatus(obj)
return common.StatusEventToComponentAttributes(*s, make(map[manifests.GroupName]string)), nil
return common.StatusEventToComponentAttributes(*s, make(map[schema.GroupName]string)), nil
}

func (in *ResourceCache) saveResourceStatus(resource *unstructured.Unstructured) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cache/resource_cache_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/cli-utils/pkg/apply/event"

"github.com/pluralsh/deployment-operator/internal/kubernetes/schema"
"github.com/pluralsh/deployment-operator/pkg/common"
"github.com/pluralsh/deployment-operator/pkg/manifests"
)

type SHAType string
Expand Down Expand Up @@ -80,5 +80,5 @@ func (in *ResourceCacheEntry) RequiresApply(manifestSHA string) bool {
// SetStatus saves the last seen resource [event.StatusEvent] and converts it to a simpler
// [console.ComponentAttributes] structure.
func (in *ResourceCacheEntry) SetStatus(se event.StatusEvent) {
in.status = common.StatusEventToComponentAttributes(se, make(map[manifests.GroupName]string))
in.status = common.StatusEventToComponentAttributes(se, make(map[schema.GroupName]string))
}
3 changes: 2 additions & 1 deletion pkg/common/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package common
import (
"sync"

"github.com/pluralsh/deployment-operator/pkg/lua"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

"github.com/pluralsh/deployment-operator/pkg/lua"
)

func init() {
Expand Down
9 changes: 5 additions & 4 deletions pkg/common/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package common

import (
console "github.com/pluralsh/console/go/client"
dlog "github.com/pluralsh/deployment-operator/pkg/log"
"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"
"sigs.k8s.io/cli-utils/pkg/apply/event"
"sigs.k8s.io/cli-utils/pkg/kstatus/status"

schema2 "github.com/pluralsh/deployment-operator/internal/kubernetes/schema"
dlog "github.com/pluralsh/deployment-operator/pkg/log"
)

func StatusEventToComponentAttributes(e event.StatusEvent, vcache map[manifests.GroupName]string) *console.ComponentAttributes {
func StatusEventToComponentAttributes(e event.StatusEvent, vcache map[schema2.GroupName]string) *console.ComponentAttributes {
if e.Resource == nil {
return nil
}
gvk := e.Resource.GroupVersionKind()
gname := manifests.GroupName{
gname := schema2.GroupName{
Group: gvk.Group,
Kind: gvk.Kind,
Name: e.Resource.GetName(),
Expand Down
7 changes: 4 additions & 3 deletions pkg/controller/service/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/pluralsh/deployment-operator/cmd/agent/args"
"github.com/pluralsh/deployment-operator/internal/kubernetes/schema"
agentcommon "github.com/pluralsh/deployment-operator/pkg/common"

clienterrors "github.com/pluralsh/deployment-operator/internal/errors"
Expand Down Expand Up @@ -61,8 +62,8 @@ type ServiceReconciler struct {
UtilFactory util.Factory
RestoreNamespace string

mapper meta.RESTMapper
pinger *ping.Pinger
mapper meta.RESTMapper
pinger *ping.Pinger
}

func NewServiceReconciler(consoleClient client.Client, config *rest.Config, refresh, manifestTTL time.Duration, restoreNamespace, consoleURL string) (*ServiceReconciler, error) {
Expand Down Expand Up @@ -353,7 +354,7 @@ func (s *ServiceReconciler) Reconcile(ctx context.Context, id string) (result re
})

metrics.Record().ServiceDeletion(id)
err = s.UpdatePruneStatus(ctx, svc, ch, map[manis.GroupName]string{})
err = s.UpdatePruneStatus(ctx, svc, ch, map[schema.GroupName]string{})
return
}

Expand Down
11 changes: 8 additions & 3 deletions pkg/controller/service/reconciler_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import (
"sigs.k8s.io/cli-utils/pkg/print/stats"
"sigs.k8s.io/controller-runtime/pkg/log"

internalschema "github.com/pluralsh/deployment-operator/internal/kubernetes/schema"
"github.com/pluralsh/deployment-operator/internal/metrics"
"github.com/pluralsh/deployment-operator/pkg/cache"
"github.com/pluralsh/deployment-operator/pkg/manifests"
)

func (s *ServiceReconciler) UpdatePruneStatus(ctx context.Context, svc *console.GetServiceDeploymentForAgent_ServiceDeployment, ch <-chan event.Event, vcache map[manifests.GroupName]string) error {
func (s *ServiceReconciler) UpdatePruneStatus(
ctx context.Context,
svc *console.GetServiceDeploymentForAgent_ServiceDeployment,
ch <-chan event.Event,
vcache map[internalschema.GroupName]string,
) error {
logger := log.FromContext(ctx)

var statsCollector stats.Stats
Expand Down Expand Up @@ -61,7 +66,7 @@ func (s *ServiceReconciler) UpdateApplyStatus(
svc *console.GetServiceDeploymentForAgent_ServiceDeployment,
ch <-chan event.Event,
printStatus bool,
vcache map[manifests.GroupName]string,
vcache map[internalschema.GroupName]string,
) error {
logger := log.FromContext(ctx)
start, err := metrics.FromContext[time.Time](ctx, metrics.ContextKeyTimeStart)
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/service/status_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"sigs.k8s.io/cli-utils/pkg/apply/event"
"sigs.k8s.io/cli-utils/pkg/object"

"github.com/pluralsh/deployment-operator/internal/kubernetes/schema"
"github.com/pluralsh/deployment-operator/pkg/cache"
"github.com/pluralsh/deployment-operator/pkg/common"
"github.com/pluralsh/deployment-operator/pkg/log"
"github.com/pluralsh/deployment-operator/pkg/manifests"
)

type serviceComponentsStatusCollector struct {
Expand Down Expand Up @@ -58,12 +58,12 @@ func (sc *serviceComponentsStatusCollector) refetch(resource *unstructured.Unstr
return response
}

func (sc *serviceComponentsStatusCollector) fromApplyResult(e event.ApplyEvent, vcache map[manifests.GroupName]string) *console.ComponentAttributes {
func (sc *serviceComponentsStatusCollector) fromApplyResult(e event.ApplyEvent, vcache map[schema.GroupName]string) *console.ComponentAttributes {
if e.Resource == nil {
return nil
}
gvk := e.Resource.GroupVersionKind()
gname := manifests.GroupName{
gname := schema.GroupName{
Group: gvk.Group,
Kind: gvk.Kind,
Name: e.Resource.GetName(),
Expand Down Expand Up @@ -96,7 +96,7 @@ func (sc *serviceComponentsStatusCollector) fromApplyResult(e event.ApplyEvent,
}
}

func (sc *serviceComponentsStatusCollector) componentsAttributes(vcache map[manifests.GroupName]string) []*console.ComponentAttributes {
func (sc *serviceComponentsStatusCollector) componentsAttributes(vcache map[schema.GroupName]string) []*console.ComponentAttributes {
components := make([]*console.ComponentAttributes, 0, len(sc.latestStatus))

if sc.DryRun {
Expand All @@ -123,7 +123,7 @@ func (sc *serviceComponentsStatusCollector) componentsAttributes(vcache map[mani
log.Logger.Error(err, "failed to get cache status")
continue
}
gname := manifests.GroupName{
gname := schema.GroupName{
Group: e.Group,
Kind: e.Kind,
Name: e.Name,
Expand Down
14 changes: 5 additions & 9 deletions pkg/manifests/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ package manifests

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

type GroupName struct {
Group string
Kind string
Name string
}
"github.com/pluralsh/deployment-operator/internal/kubernetes/schema"
)

func VersionCache(manifests []*unstructured.Unstructured) map[GroupName]string {
res := map[GroupName]string{}
func VersionCache(manifests []*unstructured.Unstructured) map[schema.GroupName]string {
res := map[schema.GroupName]string{}
for _, man := range manifests {
gvk := man.GroupVersionKind()
name := GroupName{
name := schema.GroupName{
Group: gvk.Group,
Kind: gvk.Kind,
Name: man.GetName(),
Expand Down

0 comments on commit 8e5a334

Please sign in to comment.