Skip to content

Commit

Permalink
Filter resources with label selector to catch mistakes by an aggregat…
Browse files Browse the repository at this point in the history
…ed api (#160)

Signed-off-by: Garrett Cheadle <gcheadle@vmware.com>
  • Loading branch information
cari-lynn authored and gcheadle-vmware committed Mar 4, 2021
1 parent 2ab6fdd commit b47889d
Show file tree
Hide file tree
Showing 73 changed files with 27,018 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.8
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,13 @@ 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.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/vito/go-interact v0.0.0-20171111012221-fa338ed9e9ec h1:Klu98tQ9Z1t23gvC7p7sCmvxkZxLhBHLNyrUPsWsYFg=
Expand Down Expand Up @@ -400,6 +403,8 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
9 changes: 9 additions & 0 deletions pkg/kapp/resources/identified_resources_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ func (r IdentifiedResources) List(labelSelector labels.Selector, resRefs []Resou
return nil, err
}

// Filter resources by label in case of aggregated api https://github.com/vmware-tanzu/carvel-kapp/issues/160
var filteredResources []Resource
for _, res := range resources {
if labelSelector.Matches(labels.Set(res.Labels())) {
filteredResources = append(filteredResources, res)
}
}
resources = filteredResources

// Mark resources that were not created by kapp as transient
for i, res := range resources {
if !NewIdentityAnnotation(res).Valid() {
Expand Down
119 changes: 119 additions & 0 deletions pkg/kapp/resources/identified_resources_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package resources

import (
"github.com/cppforlife/go-cli-ui/ui"
logger2 "github.com/k14s/kapp/pkg/kapp/logger"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"testing"

"k8s.io/apimachinery/pkg/labels"
)

func TestIdentifiedResourcesListReturnsLabeledResources(t *testing.T) {
mockedResTypes := new(ResourceTypesMock)
mockedRess := new(ResourcesMock)

mockedRess.Mock.On("All").Return(makeNewResources(t), nil)

ui := ui.NewNoopUI()
logger := logger2.NewUILogger(ui)
identifiedResources := NewIdentifiedResources(nil, mockedResTypes, mockedRess, []string{}, logger)

kappLabel := make(map[string]string)
kappLabel["kapp.k14s.io/app"] = "app-name"
sel := labels.Set(kappLabel).AsSelector()

resources, err := identifiedResources.List(sel, nil)

mockedRess.AssertCalled(t, "All")
require.Nil(t, err)
require.NotNil(t, resources)

for _, res := range resources {
require.Contains(t, res.Labels(), "kapp.k14s.io/app")
}
}

type ResourcesMock struct {
mock.Mock
}

func (r *ResourcesMock) All(resTypes []ResourceType, opts AllOpts) ([]Resource, error) {
args := r.Called()
return args.Get(0).([]Resource), args.Error(1)
}
func (r *ResourcesMock) Delete(res Resource) error { return nil }
func (r *ResourcesMock) Exists(res Resource) (bool, error) { return true, nil }
func (r *ResourcesMock) Get(res Resource) (Resource, error) { return nil, nil }
func (r *ResourcesMock) Patch(res Resource, pt types.PatchType, bs []byte) (Resource, error) {
return nil, nil
}
func (r *ResourcesMock) Update(res Resource) (Resource, error) { return nil, nil }
func (r *ResourcesMock) Create(resource Resource) (Resource, error) { return nil, nil }

type ResourceTypesMock struct {
mock.Mock
}

func (r *ResourceTypesMock) All() ([]ResourceType, error) { return nil, nil }
func (r *ResourceTypesMock) Find(_ Resource) (ResourceType, error) { return ResourceType{}, nil }
func (r *ResourceTypesMock) CanIgnoreFailingGroupVersion(_ schema.GroupVersion) bool { return true }

func makeNewResources(t *testing.T) []Resource {
t.Helper()
antreaBs := `---
apiVersion: clusterinformation.antrea.tanzu.vmware.com/v1beta1
kind: AntreaControllerInfo
metadata:
name: antrea-controller
version: v0.10.1
`

deploymentBs := `---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
kapp.k14s.io/identity: v1;default/apps/Deployment/nginx-deployment;apps/v1
kapp.k14s.io/original: '{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"labels":{"app":"nginx","kapp.k14s.io/app":"1614279362730868000","kapp.k14s.io/association":"v1.5771d3c49b880a055e733831a44ae242"},"name":"nginx-deployment","namespace":"default"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"nginx","kapp.k14s.io/app":"1614279362730868000"}},"template":{"metadata":{"labels":{"app":"nginx","kapp.k14s.io/app":"1614279362730868000","kapp.k14s.io/association":"v1.5771d3c49b880a055e733831a44ae242"}},"spec":{"containers":[{"image":"nginx:1.14.2","name":"nginx","ports":[{"containerPort":80}]}]}}}}'
kapp.k14s.io/original-diff-md5: 2b7269146768d693cb97b932660a532d
labels:
app: nginx
kapp.k14s.io/app: "app-name"
kapp.k14s.io/association: v1.5771d3c49b880a055e733831a44ae242
name: nginx-deployment
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx
kapp.k14s.io/app: "1614279362730868000"
template:
metadata:
labels:
app: nginx
kapp.k14s.io/app: "1614279362730868000"
kapp.k14s.io/association: v1.5771d3c49b880a055e733831a44ae242
spec:
containers:
- image: nginx:1.14.2
name: nginx
ports:
- containerPort: 80
protocol: TCP
`

antreaRes, err := NewResourceFromBytes([]byte(antreaBs))
require.Nil(t, err)
deploymentRes, err := NewResourceFromBytes([]byte(deploymentBs))
require.Nil(t, err)

return []Resource{antreaRes, deploymentRes}
}
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b47889d

Please sign in to comment.