Skip to content

Commit

Permalink
Add --dangerous-scope-to-used-gvks
Browse files Browse the repository at this point in the history
  • Loading branch information
100mik committed Feb 7, 2022
1 parent 5886f38 commit 270ac69
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/kapp/app/app_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Meta struct {
LastChangeName string `json:"lastChangeName,omitempty"`
LastChange ChangeMeta `json:"lastChange,omitempty"`

UsedGVs []schema.GroupVersion `json:"usedGVs,omitempty"`
UsedGVs []schema.GroupVersion `json:"usedGVs,omitempty"`
UsedGVKs []schema.GroupVersionKind `json:"usedGVKs,omitempty"`
}

func NewAppMetaFromData(data map[string]string) (Meta, error) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/kapp/app/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type App interface {
LabelSelector() (labels.Selector, error)
UsedGVs() ([]schema.GroupVersion, error)
UpdateUsedGVs([]schema.GroupVersion) error
UsedGVKs() ([]schema.GroupVersionKind, error)
UpdateUsedGVKs([]schema.GroupVersionKind) ([]schema.GroupVersionKind, error)

CreateOrUpdate(map[string]string) error
Exists() (bool, string, error)
Expand Down
4 changes: 4 additions & 0 deletions pkg/kapp/app/labeled_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (a *LabeledApp) LabelSelector() (labels.Selector, error) {

func (a *LabeledApp) UsedGVs() ([]schema.GroupVersion, error) { return nil, nil }
func (a *LabeledApp) UpdateUsedGVs(gvs []schema.GroupVersion) error { return nil }
func (a *LabeledApp) UsedGVKs() ([]schema.GroupVersionKind, error) { return nil, nil }
func (a *LabeledApp) UpdateUsedGVKs(gvks []schema.GroupVersionKind) ([]schema.GroupVersionKind, error) {
return nil, nil
}

func (a *LabeledApp) CreateOrUpdate(labels map[string]string) error { return nil }
func (a *LabeledApp) Exists() (bool, string, error) { return true, "", nil }
Expand Down
37 changes: 37 additions & 0 deletions pkg/kapp/app/recorded_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,43 @@ func (a *RecordedApp) UpdateUsedGVs(gvs []schema.GroupVersion) error {
})
}

func (a *RecordedApp) UsedGVKs() ([]schema.GroupVersionKind, error) {
meta, err := a.meta()
if err != nil {
return nil, err
}

return meta.UsedGVKs, nil
}

func (a *RecordedApp) UpdateUsedGVKs(gvks []schema.GroupVersionKind) ([]schema.GroupVersionKind, error) {
gvksByGVK := map[schema.GroupVersionKind]struct{}{}
var uniqGVKs []schema.GroupVersionKind

usedGVKs, err := a.UsedGVKs()
if err != nil {
return nil, err
}

for _, gvk := range usedGVKs {
if _, found := gvksByGVK[gvk]; !found {
gvksByGVK[gvk] = struct{}{}
uniqGVKs = append(uniqGVKs, gvk)
}
}

for _, gvk := range gvks {
if _, found := gvksByGVK[gvk]; !found {
gvksByGVK[gvk] = struct{}{}
uniqGVKs = append(uniqGVKs, gvk)
}
}

return usedGVKs, a.update(func(meta *Meta) {
meta.UsedGVKs = uniqGVKs
})
}

