Skip to content

Commit

Permalink
Pass used GVKs using opts
Browse files Browse the repository at this point in the history
  • Loading branch information
100mik committed Feb 7, 2022
1 parent 270ac69 commit 5f488de
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 27 deletions.
14 changes: 10 additions & 4 deletions pkg/kapp/cmd/app/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -133,16 +134,16 @@ func (o *DeployOptions) Run() error {
return err
}

var usedGVKs []schema.GroupVersionKind
if o.ResourceTypesFlags.ScopeToUsedGVKs {
usedGVKs, err := app.UpdateUsedGVKs(NewUsedGVsScope(newResources).GVKs())
usedGVKs, err = app.UpdateUsedGVKs(NewUsedGVsScope(newResources).GVKs())
if err != nil {
return err
}
labeledResources.ScopeToGVKs(usedGVKs)
}

existingResources, existingPodRs, err := o.existingResources(
newResources, labeledResources, resourceFilter, supportObjs.Apps)
newResources, labeledResources, resourceFilter, supportObjs.Apps, usedGVKs)
if err != nil {
return err
}
Expand Down Expand Up @@ -280,7 +281,7 @@ func (o *DeployOptions) newResourcesFromFiles() ([]ctlres.Resource, error) {

func (o *DeployOptions) existingResources(newResources []ctlres.Resource,
labeledResources *ctlres.LabeledResources, resourceFilter ctlres.ResourceFilter,
apps ctlapp.Apps) ([]ctlres.Resource, []ctlres.Resource, error) {
apps ctlapp.Apps, usedGVKs []schema.GroupVersionKind) ([]ctlres.Resource, []ctlres.Resource, error) {

labelErrorResolutionFunc := func(key string, val string) string {
items, _ := apps.List(nil)
Expand All @@ -301,6 +302,11 @@ func (o *DeployOptions) existingResources(newResources []ctlres.Resource,
// Prevent accidently overriding kapp state records
DisallowedResourcesByLabelKeys: []string{ctlapp.KappIsAppLabelKey},
LabelErrorResolutionFunc: labelErrorResolutionFunc,

//Scope resource searching to UsedGVKs
IdentifiedResourcesListOpts: ctlres.IdentifiedResourcesListOpts{
GVKsScope: usedGVKs,
},
}

existingResources, err := labeledResources.AllAndMatching(newResources, matchingOpts)
Expand Down
1 change: 0 additions & 1 deletion pkg/kapp/cmd/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func FactoryClients(depsFactory cmdcore.DepsFactory, nsFlags cmdcore.NamespaceFl
resTypes := ctlres.NewResourceTypesImpl(coreClient, ctlres.ResourceTypesImplOpts{
IgnoreFailingAPIServices: resTypesFlags.IgnoreFailingAPIServices,
CanIgnoreFailingAPIService: resTypesFlags.CanIgnoreFailingAPIService,
ScopeToUsedGVKs: resTypesFlags.ScopeToUsedGVKs,
})

resourcesImplOpts := ctlres.ResourcesImplOpts{
Expand Down
9 changes: 1 addition & 8 deletions pkg/kapp/resources/identified_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/k14s/kapp/pkg/kapp/logger"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -18,15 +17,13 @@ type IdentifiedResources struct {
resources Resources
fallbackAllowedNamespaces []string
logger logger.Logger

GVKScope []schema.GroupVersionKind
}

func NewIdentifiedResources(coreClient kubernetes.Interface, resourceTypes ResourceTypes,
resources Resources, fallbackAllowedNamespaces []string, logger logger.Logger) IdentifiedResources {

return IdentifiedResources{coreClient, resourceTypes, resources,
fallbackAllowedNamespaces, logger.NewPrefixed("IdentifiedResources"), nil}
fallbackAllowedNamespaces, logger.NewPrefixed("IdentifiedResources")}
}

func (r IdentifiedResources) Create(resource Resource) (Resource, error) {
Expand Down Expand Up @@ -105,7 +102,3 @@ func (r IdentifiedResources) Exists(resource Resource, existsOpts ExistsOpts) (R
defer r.logger.DebugFunc(fmt.Sprintf("Exists(%s)", resource.Description())).Finish()
return r.resources.Exists(resource, existsOpts)
}

func (r *IdentifiedResources) ScopeToGVKs(gvks []schema.GroupVersionKind) {
r.GVKScope = gvks
}
5 changes: 3 additions & 2 deletions pkg/kapp/resources/identified_resources_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

type IdentifiedResourcesListOpts struct {
IgnoreCachedResTypes bool
GVKsScope []schema.GroupVersionKind
}

func (r IdentifiedResources) List(labelSelector labels.Selector, resRefs []ResourceRef, opts IdentifiedResourcesListOpts) ([]Resource, error) {
Expand All @@ -36,8 +37,8 @@ func (r IdentifiedResources) List(labelSelector labels.Selector, resRefs []Resou
schema.GroupVersionResource{Version: "v1", Resource: "componentstatuses"},
})

if r.resourceTypes.ScopeToUsedGVKs() {
resTypes = MatchingAnyGVK(resTypes, r.GVKScope)
if opts.GVKsScope != nil {
resTypes = MatchingAnyGVK(resTypes, opts.GVKsScope)
}

if len(resRefs) > 0 {
Expand Down
5 changes: 0 additions & 5 deletions pkg/kapp/resources/labeled_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/k14s/kapp/pkg/kapp/logger"
"github.com/k14s/kapp/pkg/kapp/util"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type OwnershipLabelModsFunc func(kvs map[string]string) []StringMapAppendMod
Expand Down Expand Up @@ -85,10 +84,6 @@ func (a *LabeledResources) All(listOpts IdentifiedResourcesListOpts) ([]Resource
return resources, nil
}

func (a *LabeledResources) ScopeToGVKs(gvks []schema.GroupVersionKind) {
a.identifiedResources.ScopeToGVKs(gvks)
}

type AllAndMatchingOpts struct {
ExistingNonLabeledResourcesCheck bool
ExistingNonLabeledResourcesCheckConcurrency int
Expand Down
7 changes: 0 additions & 7 deletions pkg/kapp/resources/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ type ResourceTypes interface {
All(ignoreCachedResTypes bool) ([]ResourceType, error)
Find(Resource) (ResourceType, error)
CanIgnoreFailingGroupVersion(schema.GroupVersion) bool
ScopeToUsedGVKs() bool
}

type ResourceTypesImplOpts struct {
IgnoreFailingAPIServices bool
CanIgnoreFailingAPIService func(schema.GroupVersion) bool

ScopeToUsedGVKs bool
}

type ResourceTypesImpl struct {
Expand Down Expand Up @@ -93,10 +90,6 @@ func (g *ResourceTypesImpl) all() ([]ResourceType, error) {
return pairs, nil
}

func (g *ResourceTypesImpl) ScopeToUsedGVKs() bool {
return g.opts.ScopeToUsedGVKs
}

func (g *ResourceTypesImpl) CanIgnoreFailingGroupVersion(groupVer schema.GroupVersion) bool {
return g.canIgnoreFailingGroupVersions(map[schema.GroupVersion]error{groupVer: nil})
}
Expand Down

0 comments on commit 5f488de

Please sign in to comment.