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

Add apply-time-mutation feature #400

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ require (
github.com/onsi/ginkgo v1.16.2
github.com/onsi/gomega v1.12.0
github.com/spf13/cobra v1.1.3
github.com/spyzhov/ajson v0.4.2
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
k8s.io/api v0.21.1
k8s.io/apiextensions-apiserver v0.21.1
k8s.io/apimachinery v0.21.1
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/spyzhov/ajson v0.4.2 h1:JMByd/jZApPKDvNsmO90X2WWGbmT2ahDFp73QhZbg3s=
github.com/spyzhov/ajson v0.4.2/go.mod h1:63V+CGM6f1Bu/p4nLIN8885ojBdt88TbLoSFzyqMuVA=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
Expand Down Expand Up @@ -756,8 +758,9 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
Expand Down Expand Up @@ -927,8 +930,9 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
Expand Down
12 changes: 11 additions & 1 deletion pkg/apply/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/apply/cache"
"sigs.k8s.io/cli-utils/pkg/apply/event"
"sigs.k8s.io/cli-utils/pkg/apply/filter"
"sigs.k8s.io/cli-utils/pkg/apply/info"
"sigs.k8s.io/cli-utils/pkg/apply/mutator"
"sigs.k8s.io/cli-utils/pkg/apply/poller"
"sigs.k8s.io/cli-utils/pkg/apply/prune"
"sigs.k8s.io/cli-utils/pkg/apply/solver"
Expand Down Expand Up @@ -190,10 +192,18 @@ func (a *Applier) Run(ctx context.Context, invInfo inventory.InventoryInfo, obje
LocalNamespaces: localNamespaces(invInfo, object.UnstructuredsToObjMetasOrDie(objects)),
},
}
// Build list of apply mutators.
applyMutators := []mutator.Interface{
&mutator.ApplyTimeMutator{
Client: client,
Mapper: mapper,
ResourceCache: cache.NewResourceCacheMap(),
},
}
// Build the task queue by appending tasks in the proper order.
taskQueue, err := taskBuilder.
AppendInvAddTask(invInfo, applyObjs, options.DryRunStrategy).
AppendApplyWaitTasks(applyObjs, applyFilters, opts).
AppendApplyWaitTasks(applyObjs, applyFilters, applyMutators, opts).
AppendPruneWaitTasks(pruneObjs, pruneFilters, opts).
AppendInvSetTask(invInfo, options.DryRunStrategy).
Build()
Expand Down
24 changes: 24 additions & 0 deletions pkg/apply/cache/resource_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package cache

import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/cli-utils/pkg/object"
)

// ResourceCache stores unstructured resource objects in memory
seans3 marked this conversation as resolved.
Show resolved Hide resolved
type ResourceCache interface {
karlkfi marked this conversation as resolved.
Show resolved Hide resolved
// Put the resource into the cache, generating the ObjMetadata from the object.
Put(obj *unstructured.Unstructured) error
// Set the resource in the cache using the supplied key.
Set(objMeta object.ObjMetadata, obj *unstructured.Unstructured)
// Get the resource associated with the key from the cache.
// Returns (nil, true) if not found in the cache.
Get(objMeta object.ObjMetadata) (*unstructured.Unstructured, bool)
// Remove the resource associated with the key from the cache.
Remove(objMeta object.ObjMetadata)
// Clear the cache.
Clear()
}
79 changes: 79 additions & 0 deletions pkg/apply/cache/resource_cache_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2020 The Kubernetes Authors.
seans3 marked this conversation as resolved.
Show resolved Hide resolved
// SPDX-License-Identifier: Apache-2.0

package cache

import (
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
"sigs.k8s.io/cli-utils/pkg/object"
)

// ResourceCacheMap stores unstructured resource objects in a map.
// ResourceCacheMap is NOT thread-safe.
type ResourceCacheMap struct {
cache map[object.ObjMetadata]*unstructured.Unstructured
}

// NewResourceCacheMap returns a new empty ResourceCacheMap
func NewResourceCacheMap() *ResourceCacheMap {
return &ResourceCacheMap{
cache: make(map[object.ObjMetadata]*unstructured.Unstructured),
}
}

// Load adds the resources into the cache, replacing any existing resource with
// the same ID. Returns an error if any resource is invalid.
func (rc *ResourceCacheMap) Load(values ...*unstructured.Unstructured) error {
for _, value := range values {
key, err := object.UnstructuredToObjMeta(value)
if err != nil {
return fmt.Errorf("failed to create resource cache key: %w", err)
}
rc.cache[key] = value
}
return nil
}

// Put adds the resource into the cache, replacing any existing resource with
// the same ID. Returns an error if resource is invalid.
func (rc *ResourceCacheMap) Put(value *unstructured.Unstructured) error {
key, err := object.UnstructuredToObjMeta(value)
if err != nil {
return fmt.Errorf("failed to create resource cache key: %w", err)
}
rc.Set(key, value)
return nil
}

// Set the resource in the cache using the supplied key, and replacing any
// existing resource with the same key.
func (rc *ResourceCacheMap) Set(key object.ObjMetadata, value *unstructured.Unstructured) {
rc.cache[key] = value
}

// Get retrieves the resource associated with the key from the cache.
// Returns (nil, true) if not found in the cache.
func (rc *ResourceCacheMap) Get(key object.ObjMetadata) (*unstructured.Unstructured, bool) {
obj, found := rc.cache[key]
if klog.V(4).Enabled() {
if found {
klog.Infof("resource cache hit: %s", key)
} else {
klog.Infof("resource cache miss: %s", key)
}
}
return obj, found
}

// Remove the resource associated with the key from the cache.
func (rc *ResourceCacheMap) Remove(key object.ObjMetadata) {
delete(rc.cache, key)
}

// Clear the cache.
func (rc *ResourceCacheMap) Clear() {
rc.cache = make(map[object.ObjMetadata]*unstructured.Unstructured)
}
Loading