func (a *RecordedApp) CreateOrUpdate(labels map[string]string) error {
defer a.logger.DebugFunc("CreateOrUpdate").Finish()

Expand Down
8 changes: 8 additions & 0 deletions pkg/kapp/cmd/app/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ func (o *DeployOptions) Run() error {
return err
}

if o.ResourceTypesFlags.ScopeToUsedGVKs {
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)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/kapp/cmd/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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
4 changes: 4 additions & 0 deletions pkg/kapp/cmd/app/resource_types_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type ResourceTypesFlags struct {
CanIgnoreFailingAPIService func(schema.GroupVersion) bool

ScopeToFallbackAllowedNamespaces bool

ScopeToUsedGVKs bool
}

func (s *ResourceTypesFlags) Set(cmd *cobra.Command) {
Expand All @@ -21,6 +23,8 @@ func (s *ResourceTypesFlags) Set(cmd *cobra.Command) {

cmd.Flags().BoolVar(&s.ScopeToFallbackAllowedNamespaces, "dangerous-scope-to-fallback-allowed-namespaces",
false, "Scope resource searching to fallback allowed namespaces")

cmd.Flags().BoolVar(&s.ScopeToUsedGVKs, "dangerous-scope-to-used-gvks", false, "Scope resource searching to group versions used in the app")
}

func (s *ResourceTypesFlags) FailingAPIServicePolicy() *FailingAPIServicesPolicy {
Expand Down
27 changes: 27 additions & 0 deletions pkg/kapp/cmd/app/used_gvks_scope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package app

import (
ctlres "github.com/k14s/kapp/pkg/kapp/resources"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type UsedGVKsScope struct {
newResources []ctlres.Resource
}

func NewUsedGVsScope(newResources []ctlres.Resource) *UsedGVKsScope {
return &UsedGVKsScope{newResources}
}

func (s *UsedGVKsScope) GVKs() []schema.GroupVersionKind {
var result []schema.GroupVersionKind

for _, res := range s.newResources {
result = append(result, res.GroupVersionKind())
}

return result
}
9 changes: 8 additions & 1 deletion pkg/kapp/resources/identified_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -17,13 +18,15 @@ 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")}
fallbackAllowedNamespaces, logger.NewPrefixed("IdentifiedResources"), nil}
}

func (r IdentifiedResources) Create(resource Resource) (Resource, error) {
Expand Down Expand Up @@ -102,3 +105,7 @@ 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
}
4 changes: 4 additions & 0 deletions pkg/kapp/resources/identified_resources_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ 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 len(resRefs) > 0 {
resTypes = MatchingAny(resTypes, resRefs)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/kapp/resources/identified_resources_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ func (r *FakeResourceTypes) Find(ctlres.Resource) (ctlres.ResourceType, error) {
return ctlres.ResourceType{}, nil
}
func (r *FakeResourceTypes) CanIgnoreFailingGroupVersion(schema.GroupVersion) bool { return true }
func (r *FakeResourceTypes) ScopeToUsedGVKs() bool { return false }
5 changes: 5 additions & 0 deletions pkg/kapp/resources/labeled_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 @@ -84,6 +85,10 @@ 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
5 changes: 5 additions & 0 deletions pkg/kapp/resources/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

type Resource interface {
GroupVersionResource() schema.GroupVersionResource
GroupVersionKind() schema.GroupVersionKind
GroupVersion() schema.GroupVersion
Kind() string
APIVersion() string
Expand Down Expand Up @@ -138,6 +139,10 @@ func (r *ResourceImpl) GroupVersionResource() schema.GroupVersionResource {
return r.resType.GroupVersionResource
}

func (r *ResourceImpl) GroupVersionKind() schema.GroupVersionKind {
return r.un.GroupVersionKind()
}

func (r *ResourceImpl) GroupVersion() schema.GroupVersion {
pieces := strings.Split(r.APIVersion(), "/")
if len(pieces) > 2 {
Expand Down
20 changes: 20 additions & 0 deletions pkg/kapp/resources/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ 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 @@ -90,6 +93,10 @@ 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 Expand Up @@ -269,3 +276,16 @@ func NonMatching(in []ResourceType, ref ResourceRef) []ResourceType {
}
return out
}

// TODO: Extend ResourceRef and PartialResourceRefd to allow GVK matching
func MatchingAnyGVK(in []ResourceType, gvks []schema.GroupVersionKind) []ResourceType {
var out []ResourceType
for _, item := range in {
for _, gvk := range gvks {
if item.APIResource.Group == gvk.Group && item.APIResource.Version == item.APIResource.Version && item.Kind == gvk.Kind {
out = append(out, item)
}
}
}
return out
}

0 comments on commit 270ac69

Please sign in to comment.